Commit 3a1694dea32b5fc5a284c3dec02692abcd5bc6de
1 parent
0fcc0072
1. 微信 App 支付 WxAppPayClient 实现
2. 优化微信支付的下单逻辑,增加 build request 公用方法
Showing
5 changed files
with
91 additions
and
41 deletions
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/weixin/AbstractWxPayClient.java
| @@ -113,6 +113,41 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -113,6 +113,41 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 113 | protected abstract PayOrderRespDTO doUnifiedOrderV3(PayOrderUnifiedReqDTO reqDTO) | 113 | protected abstract PayOrderRespDTO doUnifiedOrderV3(PayOrderUnifiedReqDTO reqDTO) |
| 114 | throws WxPayException; | 114 | throws WxPayException; |
| 115 | 115 | ||
| 116 | + /** | ||
| 117 | + * 【V2】创建微信下单请求 | ||
| 118 | + * | ||
| 119 | + * @param reqDTO 下信息 | ||
| 120 | + * @return 下单请求 | ||
| 121 | + */ | ||
| 122 | + protected WxPayUnifiedOrderRequest buildPayUnifiedOrderRequestV2(PayOrderUnifiedReqDTO reqDTO) { | ||
| 123 | + return WxPayUnifiedOrderRequest.newBuilder() | ||
| 124 | + .outTradeNo(reqDTO.getOutTradeNo()) | ||
| 125 | + .body(reqDTO.getSubject()) | ||
| 126 | + .detail(reqDTO.getBody()) | ||
| 127 | + .totalFee(reqDTO.getPrice()) // 单位分 | ||
| 128 | + .timeExpire(formatDateV2(reqDTO.getExpireTime())) | ||
| 129 | + .spbillCreateIp(reqDTO.getUserIp()) | ||
| 130 | + .notifyUrl(reqDTO.getNotifyUrl()) | ||
| 131 | + .build(); | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + /** | ||
| 135 | + * 【V3】创建微信下单请求 | ||
| 136 | + * | ||
| 137 | + * @param reqDTO 下信息 | ||
| 138 | + * @return 下单请求 | ||
| 139 | + */ | ||
| 140 | + protected WxPayUnifiedOrderV3Request buildPayUnifiedOrderRequestV3(PayOrderUnifiedReqDTO reqDTO) { | ||
| 141 | + WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request(); | ||
| 142 | + request.setOutTradeNo(reqDTO.getOutTradeNo()); | ||
| 143 | + request.setDescription(reqDTO.getSubject()); | ||
| 144 | + request.setAmount(new WxPayUnifiedOrderV3Request.Amount().setTotal(reqDTO.getPrice())); // 单位分 | ||
| 145 | + request.setTimeExpire(formatDateV3(reqDTO.getExpireTime())); | ||
| 146 | + request.setSceneInfo(new WxPayUnifiedOrderV3Request.SceneInfo().setPayerClientIp(reqDTO.getUserIp())); | ||
| 147 | + request.setNotifyUrl(reqDTO.getNotifyUrl()); | ||
| 148 | + return request; | ||
| 149 | + } | ||
| 150 | + | ||
| 116 | @Override | 151 | @Override |
| 117 | public PayOrderRespDTO doParseOrderNotify(Map<String, String> params, String body) throws WxPayException { | 152 | public PayOrderRespDTO doParseOrderNotify(Map<String, String> params, String body) throws WxPayException { |
| 118 | switch (config.getApiVersion()) { | 153 | switch (config.getApiVersion()) { |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/weixin/WxAppPayClient.java
| @@ -3,10 +3,28 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.weixin; | @@ -3,10 +3,28 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.weixin; | ||
| 3 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; | 3 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; |
| 4 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; | 4 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; |
| 5 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; | 5 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; |
| 6 | +import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum; | ||
| 7 | +import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult; | ||
| 8 | +import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; | ||
| 9 | +import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request; | ||
| 10 | +import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result; | ||
| 11 | +import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum; | ||
| 6 | import com.github.binarywang.wxpay.constant.WxPayConstants; | 12 | import com.github.binarywang.wxpay.constant.WxPayConstants; |
| 7 | import com.github.binarywang.wxpay.exception.WxPayException; | 13 | import com.github.binarywang.wxpay.exception.WxPayException; |
| 14 | +import lombok.extern.slf4j.Slf4j; | ||
| 8 | 15 | ||
| 9 | -// TODO 芋艿:未实现 | 16 | +import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; |
| 17 | + | ||
| 18 | +/** | ||
| 19 | + * 微信支付【App 支付】的 PayClient 实现类 | ||
| 20 | + * | ||
| 21 | + * 文档:<a href="https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_5_3.shtml">App 支付</a> | ||
| 22 | + * | ||
| 23 | + * // TODO 芋艿:未详细测试,因为手头没 App | ||
| 24 | + * | ||
| 25 | + * @author 芋道源码 | ||
| 26 | + */ | ||
| 27 | +@Slf4j | ||
| 10 | public class WxAppPayClient extends AbstractWxPayClient { | 28 | public class WxAppPayClient extends AbstractWxPayClient { |
| 11 | 29 | ||
| 12 | public WxAppPayClient(Long channelId, WxPayClientConfig config) { | 30 | public WxAppPayClient(Long channelId, WxPayClientConfig config) { |
| @@ -20,12 +38,26 @@ public class WxAppPayClient extends AbstractWxPayClient { | @@ -20,12 +38,26 @@ public class WxAppPayClient extends AbstractWxPayClient { | ||
| 20 | 38 | ||
| 21 | @Override | 39 | @Override |
| 22 | protected PayOrderRespDTO doUnifiedOrderV2(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { | 40 | protected PayOrderRespDTO doUnifiedOrderV2(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { |
| 23 | - return null; | 41 | + // 构建 WxPayUnifiedOrderRequest 对象 |
| 42 | + WxPayUnifiedOrderRequest request = buildPayUnifiedOrderRequestV2(reqDTO); | ||
| 43 | + // 执行请求 | ||
| 44 | + WxPayMpOrderResult response = client.createOrder(request); | ||
| 45 | + | ||
| 46 | + // 转换结果 | ||
| 47 | + return PayOrderRespDTO.waitingOf(PayOrderDisplayModeEnum.APP.getMode(), toJsonString(response), | ||
| 48 | + reqDTO.getOutTradeNo(), response); | ||
| 24 | } | 49 | } |
| 25 | 50 | ||
| 26 | @Override | 51 | @Override |
| 27 | protected PayOrderRespDTO doUnifiedOrderV3(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { | 52 | protected PayOrderRespDTO doUnifiedOrderV3(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { |
| 28 | - return null; | 53 | + // 构建 WxPayUnifiedOrderV3Request 对象 |
| 54 | + WxPayUnifiedOrderV3Request request = buildPayUnifiedOrderRequestV3(reqDTO); | ||
| 55 | + // 执行请求 | ||
| 56 | + WxPayUnifiedOrderV3Result.JsapiResult response = client.createOrderV3(TradeTypeEnum.APP, request); | ||
| 57 | + | ||
| 58 | + // 转换结果 | ||
| 59 | + return PayOrderRespDTO.waitingOf(PayOrderDisplayModeEnum.APP.getMode(), toJsonString(response), | ||
| 60 | + reqDTO.getOutTradeNo(), response); | ||
| 29 | } | 61 | } |
| 30 | 62 | ||
| 31 | } | 63 | } |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/weixin/WxNativePayClient.java
| @@ -34,16 +34,7 @@ public class WxNativePayClient extends AbstractWxPayClient { | @@ -34,16 +34,7 @@ public class WxNativePayClient extends AbstractWxPayClient { | ||
| 34 | @Override | 34 | @Override |
| 35 | protected PayOrderRespDTO doUnifiedOrderV2(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { | 35 | protected PayOrderRespDTO doUnifiedOrderV2(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { |
| 36 | // 构建 WxPayUnifiedOrderRequest 对象 | 36 | // 构建 WxPayUnifiedOrderRequest 对象 |
| 37 | - WxPayUnifiedOrderRequest request = WxPayUnifiedOrderRequest.newBuilder() | ||
| 38 | - .outTradeNo(reqDTO.getOutTradeNo()) | ||
| 39 | - .body(reqDTO.getSubject()) | ||
| 40 | - .detail(reqDTO.getBody()) | ||
| 41 | - .totalFee(reqDTO.getPrice()) // 单位分 | ||
| 42 | - .productId(reqDTO.getOutTradeNo()) | ||
| 43 | - .timeExpire(formatDateV2(reqDTO.getExpireTime())) | ||
| 44 | - .spbillCreateIp(reqDTO.getUserIp()) | ||
| 45 | - .notifyUrl(reqDTO.getNotifyUrl()) | ||
| 46 | - .build(); | 37 | + WxPayUnifiedOrderRequest request = buildPayUnifiedOrderRequestV2(reqDTO); |
| 47 | // 执行请求 | 38 | // 执行请求 |
| 48 | WxPayNativeOrderResult response = client.createOrder(request); | 39 | WxPayNativeOrderResult response = client.createOrder(request); |
| 49 | 40 | ||
| @@ -54,14 +45,8 @@ public class WxNativePayClient extends AbstractWxPayClient { | @@ -54,14 +45,8 @@ public class WxNativePayClient extends AbstractWxPayClient { | ||
| 54 | 45 | ||
| 55 | @Override | 46 | @Override |
| 56 | protected PayOrderRespDTO doUnifiedOrderV3(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { | 47 | protected PayOrderRespDTO doUnifiedOrderV3(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { |
| 57 | - // 构建 WxPayUnifiedOrderRequest 对象 | ||
| 58 | - WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request() | ||
| 59 | - .setOutTradeNo(reqDTO.getOutTradeNo()) | ||
| 60 | - .setDescription(reqDTO.getSubject()) | ||
| 61 | - .setAmount(new WxPayUnifiedOrderV3Request.Amount().setTotal(reqDTO.getPrice())) // 单位分 | ||
| 62 | - .setTimeExpire(formatDateV3(reqDTO.getExpireTime())) | ||
| 63 | - .setSceneInfo(new WxPayUnifiedOrderV3Request.SceneInfo().setPayerClientIp(reqDTO.getUserIp())) | ||
| 64 | - .setNotifyUrl(reqDTO.getNotifyUrl()); | 48 | + // 构建 WxPayUnifiedOrderV3Request 对象 |
| 49 | + WxPayUnifiedOrderV3Request request = buildPayUnifiedOrderRequestV3(reqDTO); | ||
| 65 | // 执行请求 | 50 | // 执行请求 |
| 66 | String response = client.createOrderV3(TradeTypeEnum.NATIVE, request); | 51 | String response = client.createOrderV3(TradeTypeEnum.NATIVE, request); |
| 67 | 52 |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/weixin/WxPubPayClient.java
| @@ -4,7 +4,6 @@ import cn.hutool.core.map.MapUtil; | @@ -4,7 +4,6 @@ import cn.hutool.core.map.MapUtil; | ||
| 4 | import cn.hutool.core.util.StrUtil; | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; | 5 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; |
| 6 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; | 6 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; |
| 7 | -import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; | ||
| 8 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; | 7 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; |
| 9 | import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum; | 8 | import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum; |
| 10 | import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult; | 9 | import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult; |
| @@ -45,16 +44,8 @@ public class WxPubPayClient extends AbstractWxPayClient { | @@ -45,16 +44,8 @@ public class WxPubPayClient extends AbstractWxPayClient { | ||
| 45 | @Override | 44 | @Override |
| 46 | protected PayOrderRespDTO doUnifiedOrderV2(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { | 45 | protected PayOrderRespDTO doUnifiedOrderV2(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { |
| 47 | // 构建 WxPayUnifiedOrderRequest 对象 | 46 | // 构建 WxPayUnifiedOrderRequest 对象 |
| 48 | - WxPayUnifiedOrderRequest request = WxPayUnifiedOrderRequest.newBuilder() | ||
| 49 | - .outTradeNo(reqDTO.getOutTradeNo()) | ||
| 50 | - .body(reqDTO.getSubject()) | ||
| 51 | - .detail(reqDTO.getBody()) | ||
| 52 | - .totalFee(reqDTO.getPrice()) // 单位分 | ||
| 53 | - .timeExpire(formatDateV2(reqDTO.getExpireTime())) | ||
| 54 | - .spbillCreateIp(reqDTO.getUserIp()) | ||
| 55 | - .openid(getOpenid(reqDTO)) | ||
| 56 | - .notifyUrl(reqDTO.getNotifyUrl()) | ||
| 57 | - .build(); | 47 | + WxPayUnifiedOrderRequest request = buildPayUnifiedOrderRequestV2(reqDTO) |
| 48 | + .setOpenid(getOpenid(reqDTO)); | ||
| 58 | // 执行请求 | 49 | // 执行请求 |
| 59 | WxPayMpOrderResult response = client.createOrder(request); | 50 | WxPayMpOrderResult response = client.createOrder(request); |
| 60 | 51 | ||
| @@ -66,14 +57,8 @@ public class WxPubPayClient extends AbstractWxPayClient { | @@ -66,14 +57,8 @@ public class WxPubPayClient extends AbstractWxPayClient { | ||
| 66 | @Override | 57 | @Override |
| 67 | protected PayOrderRespDTO doUnifiedOrderV3(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { | 58 | protected PayOrderRespDTO doUnifiedOrderV3(PayOrderUnifiedReqDTO reqDTO) throws WxPayException { |
| 68 | // 构建 WxPayUnifiedOrderRequest 对象 | 59 | // 构建 WxPayUnifiedOrderRequest 对象 |
| 69 | - WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request(); | ||
| 70 | - request.setOutTradeNo(reqDTO.getOutTradeNo()); | ||
| 71 | - request.setDescription(reqDTO.getSubject()); | ||
| 72 | - request.setAmount(new WxPayUnifiedOrderV3Request.Amount().setTotal(reqDTO.getPrice())); // 单位分 | ||
| 73 | - request.setTimeExpire(formatDateV3(reqDTO.getExpireTime())); | ||
| 74 | - request.setPayer(new WxPayUnifiedOrderV3Request.Payer().setOpenid(getOpenid(reqDTO))); | ||
| 75 | - request.setSceneInfo(new WxPayUnifiedOrderV3Request.SceneInfo().setPayerClientIp(reqDTO.getUserIp())); | ||
| 76 | - request.setNotifyUrl(reqDTO.getNotifyUrl()); | 60 | + WxPayUnifiedOrderV3Request request = buildPayUnifiedOrderRequestV3(reqDTO) |
| 61 | + .setPayer(new WxPayUnifiedOrderV3Request.Payer().setOpenid(getOpenid(reqDTO))); | ||
| 77 | // 执行请求 | 62 | // 执行请求 |
| 78 | WxPayUnifiedOrderV3Result.JsapiResult response = client.createOrderV3(TradeTypeEnum.JSAPI, request); | 63 | WxPayUnifiedOrderV3Result.JsapiResult response = client.createOrderV3(TradeTypeEnum.JSAPI, request); |
| 79 | 64 |
yudao-ui-admin/src/views/pay/cashier/index.vue
| @@ -212,6 +212,16 @@ export default { | @@ -212,6 +212,16 @@ export default { | ||
| 212 | return; | 212 | return; |
| 213 | } | 213 | } |
| 214 | 214 | ||
| 215 | + // 微信公众号、小程序支付,无法在 PC 网页中进行 | ||
| 216 | + if (channelCode === PayChannelEnum.WX_PUB.code) { | ||
| 217 | + this.$message.error('微信公众号支付:不支持 PC 网站'); | ||
| 218 | + return; | ||
| 219 | + } | ||
| 220 | + if (channelCode === PayChannelEnum.WX_LITE.code) { | ||
| 221 | + this.$message.error('微信小程序:不支持 PC 网站'); | ||
| 222 | + return; | ||
| 223 | + } | ||
| 224 | + | ||
| 215 | // 默认的提交处理 | 225 | // 默认的提交处理 |
| 216 | this.submit0(channelCode) | 226 | this.submit0(channelCode) |
| 217 | }, | 227 | }, |
| @@ -292,7 +302,10 @@ export default { | @@ -292,7 +302,10 @@ export default { | ||
| 292 | /** 提交支付后(App) */ | 302 | /** 提交支付后(App) */ |
| 293 | displayApp(channelCode, data) { | 303 | displayApp(channelCode, data) { |
| 294 | if (channelCode === PayChannelEnum.ALIPAY_APP.code) { | 304 | if (channelCode === PayChannelEnum.ALIPAY_APP.code) { |
| 295 | - this.$message.error('支付宝 App 无法在网页支付!'); | 305 | + this.$message.error('支付宝 App 支付:无法在网页支付!'); |
| 306 | + } | ||
| 307 | + if (channelCode === PayChannelEnum.WX_APP.code) { | ||
| 308 | + this.$message.error('微信 App 支付:无法在网页支付!'); | ||
| 296 | } | 309 | } |
| 297 | this.submitLoading = false | 310 | this.submitLoading = false |
| 298 | }, | 311 | }, |