PaymentResultManager.java
3.66 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
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
package com.diligrp.cashier.trade.manager;
import com.diligrp.cashier.shared.service.ServiceEndpointSupport;
import com.diligrp.cashier.shared.service.ThreadPoolService;
import com.diligrp.cashier.shared.spi.IPaymentEventListener;
import com.diligrp.cashier.shared.util.JsonUtils;
import com.diligrp.cashier.shared.util.ObjectUtils;
import com.diligrp.cashier.trade.domain.OnlineRefundResult;
import com.diligrp.cashier.trade.domain.OnlinePaymentResult;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("paymentResultManager")
public class PaymentResultManager {
private static final Logger LOG = LoggerFactory.getLogger(PaymentResultManager.class);
@Resource
private ObjectProvider<IPaymentEventListener> eventListeners;
/**
* 通知业务系统在线支付通道处理结果
*/
public void notifyPaymentResult(String uri, OnlinePaymentResult payload) {
ThreadPoolService.getIoThreadPoll().submit(() -> {
List<IPaymentEventListener> lifeCycles = eventListeners.stream().toList();
for (IPaymentEventListener listener : lifeCycles) {
try {
listener.onEvent(payload);
} catch (Exception ex) {
LOG.error("Failed to notify trade payment result", ex);
}
}
});
if (ObjectUtils.isEmpty(uri)) {
return;
}
ThreadPoolService.getIoThreadPoll().submit(() -> {
try {
String body = JsonUtils.toJsonString(payload);
LOG.info("Notifying online trade payment result: {}", body);
ServiceEndpointSupport.HttpResult httpResult = new NotifyHttpClient(uri).send(body);
if (httpResult.statusCode != 200) {
LOG.error("Failed to notify trade payment result");
}
} catch (Exception ex) {
LOG.error("Failed to notify trade payment result", ex);
}
});
}
/**
* 通知业务系统退款处理结果
*/
public void notifyRefundResult(String uri, OnlineRefundResult payload) {
ThreadPoolService.getIoThreadPoll().submit(() -> {
List<IPaymentEventListener> lifeCycles = eventListeners.stream().toList();
for (IPaymentEventListener listener : lifeCycles) {
try {
listener.onEvent(payload);
} catch (Exception ex) {
LOG.error("Failed to notify trade refund result", ex);
}
}
});
if (ObjectUtils.isEmpty(uri)) {
return;
}
ThreadPoolService.getIoThreadPoll().submit(() -> {
try {
String body = JsonUtils.toJsonString(payload);
LOG.info("Notifying online trade refund result: {}", body);
ServiceEndpointSupport.HttpResult httpResult = new NotifyHttpClient(uri).send(body);
if (httpResult.statusCode != 200) {
LOG.error("Failed to notify trade refund result");
}
} catch (Exception ex) {
LOG.error("Failed to notify trade refund result", ex);
}
});
}
private static class NotifyHttpClient extends ServiceEndpointSupport {
private final String baseUrl;
public NotifyHttpClient(String baseUrl) {
this.baseUrl = baseUrl;
}
public HttpResult send(String body) {
return send(baseUrl, body);
}
}
}