Commit 717caf527a130590b9554cd6b5d002bd8f00dd58
1 parent
f46a0371
mall + pay:
1. 增加微信支付 Client 的查询订单接口
Showing
2 changed files
with
60 additions
and
7 deletions
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayPayClient.java
| @@ -115,7 +115,7 @@ public abstract class AbstractAlipayPayClient extends AbstractPayClient<AlipayPa | @@ -115,7 +115,7 @@ public abstract class AbstractAlipayPayClient extends AbstractPayClient<AlipayPa | ||
| 115 | outTradeNo, response); | 115 | outTradeNo, response); |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | - private Integer parseStatus(String tradeStatus) { | 118 | + private static Integer parseStatus(String tradeStatus) { |
| 119 | return Objects.equals("WAIT_BUYER_PAY", tradeStatus) ? PayOrderStatusRespEnum.WAITING.getStatus() | 119 | return Objects.equals("WAIT_BUYER_PAY", tradeStatus) ? PayOrderStatusRespEnum.WAITING.getStatus() |
| 120 | : ObjectUtils.equalsAny(tradeStatus, "TRADE_FINISHED", "TRADE_SUCCESS") ? PayOrderStatusRespEnum.SUCCESS.getStatus() | 120 | : ObjectUtils.equalsAny(tradeStatus, "TRADE_FINISHED", "TRADE_SUCCESS") ? PayOrderStatusRespEnum.SUCCESS.getStatus() |
| 121 | : Objects.equals("TRADE_CLOSED", tradeStatus) ? PayOrderStatusRespEnum.CLOSED.getStatus() : null; | 121 | : Objects.equals("TRADE_CLOSED", tradeStatus) ? PayOrderStatusRespEnum.CLOSED.getStatus() : null; |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/weixin/AbstractWxPayClient.java
| @@ -16,8 +16,12 @@ import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; | @@ -16,8 +16,12 @@ import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; | ||
| 16 | import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result; | 16 | import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result; |
| 17 | import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult; | 17 | import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult; |
| 18 | import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyV3Result; | 18 | import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyV3Result; |
| 19 | +import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest; | ||
| 20 | +import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryV3Request; | ||
| 19 | import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; | 21 | import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest; |
| 20 | import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request; | 22 | import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request; |
| 23 | +import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult; | ||
| 24 | +import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryV3Result; | ||
| 21 | import com.github.binarywang.wxpay.bean.result.WxPayRefundResult; | 25 | import com.github.binarywang.wxpay.bean.result.WxPayRefundResult; |
| 22 | import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result; | 26 | import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result; |
| 23 | import com.github.binarywang.wxpay.config.WxPayConfig; | 27 | import com.github.binarywang.wxpay.config.WxPayConfig; |
| @@ -115,7 +119,6 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -115,7 +119,6 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 115 | 119 | ||
| 116 | @Override | 120 | @Override |
| 117 | public PayOrderRespDTO doParseOrderNotify(Map<String, String> params, String body) throws WxPayException { | 121 | public PayOrderRespDTO doParseOrderNotify(Map<String, String> params, String body) throws WxPayException { |
| 118 | - // 微信支付 v2 回调结果处理 | ||
| 119 | switch (config.getApiVersion()) { | 122 | switch (config.getApiVersion()) { |
| 120 | case API_VERSION_V2: | 123 | case API_VERSION_V2: |
| 121 | return doParseOrderNotifyV2(body); | 124 | return doParseOrderNotifyV2(body); |
| @@ -130,7 +133,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -130,7 +133,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 130 | // 1. 解析回调 | 133 | // 1. 解析回调 |
| 131 | WxPayOrderNotifyResult response = client.parseOrderNotifyResult(body); | 134 | WxPayOrderNotifyResult response = client.parseOrderNotifyResult(body); |
| 132 | // 2. 构建结果 | 135 | // 2. 构建结果 |
| 133 | - // 微信支付的回调,只有 SUCCESS 支付成功、CLOSED 支付失败两种情况,无需像支付宝一样解析的比较复杂 | 136 | + // V2 微信支付的回调,只有 SUCCESS 支付成功、CLOSED 支付失败两种情况,无需像支付宝一样解析的比较复杂 |
| 134 | Integer status = Objects.equals(response.getResultCode(), "SUCCESS") ? | 137 | Integer status = Objects.equals(response.getResultCode(), "SUCCESS") ? |
| 135 | PayOrderStatusRespEnum.SUCCESS.getStatus() : PayOrderStatusRespEnum.CLOSED.getStatus(); | 138 | PayOrderStatusRespEnum.SUCCESS.getStatus() : PayOrderStatusRespEnum.CLOSED.getStatus(); |
| 136 | return PayOrderRespDTO.of(status, response.getTransactionId(), response.getOpenid(), parseDateV2(response.getTimeEnd()), | 139 | return PayOrderRespDTO.of(status, response.getTransactionId(), response.getOpenid(), parseDateV2(response.getTimeEnd()), |
| @@ -142,9 +145,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -142,9 +145,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 142 | WxPayOrderNotifyV3Result response = client.parseOrderNotifyV3Result(body, null); | 145 | WxPayOrderNotifyV3Result response = client.parseOrderNotifyV3Result(body, null); |
| 143 | WxPayOrderNotifyV3Result.DecryptNotifyResult result = response.getResult(); | 146 | WxPayOrderNotifyV3Result.DecryptNotifyResult result = response.getResult(); |
| 144 | // 2. 构建结果 | 147 | // 2. 构建结果 |
| 145 | - // 微信支付的回调,只有 SUCCESS 支付成功、CLOSED 支付失败两种情况,无需像支付宝一样解析的比较复杂 | ||
| 146 | - Integer status = Objects.equals(result.getTradeState(), "SUCCESS") ? | ||
| 147 | - PayOrderStatusRespEnum.SUCCESS.getStatus() : PayOrderStatusRespEnum.CLOSED.getStatus(); | 148 | + Integer status = parseStatus(result.getTradeState()); |
| 148 | String openid = result.getPayer() != null ? result.getPayer().getOpenid() : null; | 149 | String openid = result.getPayer() != null ? result.getPayer().getOpenid() : null; |
| 149 | return PayOrderRespDTO.of(status, result.getTransactionId(), openid, parseDateV3(result.getSuccessTime()), | 150 | return PayOrderRespDTO.of(status, result.getTransactionId(), openid, parseDateV3(result.getSuccessTime()), |
| 150 | result.getOutTradeNo(), body); | 151 | result.getOutTradeNo(), body); |
| @@ -152,7 +153,59 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -152,7 +153,59 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 152 | 153 | ||
| 153 | @Override | 154 | @Override |
| 154 | protected PayOrderRespDTO doGetOrder(String outTradeNo) throws Throwable { | 155 | protected PayOrderRespDTO doGetOrder(String outTradeNo) throws Throwable { |
| 155 | - return null; | 156 | + switch (config.getApiVersion()) { |
| 157 | + case API_VERSION_V2: | ||
| 158 | + return doGetOrderV2(outTradeNo); | ||
| 159 | + case WxPayClientConfig.API_VERSION_V3: | ||
| 160 | + return doGetOrderV3(outTradeNo); | ||
| 161 | + default: | ||
| 162 | + throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); | ||
| 163 | + } | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + private PayOrderRespDTO doGetOrderV2(String outTradeNo) throws WxPayException { | ||
| 167 | + // 构建 WxPayUnifiedOrderRequest 对象 | ||
| 168 | + WxPayOrderQueryRequest request = WxPayOrderQueryRequest.newBuilder() | ||
| 169 | + .outTradeNo(outTradeNo).build(); | ||
| 170 | + // 执行请求 | ||
| 171 | + WxPayOrderQueryResult response = client.queryOrder(request); | ||
| 172 | + | ||
| 173 | + // 转换结果 | ||
| 174 | + Integer status = parseStatus(response.getTradeState()); | ||
| 175 | + return PayOrderRespDTO.of(status, response.getTransactionId(), response.getOpenid(), parseDateV2(response.getTimeEnd()), | ||
| 176 | + outTradeNo, response); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + private PayOrderRespDTO doGetOrderV3(String outTradeNo) throws WxPayException { | ||
| 180 | + // 构建 WxPayUnifiedOrderRequest 对象 | ||
| 181 | + WxPayOrderQueryV3Request request = new WxPayOrderQueryV3Request() | ||
| 182 | + .setOutTradeNo(outTradeNo); | ||
| 183 | + // 执行请求 | ||
| 184 | + WxPayOrderQueryV3Result response = client.queryOrderV3(request); | ||
| 185 | + | ||
| 186 | + // 转换结果 | ||
| 187 | + Integer status = parseStatus(response.getTradeState()); | ||
| 188 | + String openid = response.getPayer() != null ? response.getPayer().getOpenid() : null; | ||
| 189 | + return PayOrderRespDTO.of(status, response.getTransactionId(), openid, parseDateV3(response.getSuccessTime()), | ||
| 190 | + outTradeNo, response); | ||
| 191 | + } | ||
| 192 | + | ||
| 193 | + private static Integer parseStatus(String tradeState) { | ||
| 194 | + switch (tradeState) { | ||
| 195 | + case "NOTPAY": | ||
| 196 | + case "USERPAYING": // 支付中,等待用户输入密码(条码支付独有) | ||
| 197 | + return PayOrderStatusRespEnum.WAITING.getStatus(); | ||
| 198 | + case "SUCCESS": | ||
| 199 | + return PayOrderStatusRespEnum.SUCCESS.getStatus(); | ||
| 200 | + case "REFUND": | ||
| 201 | + return PayOrderStatusRespEnum.REFUND.getStatus(); | ||
| 202 | + case "CLOSED": | ||
| 203 | + case "REVOKED": // 已撤销(刷卡支付独有) | ||
| 204 | + case "PAYERROR": // 支付失败(其它原因,如银行返回失败) | ||
| 205 | + return PayOrderStatusRespEnum.CLOSED.getStatus(); | ||
| 206 | + default: | ||
| 207 | + throw new IllegalArgumentException(StrUtil.format("未知的支付状态({})", tradeState)); | ||
| 208 | + } | ||
| 156 | } | 209 | } |
| 157 | 210 | ||
| 158 | // ============ 退款相关 ========== | 211 | // ============ 退款相关 ========== |