mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-16 20:15:06 +08:00
完成支付的下单和提交订单的逻辑
This commit is contained in:
@ -3,16 +3,15 @@ package cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.order;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.merchant.PayAppDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.merchant.PayChannelDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.merchant.PayMerchantDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.enums.order.PayOrderNotifyStatusEnum;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.enums.order.PayOrderRefundStatusEnum;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.enums.order.PayOrderStatusEnum;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import cn.iocoder.yudao.framework.pay.core.enums.PayChannelEnum;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 支付订单 DO
|
||||
@ -61,7 +60,7 @@ public class PayOrderDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 商户订单编号
|
||||
* 例如说,内部系统 A 的订单号。需要保证每个 PayMerchantDO 唯一 TODO 芋艿:需要在测试下
|
||||
* 例如说,内部系统 A 的订单号。需要保证每个 PayMerchantDO 唯一
|
||||
*/
|
||||
private String merchantOrderId;
|
||||
/**
|
||||
@ -72,6 +71,16 @@ public class PayOrderDO extends BaseDO {
|
||||
* 商品描述信息
|
||||
*/
|
||||
private String body;
|
||||
/**
|
||||
* 异步通知地址
|
||||
*/
|
||||
private String notifyUrl;
|
||||
/**
|
||||
* 通知商户支付结果的回调状态
|
||||
*
|
||||
* 枚举 {@link PayOrderNotifyStatusEnum}
|
||||
*/
|
||||
private Integer notifyStatus;
|
||||
// /**
|
||||
// * 商户拓展参数
|
||||
// */
|
||||
@ -99,11 +108,6 @@ public class PayOrderDO extends BaseDO {
|
||||
* 枚举 {@link PayOrderStatusEnum}
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 通知商户支付结果的回调状态
|
||||
* TODO 芋艿:0 未发送 1 已发送
|
||||
*/
|
||||
private Integer notifyStatus;
|
||||
/**
|
||||
* 用户 IP
|
||||
*/
|
||||
@ -122,23 +126,12 @@ public class PayOrderDO extends BaseDO {
|
||||
* 关联 {@link PayOrderDO#getId()}
|
||||
*/
|
||||
private Long successExtensionId;
|
||||
/**
|
||||
* 支付渠道的额外参数
|
||||
*
|
||||
* 参见 https://www.pingxx.com/api/支付渠道%20extra%20参数说明.html
|
||||
*/
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private Map<String, String> channelExtras;
|
||||
/**
|
||||
* 异步通知地址
|
||||
*/
|
||||
private String notifyUrl;
|
||||
|
||||
// ========== 退款相关字段 ==========
|
||||
/**
|
||||
* 退款状态
|
||||
*
|
||||
* TODO 芋艿:0 - 未退款;1 - 部分退款; 2 - 全额退款
|
||||
* 枚举 {@link PayOrderRefundStatusEnum}
|
||||
*/
|
||||
private Integer refundStatus;
|
||||
/**
|
||||
|
@ -3,9 +3,13 @@ package cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.order;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.merchant.PayChannelDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.enums.order.PayOrderStatusEnum;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 支付订单拓展 DO
|
||||
*
|
||||
@ -49,7 +53,7 @@ public class PayOrderExtensionDO extends BaseDO {
|
||||
/**
|
||||
* 渠道编码
|
||||
*/
|
||||
private Integer channelCode;
|
||||
private String channelCode;
|
||||
/**
|
||||
* 用户 IP
|
||||
*/
|
||||
@ -61,6 +65,13 @@ public class PayOrderExtensionDO extends BaseDO {
|
||||
* 注意,只包含上述枚举的 WAITING 和 SUCCESS
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 支付渠道的额外参数
|
||||
*
|
||||
* 参见 https://www.pingxx.com/api/支付渠道%20extra%20参数说明.html
|
||||
*/
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private Map<String, String> channelExtras;
|
||||
/**
|
||||
* 支付渠道异步通知的内容
|
||||
*
|
||||
|
@ -71,6 +71,15 @@ public class PayRefundDO extends BaseDO {
|
||||
// * 商户拓展参数
|
||||
// */
|
||||
// private String merchantExtra;
|
||||
/**
|
||||
* 异步通知地址
|
||||
*/
|
||||
private String notifyUrl;
|
||||
/**
|
||||
* 通知商户退款结果的回调状态
|
||||
* TODO 芋艿:0 未发送 1 已发送
|
||||
*/
|
||||
private Integer notifyStatus;
|
||||
|
||||
// ========== 退款相关字段 ==========
|
||||
/**
|
||||
@ -79,11 +88,6 @@ public class PayRefundDO extends BaseDO {
|
||||
* TODO 芋艿:状态枚举
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 通知商户退款结果的回调状态
|
||||
* TODO 芋艿:0 未发送 1 已发送
|
||||
*/
|
||||
private Integer notifyStatus;
|
||||
/**
|
||||
* 用户 IP
|
||||
*/
|
||||
@ -110,10 +114,6 @@ public class PayRefundDO extends BaseDO {
|
||||
* 参见 https://www.pingxx.com/api/Refunds%20退款概述.html
|
||||
*/
|
||||
private String channelExtra;
|
||||
/**
|
||||
* 异步通知地址
|
||||
*/
|
||||
private String notifyUrl;
|
||||
|
||||
// ========== 渠道相关字段 ==========
|
||||
/**
|
||||
|
@ -0,0 +1,29 @@
|
||||
package cn.iocoder.yudao.coreservice.modules.pay.enums.order;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 支付订单的通知状态枚举
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum PayOrderNotifyStatusEnum implements IntArrayValuable {
|
||||
|
||||
NO(0, "未通知"),
|
||||
SUCCESS(10, "通知成功"),
|
||||
FAILURE(20, "通知失败")
|
||||
;
|
||||
|
||||
private final Integer status;
|
||||
private final String name;
|
||||
|
||||
@Override
|
||||
public int[] array() {
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package cn.iocoder.yudao.coreservice.modules.pay.enums.order;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 支付订单的退款状态枚举
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum PayOrderRefundStatusEnum implements IntArrayValuable {
|
||||
|
||||
NO(0, "未退款"),
|
||||
SOME(10, "部分退款"),
|
||||
ALL(20, "全部退款")
|
||||
;
|
||||
|
||||
private final Integer status;
|
||||
private final String name;
|
||||
|
||||
@Override
|
||||
public int[] array() {
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
}
|
@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
|
||||
@ -50,6 +51,7 @@ public class PayChannelCoreServiceImpl implements PayChannelCoreService {
|
||||
private PayClientFactory payClientFactory;
|
||||
|
||||
@Override
|
||||
@PostConstruct
|
||||
public void initPayClients() {
|
||||
// 获取支付渠道,如果有更新
|
||||
List<PayChannelDO> payChannels = this.loadPayChannelIfUpdate(maxUpdateTime);
|
||||
|
@ -28,6 +28,6 @@ public interface PayOrderCoreService {
|
||||
* @param reqDTO 提交请求
|
||||
* @return 提交结果
|
||||
*/
|
||||
PayOrderSubmitRespDTO submitPayOrder(PayOrderSubmitReqDTO reqDTO);
|
||||
PayOrderSubmitRespDTO submitPayOrder(@Valid PayOrderSubmitReqDTO reqDTO);
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,14 @@
|
||||
package cn.iocoder.yudao.coreservice.modules.pay.service.order.dto;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 支付单提交 Request DTO
|
||||
@ -38,4 +41,10 @@ public class PayOrderSubmitReqDTO implements Serializable {
|
||||
@NotEmpty(message = "用户 IP 不能为空")
|
||||
private String userIp;
|
||||
|
||||
/**
|
||||
* 支付渠道的额外参数
|
||||
*/
|
||||
@TableField(typeHandler = JacksonTypeHandler.class)
|
||||
private Map<String, String> channelExtras;
|
||||
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.order.PayOrderDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.order.PayOrderExtensionDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.dal.mysql.order.PayOrderCoreMapper;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.dal.mysql.order.PayOrderExtensionCoreMapper;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.enums.order.PayOrderNotifyStatusEnum;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.enums.order.PayOrderStatusEnum;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.service.merchant.PayAppCoreService;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.service.merchant.PayChannelCoreService;
|
||||
@ -20,12 +21,14 @@ import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
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.PayOrderUnifiedReqDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.iocoder.yudao.coreservice.modules.pay.enums.PayErrorCodeCoreConstants.*;
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
@ -36,7 +39,7 @@ import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionU
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Service
|
||||
@Valid
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class PayOrderCoreServiceImpl implements PayOrderCoreService {
|
||||
|
||||
@ -68,10 +71,16 @@ public class PayOrderCoreServiceImpl implements PayOrderCoreService {
|
||||
}
|
||||
|
||||
// 创建支付交易单
|
||||
// TODO 芋艿:需要看看,还有啥要补全的字段
|
||||
order = PayOrderCoreConvert.INSTANCE.convert(reqDTO)
|
||||
.setStatus(PayOrderStatusEnum.WAITING.getStatus())
|
||||
.setNotifyUrl(app.getPayNotifyUrl());
|
||||
.setMerchantId(app.getMerchantId()).setAppId(app.getId());
|
||||
// 商户相关字段
|
||||
order.setNotifyUrl(app.getPayNotifyUrl())
|
||||
.setNotifyStatus(PayOrderNotifyStatusEnum.NO.getStatus());
|
||||
// 订单相关字段
|
||||
order.setStatus(PayOrderStatusEnum.WAITING.getStatus());
|
||||
// 退款相关字段
|
||||
order.setRefundStatus(PayOrderNotifyStatusEnum.NO.getStatus())
|
||||
.setRefundTimes(0).setRefundAmount(0L);
|
||||
payOrderCoreMapper.insert(order);
|
||||
// 最终返回
|
||||
return order.getId();
|
||||
@ -80,9 +89,9 @@ public class PayOrderCoreServiceImpl implements PayOrderCoreService {
|
||||
@Override
|
||||
public PayOrderSubmitRespDTO submitPayOrder(PayOrderSubmitReqDTO reqDTO) {
|
||||
// 校验 App
|
||||
PayAppDO app = payAppCoreService.validPayApp(reqDTO.getId());
|
||||
PayAppDO app = payAppCoreService.validPayApp(reqDTO.getAppId());
|
||||
// 校验支付渠道是否有效
|
||||
PayChannelDO channel = payChannelCoreService.validPayChannel(reqDTO.getId(), reqDTO.getChannelCode());
|
||||
PayChannelDO channel = payChannelCoreService.validPayChannel(reqDTO.getAppId(), reqDTO.getChannelCode());
|
||||
// 校验支付客户端是否正确初始化
|
||||
PayClient client = payClientFactory.getPayClient(channel.getId());
|
||||
if (client == null) {
|
||||
@ -92,7 +101,7 @@ public class PayOrderCoreServiceImpl implements PayOrderCoreService {
|
||||
|
||||
// 获得 PayOrderDO ,并校验其是否存在
|
||||
PayOrderDO order = payOrderCoreMapper.selectById(reqDTO.getId());
|
||||
if (order == null || order.getAppId().equals(reqDTO.getAppId())) { // 是否存在
|
||||
if (order == null || !Objects.equals(order.getAppId(), reqDTO.getAppId())) { // 是否存在
|
||||
throw exception(PAY_ORDER_NOT_FOUND);
|
||||
}
|
||||
if (!PayOrderStatusEnum.WAITING.getStatus().equals(order.getStatus())) { // 校验状态,必须是待支付
|
||||
@ -102,18 +111,25 @@ public class PayOrderCoreServiceImpl implements PayOrderCoreService {
|
||||
// 插入 PayOrderExtensionDO
|
||||
PayOrderExtensionDO orderExtension = PayOrderCoreConvert.INSTANCE.convert(reqDTO)
|
||||
.setOrderId(order.getId()).setNo(generateOrderExtensionNo())
|
||||
.setChannelId(channel.getId()).setChannelCode(channel.getCode())
|
||||
.setStatus(PayOrderStatusEnum.WAITING.getStatus());
|
||||
payOrderExtensionCoreMapper.insert(orderExtension);
|
||||
|
||||
// 调用三方接口
|
||||
// TODO 暂时传入 extra = null
|
||||
CommonResult<?> invokeResult = client.unifiedOrder(PayOrderCoreConvert.INSTANCE.convert2(reqDTO));
|
||||
invokeResult.checkError();
|
||||
PayOrderUnifiedReqDTO unifiedOrderReqDTO = PayOrderCoreConvert.INSTANCE.convert2(reqDTO);
|
||||
// 商户相关字段
|
||||
unifiedOrderReqDTO.setMerchantOrderId(order.getMerchantOrderId())
|
||||
.setSubject(order.getSubject()).setBody(order.getBody())
|
||||
.setNotifyUrl(app.getPayNotifyUrl());
|
||||
// 订单相关字段
|
||||
unifiedOrderReqDTO.setAmount(order.getAmount()).setExpireTime(order.getExpireTime());
|
||||
CommonResult<?> unifiedOrderResult = client.unifiedOrder(unifiedOrderReqDTO);
|
||||
unifiedOrderResult.checkError();
|
||||
|
||||
// TODO 轮询三方接口,是否已经支付的任务
|
||||
// 返回成功
|
||||
return new PayOrderSubmitRespDTO().setExtensionId(orderExtension.getId())
|
||||
.setInvokeResponse(JsonUtils.toJsonString(invokeResult));
|
||||
.setInvokeResponse(JsonUtils.toJsonString(unifiedOrderResult));
|
||||
}
|
||||
|
||||
private String generateOrderExtensionNo() {
|
||||
|
@ -0,0 +1,52 @@
|
||||
package cn.iocoder.yudao.coreservice.modules.pay.service.order;
|
||||
|
||||
import cn.iocoder.yudao.coreservice.BaseDbIntegrationTest;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.service.merchant.impl.PayAppCoreServiceImpl;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.service.merchant.impl.PayChannelCoreServiceImpl;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.service.order.dto.PayOrderCreateReqDTO;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.service.order.dto.PayOrderSubmitReqDTO;
|
||||
import cn.iocoder.yudao.coreservice.modules.pay.service.order.impl.PayOrderCoreServiceImpl;
|
||||
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
||||
import cn.iocoder.yudao.framework.pay.config.YudaoPayAutoConfiguration;
|
||||
import cn.iocoder.yudao.framework.pay.core.enums.PayChannelEnum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Duration;
|
||||
|
||||
@Import({PayOrderCoreServiceImpl.class, PayAppCoreServiceImpl.class,
|
||||
PayChannelCoreServiceImpl.class, YudaoPayAutoConfiguration.class})
|
||||
public class PayOrderCoreServiceTest extends BaseDbIntegrationTest {
|
||||
|
||||
@Resource
|
||||
private PayOrderCoreService payOrderCoreService;
|
||||
|
||||
@Test
|
||||
public void testCreatePayOrder() {
|
||||
// 构造请求
|
||||
PayOrderCreateReqDTO reqDTO = new PayOrderCreateReqDTO();
|
||||
reqDTO.setAppId(6L);
|
||||
reqDTO.setUserIp("127.0.0.1");
|
||||
reqDTO.setMerchantOrderId(String.valueOf(System.currentTimeMillis()));
|
||||
reqDTO.setSubject("标题");
|
||||
reqDTO.setBody("内容");
|
||||
reqDTO.setAmount(100);
|
||||
reqDTO.setExpireTime(DateUtils.addTime(Duration.ofDays(1)));
|
||||
// 发起请求
|
||||
payOrderCoreService.createPayOrder(reqDTO);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubmitPayOrder() {
|
||||
// 构造请求
|
||||
PayOrderSubmitReqDTO reqDTO = new PayOrderSubmitReqDTO();
|
||||
reqDTO.setId(10L);
|
||||
reqDTO.setAppId(6L);
|
||||
reqDTO.setChannelCode(PayChannelEnum.WX_PUB.getCode());
|
||||
reqDTO.setUserIp("127.0.0.1");
|
||||
// 发起请求
|
||||
payOrderCoreService.submitPayOrder(reqDTO);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
package cn.iocoder.yudao.coreservice.modules.pay.service;
|
@ -60,7 +60,7 @@ mybatis-plus:
|
||||
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
|
||||
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
|
||||
mapper-locations: classpath*:mapper/*.xml
|
||||
type-aliases-package: ${yudao.info.base-package}.modules.*.dal.dataobject, ${yudao.core-service.base-package}.modules.*.dal.dataobject
|
||||
type-aliases-package: ${yudao.core-service.base-package}.modules.*.dal.dataobject
|
||||
|
||||
--- #################### 定时任务相关配置 ####################
|
||||
|
||||
|
Reference in New Issue
Block a user