fix 一些小问题修改, 签名, 开发接口相关字段等

This commit is contained in:
DaxPay
2024-05-10 21:31:08 +08:00
parent f391e2e64b
commit a1ab481f96
21 changed files with 80 additions and 71 deletions

View File

@@ -19,9 +19,6 @@ import org.slf4j.MDC;
@EqualsAndHashCode(callSuper = true)
public class DaxResult<T> extends ResResult<T> {
/** 数据签名 */
private String sign;
/** 追踪ID */
private String traceId = MDC.get(CommonCode.TRACE_ID);

View File

@@ -18,11 +18,8 @@ import java.time.LocalDateTime;
@Schema(title = "支付通用返回参数")
public class PaymentCommonResult {
@Schema(description = "响应数据签名值")
private String sign;
@Schema(description = "错误码")
private String code = "0";
@Schema(description = "状态码")
private Integer code = 0;
@Schema(description = "错误信息")
private String msg;
@@ -31,4 +28,7 @@ public class PaymentCommonResult {
@JsonSerialize(using = LocalDateTimeToTimestampSerializer.class)
private LocalDateTime resTime = LocalDateTime.now();
@Schema(description = "响应数据签名值")
private String sign;
}

View File

@@ -6,6 +6,8 @@ import cn.daxpay.single.service.core.order.refund.entity.RefundOrderExtra;
import lombok.Data;
import lombok.experimental.Accessors;
import java.time.LocalDateTime;
/**
* 异步退款信息
* @author xxm
@@ -34,6 +36,9 @@ public class RefundLocal {
/** 退款订单 */
private RefundOrder refundOrder;
/** 退款完成时间 */
private LocalDateTime finishTime;
/** 退款订单扩展 */
private RefundOrderExtra runOrderExtra;
}

View File

@@ -14,6 +14,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Objects;
/**
@@ -54,6 +55,7 @@ public class AliPayRefundService {
// 接口返回fund_change=Y为退款成功fund_change=N或无此字段值返回时需通过退款查询接口进一步确认退款状态
if (response.getFundChange().equals("Y")){
refundInfo.setStatus(RefundStatusEnum.SUCCESS);
refundInfo.setFinishTime(LocalDateTime.now());
}
}
catch (AlipayApiException e) {

View File

@@ -13,6 +13,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 云闪付退款操作
@@ -44,5 +45,7 @@ public class UnionPayRefundService {
// 云闪付退款是否成功需要查询状态, 所以设置为退款中状态
RefundLocal refundInfo = PaymentContextLocal.get().getRefundInfo();
refundInfo.setStatus(RefundStatusEnum.PROGRESS).setOutRefundNo(outRefundNo);
// 设置退款时间
refundInfo.setFinishTime(LocalDateTime.now());
}
}

View File

@@ -19,7 +19,6 @@ import com.baomidou.lock.LockTemplate;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Objects;
@@ -43,7 +42,6 @@ public class PayCloseService {
/**
* 关闭支付
*/
@Transactional(rollbackFor = Exception.class)
public PayCloseResult close(PayCloseParam param){
PayOrder payOrder = payOrderQueryService.findByBizOrOrderNo(param.getOrderNo(), param.getBizTradeNo())
.orElseThrow(() -> new PayFailureException("未查询到支付订单"));
@@ -63,11 +61,11 @@ public class PayCloseService {
*/
private PayCloseResult close(PayOrder payOrder) {
PayCloseResult result = new PayCloseResult();
// 状态检查, 只有支付中可以进行取消支付
if (!Objects.equals(payOrder.getStatus(), PayStatusEnum.PROGRESS.getCode())) {
throw new PayFailureException("订单不是支付中, 无法进行关闭订单");
}
try {
// 状态检查, 只有支付中可以进行取消支付
if (!Objects.equals(payOrder.getStatus(), PayStatusEnum.PROGRESS.getCode())) {
throw new PayFailureException("订单不是支付中, 无法进行关闭订单");
}
AbsPayCloseStrategy strategy = PayCloseStrategyFactory.create(payOrder.getChannel());
// 设置支付订单
strategy.setOrder(payOrder);
@@ -84,8 +82,8 @@ public class PayCloseService {
log.error("关闭订单失败:", e);
// 记录关闭失败的记录
this.saveRecord(payOrder, false, e.getMessage());
result.setCode("1").setMsg(e.getMessage());
return result;
throw new PayFailureException("关闭订单失败");
}
}

View File

@@ -1,6 +1,5 @@
package cn.daxpay.single.service.core.payment.common.aop;
import cn.bootx.platform.common.core.rest.Res;
import cn.bootx.platform.common.core.rest.ResResult;
import cn.bootx.platform.common.core.util.ValidationUtil;
import cn.daxpay.single.exception.pay.PayFailureException;
@@ -8,6 +7,7 @@ import cn.daxpay.single.param.PaymentCommonParam;
import cn.daxpay.single.result.PaymentCommonResult;
import cn.daxpay.single.service.annotation.PaymentApi;
import cn.daxpay.single.service.core.payment.common.service.PaymentSignService;
import cn.daxpay.single.util.DaxRes;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
@@ -49,9 +49,11 @@ public class PaymentVerifySignAop {
} catch (PayFailureException ex) {
// 如果抛出支付异常, 包裹异常信息, 进行返回
PaymentCommonResult commonResult = new PaymentCommonResult();
// todo 后期错误码统一管理后, 进行更改
commonResult.setCode(1);
commonResult.setMsg(ex.getMessage());
paymentSignService.sign(commonResult);
return Res.ok(commonResult);
return DaxRes.ok(commonResult);
}
// 对返回值进行签名
if (proceed instanceof ResResult){

View File

@@ -32,7 +32,7 @@ public class PaymentAssistService {
/**
* 初始化平台配置上下文
*/
private void initPlatform(){
public void initPlatform(){
PlatformConfig config = platformConfigService.getConfig();
PlatformLocal platform = PaymentContextLocal.get().getPlatformInfo();
platform.setSignType(config.getSignType());

View File

@@ -60,8 +60,7 @@ public class PaymentSignService {
* 对对象进行签名
*/
public void sign(PaymentCommonResult result) {
PlatformLocal platformInfo = PaymentContextLocal.get()
.getPlatformInfo();
PlatformLocal platformInfo = PaymentContextLocal.get().getPlatformInfo();
String signType = platformInfo.getSignType();
if (Objects.equals(PaySignTypeEnum.HMAC_SHA256.getCode(), signType)){
result.setSign(PaySignUtil.hmacSha256Sign(result, platformInfo.getSignSecret()));

View File

@@ -2,10 +2,12 @@ package cn.daxpay.single.service.core.payment.notice.result;
import cn.daxpay.single.code.PayChannelEnum;
import cn.daxpay.single.code.PayStatusEnum;
import cn.daxpay.single.result.PaymentCommonResult;
import cn.daxpay.single.serializer.LocalDateTimeToTimestampSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.time.LocalDateTime;
@@ -15,10 +17,11 @@ import java.time.LocalDateTime;
* @author xxm
* @since 2024/1/7
*/
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@Schema(title = "支付异步通知类")
public class PayNoticeResult {
public class PayNoticeResult extends PaymentCommonResult {
/** 订单号 */
@Schema(description = "订单号")
@@ -73,8 +76,4 @@ public class PayNoticeResult {
@Schema(description = "商户扩展参数,回调时会原样返回")
private String attach;
/** 签名 */
@Schema(description = "签名")
private String sign;
}

View File

@@ -1,25 +1,20 @@
package cn.daxpay.single.service.core.payment.notice.service;
import cn.bootx.platform.common.jackson.util.JacksonUtil;
import cn.daxpay.single.code.PaySignTypeEnum;
import cn.daxpay.single.service.code.ClientNoticeTypeEnum;
import cn.daxpay.single.service.core.order.pay.entity.PayOrder;
import cn.daxpay.single.service.core.order.pay.entity.PayOrderExtra;
import cn.daxpay.single.service.core.order.refund.entity.RefundOrder;
import cn.daxpay.single.service.core.order.refund.entity.RefundOrderExtra;
import cn.daxpay.single.service.core.payment.common.service.PaymentAssistService;
import cn.daxpay.single.service.core.payment.common.service.PaymentSignService;
import cn.daxpay.single.service.core.payment.notice.result.PayNoticeResult;
import cn.daxpay.single.service.core.payment.notice.result.RefundNoticeResult;
import cn.daxpay.single.service.core.system.config.entity.PlatformConfig;
import cn.daxpay.single.service.core.system.config.service.PlatformConfigService;
import cn.daxpay.single.service.core.task.notice.entity.ClientNoticeTask;
import cn.daxpay.single.util.PaySignUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Objects;
/**
* 客户系统消息通知任务支撑服务
* @author xxm
@@ -30,15 +25,16 @@ import java.util.Objects;
@RequiredArgsConstructor
public class ClientNoticeAssistService {
private final PlatformConfigService configService;
private final PaymentSignService paymentSignService;
private final PaymentAssistService paymentAssistService;
/**
* 构建出支付通知任务对象
*/
public ClientNoticeTask buildPayTask(PayOrder order, PayOrderExtra orderExtra){
// 获取系统签名
paymentAssistService.initPlatform();
PayNoticeResult payNoticeResult = new PayNoticeResult()
.setOrderNo(order.getOrderNo())
.setBizOrderNo(order.getBizOrderNo())
@@ -52,13 +48,7 @@ public class ClientNoticeAssistService {
.setStatus(order.getStatus())
.setAttach(orderExtra.getAttach());
PlatformConfig config = configService.getConfig();
// 签名
if (Objects.equals(config.getSignType(), PaySignTypeEnum.MD5.getCode())){
payNoticeResult.setSign(PaySignUtil.md5Sign(payNoticeResult,config.getSignSecret()));
} else {
payNoticeResult.setSign(PaySignUtil.hmacSha256Sign(payNoticeResult,config.getSignSecret()));
}
paymentSignService.sign(payNoticeResult);
return new ClientNoticeTask()
.setUrl(orderExtra.getNotifyUrl())
// 时间序列化进行了重写, 所以使用Jackson的序列化工具类
@@ -74,6 +64,8 @@ public class ClientNoticeAssistService {
* 构建出退款通知任务对象
*/
public ClientNoticeTask buildRefundTask(RefundOrder order, RefundOrderExtra orderExtra){
// 获取系统签名
paymentAssistService.initPlatform();
// 创建退款通知内容
RefundNoticeResult payNoticeResult = new RefundNoticeResult()
.setRefundNo(order.getRefundNo())

View File

@@ -25,7 +25,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@@ -89,13 +88,18 @@ public class RefundAssistService {
PayStatusEnum.FAIL.getCode());
if (tradesStatus.contains(payOrder.getStatus())) {
PayStatusEnum statusEnum = PayStatusEnum.findByCode(payOrder.getStatus());
throw new PayFailureException("当前订状态["+statusEnum.getName()+"]不允许发起退款操作");
throw new PayFailureException("当前支付单订状态["+statusEnum.getName()+"]不允许发起退款操作");
}
// 退款号唯一校验
if (StrUtil.isNotBlank(param.getBizRefundNo()) && refundOrderManager.existsByRefundNo(param.getBizRefundNo())){
throw new PayFailureException("退款单号已存在");
}
// 金额判断
if (param.getAmount() > payOrder.getRefundableBalance()){
throw new PayFailureException("退款金额不能大于支付金额");
}
}
/**
@@ -141,8 +145,8 @@ public class RefundAssistService {
.setOutRefundNo(refundInfo.getOutRefundNo());
// 退款成功更新退款时间
if (Objects.equals(refundOrder.getStatus(), SUCCESS.getCode())){
// TODO 读取网关返回的退款时间和完成时间
refundOrder.setFinishTime(LocalDateTime.now());
// 读取网关返回的退款时间和完成时间
refundOrder.setFinishTime(refundInfo.getFinishTime());
}
refundOrderManager.updateById(refundOrder);
}
@@ -169,9 +173,7 @@ public class RefundAssistService {
RefundResult refundResult = new RefundResult();
refundResult.setRefundNo(refundOrder.getRefundNo())
.setBizRefundNo(refundOrder.getBizRefundNo());
refundResult.setCode(refundOrder.getStatus())
.setMsg(refundOrder.getErrorMsg())
.setCode(refundOrder.getErrorCode());
refundResult.setMsg(refundOrder.getErrorMsg());
// 进行签名
String signType = platformInfo.getSignType();