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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
| /**
* 统一支付接口
*/
public interface PaymentProcessor {
PaymentResult processPayment(PaymentRequest request);
PaymentResult queryPayment(String transactionId);
RefundResult processRefund(RefundRequest request);
}
/**
* 支付请求对象
*/
public class PaymentRequest {
private String orderId;
private double amount;
private String currency;
private String description;
public PaymentRequest(String orderId, double amount, String currency, String description) {
this.orderId = orderId;
this.amount = amount;
this.currency = currency;
this.description = description;
}
// getter方法
public String getOrderId() { return orderId; }
public double getAmount() { return amount; }
public String getCurrency() { return currency; }
public String getDescription() { return description; }
}
/**
* 支付结果对象
*/
public class PaymentResult {
private boolean success;
private String transactionId;
private String message;
public PaymentResult(boolean success, String transactionId, String message) {
this.success = success;
this.transactionId = transactionId;
this.message = message;
}
public boolean isSuccess() { return success; }
public String getTransactionId() { return transactionId; }
public String getMessage() { return message; }
@Override
public String toString() {
return String.format("PaymentResult{success=%s, transactionId='%s', message='%s'}",
success, transactionId, message);
}
}
/**
* 退款请求对象
*/
public class RefundRequest {
private String originalTransactionId;
private double refundAmount;
private String reason;
public RefundRequest(String originalTransactionId, double refundAmount, String reason) {
this.originalTransactionId = originalTransactionId;
this.refundAmount = refundAmount;
this.reason = reason;
}
public String getOriginalTransactionId() { return originalTransactionId; }
public double getRefundAmount() { return refundAmount; }
public String getReason() { return reason; }
}
/**
* 退款结果对象
*/
public class RefundResult {
private boolean success;
private String refundId;
private String message;
public RefundResult(boolean success, String refundId, String message) {
this.success = success;
this.refundId = refundId;
this.message = message;
}
public boolean isSuccess() { return success; }
public String getRefundId() { return refundId; }
public String getMessage() { return message; }
@Override
public String toString() {
return String.format("RefundResult{success=%s, refundId='%s', message='%s'}",
success, refundId, message);
}
}
/**
* 支付宝支付SDK(第三方)
*/
public class AlipaySDK {
public String createPayment(String orderNo, double money, String desc) {
System.out.println("调用支付宝SDK创建支付订单");
System.out.println("订单号: " + orderNo + ", 金额: " + money + ", 描述: " + desc);
return "ALIPAY_" + System.currentTimeMillis();
}
public boolean checkPaymentStatus(String alipayTransactionId) {
System.out.println("查询支付宝支付状态: " + alipayTransactionId);
return true; // 模拟支付成功
}
public String refundPayment(String transactionId, double amount, String reason) {
System.out.println("支付宝退款: " + transactionId + ", 金额: " + amount + ", 原因: " + reason);
return "REFUND_" + System.currentTimeMillis();
}
}
/**
* 微信支付SDK(第三方)
*/
public class WeChatPaySDK {
public String submitPayOrder(String orderCode, int amountInCents, String productInfo) {
System.out.println("调用微信支付SDK提交订单");
System.out.println("订单码: " + orderCode + ", 金额(分): " + amountInCents + ", 商品信息: " + productInfo);
return "WECHAT_" + System.currentTimeMillis();
}
public int getPaymentResult(String wechatOrderId) {
System.out.println("查询微信支付结果: " + wechatOrderId);
return 1; // 1表示成功,0表示失败
}
public boolean applyRefund(String originalOrderId, int refundAmountInCents) {
System.out.println("微信支付退款申请: " + originalOrderId + ", 退款金额(分): " + refundAmountInCents);
return true;
}
}
/**
* 支付宝适配器
*/
public class AlipayAdapter implements PaymentProcessor {
private AlipaySDK alipaySDK;
public AlipayAdapter() {
this.alipaySDK = new AlipaySDK();
}
@Override
public PaymentResult processPayment(PaymentRequest request) {
try {
String transactionId = alipaySDK.createPayment(
request.getOrderId(),
request.getAmount(),
request.getDescription()
);
return new PaymentResult(true, transactionId, "支付宝支付成功");
} catch (Exception e) {
return new PaymentResult(false, null, "支付宝支付失败: " + e.getMessage());
}
}
@Override
public PaymentResult queryPayment(String transactionId) {
boolean success = alipaySDK.checkPaymentStatus(transactionId);
return new PaymentResult(success, transactionId,
success ? "支付成功" : "支付失败");
}
@Override
public RefundResult processRefund(RefundRequest request) {
try {
String refundId = alipaySDK.refundPayment(
request.getOriginalTransactionId(),
request.getRefundAmount(),
request.getReason()
);
return new RefundResult(true, refundId, "支付宝退款成功");
} catch (Exception e) {
return new RefundResult(false, null, "支付宝退款失败: " + e.getMessage());
}
}
}
/**
* 微信支付适配器
*/
public class WeChatPayAdapter implements PaymentProcessor {
private WeChatPaySDK weChatPaySDK;
public WeChatPayAdapter() {
this.weChatPaySDK = new WeChatPaySDK();
}
@Override
public PaymentResult processPayment(PaymentRequest request) {
try {
// 微信支付金额需要转换为分
int amountInCents = (int) (request.getAmount() * 100);
String transactionId = weChatPaySDK.submitPayOrder(
request.getOrderId(),
amountInCents,
request.getDescription()
);
return new PaymentResult(true, transactionId, "微信支付成功");
} catch (Exception e) {
return new PaymentResult(false, null, "微信支付失败: " + e.getMessage());
}
}
@Override
public PaymentResult queryPayment(String transactionId) {
int result = weChatPaySDK.getPaymentResult(transactionId);
return new PaymentResult(result == 1, transactionId,
result == 1 ? "支付成功" : "支付失败");
}
@Override
public RefundResult processRefund(RefundRequest request) {
try {
// 微信退款金额需要转换为分
int refundAmountInCents = (int) (request.getRefundAmount() * 100);
boolean success = weChatPaySDK.applyRefund(
request.getOriginalTransactionId(),
refundAmountInCents
);
String refundId = success ? "WECHAT_REFUND_" + System.currentTimeMillis() : null;
return new RefundResult(success, refundId,
success ? "微信退款成功" : "微信退款失败");
} catch (Exception e) {
return new RefundResult(false, null, "微信退款失败: " + e.getMessage());
}
}
}
/**
* 支付服务管理器
*/
public class PaymentService {
private PaymentProcessor paymentProcessor;
public PaymentService(PaymentProcessor paymentProcessor) {
this.paymentProcessor = paymentProcessor;
}
public void processOrder(String orderId, double amount, String description) {
System.out.println("\n=== 处理订单支付 ===");
System.out.println("订单ID: " + orderId);
System.out.println("支付金额: ¥" + amount);
PaymentRequest request = new PaymentRequest(orderId, amount, "CNY", description);
PaymentResult result = paymentProcessor.processPayment(request);
System.out.println("支付结果: " + result);
if (result.isSuccess()) {
// 查询支付状态
PaymentResult queryResult = paymentProcessor.queryPayment(result.getTransactionId());
System.out.println("支付状态查询: " + queryResult);
}
}
public void processRefund(String transactionId, double refundAmount, String reason) {
System.out.println("\n=== 处理退款申请 ===");
System.out.println("原交易ID: " + transactionId);
System.out.println("退款金额: ¥" + refundAmount);
RefundRequest request = new RefundRequest(transactionId, refundAmount, reason);
RefundResult result = paymentProcessor.processRefund(request);
System.out.println("退款结果: " + result);
}
}
// 支付系统适配器演示
public class PaymentAdapterDemo {
public static void main(String[] args) {
System.out.println("=== 支付系统适配器演示 ===");
// 使用支付宝支付
System.out.println("\n=== 支付宝支付 ===");
PaymentProcessor alipayProcessor = new AlipayAdapter();
PaymentService alipayService = new PaymentService(alipayProcessor);
alipayService.processOrder("ORDER_001", 99.99, "购买商品A");
// 模拟退款
alipayService.processRefund("ALIPAY_" + System.currentTimeMillis(), 50.0, "用户主动退款");
// 使用微信支付
System.out.println("\n=== 微信支付 ===");
PaymentProcessor wechatProcessor = new WeChatPayAdapter();
PaymentService wechatService = new PaymentService(wechatProcessor);
wechatService.processOrder("ORDER_002", 199.99, "购买商品B");
// 模拟退款
wechatService.processRefund("WECHAT_" + System.currentTimeMillis(), 100.0, "商品质量问题");
System.out.println("\n观察:相同的PaymentService代码可以处理不同的支付渠道!");
}
}
|