mall + pay:

1. 拆分支付回调、退款回调的 URL
2. 修复微信支付回调的时间解析错误
This commit is contained in:
YunaiV
2023-07-17 11:47:45 +08:00
parent 66ed61c641
commit 68a4ef98ca
14 changed files with 166 additions and 76 deletions

View File

@ -3,8 +3,8 @@ package cn.iocoder.yudao.module.pay.controller.admin.notify;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.pay.core.client.PayClient;
import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory;
import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
import io.swagger.v3.oas.annotations.Operation;
@ -18,7 +18,6 @@ import javax.annotation.security.PermitAll;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.PAY_CHANNEL_CLIENT_NOT_FOUND;
@Tag(name = "管理后台 - 支付通知")
@ -36,22 +35,14 @@ public class PayNotifyController {
@Resource
private PayClientFactory payClientFactory;
/**
* 统一的渠道支付回调,支付宝的退款回调
*
* @param channelId 渠道编号
* @param params form 参数
* @param body request body
* @return 成功返回 "success"
*/
@PostMapping(value = "/callback/{channelId}")
@Operation(summary = "支付渠道的统一回调接口 - 包括支付回调,退款回调")
@PostMapping(value = "/order/{channelId}")
@Operation(summary = "支付渠道的统一【支付】回调")
@PermitAll
@OperateLog(enable = false) // 回调地址,无需记录操作日志
public String notifyCallback(@PathVariable("channelId") Long channelId,
@RequestParam(required = false) Map<String, String> params,
@RequestBody(required = false) String body) {
log.info("[notifyCallback][channelId({}) 回调数据({}/{})]", channelId, params, body);
public String notifyOrder(@PathVariable("channelId") Long channelId,
@RequestParam(required = false) Map<String, String> params,
@RequestBody(required = false) String body) {
log.info("[notifyOrder][channelId({}) 回调数据({}/{})]", channelId, params, body);
// 1. 校验支付渠道是否存在
PayClient payClient = payClientFactory.getPayClient(channelId);
if (payClient == null) {
@ -60,20 +51,30 @@ public class PayNotifyController {
}
// 2. 解析通知数据
Object notify = payClient.parseNotify(params, body);
PayOrderRespDTO notify = payClient.parseOrderNotify(params, body);
orderService.notifyOrder(channelId, notify);
return "success";
}
// 3. 处理通知
// 3.1:退款通知
if (notify instanceof PayRefundRespDTO) {
refundService.notifyRefund(channelId, (PayRefundRespDTO) notify);
return "success";
@PostMapping(value = "/refund/{channelId}")
@Operation(summary = "支付渠道的统一【退款】回调")
@PermitAll
@OperateLog(enable = false) // 回调地址,无需记录操作日志
public String notifyRefund(@PathVariable("channelId") Long channelId,
@RequestParam(required = false) Map<String, String> params,
@RequestBody(required = false) String body) {
log.info("[notifyRefund][channelId({}) 回调数据({}/{})]", channelId, params, body);
// 1. 校验支付渠道是否存在
PayClient payClient = payClientFactory.getPayClient(channelId);
if (payClient == null) {
log.error("[notifyCallback][渠道编号({}) 找不到对应的支付客户端]", channelId);
throw exception(PAY_CHANNEL_CLIENT_NOT_FOUND);
}
// 3.2:支付通知
if (notify instanceof PayOrderRespDTO) {
orderService.notifyOrder(channelId, (PayOrderRespDTO) notify);
return "success";
}
throw new UnsupportedOperationException("未知通知:" + toJsonString(notify));
// 2. 解析通知数据
PayRefundRespDTO notify = payClient.parseRefundNotify(params, body);
refundService.notifyRefund(channelId, notify);
return "success";
}
}

View File

@ -48,7 +48,7 @@ public class PayAppDO extends BaseDO {
/**
* 支付结果的回调地址
*/
private String payNotifyUrl;
private String orderNotifyUrl;
/**
* 退款结果的回调地址
*/

View File

@ -20,11 +20,11 @@ import javax.annotation.Resource;
public class PayNotifyJob implements JobHandler {
@Resource
private PayNotifyService payNotifyCoreService;
private PayNotifyService payNotifyService;
@Override
public String execute(String param) throws Exception {
int notifyCount = payNotifyCoreService.executeNotify();
int notifyCount = payNotifyService.executeNotify();
return String.format("执行支付通知 %s 个", notifyCount);
}

View File

@ -125,7 +125,7 @@ public class PayOrderServiceImpl implements PayOrderService {
// 创建支付交易单
order = PayOrderConvert.INSTANCE.convert(reqDTO).setAppId(app.getId())
// 商户相关字段
.setNotifyUrl(app.getPayNotifyUrl()).setNotifyStatus(PayOrderNotifyStatusEnum.NO.getStatus())
.setNotifyUrl(app.getOrderNotifyUrl()).setNotifyStatus(PayOrderNotifyStatusEnum.NO.getStatus())
// 订单相关字段
.setStatus(PayOrderStatusEnum.WAITING.getStatus())
// 退款相关字段
@ -206,7 +206,7 @@ public class PayOrderServiceImpl implements PayOrderService {
* @return 支付渠道的回调地址 配置地址 + "/" + channel id
*/
private String genChannelPayNotifyUrl(PayChannelDO channel) {
return payProperties.getCallbackUrl() + "/" + channel.getId();
return payProperties.getOrderNotifyUrl() + "/" + channel.getId();
}
private String generateOrderExtensionNo() {

View File

@ -134,7 +134,7 @@ public class PayRefundServiceImpl implements PayRefundService {
.setRefundPrice(reqDTO.getPrice())
.setOutTradeNo(orderExtension.getNo())
.setOutRefundNo(refund.getNo())
.setNotifyUrl(genChannelPayNotifyUrl(channel)) // TODO 芋艿:优化下 notifyUrl
.setNotifyUrl(genChannelPayNotifyUrl(channel))
.setReason(reqDTO.getReason());
PayRefundRespDTO refundRespDTO = client.unifiedRefund(unifiedReqDTO); // TODO 增加一个 channelErrorCode、channelErrorMsg 字段
// 2.3 处理退款返回
@ -183,7 +183,7 @@ public class PayRefundServiceImpl implements PayRefundService {
* @return 支付渠道的回调地址 配置地址 + "/" + channel id
*/
private String genChannelPayNotifyUrl(PayChannelDO channel) {
return payProperties.getCallbackUrl() + "/" + channel.getId();
return payProperties.getRefundNotifyUrl() + "/" + channel.getId();
}
private String generateRefundNo() {