refactor(cashier): 重构收银台模块

- 将 ChannelCashierController 重命名为 CashierCodeController
- 更新了 CashierCodeService 和 CashierCodeConfigService 的实现
- 新增了 CashierCodeAuthCodeParam 和 CashierCodeAuthUrlParam 类
- 调整了 WechatPayConfig 和 WechatAuthService 的实现- 更新了相关文档和注释
This commit is contained in:
DaxPay
2024-11-25 20:28:02 +08:00
parent 8901fb7463
commit e63fb5e335
15 changed files with 237 additions and 170 deletions

View File

@@ -15,7 +15,7 @@ import lombok.experimental.Accessors;
public class CashierAuthUrlParam {
@Schema(description = "应用号")
private String appId;
private String cashierCode;
@Schema(description = "收银台类型")
private String cashierType;

View File

@@ -16,8 +16,8 @@ import java.math.BigDecimal;
@Schema(title = "通道收银支付参数")
public class CashierPayParam {
@Schema(description = "应用号")
private String appId;
@Schema(description = "收银码牌Code")
private String cashierCode;
@Schema(description = "支付金额")
private BigDecimal amount;

View File

@@ -37,4 +37,13 @@ public class CashierCodeResult {
*/
@Schema(description = "支付方式")
private String payMethod;
/** 应用号 */
@Schema(description = "应用号")
private String appId;
/** 是否分账 */
@Schema(description = "是否分账")
private boolean allocation;
}

View File

@@ -1,39 +0,0 @@
package org.dromara.daxpay.service.controller.cashier;
import cn.bootx.platform.core.annotation.IgnoreAuth;
import cn.bootx.platform.core.rest.Res;
import cn.bootx.platform.core.rest.result.Result;
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.result.cashier.CashierCodeResult;
import org.dromara.daxpay.service.service.config.CashierCodeConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 支付码牌配置
* @author xxm
* @since 2024/11/20
*/
@Validated
@IgnoreAuth
@Tag(name = "支付码牌配置")
@RestController
@RequestMapping("/cashier/code")
@RequiredArgsConstructor
public class CashierCodeController {
private final CashierCodeConfigService cashierCodeConfigService;
@Operation(summary = "根据code和类型查询码牌信息和配置")
@GetMapping("/findByCashierType")
public Result<CashierCodeResult> findByCashierType(@NotBlank(message = "码牌code不可为空") String cashierCode,
@NotBlank(message = "码牌类型不可为空") String cashierType){
return Res.ok(cashierCodeConfigService.findByCashierType(cashierCode, cashierType));
}
}

View File

@@ -0,0 +1,64 @@
package org.dromara.daxpay.service.controller.unipay;
import cn.bootx.platform.core.annotation.IgnoreAuth;
import cn.bootx.platform.core.rest.Res;
import cn.bootx.platform.core.rest.result.Result;
import cn.bootx.platform.core.util.ValidationUtil;
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.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.service.service.cashier.CashierCodeService;
import org.dromara.daxpay.service.service.config.CashierCodeConfigService;
import org.springframework.web.bind.annotation.*;
/**
* 通道码牌服务
* @author xxm
* @since 2024/9/28
*/
@IgnoreAuth
@Tag(name = "通道码牌服务")
@RestController
@RequestMapping("/unipay/cashier/code")
@RequiredArgsConstructor
public class CashierCodeController {
private final CashierCodeConfigService codeConfigService;
private final CashierCodeService cashierCodeService;
@Operation(summary = "根据code和类型查询码牌信息和配置")
@GetMapping("/findByCashierType")
public Result<CashierCodeResult> findByCashierType(@NotBlank(message = "码牌code不可为空") String cashierCode,
@NotBlank(message = "码牌类型不可为空") String cashierType){
return Res.ok(codeConfigService.findByCashierType(cashierCode, cashierType));
}
@Operation(summary = "获取码牌所需授权链接, 用于获取OpenId一类的信息")
@PostMapping("/generateAuthUrl")
public Result<String> generateAuthUrl(@RequestBody CashierAuthUrlParam param){
ValidationUtil.validateParam(param);
return Res.ok(cashierCodeService.generateAuthUrl(param));
}
@Operation(summary = "授权获取结果")
@PostMapping("/auth")
public Result<AuthResult> auth(@RequestBody CashierCodeAuthCodeParam param){
ValidationUtil.validateParam(param);
return Res.ok(cashierCodeService.auth(param));
}
@Operation(summary = "发起支付")
@PostMapping("/pay")
public Result<PayResult> cashierPay(@RequestBody CashierPayParam param){
ValidationUtil.validateParam(param);
return Res.ok(cashierCodeService.cashierPay(param));
}
}

View File

@@ -1,55 +0,0 @@
package org.dromara.daxpay.service.controller.unipay;
import cn.bootx.platform.core.annotation.IgnoreAuth;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.daxpay.service.service.assist.PaymentAssistService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 通道收银台服务
* @author xxm
* @since 2024/9/28
*/
@IgnoreAuth
@Tag(name = "通道收银台服务")
@RestController
@RequestMapping("/unipay/cashier/code")
@RequiredArgsConstructor
public class ChannelCashierController {
private final PaymentAssistService paymentAssistService;
// @Operation(summary = "获取收银台信息")
// @GetMapping("/getCashierInfo")
// public Result<ChannelCashierConfigResult> getCashierInfo(String cashierType,String appId){
// paymentAssistService.initMchApp(appId);
// return Res.ok(cashierConfigService.findByCashierType(cashierType));
// }
//
//
// @Operation(summary = "获取收银台所需授权链接, 用于获取OpenId一类的信息")
// @PostMapping("/generateAuthUrl")
// public Result<String> generateAuthUrl(@RequestBody CashierAuthUrlParam param){
// ValidationUtil.validateParam(param);
// paymentAssistService.initMchApp(param.getAppId());
// return Res.ok(channelCashierService.generateAuthUrl(param));
// }
//
// @Operation(summary = "授权获取结果")
// @PostMapping("/auth")
// public Result<AuthResult> auth(@RequestBody CashierAuthCodeParam param){
// ValidationUtil.validateParam(param);
// paymentAssistService.initMchApp(param.getAppId());
// return Res.ok(channelCashierService.auth(param));
// }
//
// @Operation(summary = "发起支付")
// @PostMapping("/pay")
// public Result<PayResult> cashierPay(@RequestBody CashierPayParam param){
// ValidationUtil.validateParam(param);
// paymentAssistService.initMchApp(param.getAppId());
// return Res.ok(channelCashierService.cashierPay(param));
// }
}

View File

@@ -3,8 +3,6 @@ package org.dromara.daxpay.service.dao.config;
import cn.bootx.platform.common.mybatisplus.impl.BaseManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.daxpay.service.common.context.MchAppLocal;
import org.dromara.daxpay.service.common.local.PaymentContextLocal;
import org.dromara.daxpay.service.entity.config.CashierCodeTypeConfig;
import org.springframework.stereotype.Repository;
@@ -54,11 +52,9 @@ public class CashierCodeTypeConfigManager extends BaseManager<CashierCodeTypeCon
* 根据类型查询
*/
public Optional<CashierCodeTypeConfig> findByCashierType(Long cashierCodeId, String cashierType) {
MchAppLocal mchAppInfo = PaymentContextLocal.get().getMchAppInfo();
return lambdaQuery()
.eq(CashierCodeTypeConfig::getCashierCodeId, cashierCodeId)
.eq(CashierCodeTypeConfig::getType, cashierType)
.eq(CashierCodeTypeConfig::getAppId, mchAppInfo.getAppId())
.oneOpt();
}

View File

@@ -0,0 +1,25 @@
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

@@ -0,0 +1,22 @@
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

@@ -1,9 +1,28 @@
package org.dromara.daxpay.service.service.cashier;
import cn.bootx.platform.common.spring.util.WebServletUtil;
import cn.bootx.platform.core.util.ValidationUtil;
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.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.CashierCodeConfigService;
import org.dromara.daxpay.service.service.trade.pay.PayService;
import org.dromara.daxpay.service.strategy.AbsChannelCashierStrategy;
import org.dromara.daxpay.service.util.PaymentStrategyFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
/**
* 通知支付控制台服务
* @author xxm
@@ -14,63 +33,71 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
public class CashierCodeService {
//
// private final PaymentAssistService paymentAssistService;
//
// private final PayService payService;
//
// /**
// * 生成授权链接跳转链接, 主要是微信类通道使用, 用于获取OpenId
// */
// public String generateAuthUrl(CashierAuthUrlParam param){
// // 查询配置
// var cashierConfig = lCashierConfigService.findByCashierType(param.getCashierType());
// // 获取策略
// AbsChannelCashierStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsChannelCashierStrategy.class);
// return cashierStrategy.generateAuthUrl(param);
// }
//
//
// /**
// * 授权结果
// */
// public AuthResult auth(CashierAuthCodeParam param) {
// // 查询配置
// var cashierConfig = lCashierConfigService.findByCashierType(param.getCashierType());
// // 获取策略
// AbsChannelCashierStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsChannelCashierStrategy.class);
// return cashierStrategy.doAuth(param);
// }
//
// /**
// * 支付处理
// */
// public PayResult cashierPay(CashierPayParam param){
// String clientIP = JakartaServletUtil.getClientIP(WebServletUtil.getRequest());
// // 查询配置
// var cashierConfig = lCashierConfigService.findByCashierType(param.getCashierType());
// // 构建支付参数
// PayParam payParam = new PayParam();
// payParam.setBizOrderNo(TradeNoGenerateUtil.pay());
// payParam.setTitle(StrUtil.format("手机收银金额: {}元", param.getAmount()));
// payParam.setDescription(param.getDescription());
// payParam.setChannel(cashierConfig.getChannel());
// payParam.setMethod(cashierConfig.getPayMethod());
// payParam.setAmount(param.getAmount());
// payParam.setAppId(param.getAppId());
// payParam.setClientIp(clientIP);
// payParam.setReqTime(LocalDateTime.now());
// String sign = paymentAssistService.genSign(payParam);
// payParam.setSign(sign);
//
// // 获取策略
// AbsChannelCashierStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsChannelCashierStrategy.class);
// // 进行参数预处理
// cashierStrategy.handlePayParam(param, payParam);
// // 参数校验
// ValidationUtil.validateParam(payParam);
// // 发起支付
// return payService.pay(payParam);
// }
private final CashierCodeConfigService codeConfigService;
private final PaymentAssistService paymentAssistService;
private final PayService payService;
/**
* 生成授权链接跳转链接, 主要是微信类通道使用, 用于获取OpenId
*/
public String generateAuthUrl(CashierAuthUrlParam 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);
}
/**
* 授权结果
*/
public AuthResult auth(CashierCodeAuthCodeParam param) {
// 查询配置
var cashierConfig = codeConfigService.findByCashierType(param.getCode(),param.getType());
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());
return cashierStrategy.doAuth(authCodeParam);
}
/**
* 支付处理
*/
public PayResult cashierPay(CashierPayParam param){
String clientIP = JakartaServletUtil.getClientIP(WebServletUtil.getRequest());
// 查询配置
var cashierConfig = codeConfigService.findByCashierType(param.getCashierCode(), param.getCashierType());
paymentAssistService.initMchApp(cashierConfig.getAppId());
// 构建支付参数
PayParam payParam = new PayParam();
payParam.setBizOrderNo(TradeNoGenerateUtil.pay());
payParam.setTitle(StrUtil.format("手机收银金额: {}元", param.getAmount()));
payParam.setDescription(param.getDescription());
payParam.setChannel(cashierConfig.getChannel());
payParam.setMethod(cashierConfig.getPayMethod());
payParam.setAppId(cashierConfig.getAppId());
payParam.setAmount(param.getAmount());
payParam.setClientIp(clientIP);
payParam.setReqTime(LocalDateTime.now());
String sign = paymentAssistService.genSign(payParam);
payParam.setSign(sign);
// 获取策略
AbsChannelCashierStrategy cashierStrategy = PaymentStrategyFactory.create(cashierConfig.getChannel(), AbsChannelCashierStrategy.class);
// 进行参数预处理
cashierStrategy.handlePayParam(param, payParam);
// 参数校验
ValidationUtil.validateParam(payParam);
// 发起支付
return payService.pay(payParam);
}
}

View File

@@ -93,7 +93,7 @@ public class CashierCodeConfigService {
.orElseThrow(() -> new DataNotExistException("收银码牌配置不存在"));
PlatformConfig platformConfig = platformConfigService.getConfig();
String serverUrl = platformConfig.getGatewayMobileUrl();
return StrUtil.format("{}/cashier/code/{}", serverUrl, codeConfig.getCode());
return StrUtil.format("{}/cashier/{}", serverUrl, codeConfig.getCode());
}
/**
@@ -110,10 +110,11 @@ public class CashierCodeConfigService {
CashierCodeTypeConfig codeTypeConfig = cashierCodeTypeConfigManager.findByCashierType(codeConfig.getId(), cashierType)
.orElseThrow(() -> new DataNotExistException("收银码牌配置不存在"));
return new CashierCodeResult()
.setAppId(codeConfig.getAppId())
.setAllocation(codeTypeConfig.isAllocation())
.setChannel(codeTypeConfig.getChannel())
.setPayMethod(codeTypeConfig.getPayMethod())
.setName(codeConfig.getName())
.setTemplateCode(codeConfig.getTemplateCode());
}
}