Commit 8c5681f4f92f098c903cedeb64ff638cebd19dc9
1 parent
f3e1fc4f
[新增]接入支付宝【PC网站支付】
Showing
2 changed files
with
62 additions
and
1 deletions
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; |
| 5 | 5 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; |
| 6 | 6 | import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; |
| 7 | 7 | import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPayClientConfig; |
| 8 | +import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPcPayClient; | |
| 8 | 9 | import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayQrPayClient; |
| 9 | 10 | import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayWapPayClient; |
| 10 | 11 | import cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXLitePayClient; |
| ... | ... | @@ -69,7 +70,7 @@ public class PayClientFactoryImpl implements PayClientFactory { |
| 69 | 70 | case ALIPAY_WAP: return (AbstractPayClient<Config>) new AlipayWapPayClient(channelId, (AlipayPayClientConfig) config); |
| 70 | 71 | case ALIPAY_QR: return (AbstractPayClient<Config>) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); |
| 71 | 72 | case ALIPAY_APP: return (AbstractPayClient<Config>) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); |
| 72 | - case ALIPAY_PC: return (AbstractPayClient<Config>) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); | |
| 73 | + case ALIPAY_PC: return (AbstractPayClient<Config>) new AlipayPcPayClient(channelId, (AlipayPayClientConfig) config); | |
| 73 | 74 | } |
| 74 | 75 | // 创建失败,错误日志 + 抛出异常 |
| 75 | 76 | log.error("[createPayClient][配置({}) 找不到合适的客户端实现]", config); | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClient.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; | |
| 2 | + | |
| 3 | +import cn.hutool.core.util.StrUtil; | |
| 4 | +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | |
| 5 | +import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; | |
| 6 | +import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderUnifiedReqDTO; | |
| 7 | +import cn.iocoder.yudao.framework.pay.core.enums.PayChannelEnum; | |
| 8 | +import com.alibaba.fastjson.JSONObject; | |
| 9 | +import com.alipay.api.AlipayApiException; | |
| 10 | +import com.alipay.api.domain.AlipayTradePagePayModel; | |
| 11 | +import com.alipay.api.request.AlipayTradePagePayRequest; | |
| 12 | +import com.alipay.api.response.AlipayTradePagePayResponse; | |
| 13 | +import lombok.extern.slf4j.Slf4j; | |
| 14 | + | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * 支付宝【PC网站支付】的 PayClient 实现类 | |
| 18 | + * 文档:https://opendocs.alipay.com/open/270/105898 | |
| 19 | + * | |
| 20 | + * @author XGD | |
| 21 | + */ | |
| 22 | +@Slf4j | |
| 23 | +public class AlipayPcPayClient extends AbstractAlipayClient { | |
| 24 | + | |
| 25 | + public AlipayPcPayClient(Long channelId, AlipayPayClientConfig config) { | |
| 26 | + super(channelId, PayChannelEnum.ALIPAY_PC.getCode(), config, new AlipayPayCodeMapping()); | |
| 27 | + } | |
| 28 | + | |
| 29 | + @Override | |
| 30 | + public PayCommonResult<AlipayTradePagePayResponse> doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { | |
| 31 | + // 构建 AlipayTradePagePayModel 请求 | |
| 32 | + AlipayTradePagePayModel model = new AlipayTradePagePayModel(); | |
| 33 | + // 构建 AlipayTradePagePayRequest | |
| 34 | + AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); | |
| 35 | + request.setBizModel(model); | |
| 36 | + JSONObject bizContent = new JSONObject(); | |
| 37 | + // 参数说明可查看: https://opendocs.alipay.com/open/028r8t?scene=22 | |
| 38 | + bizContent.put("out_trade_no", reqDTO.getMerchantOrderId()); | |
| 39 | + bizContent.put("total_amount", calculateAmount(reqDTO.getAmount())); | |
| 40 | + bizContent.put("subject", reqDTO.getSubject()); | |
| 41 | + bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY"); | |
| 42 | + // PC扫码支付的方式:支持前置模式和跳转模式。4: 订单码-可定义宽度的嵌入式二维码 | |
| 43 | + bizContent.put("qr_pay_mode", "4"); | |
| 44 | + // 自定义二维码宽度 | |
| 45 | + bizContent.put("qrcode_width", "150"); | |
| 46 | + request.setBizContent(bizContent.toJSONString()); | |
| 47 | + request.setNotifyUrl(reqDTO.getNotifyUrl()); | |
| 48 | + request.setReturnUrl(""); | |
| 49 | + // 执行请求 | |
| 50 | + AlipayTradePagePayResponse response; | |
| 51 | + try { | |
| 52 | + response = client.pageExecute(request); | |
| 53 | + } catch (AlipayApiException e) { | |
| 54 | + log.error("[unifiedOrder][request({}) 发起支付失败]", JsonUtils.toJsonString(reqDTO), e); | |
| 55 | + return PayCommonResult.build(e.getErrCode(), e.getErrMsg(), null, codeMapping); | |
| 56 | + } | |
| 57 | + // 响应为表单格式,前端可嵌入响应的页面或关闭当前支付窗口 | |
| 58 | + return PayCommonResult.build(StrUtil.blankToDefault(response.getCode(),"10000") ,response.getMsg(), response, codeMapping); | |
| 59 | + } | |
| 60 | +} | ... | ... |