Commit 75633aa84ba63bcc64d7b1aa2a4e58f3e55b4018
1 parent
bac30d47
增加微信公众号的支付回调接口
Showing
13 changed files
with
173 additions
and
11 deletions
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/dal/dataobject/order/PayOrderDO.java
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/dal/dataobject/order/PayOrderExtensionDO.java
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/dal/mysql/order/PayOrderCoreMapper.java
| 1 | 1 | package cn.iocoder.yudao.coreservice.modules.pay.dal.mysql.order; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.order.PayOrderDO; |
| 4 | +import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.order.PayOrderExtensionDO; | |
| 4 | 5 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 5 | 6 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 6 | 7 | import org.apache.ibatis.annotations.Mapper; |
| ... | ... | @@ -13,4 +14,9 @@ public interface PayOrderCoreMapper extends BaseMapperX<PayOrderDO> { |
| 13 | 14 | .eq("merchant_order_id", merchantOrderId)); |
| 14 | 15 | } |
| 15 | 16 | |
| 17 | + default int updateByIdAndStatus(Long id, Integer status, PayOrderDO update) { | |
| 18 | + return update(update, new QueryWrapper<PayOrderDO>() | |
| 19 | + .eq("id", id).eq("status", status)); | |
| 20 | + } | |
| 21 | + | |
| 16 | 22 | } | ... | ... |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/dal/mysql/order/PayOrderExtensionCoreMapper.java
| 1 | 1 | package cn.iocoder.yudao.coreservice.modules.pay.dal.mysql.order; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.order.PayOrderDO; | |
| 4 | 3 | import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.order.PayOrderExtensionDO; |
| 5 | 4 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 6 | 5 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| ... | ... | @@ -9,4 +8,13 @@ import org.apache.ibatis.annotations.Mapper; |
| 9 | 8 | @Mapper |
| 10 | 9 | public interface PayOrderExtensionCoreMapper extends BaseMapperX<PayOrderExtensionDO> { |
| 11 | 10 | |
| 11 | + default PayOrderExtensionDO selectByOrderExtensionNo(String orderExtensionNo) { | |
| 12 | + return selectOne("order_extension_no", orderExtensionNo); | |
| 13 | + } | |
| 14 | + | |
| 15 | + default int updateByIdAndStatus(Long id, Integer status, PayOrderExtensionDO update) { | |
| 16 | + return update(update, new QueryWrapper<PayOrderExtensionDO>() | |
| 17 | + .eq("id", id).eq("status", status)); | |
| 18 | + } | |
| 19 | + | |
| 12 | 20 | } | ... | ... |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/enums/PayErrorCodeCoreConstants.java
| ... | ... | @@ -19,13 +19,13 @@ public interface PayErrorCodeCoreConstants { |
| 19 | 19 | ErrorCode PAY_CHANNEL_CLIENT_NOT_FOUND = new ErrorCode(1007001002, "支付渠道的客户端不存在"); |
| 20 | 20 | |
| 21 | 21 | // ========== ORDER 模块 1-007-002-000 ========== |
| 22 | - ErrorCode PAY_ORDER_NOT_FOUND = new ErrorCode(100401000, "支付订单不存在"); | |
| 23 | - ErrorCode PAY_ORDER_STATUS_IS_NOT_WAITING = new ErrorCode(100401001, "支付订单不处于待支付"); | |
| 24 | - ErrorCode PAY_ORDER_STATUS_IS_NOT_SUCCESS = new ErrorCode(100401002, "支付订单不处于已支付"); | |
| 25 | - ErrorCode PAY_ORDER_ERROR_USER = new ErrorCode(100401003, "支付订单用户不正确"); | |
| 26 | - | |
| 27 | - ErrorCode PAY_ORDER_EXTENSION_NOT_FOUND = new ErrorCode(100401050, "支付交易拓展单不存在"); | |
| 28 | - ErrorCode PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING = new ErrorCode(100401051, "支付交易拓展单不处于待支付"); | |
| 29 | - ErrorCode PAY_ORDER_EXTENSION_STATUS_IS_NOT_SUCCESS = new ErrorCode(100401052, "支付订单不处于已支付"); | |
| 22 | + ErrorCode PAY_ORDER_NOT_FOUND = new ErrorCode(1007002000, "支付订单不存在"); | |
| 23 | + ErrorCode PAY_ORDER_STATUS_IS_NOT_WAITING = new ErrorCode(1007002001, "支付订单不处于待支付"); | |
| 24 | + ErrorCode PAY_ORDER_STATUS_IS_NOT_SUCCESS = new ErrorCode(1007002002, "支付订单不处于已支付"); | |
| 25 | + ErrorCode PAY_ORDER_ERROR_USER = new ErrorCode(1007002003, "支付订单用户不正确"); | |
| 26 | + // ========== ORDER 模块(拓展单) 1-007-003-000 ========== | |
| 27 | + ErrorCode PAY_ORDER_EXTENSION_NOT_FOUND = new ErrorCode(1007003000, "支付交易拓展单不存在"); | |
| 28 | + ErrorCode PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING = new ErrorCode(1007003001, "支付交易拓展单不处于待支付"); | |
| 29 | + ErrorCode PAY_ORDER_EXTENSION_STATUS_IS_NOT_SUCCESS = new ErrorCode(1007003002, "支付订单不处于已支付"); | |
| 30 | 30 | |
| 31 | 31 | } | ... | ... |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/service/order/PayOrderCoreService.java
| ... | ... | @@ -39,4 +39,13 @@ public interface PayOrderCoreService { |
| 39 | 39 | */ |
| 40 | 40 | PayOrderSubmitRespDTO submitPayOrder(@Valid PayOrderSubmitReqDTO reqDTO); |
| 41 | 41 | |
| 42 | + /** | |
| 43 | + * 通知支付单成功 | |
| 44 | + * | |
| 45 | + * @param channelId 渠道编号 | |
| 46 | + * @param channelCode 渠道编码 | |
| 47 | + * @param notifyData 通知数据 | |
| 48 | + */ | |
| 49 | + void notifyPayOrder(Long channelId, String channelCode, String notifyData) throws Exception; | |
| 50 | + | |
| 42 | 51 | } | ... | ... |
yudao-core-service/src/main/java/cn/iocoder/yudao/coreservice/modules/pay/service/order/impl/PayOrderCoreServiceImpl.java
| ... | ... | @@ -23,6 +23,7 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 23 | 23 | import cn.iocoder.yudao.framework.pay.config.PayProperties; |
| 24 | 24 | import cn.iocoder.yudao.framework.pay.core.client.PayClient; |
| 25 | 25 | import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; |
| 26 | +import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderNotifyRespDTO; | |
| 26 | 27 | import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderUnifiedReqDTO; |
| 27 | 28 | import lombok.extern.slf4j.Slf4j; |
| 28 | 29 | import org.springframework.stereotype.Service; |
| ... | ... | @@ -99,7 +100,7 @@ public class PayOrderCoreServiceImpl implements PayOrderCoreService { |
| 99 | 100 | @Override |
| 100 | 101 | public PayOrderSubmitRespDTO submitPayOrder(PayOrderSubmitReqDTO reqDTO) { |
| 101 | 102 | // 校验 App |
| 102 | - PayAppDO app = payAppCoreService.validPayApp(reqDTO.getAppId()); | |
| 103 | + payAppCoreService.validPayApp(reqDTO.getAppId()); | |
| 103 | 104 | // 校验支付渠道是否有效 |
| 104 | 105 | PayChannelDO channel = payChannelCoreService.validPayChannel(reqDTO.getAppId(), reqDTO.getChannelCode()); |
| 105 | 106 | // 校验支付客户端是否正确初始化 |
| ... | ... | @@ -172,4 +173,61 @@ public class PayOrderCoreServiceImpl implements PayOrderCoreService { |
| 172 | 173 | ; |
| 173 | 174 | } |
| 174 | 175 | |
| 176 | + @Override | |
| 177 | + public void notifyPayOrder(Long channelId, String channelCode, String notifyData) throws Exception { | |
| 178 | + // TODO 芋艿,记录回调日志 | |
| 179 | + log.info("[notifyPayOrder][channelId({}) 回调数据({})]", channelId, notifyData); | |
| 180 | + | |
| 181 | + // 校验支付渠道是否有效 | |
| 182 | + PayChannelDO channel = payChannelCoreService.validPayChannel(channelId, channelCode); | |
| 183 | + // 校验支付客户端是否正确初始化 | |
| 184 | + PayClient client = payClientFactory.getPayClient(channel.getId()); | |
| 185 | + if (client == null) { | |
| 186 | + log.error("[notifyPayOrder][渠道编号({}) 找不到对应的支付客户端]", channel.getId()); | |
| 187 | + throw exception(PAY_CHANNEL_CLIENT_NOT_FOUND); | |
| 188 | + } | |
| 189 | + // 解析支付结果 | |
| 190 | + PayOrderNotifyRespDTO notifyRespDTO = client.parseOrderNotify(notifyData); | |
| 191 | + | |
| 192 | + // TODO 芋艿,先最严格的校验。即使调用方重复调用,实际哪个订单已经被重复回调的支付,也返回 false 。也没问题,因为实际已经回调成功了。 | |
| 193 | + // 1.1 查询 PayOrderExtensionDO | |
| 194 | + PayOrderExtensionDO orderExtension = payOrderExtensionCoreMapper.selectByOrderExtensionNo( | |
| 195 | + notifyRespDTO.getOrderExtensionNo()); | |
| 196 | + if (orderExtension == null) { | |
| 197 | + throw exception(PAY_ORDER_EXTENSION_NOT_FOUND); | |
| 198 | + } | |
| 199 | + if (!PayOrderStatusEnum.WAITING.getStatus().equals(orderExtension.getStatus())) { // 校验状态,必须是待支付 | |
| 200 | + throw exception(PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING); | |
| 201 | + } | |
| 202 | + // 1.2 更新 PayOrderExtensionDO | |
| 203 | + int updateCounts = payOrderExtensionCoreMapper.updateByIdAndStatus(orderExtension.getOrderId(), | |
| 204 | + PayOrderStatusEnum.WAITING.getStatus(), PayOrderExtensionDO.builder().id(orderExtension.getId()) | |
| 205 | + .status(PayOrderStatusEnum.SUCCESS.getStatus()).channelNotifyData(notifyData).build()); | |
| 206 | + if (updateCounts == 0) { // 校验状态,必须是待支付 | |
| 207 | + throw exception(PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING); | |
| 208 | + } | |
| 209 | + log.info("[notifyPayOrder][支付拓展单({}) 更新为已支付]", orderExtension.getId()); | |
| 210 | + | |
| 211 | + // 2.1 判断 PayOrderDO 是否处于待支付 | |
| 212 | + PayOrderDO order = payOrderCoreMapper.selectById(orderExtension.getOrderId()); | |
| 213 | + if (order == null) { | |
| 214 | + throw exception(PAY_ORDER_NOT_FOUND); | |
| 215 | + } | |
| 216 | + if (!PayOrderStatusEnum.WAITING.getStatus().equals(order.getStatus())) { // 校验状态,必须是待支付 | |
| 217 | + throw exception(PAY_ORDER_STATUS_IS_NOT_WAITING); | |
| 218 | + } | |
| 219 | + // 2.2 更新 PayOrderDO | |
| 220 | + updateCounts = payOrderCoreMapper.updateByIdAndStatus(order.getId(), PayOrderStatusEnum.WAITING.getStatus(), | |
| 221 | + PayOrderDO.builder().status(PayOrderStatusEnum.SUCCESS.getStatus()).channelId(channelId).channelCode(channelCode) | |
| 222 | + .successTime(notifyRespDTO.getSuccessTime()).notifyTime(new Date()) | |
| 223 | + .successExtensionId(orderExtension.getId()).build()); | |
| 224 | + if (updateCounts == 0) { // 校验状态,必须是待支付 | |
| 225 | + throw exception(PAY_ORDER_STATUS_IS_NOT_WAITING); | |
| 226 | + } | |
| 227 | + log.info("[notifyPayOrder][支付订单({}) 更新为已支付]", order.getId()); | |
| 228 | + | |
| 229 | + // 3. 插入支付通知记录 | |
| 230 | +// payNotifyService.addPayTransactionNotifyTask(order, orderExtension); | |
| 231 | + } | |
| 232 | + | |
| 175 | 233 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClient.java
| 1 | 1 | package cn.iocoder.yudao.framework.pay.core.client; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderNotifyRespDTO; | |
| 3 | 4 | import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderUnifiedReqDTO; |
| 4 | 5 | |
| 5 | 6 | /** |
| ... | ... | @@ -24,4 +25,13 @@ public interface PayClient { |
| 24 | 25 | */ |
| 25 | 26 | PayCommonResult<?> unifiedOrder(PayOrderUnifiedReqDTO reqDTO); |
| 26 | 27 | |
| 28 | + /** | |
| 29 | + * 解析支付单的通知结果 | |
| 30 | + * | |
| 31 | + * @param data 通知结果 | |
| 32 | + * @return 解析结果 | |
| 33 | + * @throws Exception 解析失败,抛出异常 | |
| 34 | + */ | |
| 35 | + PayOrderNotifyRespDTO parseOrderNotify(String data) throws Exception; | |
| 36 | + | |
| 27 | 37 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/dto/PayOrderNotifyRespDTO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.pay.core.client.dto; | |
| 2 | + | |
| 3 | +import lombok.AllArgsConstructor; | |
| 4 | +import lombok.Data; | |
| 5 | +import lombok.NoArgsConstructor; | |
| 6 | + | |
| 7 | +import java.util.Date; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 支付通知 Response DTO | |
| 11 | + * | |
| 12 | + * @author 芋道源码 | |
| 13 | + */ | |
| 14 | +@Data | |
| 15 | +@NoArgsConstructor | |
| 16 | +@AllArgsConstructor | |
| 17 | +public class PayOrderNotifyRespDTO { | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * 支付订单号(支付模块的) | |
| 21 | + */ | |
| 22 | + private String orderExtensionNo; | |
| 23 | + /** | |
| 24 | + * 支付渠道 | |
| 25 | + */ | |
| 26 | + private String channelOrderNo; | |
| 27 | + /** | |
| 28 | + * 支付渠道 | |
| 29 | + */ | |
| 30 | + private Date successTime; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * 通知的原始数据 | |
| 34 | + * | |
| 35 | + * 主要用于持久化,方便后续修复数据,或者排错 | |
| 36 | + */ | |
| 37 | + private String data; | |
| 38 | + | |
| 39 | +} | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/dto/PayOrderUnifiedReqDTO.java
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayQrPayClient.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.bean.BeanUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; |
| 5 | +import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderNotifyRespDTO; | |
| 5 | 6 | import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderUnifiedReqDTO; |
| 6 | 7 | import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; |
| 7 | 8 | import cn.iocoder.yudao.framework.pay.core.enums.PayChannelEnum; |
| ... | ... | @@ -65,4 +66,9 @@ public class AlipayQrPayClient extends AbstractPayClient<AlipayPayClientConfig> |
| 65 | 66 | return PayCommonResult.build(response.getCode(), response.getMsg(), response, codeMapping); |
| 66 | 67 | } |
| 67 | 68 | |
| 69 | + @Override | |
| 70 | + public PayOrderNotifyRespDTO parseOrderNotify(String data) throws Exception { | |
| 71 | + // TODO 芋艿:待完成 | |
| 72 | + return null; | |
| 73 | + } | |
| 68 | 74 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayWapPayClient.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.bean.BeanUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; |
| 5 | +import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderNotifyRespDTO; | |
| 5 | 6 | import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderUnifiedReqDTO; |
| 6 | 7 | import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; |
| 7 | 8 | import cn.iocoder.yudao.framework.pay.core.enums.PayChannelEnum; |
| ... | ... | @@ -61,4 +62,9 @@ public class AlipayWapPayClient extends AbstractPayClient<AlipayPayClientConfig> |
| 61 | 62 | return PayCommonResult.build(response.getCode(), response.getMsg(), response, codeMapping); |
| 62 | 63 | } |
| 63 | 64 | |
| 65 | + @Override | |
| 66 | + public PayOrderNotifyRespDTO parseOrderNotify(String data) throws Exception { | |
| 67 | + // TODO 芋艿:待完成 | |
| 68 | + return null; | |
| 69 | + } | |
| 64 | 70 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/wx/WXPubPayClient.java
| ... | ... | @@ -2,14 +2,17 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.wx; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.bean.BeanUtil; |
| 4 | 4 | import cn.hutool.core.date.DateUtil; |
| 5 | +import cn.hutool.core.lang.Assert; | |
| 5 | 6 | import cn.hutool.core.map.MapUtil; |
| 6 | 7 | import cn.hutool.core.util.StrUtil; |
| 7 | 8 | import cn.iocoder.yudao.framework.common.util.io.FileUtils; |
| 8 | 9 | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| 9 | 10 | import cn.iocoder.yudao.framework.pay.core.client.PayCommonResult; |
| 11 | +import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderNotifyRespDTO; | |
| 10 | 12 | import cn.iocoder.yudao.framework.pay.core.client.dto.PayOrderUnifiedReqDTO; |
| 11 | 13 | import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; |
| 12 | 14 | import cn.iocoder.yudao.framework.pay.core.enums.PayChannelEnum; |
| 15 | +import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; | |
| 13 | 16 | import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult; |
| 14 | 17 | import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; |
| 15 | 18 | import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request; |
| ... | ... | @@ -22,6 +25,8 @@ import com.github.binarywang.wxpay.service.WxPayService; |
| 22 | 25 | import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; |
| 23 | 26 | import lombok.extern.slf4j.Slf4j; |
| 24 | 27 | |
| 28 | +import java.util.Objects; | |
| 29 | + | |
| 25 | 30 | import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; |
| 26 | 31 | import static cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXCodeMapping.CODE_SUCCESS; |
| 27 | 32 | import static cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXCodeMapping.MESSAGE_SUCCESS; |
| ... | ... | @@ -126,4 +131,13 @@ public class WXPubPayClient extends AbstractPayClient<WXPayClientConfig> { |
| 126 | 131 | return openid; |
| 127 | 132 | } |
| 128 | 133 | |
| 134 | + @Override | |
| 135 | + public PayOrderNotifyRespDTO parseOrderNotify(String data) throws WxPayException { | |
| 136 | + WxPayOrderNotifyResult notifyResult = client.parseOrderNotifyResult(data); | |
| 137 | + Assert.isTrue(Objects.equals(notifyResult.getResultCode(), "SUCCESS"), "支付结果非 SUCCESS"); | |
| 138 | + // 转换结果 | |
| 139 | + return new PayOrderNotifyRespDTO(notifyResult.getOutTradeNo(), notifyResult.getTransactionId(), | |
| 140 | + DateUtil.parse(notifyResult.getTimeEnd(), "yyyyMMddHHmmss"), data); | |
| 141 | + } | |
| 142 | + | |
| 129 | 143 | } | ... | ... |