From 8c5681f4f92f098c903cedeb64ff638cebd19dc9 Mon Sep 17 00:00:00 2001 From: xgd Date: Fri, 29 Jul 2022 17:31:35 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=96=B0=E5=A2=9E]=E6=8E=A5=E5=85=A5=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E5=AE=9D=E3=80=90PC=E7=BD=91=E7=AB=99=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/impl/PayClientFactoryImpl.java | 3 +- .../client/impl/alipay/AlipayPcPayClient.java | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClient.java diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java index aefb3ba3b..421dbfe7d 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java @@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.pay.core.client.PayClient; import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPayClientConfig; +import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPcPayClient; import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayQrPayClient; import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayWapPayClient; import cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXLitePayClient; @@ -69,7 +70,7 @@ public class PayClientFactoryImpl implements PayClientFactory { case ALIPAY_WAP: return (AbstractPayClient) new AlipayWapPayClient(channelId, (AlipayPayClientConfig) config); case ALIPAY_QR: return (AbstractPayClient) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); case ALIPAY_APP: return (AbstractPayClient) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); - case ALIPAY_PC: return (AbstractPayClient) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); + case ALIPAY_PC: return (AbstractPayClient) new AlipayPcPayClient(channelId, (AlipayPayClientConfig) config); } // 创建失败,错误日志 + 抛出异常 log.error("[createPayClient][配置({}) 找不到合适的客户端实现]", config); diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClient.java new file mode 100644 index 000000000..41a391c05 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClient.java @@ -0,0 +1,60 @@ +package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; + +import cn.hutool.core.util.StrUtil; +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; +import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; +import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderUnifiedReqDTO; +import cn.iocoder.yudao.framework.pay.core.enums.PayChannelEnum; +import com.alibaba.fastjson.JSONObject; +import com.alipay.api.AlipayApiException; +import com.alipay.api.domain.AlipayTradePagePayModel; +import com.alipay.api.request.AlipayTradePagePayRequest; +import com.alipay.api.response.AlipayTradePagePayResponse; +import lombok.extern.slf4j.Slf4j; + + +/** + * 支付宝【PC网站支付】的 PayClient 实现类 + * 文档:https://opendocs.alipay.com/open/270/105898 + * + * @author XGD + */ +@Slf4j +public class AlipayPcPayClient extends AbstractAlipayClient { + + public AlipayPcPayClient(Long channelId, AlipayPayClientConfig config) { + super(channelId, PayChannelEnum.ALIPAY_PC.getCode(), config, new AlipayPayCodeMapping()); + } + + @Override + public PayCommonResult doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { + // 构建 AlipayTradePagePayModel 请求 + AlipayTradePagePayModel model = new AlipayTradePagePayModel(); + // 构建 AlipayTradePagePayRequest + AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); + request.setBizModel(model); + JSONObject bizContent = new JSONObject(); + // 参数说明可查看: https://opendocs.alipay.com/open/028r8t?scene=22 + bizContent.put("out_trade_no", reqDTO.getMerchantOrderId()); + bizContent.put("total_amount", calculateAmount(reqDTO.getAmount())); + bizContent.put("subject", reqDTO.getSubject()); + bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY"); + // PC扫码支付的方式:支持前置模式和跳转模式。4: 订单码-可定义宽度的嵌入式二维码 + bizContent.put("qr_pay_mode", "4"); + // 自定义二维码宽度 + bizContent.put("qrcode_width", "150"); + request.setBizContent(bizContent.toJSONString()); + request.setNotifyUrl(reqDTO.getNotifyUrl()); + request.setReturnUrl(""); + // 执行请求 + AlipayTradePagePayResponse response; + try { + response = client.pageExecute(request); + } catch (AlipayApiException e) { + log.error("[unifiedOrder][request({}) 发起支付失败]", JsonUtils.toJsonString(reqDTO), e); + return PayCommonResult.build(e.getErrCode(), e.getErrMsg(), null, codeMapping); + } + // 响应为表单格式,前端可嵌入响应的页面或关闭当前支付窗口 + return PayCommonResult.build(StrUtil.blankToDefault(response.getCode(),"10000") ,response.getMsg(), response, codeMapping); + } +}