refactor(payment): 重构收银台相关代码

- 修改类名和包名,优化代码结构- 新增 AbsCheckoutStrategy 类,用于收银台支付
- 更新 CashierCodeService 和 CheckoutService,使用新的策略类
-调整 CashierCodeAuthUrlParam 和 CheckoutAggregatePayParam 的命名
This commit is contained in:
DaxPay
2024-12-02 19:11:42 +08:00
parent 65f186a31f
commit 4ea1e39e2f
27 changed files with 400 additions and 283 deletions

View File

@@ -8,12 +8,12 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.dromara.daxpay.core.param.cashier.CashierAuthUrlParam;
import org.dromara.daxpay.core.param.cashier.CashierPayParam;
import org.dromara.daxpay.core.param.cashier.CashierCodeAuthUrlParam;
import org.dromara.daxpay.core.param.cashier.CashierCodePayParam;
import org.dromara.daxpay.core.result.assist.AuthResult;
import org.dromara.daxpay.core.result.cashier.CashierCodeResult;
import org.dromara.daxpay.core.result.trade.pay.PayResult;
import org.dromara.daxpay.service.param.cashier.CashierCodeAuthCodeParam;
import org.dromara.daxpay.core.param.cashier.CashierCodeAuthCodeParam;
import org.dromara.daxpay.service.service.cashier.CashierCodeService;
import org.dromara.daxpay.service.service.config.cashier.CashierCodeConfigService;
import org.springframework.web.bind.annotation.*;
@@ -43,7 +43,7 @@ public class CashierCodeController {
@Operation(summary = "获取码牌所需授权链接, 用于获取OpenId一类的信息")
@PostMapping("/generateAuthUrl")
public Result<String> generateAuthUrl(@RequestBody CashierAuthUrlParam param){
public Result<String> generateAuthUrl(@RequestBody CashierCodeAuthUrlParam param){
ValidationUtil.validateParam(param);
return Res.ok(cashierCodeService.generateAuthUrl(param));
}
@@ -57,7 +57,7 @@ public class CashierCodeController {
@Operation(summary = "发起支付")
@PostMapping("/pay")
public Result<PayResult> cashierPay(@RequestBody CashierPayParam param){
public Result<PayResult> cashierPay(@RequestBody CashierCodePayParam param){
ValidationUtil.validateParam(param);
return Res.ok(cashierCodeService.cashierPay(param));
}

View File

@@ -5,7 +5,7 @@ import cn.bootx.platform.core.rest.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.daxpay.core.param.checkout.CheckoutParam;
import org.dromara.daxpay.core.param.checkout.CheckoutCreatParam;
import org.dromara.daxpay.core.param.checkout.CheckoutPayParam;
import org.dromara.daxpay.core.result.DaxResult;
import org.dromara.daxpay.core.result.checkout.CheckoutAggregateOrderAndConfigResult;
@@ -13,8 +13,8 @@ import org.dromara.daxpay.core.result.checkout.CheckoutOrderAndConfigResult;
import org.dromara.daxpay.core.result.checkout.CheckoutUrlResult;
import org.dromara.daxpay.core.util.DaxRes;
import org.dromara.daxpay.service.common.anno.PaymentVerify;
import org.dromara.daxpay.service.service.cashier.CheckoutQueryService;
import org.dromara.daxpay.service.service.cashier.CheckoutService;
import org.dromara.daxpay.service.service.checkout.CheckoutQueryService;
import org.dromara.daxpay.service.service.checkout.CheckoutService;
import org.springframework.web.bind.annotation.*;
/**
@@ -33,7 +33,7 @@ public class CheckoutController {
@PaymentVerify
@Operation(summary = "创建一个收银台链接")
@PostMapping("/creat")
public DaxResult<CheckoutUrlResult> creat(@RequestBody CheckoutParam checkoutParam){
public DaxResult<CheckoutUrlResult> creat(@RequestBody CheckoutCreatParam checkoutParam){
return DaxRes.ok(checkoutService.creat(checkoutParam));
}

View File

@@ -1,25 +0,0 @@
package org.dromara.daxpay.service.param.cashier;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 收银码牌认证码参数
* @author xxm
* @since 2024/11/25
*/
@Data
@Accessors(chain = true)
@Schema(title = "收银码牌认证码参数")
public class CashierCodeAuthCodeParam {
@Schema(description = "码牌Code")
private String code;
@Schema(description = "收银台类型")
private String type;
@Schema(description = "认证Code")
private String authCode;
}

View File

@@ -1,22 +0,0 @@
package org.dromara.daxpay.service.param.cashier;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 获取收银码牌认证url参数
* @author xxm
* @since 2024/11/25
*/
@Data
@Accessors(chain = true)
@Schema(title = "获取收银码牌认证url参数")
public class CashierCodeAuthUrlParam {
@Schema(description = "码牌Code")
private String code;
@Schema(description = "收银台类型")
private String type;
}

View File

@@ -6,25 +6,25 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.JakartaServletUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.daxpay.core.param.cashier.CashierAuthCodeParam;
import org.dromara.daxpay.core.param.cashier.CashierAuthUrlParam;
import org.dromara.daxpay.core.param.cashier.CashierPayParam;
import org.dromara.daxpay.core.param.assist.AuthCodeParam;
import org.dromara.daxpay.core.param.cashier.CashierCodeAuthCodeParam;
import org.dromara.daxpay.core.param.cashier.CashierCodeAuthUrlParam;
import org.dromara.daxpay.core.param.cashier.CashierCodePayParam;
import org.dromara.daxpay.core.param.trade.pay.PayParam;
import org.dromara.daxpay.core.result.assist.AuthResult;
import org.dromara.daxpay.core.result.trade.pay.PayResult;
import org.dromara.daxpay.core.util.TradeNoGenerateUtil;
import org.dromara.daxpay.service.param.cashier.CashierCodeAuthCodeParam;
import org.dromara.daxpay.service.service.assist.PaymentAssistService;
import org.dromara.daxpay.service.service.config.cashier.CashierCodeConfigService;
import org.dromara.daxpay.service.service.trade.pay.PayService;
import org.dromara.daxpay.service.strategy.AbsChannelCashierStrategy;
import org.dromara.daxpay.service.strategy.AbsCashierCodeStrategy;
import org.dromara.daxpay.service.util.PaymentStrategyFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
/**
* 通知支付控制台服务
* 收银码牌服务
* @author xxm
* @since 2024/9/28
*/
@@ -42,13 +42,13 @@ public class CashierCodeService {
/**
* 生成授权链接跳转链接, 主要是微信类通道使用, 用于获取OpenId
*/
public String generateAuthUrl(CashierAuthUrlParam param){
public String generateAuthUrl(CashierCodeAuthUrlParam param){
// 查询配置
var cashierConfig = codeConfigService.findByCashierType(param.getCashierCode(),param.getCashierType());
paymentAssistService.initMchApp(cashierConfig.getAppId());
// 获取策略
AbsChannelCashierStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsChannelCashierStrategy.class);
return cashierStrategy.generateAuthUrl(param);
AbsCashierCodeStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsCashierCodeStrategy.class);
return cashierStrategy.generateAuthUrl(param.getCashierCode());
}
/**
@@ -56,21 +56,19 @@ public class CashierCodeService {
*/
public AuthResult auth(CashierCodeAuthCodeParam param) {
// 查询配置
var cashierConfig = codeConfigService.findByCashierType(param.getCode(),param.getType());
var cashierConfig = codeConfigService.findByCashierType(param.getCashierCode(),param.getCashierType());
paymentAssistService.initMchApp(cashierConfig.getAppId());
// 获取策略
AbsChannelCashierStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsChannelCashierStrategy.class);
CashierAuthCodeParam authCodeParam = new CashierAuthCodeParam()
.setCashierType(param.getType())
.setAuthCode(param.getAuthCode())
.setAppId(cashierConfig.getAppId());
AbsCashierCodeStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsCashierCodeStrategy.class);
AuthCodeParam authCodeParam = new AuthCodeParam()
.setAuthCode(param.getAuthCode());
return cashierStrategy.doAuth(authCodeParam);
}
/**
* 支付处理
*/
public PayResult cashierPay(CashierPayParam param){
public PayResult cashierPay(CashierCodePayParam param){
String clientIP = JakartaServletUtil.getClientIP(WebServletUtil.getRequest());
// 查询配置
var cashierConfig = codeConfigService.findByCashierType(param.getCashierCode(), param.getCashierType());
@@ -90,7 +88,7 @@ public class CashierCodeService {
payParam.setSign(sign);
// 获取策略
AbsChannelCashierStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsChannelCashierStrategy.class);
AbsCashierCodeStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsCashierCodeStrategy.class);
// 进行参数预处理
cashierStrategy.handlePayParam(param, payParam);
// 参数校验

View File

@@ -1,4 +1,4 @@
package org.dromara.daxpay.service.service.cashier;
package org.dromara.daxpay.service.service.checkout;
import cn.bootx.platform.core.exception.ValidationFailedException;
import cn.bootx.platform.core.util.BigDecimalUtil;
@@ -12,7 +12,7 @@ import org.dromara.daxpay.core.enums.PayRefundStatusEnum;
import org.dromara.daxpay.core.enums.PayStatusEnum;
import org.dromara.daxpay.core.exception.AmountExceedLimitException;
import org.dromara.daxpay.core.exception.TradeStatusErrorException;
import org.dromara.daxpay.core.param.checkout.CheckoutParam;
import org.dromara.daxpay.core.param.checkout.CheckoutCreatParam;
import org.dromara.daxpay.core.util.PayUtil;
import org.dromara.daxpay.core.util.TradeNoGenerateUtil;
import org.dromara.daxpay.service.code.DaxPayCode;
@@ -52,7 +52,7 @@ public class CheckoutAssistService {
/**
* 校验支付状态支付成功则返回支付失败则抛出对应的异常
*/
public PayOrder getOrderAndCheck(CheckoutParam param) {
public PayOrder getOrderAndCheck(CheckoutCreatParam param) {
// 根据订单查询支付记录
PayOrder payOrder = payOrderQueryService.findByBizOrderNo(param.getBizOrderNo(), param.getAppId()).orElse(null);
return getOrderAndCheck(payOrder);
@@ -87,7 +87,7 @@ public class CheckoutAssistService {
/**
* 检验订单是否超过限额
*/
public void validationLimitAmount(CheckoutParam checkoutParam) {
public void validationLimitAmount(CheckoutCreatParam checkoutParam) {
MchAppLocal mchAppInfo = PaymentContextLocal.get()
.getMchAppInfo();
// 总额校验
@@ -100,7 +100,7 @@ public class CheckoutAssistService {
/**
* 校验订单超时时间是否正常
*/
public void validationExpiredTime(CheckoutParam payParam) {
public void validationExpiredTime(CheckoutCreatParam payParam) {
LocalDateTime expiredTime = this.getExpiredTime(payParam);
if (Objects.nonNull(expiredTime) && DateTimeUtil.lt(expiredTime,LocalDateTime.now())) {
throw new ValidationFailedException("支付超时时间设置有误, 请检查!");
@@ -113,7 +113,7 @@ public class CheckoutAssistService {
* 创建支付订单并保存, 返回支付订单
*/
@Transactional(rollbackFor = Exception.class)
public PayOrder createPayOrder(CheckoutParam checkoutParam) {
public PayOrder createPayOrder(CheckoutCreatParam checkoutParam) {
// 订单超时时间
LocalDateTime expiredTime = this.getExpiredTime(checkoutParam);
// 构建支付订单对象
@@ -138,7 +138,7 @@ public class CheckoutAssistService {
/**
* 获取支付订单超时时间
*/
private LocalDateTime getExpiredTime(CheckoutParam payParam) {
private LocalDateTime getExpiredTime(CheckoutCreatParam payParam) {
MchAppLocal mchAppLocal = PaymentContextLocal.get().getMchAppInfo();
// 支付参数传入
if (Objects.nonNull(payParam.getExpiredTime())) {

View File

@@ -1,4 +1,4 @@
package org.dromara.daxpay.service.service.cashier;
package org.dromara.daxpay.service.service.checkout;
import cn.bootx.platform.common.mybatisplus.base.MpIdEntity;
import cn.bootx.platform.core.exception.DataNotExistException;
@@ -24,7 +24,7 @@ import java.util.List;
import java.util.stream.Collectors;
/**
*
* 收银码牌查询服务
* @author xxm
* @since 2024/11/27
*/

View File

@@ -1,4 +1,4 @@
package org.dromara.daxpay.service.service.cashier;
package org.dromara.daxpay.service.service.checkout;
import cn.bootx.platform.common.spring.util.WebServletUtil;
import cn.hutool.core.util.StrUtil;
@@ -10,22 +10,27 @@ import lombok.extern.slf4j.Slf4j;
import org.dromara.daxpay.core.enums.CheckoutCallTypeEnum;
import org.dromara.daxpay.core.enums.CheckoutTypeEnum;
import org.dromara.daxpay.core.enums.PayStatusEnum;
import org.dromara.daxpay.core.exception.ConfigNotExistException;
import org.dromara.daxpay.core.exception.TradeProcessingException;
import org.dromara.daxpay.core.exception.UnsupportedAbilityException;
import org.dromara.daxpay.core.param.checkout.CheckoutAggregatePayParam;
import org.dromara.daxpay.core.param.checkout.CheckoutParam;
import org.dromara.daxpay.core.param.checkout.CheckoutCreatParam;
import org.dromara.daxpay.core.param.checkout.CheckoutPayParam;
import org.dromara.daxpay.core.param.trade.pay.PayParam;
import org.dromara.daxpay.core.result.checkout.CheckoutPayResult;
import org.dromara.daxpay.core.result.checkout.CheckoutUrlResult;
import org.dromara.daxpay.core.result.trade.pay.PayResult;
import org.dromara.daxpay.service.dao.config.checkout.CheckoutAggregateConfigManager;
import org.dromara.daxpay.service.dao.config.checkout.CheckoutItemConfigManager;
import org.dromara.daxpay.service.entity.config.PlatformConfig;
import org.dromara.daxpay.service.entity.config.checkout.CheckoutAggregateConfig;
import org.dromara.daxpay.service.entity.config.checkout.CheckoutItemConfig;
import org.dromara.daxpay.service.entity.order.pay.PayOrder;
import org.dromara.daxpay.service.service.assist.PaymentAssistService;
import org.dromara.daxpay.service.service.config.PlatformConfigService;
import org.dromara.daxpay.service.service.trade.pay.PayService;
import org.dromara.daxpay.service.strategy.AbsCheckoutStrategy;
import org.dromara.daxpay.service.util.PaymentStrategyFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@@ -46,11 +51,12 @@ public class CheckoutService {
private final PaymentAssistService paymentAssistService;
private final CheckoutItemConfigManager checkoutItemConfigManager;
private final PayService payService;
private final CheckoutAggregateConfigManager checkoutAggregateConfigManager;
/**
* 生成收银台链接
*/
public CheckoutUrlResult creat(CheckoutParam checkoutParam){
public CheckoutUrlResult creat(CheckoutCreatParam checkoutParam){
// 校验支付限额
checkoutAssistService.validationLimitAmount(checkoutParam);
// 校验超时时间, 不可早于当前
@@ -149,15 +155,14 @@ public class CheckoutService {
payParam.setReqTime(LocalDateTime.now());
// 获取策略
// AbsChannelCashierStrategy cashierStrategy = PaymentStrategyFactory.create(itemConfig.getChannel(), AbsChannelCashierStrategy.class);
var cashierStrategy = PaymentStrategyFactory.create(itemConfig.getChannel(), AbsCheckoutStrategy.class);
// 进行参数预处理
// cashierStrategy.handlePayParam(param, payParam);
cashierStrategy.handlePayParam(param, payParam);
// 发起支付
PayResult payResult = payService.pay(payParam, payOrder);
return new CheckoutPayResult()
.setUrl(payResult.getPayBody())
.setPayStatus(payResult.getStatus());
}
/**
@@ -167,7 +172,57 @@ public class CheckoutService {
// 订单信息
PayOrder payOrder = checkoutAssistService.getOrderAndCheck(param.getOrderNo());
return new PayResult();
// 获取聚合类型
CheckoutAggregateConfig aggregateConfig = checkoutAggregateConfigManager.findByAppIdAndType(payOrder.getAppId(), param.getAggregateType())
.orElseThrow(() -> new ConfigNotExistException("聚合支付配置项不存在"));
// 构建支付参数
String clientIP = JakartaServletUtil.getClientIP(WebServletUtil.getRequest());
PayParam payParam = new PayParam();
payParam.setChannel(aggregateConfig.getChannel());
payParam.setMethod(aggregateConfig.getPayMethod());
payParam.setAppId(aggregateConfig.getAppId());
payParam.setClientIp(clientIP);
payParam.setReqTime(LocalDateTime.now());
// 获取策略
var cashierStrategy = PaymentStrategyFactory.create(aggregateConfig.getChannel(), AbsCheckoutStrategy.class);
// 进行参数预处理
CheckoutPayParam cashierPayParam = new CheckoutPayParam()
.setOpenId(param.getOpenId());
cashierStrategy.handlePayParam(cashierPayParam, payParam);
// 发起支付
return payService.pay(payParam, payOrder);
}
// /**
// * 生成授权链接跳转链接, 主要是微信类通道使用, 用于获取OpenId
// */
// public String generateAuthUrl(CheckoutAggregatePayParam param){
// // 订单信息
// PayOrder payOrder = checkoutAssistService.getOrderAndCheck(param.getOrderNo());
// // 获取聚合类型
// CheckoutAggregateConfig aggregateConfig = checkoutAggregateConfigManager.findByAppIdAndType(payOrder.getAppId(), param.getAggregateType())
// .orElseThrow(() -> new ConfigNotExistException("聚合支付配置项不存在"));
// paymentAssistService.initMchApp(aggregateConfig.getAppId());
// // 获取策略
// var cashierStrategy = PaymentStrategyFactory.create(aggregateConfig.getChannel(), AbsCheckoutStrategy.class);
// return cashierStrategy.generateAuthUrl(param);
// }
//
// /**
// * 授权结果
// */
// public AuthResult auth(org.dromara.daxpay.service.param.cashier.AuthCodeParam param) {
// // 查询配置
// var cashierConfig = codeConfigService.findByCashierType(param.getCode(),param.getType());
// paymentAssistService.initMchApp(cashierConfig.getAppId());
// // 获取策略
// AbsCashierCodeStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsCashierCodeStrategy.class);
// AuthCodeParam authCodeParam = new AuthCodeParam()
// .setCashierType(param.getType())
// .setAuthCode(param.getAuthCode())
// .setAppId(cashierConfig.getAppId());
// return cashierStrategy.doAuth(authCodeParam);
// }
}

View File

@@ -0,0 +1,35 @@
package org.dromara.daxpay.service.strategy;
import org.dromara.daxpay.core.param.assist.AuthCodeParam;
import org.dromara.daxpay.core.param.cashier.CashierCodePayParam;
import org.dromara.daxpay.core.param.trade.pay.PayParam;
import org.dromara.daxpay.core.result.assist.AuthResult;
/**
* 抽象收银码牌策略
* @author xxm
* @since 2024/9/28
*/
public abstract class AbsCashierCodeStrategy implements PaymentStrategy{
/**
* 生成授权链接, 主要是微信类通道使用, 用于获取OpenId
*/
public String generateAuthUrl(String cashierCode) {
return "";
}
/**Z
* 获取认证结果
*/
public AuthResult doAuth(AuthCodeParam param) {
return new AuthResult();
}
/**
* 支付参数处理
*/
public void handlePayParam(CashierCodePayParam cashierPayParam, PayParam payParam) {
}
}

View File

@@ -1,36 +0,0 @@
package org.dromara.daxpay.service.strategy;
import org.dromara.daxpay.core.param.cashier.CashierAuthCodeParam;
import org.dromara.daxpay.core.param.cashier.CashierAuthUrlParam;
import org.dromara.daxpay.core.param.cashier.CashierPayParam;
import org.dromara.daxpay.core.param.trade.pay.PayParam;
import org.dromara.daxpay.core.result.assist.AuthResult;
/**
* 抽象通道收银台策略
* @author xxm
* @since 2024/9/28
*/
public abstract class AbsChannelCashierStrategy implements PaymentStrategy{
/**
* 生成授权链接, 主要是微信类通道使用, 用于获取OpenId
*/
public String generateAuthUrl(CashierAuthUrlParam param) {
return "";
}
/**
* 获取认证结果
*/
public AuthResult doAuth(CashierAuthCodeParam param) {
return new AuthResult();
}
/**
* 支付参数处理
*/
public void handlePayParam(CashierPayParam cashierPayParam, PayParam payParam) {
}
}

View File

@@ -0,0 +1,35 @@
package org.dromara.daxpay.service.strategy;
import org.dromara.daxpay.core.param.assist.AuthCodeParam;
import org.dromara.daxpay.core.param.cashier.CashierCodeAuthUrlParam;
import org.dromara.daxpay.core.param.checkout.CheckoutPayParam;
import org.dromara.daxpay.core.param.trade.pay.PayParam;
import org.dromara.daxpay.core.result.assist.AuthResult;
/**
* 抽象收银台策略
* @author xxm
* @since 2024/12/2
*/
public abstract class AbsCheckoutStrategy implements PaymentStrategy{
/**
* 生成授权链接, 主要是微信类通道使用, 用于获取OpenId
*/
public String generateAuthUrl(CashierCodeAuthUrlParam param) {
return "";
}
/**
* 获取认证结果
*/
public AuthResult doAuth(AuthCodeParam param) {
return new AuthResult();
}
/**
* 支付参数处理
*/
public void handlePayParam(CheckoutPayParam cashierPayParam, PayParam payParam) {
}
}