Refund.java
2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.ecommerce.payment.model;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@Entity
@Table(name = "refunds")
public class Refund {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "refund_id", unique = true, nullable = false)
private String refundId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "payment_id", nullable = false)
private Payment payment;
@Column(name = "user_id", nullable = false)
private Long userId;
@Column(name = "amount", nullable = false, precision = 12, scale = 2)
private BigDecimal amount;
@Column(name = "currency", nullable = false)
private String currency = "USD";
@Column(name = "status", nullable = false)
private String status = "PENDING"; // PENDING, PROCESSING, SUCCEEDED, FAILED, CANCELLED
@Column(name = "reason")
private String reason;
@Column(name = "gateway_refund_id")
private String gatewayRefundId;
@Column(name = "failure_reason")
private String failureReason;
@Column(name = "failure_code")
private String failureCode;
@Column(name = "metadata", columnDefinition = "TEXT")
private String metadata;
@Column(name = "created_at")
private LocalDateTime createdAt;
@Column(name = "processed_at")
private LocalDateTime processedAt;
@PrePersist
protected void onCreate() {
if (refundId == null) {
refundId = generateRefundId();
}
if (createdAt == null) {
createdAt = LocalDateTime.now();
}
}
private String generateRefundId() {
return "REF" + System.currentTimeMillis() + (int)(Math.random() * 1000);
}
// 便捷方法:从关联的Payment获取userId(如果Payment中有userId的话)
public Long getUserIdFromPayment() {
return this.payment != null ? this.payment.getUserId() : this.userId;
}
}