CardPaymentHttpClient.java
7.48 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
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
package com.diligrp.cashier.pipeline.client;
import com.diligrp.cashier.pipeline.domain.OnlineRefundRequest;
import com.diligrp.cashier.pipeline.domain.OnlineRefundResponse;
import com.diligrp.cashier.pipeline.domain.card.CardPaymentRequest;
import com.diligrp.cashier.pipeline.domain.card.CardPaymentResponse;
import com.diligrp.cashier.pipeline.domain.card.UserCardDTO;
import com.diligrp.cashier.pipeline.exception.PaymentPipelineException;
import com.diligrp.cashier.pipeline.type.OutPaymentType;
import com.diligrp.cashier.pipeline.type.PaymentState;
import com.diligrp.cashier.shared.ErrorCode;
import com.diligrp.cashier.shared.service.ServiceEndpointSupport;
import com.diligrp.cashier.shared.type.SourceType;
import com.diligrp.cashier.shared.util.CurrencyUtils;
import com.diligrp.cashier.shared.util.JsonUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class CardPaymentHttpClient extends ServiceEndpointSupport {
private static final Logger LOG = LoggerFactory.getLogger(CardPaymentHttpClient.class);
private static final String PAYMENT_URL = "/api/cashier/pay";
private static final String REFUND_URL = "/api/cashier/refund";
private static final String LIST_CARD_URL = "/cashier/query/accountList";
private final String baseUri;
private final Long outMchId;
private final Long accountId;
public CardPaymentHttpClient(String baseUri, Long outMchId, Long accountId) {
this.baseUri = baseUri;
this.outMchId = outMchId;
this.accountId = accountId;
}
public CardPaymentResponse sendPaymentRequest(CardPaymentRequest request) {
String uri = String.format("%s%s", baseUri, PAYMENT_URL);
SourceType sourceType = request.getObject("source", SourceType.class);
Map<String, Object> params = new LinkedHashMap<>();
params.put("mchId", outMchId); // 市场ID
params.put("accountId", accountId); // 市场ID
params.put("fromCardNo", request.getCardNo());
params.put("fromAccountId", request.getAccountId());
params.put("amount", request.getAmount());
params.put("serviceCost", 0);
params.put("password", request.getPassword());
params.put("outTradeNo", request.getPaymentId());
params.put("goodsDesc", request.getGoods());
params.put("description", request.getDescription());
if (sourceType != null) {
params.put("sourceId", sourceType.getCode());
params.put("sourceName", sourceType.getName());
}
String payload = JsonUtils.toJsonString(params);
LOG.debug("Sending card payment request: {}", payload);
LocalDateTime now = LocalDateTime.now();
HttpResult result = send(uri, payload);
if (result.statusCode == 200) {
LOG.debug("Received from card payment pipeline: {}", result.responseText);
Map<String, Object> response = JsonUtils.fromJsonString(result.responseText, new TypeReference<>() {});
if ("200".equals(response.get("code"))) {
Map<String, Object> data = (Map<String, Object>) response.get("data");
String outTradeNo = (String) data.get("outTradeNo");
Map<String, Object> payerId = new LinkedHashMap<>();
payerId.put("customerId", convertLong(data.get("customerId")));
payerId.put("accountId", convertLong(data.get("accountId")));
payerId.put("cardNo", data.get("cardNo"));
payerId.put("name", data.get("name"));
return new CardPaymentResponse(request.getPaymentId(), outTradeNo, OutPaymentType.DILICARD,
JsonUtils.toJsonString(payerId), now, PaymentState.SUCCESS, "园区卡支付成功");
} else {
throw new PaymentPipelineException(ErrorCode.SERVICE_ACCESS_ERROR, "园区卡支付失败: " + response.get("message"));
}
}
throw new PaymentPipelineException(ErrorCode.SERVICE_ACCESS_ERROR, "园区卡支付失败: 支付通道调用失败");
}
public OnlineRefundResponse sendRefundRequest(OnlineRefundRequest request) {
String uri = String.format("%s%s", baseUri, REFUND_URL);
Map<String, Object> params = new LinkedHashMap<>();
params.put("refundId", request.getRefundId());
params.put("outTradeNo", request.getPaymentId());
params.put("amount", request.getAmount());
String payload = JsonUtils.toJsonString(params);
LOG.debug("Sending card refund request: {}", payload);
LocalDateTime now = LocalDateTime.now();
HttpResult result = send(uri, payload);
if (result.statusCode == 200) {
LOG.debug("Received from card payment pipeline: {}", result.responseText);
Map<String, Object> response = JsonUtils.fromJsonString(result.responseText, new TypeReference<>() {});
if ("200".equals(response.get("code"))) {
return new OnlineRefundResponse(request.getRefundId(), null, now,
PaymentState.SUCCESS, "园区卡退款成功");
} else {
throw new PaymentPipelineException(ErrorCode.SERVICE_ACCESS_ERROR, "园区卡支付退款失败: " + response.get("message"));
}
}
throw new PaymentPipelineException(ErrorCode.SERVICE_ACCESS_ERROR, "园区卡支付退款失败: 支付通道调用失败");
}
public List<UserCardDTO> listUserCards(String userId) {
String uri = String.format("%s%s", baseUri, LIST_CARD_URL);
Map<String, Object> params = new LinkedHashMap<>();
params.put("mchId", outMchId);
params.put("customerId", Long.parseLong(userId));
String payload = JsonUtils.toJsonString(params);
LOG.debug("Sending list user card request: {}", payload);
HttpResult result = send(uri, payload);
if (result.statusCode == 200) {
LOG.debug("Received from card payment pipeline: {}", result.responseText);
Map<String, Object> response = JsonUtils.fromJsonString(result.responseText, new TypeReference<>() {});
if ("200".equals(response.get("code"))) {
List<UserCardDTO> userCards = new ArrayList<>();
Object data = response.get("data");
if (data != null) {
List<Map<String, Object>> cardList = JsonUtils.convertValue(data, new TypeReference<>() {});
for (Map<String, Object> card : cardList) {
String amount = CurrencyUtils.toNoSymbolCurrency(convertLong(card.get("amount")));
UserCardDTO userCard = new UserCardDTO(convertLong(card.get("customerId")),
convertLong(card.get("accountId")), (String) card.get("cardNo"),
(String) card.get("customerName"), amount);
userCards.add(userCard);
}
}
return userCards;
} else {
throw new PaymentPipelineException(ErrorCode.SERVICE_ACCESS_ERROR, "查询园区卡失败: " + response.get("message"));
}
}
throw new PaymentPipelineException(ErrorCode.SERVICE_ACCESS_ERROR, "查询园区卡失败: 支付通道调用失败");
}
private Long convertLong(Object value) {
return value != null ? ((Number) value).longValue() : null;
}
}