Subversion Repositories SmartDukaan

Rev

Rev 35403 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
35403 amit 1
package com.spice.profitmandi.dao.entity.fofo;
2
 
3
import com.spice.profitmandi.dao.convertor.LocalDateTimeAttributeConverter;
4
import com.spice.profitmandi.dao.enumuration.pinelabs.PinelabsPaymentStatus;
5
 
6
import javax.persistence.*;
7
import java.io.Serializable;
8
import java.math.BigDecimal;
9
import java.time.LocalDateTime;
35652 vikas 10
import java.util.Objects;
35403 amit 11
 
12
/**
13
 * Entity representing Pinelabs order lifecycle.
14
 * Replaces the old payment.pine_labs table with enhanced tracking capabilities.
15
 *
16
 * @author Pinelabs Integration Team
17
 * @since 1.0
18
 */
19
@Entity
35652 vikas 20
@Table(name = "fofo.pinelabs_orders", schema = "fofo")
35403 amit 21
public class PinelabsOrder implements Serializable {
22
 
23
    private static final long serialVersionUID = 1L;
24
 
25
    @Id
26
    @Column(name = "id")
27
    @GeneratedValue(strategy = GenerationType.IDENTITY)
35652 vikas 28
    private long id;
35403 amit 29
 
30
    /**
31
     * Reference to internal pending order (fofo.pending_order.id)
32
     */
33
    @Column(name = "pending_order_id", nullable = false)
34
    private Integer pendingOrderId;
35
 
36
    /**
37
     * Order ID generated by Pinelabs API
38
     */
39
    @Column(name = "pinelabs_order_id", nullable = false, unique = true, length = 100)
40
    private String pinelabsOrderId;
41
 
42
    /**
43
     * Merchant reference for the order (optional, for reconciliation)
44
     */
45
    @Column(name = "pinelabs_merchant_order_ref", length = 100)
46
    private String pinelabsMerchantOrderRef;
47
 
48
    /**
49
     * Reference to Pinelabs customer (null for guest checkout)
50
     */
35652 vikas 51
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
35403 amit 52
    @JoinColumn(name = "pinelabs_customer_id")
53
    private PinelabsCustomer pinelabsCustomer;
54
 
55
    /**
56
     * Payment method used for this order (null if not using saved card)
57
     */
58
    @ManyToOne(fetch = FetchType.LAZY)
59
    @JoinColumn(name = "payment_method_id")
60
    private PinelabsPaymentMethod paymentMethod;
61
 
62
    /**
63
     * Order amount in INR
64
     */
65
    @Column(name = "order_amount", nullable = false, precision = 12, scale = 2)
66
    private BigDecimal orderAmount;
67
 
68
    /**
69
     * Currency code (default: INR)
70
     */
71
    @Column(name = "currency", nullable = false, length = 3)
72
    private String currency = "INR";
73
 
74
    /**
75
     * Payment status
76
     */
77
    @Column(name = "payment_status", nullable = false, length = 50)
78
    @Enumerated(EnumType.STRING)
79
    private PinelabsPaymentStatus paymentStatus = PinelabsPaymentStatus.PENDING;
80
 
81
    /**
82
     * Payment method type (CARD, UPI, WALLET, NETBANKING, BNPL)
83
     */
84
    @Column(name = "payment_method_type", length = 50)
85
    private String paymentMethodType;
86
 
87
    /**
88
     * Transaction ID from Pinelabs
89
     */
90
    @Column(name = "transaction_id", length = 100)
91
    private String transactionId;
92
 
93
    /**
94
     * Pinelabs checkout URL (for redirect)
95
     */
96
    @Column(name = "payment_url", columnDefinition = "TEXT")
97
    private String paymentUrl;
98
 
99
    /**
100
     * Amount actually captured (for partial captures)
101
     */
102
    @Column(name = "captured_amount", precision = 12, scale = 2)
103
    private BigDecimal capturedAmount = BigDecimal.ZERO;
104
 
105
    /**
106
     * Amount refunded
107
     */
108
    @Column(name = "refunded_amount", precision = 12, scale = 2)
109
    private BigDecimal refundedAmount = BigDecimal.ZERO;
110
 
111
    /**
112
     * Applied offer/EMI code
113
     */
114
    @Column(name = "offer_code", length = 100)
115
    private String offerCode;
116
 
117
    /**
118
     * EMI tenure in months (if EMI selected)
119
     */
120
    @Column(name = "emi_tenure")
121
    private Integer emiTenure;
122
 
123
    /**
124
     * When the order expires (for payment link expiry)
125
     */
126
    @Convert(converter = LocalDateTimeAttributeConverter.class)
127
    @Column(name = "order_expiry_timestamp")
128
    private LocalDateTime orderExpiryTimestamp;
129
 
130
    /**
131
     * When payment callback was received from Pinelabs
132
     */
133
    @Convert(converter = LocalDateTimeAttributeConverter.class)
134
    @Column(name = "callback_received_timestamp")
135
    private LocalDateTime callbackReceivedTimestamp;
136
 
137
    /**
138
     * Raw JSON response from Pinelabs (for debugging/audit)
139
     */
140
    @Column(name = "response_data", columnDefinition = "TEXT")
141
    private String responseData;
142
 
143
    /**
144
     * Order creation timestamp
145
     */
146
    @Convert(converter = LocalDateTimeAttributeConverter.class)
147
    @Column(name = "create_timestamp", nullable = false, updatable = false)
148
    private LocalDateTime createTimestamp = LocalDateTime.now();
149
 
150
    /**
151
     * Last update timestamp
152
     */
153
    @Convert(converter = LocalDateTimeAttributeConverter.class)
154
    @Column(name = "update_timestamp", nullable = false)
155
    private LocalDateTime updateTimestamp = LocalDateTime.now();
156
 
157
    /**
158
     * Default constructor
159
     */
160
    public PinelabsOrder() {
161
    }
162
 
163
    /**
164
     * Constructor with essential fields
165
     *
166
     * @param pendingOrderId   Internal order ID
167
     * @param pinelabsOrderId  Pinelabs order ID
168
     * @param orderAmount      Order amount
169
     */
170
    public PinelabsOrder(Integer pendingOrderId, String pinelabsOrderId, BigDecimal orderAmount) {
171
        this.pendingOrderId = pendingOrderId;
172
        this.pinelabsOrderId = pinelabsOrderId;
173
        this.orderAmount = orderAmount;
174
    }
175
 
176
    // ==================== Getters and Setters ====================
177
 
35652 vikas 178
    public long getId() {
35403 amit 179
        return id;
180
    }
181
 
35652 vikas 182
    public void setId(long id) {
35403 amit 183
        this.id = id;
184
    }
185
 
186
    public Integer getPendingOrderId() {
187
        return pendingOrderId;
188
    }
189
 
190
    public void setPendingOrderId(Integer pendingOrderId) {
191
        this.pendingOrderId = pendingOrderId;
192
    }
193
 
194
    public String getPinelabsOrderId() {
195
        return pinelabsOrderId;
196
    }
197
 
198
    public void setPinelabsOrderId(String pinelabsOrderId) {
199
        this.pinelabsOrderId = pinelabsOrderId;
200
    }
201
 
202
    public String getPinelabsMerchantOrderRef() {
203
        return pinelabsMerchantOrderRef;
204
    }
205
 
206
    public void setPinelabsMerchantOrderRef(String pinelabsMerchantOrderRef) {
207
        this.pinelabsMerchantOrderRef = pinelabsMerchantOrderRef;
208
    }
209
 
210
    public PinelabsCustomer getPinelabsCustomer() {
211
        return pinelabsCustomer;
212
    }
213
 
214
    public void setPinelabsCustomer(PinelabsCustomer pinelabsCustomer) {
215
        this.pinelabsCustomer = pinelabsCustomer;
216
    }
217
 
218
    public PinelabsPaymentMethod getPaymentMethod() {
219
        return paymentMethod;
220
    }
221
 
222
    public void setPaymentMethod(PinelabsPaymentMethod paymentMethod) {
223
        this.paymentMethod = paymentMethod;
224
    }
225
 
226
    public BigDecimal getOrderAmount() {
227
        return orderAmount;
228
    }
229
 
230
    public void setOrderAmount(BigDecimal orderAmount) {
231
        this.orderAmount = orderAmount;
232
    }
233
 
234
    public String getCurrency() {
235
        return currency;
236
    }
237
 
238
    public void setCurrency(String currency) {
239
        this.currency = currency;
240
    }
241
 
242
    public PinelabsPaymentStatus getPaymentStatus() {
243
        return paymentStatus;
244
    }
245
 
246
    public void setPaymentStatus(PinelabsPaymentStatus paymentStatus) {
247
        this.paymentStatus = paymentStatus;
248
    }
249
 
250
    public String getPaymentMethodType() {
251
        return paymentMethodType;
252
    }
253
 
254
    public void setPaymentMethodType(String paymentMethodType) {
255
        this.paymentMethodType = paymentMethodType;
256
    }
257
 
258
    public String getTransactionId() {
259
        return transactionId;
260
    }
261
 
262
    public void setTransactionId(String transactionId) {
263
        this.transactionId = transactionId;
264
    }
265
 
266
    public String getPaymentUrl() {
267
        return paymentUrl;
268
    }
269
 
270
    public void setPaymentUrl(String paymentUrl) {
271
        this.paymentUrl = paymentUrl;
272
    }
273
 
274
    public BigDecimal getCapturedAmount() {
275
        return capturedAmount;
276
    }
277
 
278
    public void setCapturedAmount(BigDecimal capturedAmount) {
279
        this.capturedAmount = capturedAmount;
280
    }
281
 
282
    public BigDecimal getRefundedAmount() {
283
        return refundedAmount;
284
    }
285
 
286
    public void setRefundedAmount(BigDecimal refundedAmount) {
287
        this.refundedAmount = refundedAmount;
288
    }
289
 
290
    public String getOfferCode() {
291
        return offerCode;
292
    }
293
 
294
    public void setOfferCode(String offerCode) {
295
        this.offerCode = offerCode;
296
    }
297
 
298
    public Integer getEmiTenure() {
299
        return emiTenure;
300
    }
301
 
302
    public void setEmiTenure(Integer emiTenure) {
303
        this.emiTenure = emiTenure;
304
    }
305
 
306
    public LocalDateTime getOrderExpiryTimestamp() {
307
        return orderExpiryTimestamp;
308
    }
309
 
310
    public void setOrderExpiryTimestamp(LocalDateTime orderExpiryTimestamp) {
311
        this.orderExpiryTimestamp = orderExpiryTimestamp;
312
    }
313
 
314
    public LocalDateTime getCallbackReceivedTimestamp() {
315
        return callbackReceivedTimestamp;
316
    }
317
 
318
    public void setCallbackReceivedTimestamp(LocalDateTime callbackReceivedTimestamp) {
319
        this.callbackReceivedTimestamp = callbackReceivedTimestamp;
320
    }
321
 
322
    public String getResponseData() {
323
        return responseData;
324
    }
325
 
326
    public void setResponseData(String responseData) {
327
        this.responseData = responseData;
328
    }
329
 
330
    public LocalDateTime getCreateTimestamp() {
331
        return createTimestamp;
332
    }
333
 
334
    public void setCreateTimestamp(LocalDateTime createTimestamp) {
335
        this.createTimestamp = createTimestamp;
336
    }
337
 
338
    public LocalDateTime getUpdateTimestamp() {
339
        return updateTimestamp;
340
    }
341
 
342
    public void setUpdateTimestamp(LocalDateTime updateTimestamp) {
343
        this.updateTimestamp = updateTimestamp;
344
    }
345
 
346
    // ==================== Helper Methods ====================
347
 
348
    /**
349
     * Check if order has expired
350
     *
351
     * @return true if order has expired
352
     */
353
    public boolean isExpired() {
354
        if (orderExpiryTimestamp == null) {
355
            return false;
356
        }
357
        return LocalDateTime.now().isAfter(orderExpiryTimestamp);
358
    }
359
 
360
    /**
361
     * Check if payment is successful
362
     *
363
     * @return true if payment status is CAPTURED
364
     */
365
    public boolean isPaymentSuccessful() {
366
        return paymentStatus == PinelabsPaymentStatus.CAPTURED;
367
    }
368
 
369
    /**
370
     * Check if refund is possible
371
     *
372
     * @return true if order can be refunded
373
     */
374
    public boolean canBeRefunded() {
375
        if (paymentStatus != PinelabsPaymentStatus.CAPTURED) {
376
            return false;
377
        }
378
        if (refundedAmount == null) {
379
            return true;
380
        }
381
        // Can refund if not fully refunded
382
        return refundedAmount.compareTo(capturedAmount != null ? capturedAmount : orderAmount) < 0;
383
    }
384
 
385
    /**
386
     * Get remaining refundable amount
387
     *
388
     * @return Amount that can still be refunded
389
     */
390
    public BigDecimal getRefundableAmount() {
391
        BigDecimal totalPaid = capturedAmount != null ? capturedAmount : orderAmount;
392
        BigDecimal alreadyRefunded = refundedAmount != null ? refundedAmount : BigDecimal.ZERO;
393
        return totalPaid.subtract(alreadyRefunded);
394
    }
395
 
396
    // ==================== Object Methods ====================
397
 
398
    @Override
35652 vikas 399
    public boolean equals(Object o) {
400
        if (!(o instanceof PinelabsOrder)) return false;
401
        PinelabsOrder that = (PinelabsOrder) o;
402
        return id == that.id && Objects.equals(pendingOrderId, that.pendingOrderId) && Objects.equals(pinelabsOrderId, that.pinelabsOrderId) && Objects.equals(pinelabsMerchantOrderRef, that.pinelabsMerchantOrderRef) && Objects.equals(pinelabsCustomer, that.pinelabsCustomer) && Objects.equals(paymentMethod, that.paymentMethod) && Objects.equals(orderAmount, that.orderAmount) && Objects.equals(currency, that.currency) && paymentStatus == that.paymentStatus && Objects.equals(paymentMethodType, that.paymentMethodType) && Objects.equals(transactionId, that.transactionId) && Objects.equals(paymentUrl, that.paymentUrl) && Objects.equals(capturedAmount, that.capturedAmount) && Objects.equals(refundedAmount, that.refundedAmount) && Objects.equals(offerCode, that.offerCode) && Objects.equals(emiTenure, that.emiTenure) && Objects.equals(orderExpiryTimestamp, that.orderExpiryTimestamp) && Objects.equals(callbackReceivedTimestamp, that.callbackReceivedTimestamp) && Objects.equals(responseData, that.responseData) && Objects.equals(createTimestamp, that.createTimestamp) && Objects.equals(updateTimestamp, that.updateTimestamp);
35403 amit 403
    }
404
 
405
    @Override
35652 vikas 406
    public int hashCode() {
407
        return Objects.hash(id, pendingOrderId, pinelabsOrderId, pinelabsMerchantOrderRef, pinelabsCustomer, paymentMethod, orderAmount, currency, paymentStatus, paymentMethodType, transactionId, paymentUrl, capturedAmount, refundedAmount, offerCode, emiTenure, orderExpiryTimestamp, callbackReceivedTimestamp, responseData, createTimestamp, updateTimestamp);
35403 amit 408
    }
409
 
410
    @Override
411
    public String toString() {
412
        return "PinelabsOrder [id=" + id + ", pendingOrderId=" + pendingOrderId + ", pinelabsOrderId="
413
                + pinelabsOrderId + ", orderAmount=" + orderAmount + ", currency=" + currency + ", paymentStatus="
414
                + paymentStatus + ", paymentMethodType=" + paymentMethodType + ", transactionId=" + transactionId
415
                + ", capturedAmount=" + capturedAmount + ", refundedAmount=" + refundedAmount + ", createTimestamp="
416
                + createTimestamp + "]";
417
    }
418
}