Subversion Repositories SmartDukaan

Rev

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