update:
1. 重构支付宝支付
This commit is contained in:
@@ -7,7 +7,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.common.utils.date.DateUtil;
|
||||
import org.springblade.modules.api.constant.AppAliPayConstant;
|
||||
import org.springblade.modules.api.service.PaymentFacade;
|
||||
import org.springblade.modules.api.service.IPaymentService;
|
||||
import org.springblade.modules.common.eums.BusinessModule;
|
||||
import org.springblade.modules.common.eums.OrderStatus;
|
||||
import org.springblade.modules.common.eums.PaymentStatus;
|
||||
@@ -36,7 +36,7 @@ public class AliUserOrderCloseProcessor implements BasicProcessor {
|
||||
|
||||
|
||||
private final ICommonUserOrderService commonUserOrderService;
|
||||
private final PaymentFacade paymentFacade;
|
||||
private final IPaymentService paymentService;
|
||||
private final IOrderService orderService;
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ public class AliUserOrderCloseProcessor implements BasicProcessor {
|
||||
}
|
||||
|
||||
|
||||
AlipayTradeQueryResponse response = paymentFacade.aliQuery(orderNo);
|
||||
AlipayTradeQueryResponse response = paymentService.aliQuery(orderNo);
|
||||
log.info("查询结果: {}", response);
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ public class AliUserOrderCloseProcessor implements BasicProcessor {
|
||||
if (response.getTradeStatus().equalsIgnoreCase("TRADE_SUCCESS") || response.getTradeStatus().equalsIgnoreCase("TRADE_FINISHED")){
|
||||
orderService.paySuccess(orderNo, String.valueOf(AppAliPayConstant.ALI_MACH_ID), response.getTradeNo(), DateUtil.convertFromDate(response.getSendPayDate() , "yyyy-MM-dd HH:mm:ss"), response.getBuyerUserId());
|
||||
}else {
|
||||
Boolean isClosed = paymentFacade.aliCloseOrder(orderNo);
|
||||
Boolean isClosed = paymentService.aliCloseOrder(orderNo);
|
||||
if (!Boolean.TRUE.equals(isClosed)) {
|
||||
log.error("订单 {} 调用支付宝关闭失败,保留本地状态等待下次重试", orderNo);
|
||||
// 策略:不更新本地状态,下次定时任务再试。防止误关。
|
||||
|
||||
@@ -11,9 +11,8 @@ import org.springblade.core.mp.support.Condition;
|
||||
import org.springblade.core.mp.support.Query;
|
||||
import org.springblade.core.tool.api.R;
|
||||
import org.springblade.modules.api.request.*;
|
||||
import org.springblade.modules.api.response.CreateOrderResponse;
|
||||
import org.springblade.modules.api.response.PaymentResponse;
|
||||
import org.springblade.modules.api.service.PaymentFacade;
|
||||
import org.springblade.modules.api.service.IPaymentService;
|
||||
import org.springblade.modules.api.vo.*;
|
||||
import org.springblade.modules.common.service.*;
|
||||
import org.springblade.modules.system.service.IUserService;
|
||||
@@ -43,7 +42,7 @@ public class APPUserController {
|
||||
|
||||
private final ICommonLotteryPrizeService lotteryPrizeService;
|
||||
|
||||
private final PaymentFacade paymentFacade;
|
||||
private final IPaymentService paymentService;
|
||||
|
||||
|
||||
/**
|
||||
@@ -83,7 +82,7 @@ public class APPUserController {
|
||||
@ApiOperationSupport(order = 5)
|
||||
@Operation(summary = "支付", description = "传入PaymentRequest")
|
||||
public R<PaymentResponse> aliPay(@Valid @RequestBody PaymentRequest request) {
|
||||
return R.data(paymentFacade.aliAppPay(request));
|
||||
return R.data(paymentService.aliAppPay(request));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,7 +92,7 @@ public class APPUserController {
|
||||
@ApiOperationSupport(order = 6)
|
||||
@Operation(summary = "支付", description = "传入PaymentRequest")
|
||||
public R<PaymentResponse> wxPay(@Valid @RequestBody PaymentRequest request) {
|
||||
return R.data(paymentFacade.wxAppPay(request));
|
||||
return R.data(paymentService.wxAppPay(request));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -15,4 +15,9 @@ public class PaymentResponse implements Serializable {
|
||||
|
||||
private String params;
|
||||
|
||||
public PaymentResponse(Integer paymentType, String payChannel, String params) {
|
||||
this.paymentType = paymentType;
|
||||
this.payChannel = payChannel;
|
||||
this.params = params;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.springblade.modules.api.request.PaymentRequest;
|
||||
import org.springblade.modules.api.response.PaymentResponse;
|
||||
|
||||
|
||||
public interface PaymentFacade {
|
||||
public interface IPaymentService {
|
||||
|
||||
PaymentResponse aliAppPay(PaymentRequest request) ;
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package org.springblade.modules.api.service.impl;
|
||||
|
||||
import com.alipay.api.response.AlipayTradeCloseResponse;
|
||||
import com.alipay.api.response.AlipayTradeQueryResponse;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.common.cache.PaymentCache;
|
||||
import org.springblade.core.log.exception.ServiceException;
|
||||
import org.springblade.core.tool.utils.StringPool;
|
||||
import org.springblade.core.tool.utils.StringUtil;
|
||||
import org.springblade.modules.api.constant.AppAliPayConstant;
|
||||
import org.springblade.modules.api.request.PaymentRequest;
|
||||
import org.springblade.modules.api.response.PaymentResponse;
|
||||
import org.springblade.modules.common.eums.BusinessModule;
|
||||
import org.springblade.modules.common.eums.PaymentType;
|
||||
import org.springblade.modules.api.service.IPaymentService;
|
||||
import org.springblade.modules.common.pojo.entity.CommonUserOrderEntity;
|
||||
import org.springblade.modules.common.service.ICommonUserOrderService;
|
||||
import org.springblade.plugin.pay.api.alipay.IAliPayApiService;
|
||||
import org.springblade.plugin.pay.constant.PayConstant;
|
||||
import org.springblade.plugin.pay.entity.AliPayAccountConfig;
|
||||
import org.springblade.plugin.pay.entity.Order;
|
||||
import org.springblade.plugin.pay.service.IAliPayService;
|
||||
import org.springblade.plugin.pay.service.IOrderService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class IPaymentServiceImpl implements IPaymentService {
|
||||
|
||||
private final ICommonUserOrderService commonUserOrderService;
|
||||
|
||||
private final IOrderService orderService;
|
||||
|
||||
private final IAliPayApiService aliPayApiService;
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public PaymentResponse aliAppPay(PaymentRequest request) {
|
||||
|
||||
if (!request.getPaymentType().equals(PaymentType.ALI.getCode())) {
|
||||
throw new ServiceException("请选择正确的支付方式");
|
||||
}
|
||||
|
||||
CommonUserOrderEntity userOrder = commonUserOrderService.getById(Long.valueOf(request.getOrderId()));
|
||||
if (userOrder == null) {
|
||||
throw new ServiceException("无效订单");
|
||||
}
|
||||
|
||||
if (!userOrder.getPaymentType().equals(request.getPaymentType())) {
|
||||
throw new ServiceException("无效订单");
|
||||
}
|
||||
|
||||
AliPayAccountConfig config = PaymentCache.getAliConfig();
|
||||
Order order = orderService.save(config.getAppId(), config.getMchId(), config.getPayName(), userOrder.getItemName(), Objects.toString(userOrder.getActualAmount(), "0"), PayConstant.ALIPAY, PayConstant.APPPAY, config.getNotifyUrl(), PaymentType.ALI.getAbbr() + BusinessModule.fromCodeIgnoreCaseOrNull(userOrder.getOrderType()).getAbbr() + userOrder.getId());
|
||||
String content = aliPayApiService.appPay(order, config, StringPool.EMPTY);
|
||||
return new PaymentResponse(PaymentType.ALI.getCode(), content, PaymentType.ALI.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public AlipayTradeQueryResponse aliQuery(String orderNo) {
|
||||
AliPayAccountConfig config = PaymentCache.getAliConfig();
|
||||
|
||||
AlipayTradeQueryResponse response;
|
||||
try {
|
||||
response = aliPayApiService.query(orderNo, config);
|
||||
log.info("支付宝账单:[{}]的支付结果为:{}", orderNo, response.getBody());
|
||||
return response;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("查询订单异常:{}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean aliCloseOrder(String orderNo) {
|
||||
AliPayAccountConfig config = PaymentCache.getAliConfig();
|
||||
|
||||
AlipayTradeCloseResponse response;
|
||||
try {
|
||||
response = aliPayApiService.closeOrder(orderNo, config);
|
||||
log.info("关闭支付宝账单:[{}]的支付结果为:{}", orderNo, response.getBody());
|
||||
return response.getMsg().equalsIgnoreCase("SUCCESS")
|
||||
|| response.getCode().equalsIgnoreCase("10000");
|
||||
} catch (Exception e) {
|
||||
log.error("关闭支付宝账单异常:{}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaymentResponse wxAppPay(PaymentRequest request) {
|
||||
throw new ServiceException("当前微信支付未开放");
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package org.springblade.modules.api.service.impl;
|
||||
|
||||
import com.alipay.api.response.AlipayTradeQueryResponse;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.core.log.exception.ServiceException;
|
||||
import org.springblade.modules.api.constant.AppAliPayConstant;
|
||||
import org.springblade.modules.api.request.PaymentRequest;
|
||||
import org.springblade.modules.api.response.PaymentResponse;
|
||||
import org.springblade.modules.common.eums.BusinessModule;
|
||||
import org.springblade.modules.common.eums.PaymentType;
|
||||
import org.springblade.modules.api.service.PaymentFacade;
|
||||
import org.springblade.modules.common.pojo.entity.CommonUserOrderEntity;
|
||||
import org.springblade.modules.common.service.ICommonUserOrderService;
|
||||
import org.springblade.plugin.pay.service.IAliPayService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class PaymentFacadeImpl implements PaymentFacade {
|
||||
|
||||
private final ICommonUserOrderService commonUserOrderService;
|
||||
private final IAliPayService aliPayService;
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public PaymentResponse aliAppPay(PaymentRequest request) {
|
||||
|
||||
if (!request.getPaymentType().equals(PaymentType.ALI.getCode())){
|
||||
throw new ServiceException("请选择正确的支付方式");
|
||||
}
|
||||
|
||||
CommonUserOrderEntity order = commonUserOrderService.getById(Long.valueOf(request.getOrderId()));
|
||||
if (order == null) {
|
||||
throw new ServiceException("无效订单");
|
||||
}
|
||||
if (order.getPaymentType().equals(PaymentType.OFFLINE.getCode())) {
|
||||
throw new ServiceException("无效订单");
|
||||
}
|
||||
try {
|
||||
String content = aliPayService.appPay(
|
||||
String.valueOf(AppAliPayConstant.ALI_APP_ID),
|
||||
String.valueOf(AppAliPayConstant.ALI_MACH_ID),
|
||||
order.getItemName(),
|
||||
String.valueOf(order.getActualAmount()),
|
||||
null,
|
||||
order.getItemName(),
|
||||
PaymentType.ALI.getAbbr() + BusinessModule.fromCodeIgnoreCaseOrNull(order.getOrderType()).getAbbr(),
|
||||
order.getId()
|
||||
);
|
||||
return new PaymentResponse()
|
||||
.setPaymentType(PaymentType.ALI.getCode())
|
||||
.setParams(content)
|
||||
.setPayChannel(PaymentType.ALI.getName())
|
||||
;
|
||||
}catch (Exception e){
|
||||
log.error("支付异常 : {}" , e.getMessage());
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public AlipayTradeQueryResponse aliQuery(String orderNo) {
|
||||
return aliPayService.query(
|
||||
orderNo,
|
||||
String.valueOf(AppAliPayConstant.ALI_APP_ID),
|
||||
String.valueOf(AppAliPayConstant.ALI_MACH_ID)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean aliCloseOrder(String orderNo) {
|
||||
return aliPayService.closeOrder(
|
||||
orderNo,
|
||||
String.valueOf(AppAliPayConstant.ALI_APP_ID),
|
||||
String.valueOf(AppAliPayConstant.ALI_MACH_ID)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaymentResponse wxAppPay(PaymentRequest request) {
|
||||
throw new ServiceException("当前微信支付未开放");
|
||||
}
|
||||
}
|
||||
@@ -37,9 +37,7 @@ import org.springblade.core.secure.utils.AuthUtil;
|
||||
import org.springblade.modules.api.request.ConfirmOrderRequest;
|
||||
import org.springblade.modules.api.request.CreateMallOrderRequest;
|
||||
import org.springblade.modules.api.request.CreateTravelOrderRequest;
|
||||
import org.springblade.modules.api.request.PaymentRequest;
|
||||
import org.springblade.modules.api.response.CreateOrderResponse;
|
||||
import org.springblade.modules.api.response.PaymentResponse;
|
||||
import org.springblade.modules.api.vo.CommonTravelOrderDetailApiVO;
|
||||
import org.springblade.modules.api.vo.CommonUserOrderApiVO;
|
||||
import org.springblade.modules.api.vo.CommonUserOrderDetailApiVO;
|
||||
@@ -61,7 +59,6 @@ import org.springblade.modules.mall.service.IMallProductService;
|
||||
import org.springblade.modules.market.pojo.entity.MarketInfoEntity;
|
||||
import org.springblade.modules.market.service.IMarketInfoService;
|
||||
import org.springblade.modules.market.service.IMarketUserCommissionService;
|
||||
import org.springblade.modules.api.service.PaymentFacade;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
|
||||
@@ -161,6 +161,10 @@ public class AliPayAccountConfigServiceImpl extends BaseServiceImpl<AliPayAccoun
|
||||
|
||||
@Override
|
||||
public AliPayAccountConfig getDefault() {
|
||||
return getOne(Wrappers.<AliPayAccountConfig>lambdaQuery().eq(AliPayAccountConfig::getStatus, 1));
|
||||
AliPayAccountConfig config = getOne(Wrappers.<AliPayAccountConfig>lambdaQuery().eq(AliPayAccountConfig::getStatus, 1));
|
||||
if (config == null) {
|
||||
throw new ServiceException("该商户号尚未接入");
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public class AliPayServiceImpl implements IAliPayService {
|
||||
if (aliPayAccountConfig == null) {
|
||||
throw new ServiceException("该商户号尚未接入");
|
||||
}
|
||||
if (aliPayAccountConfig.getStatus().intValue() != 1) {
|
||||
if (aliPayAccountConfig.getStatus() != 1) {
|
||||
throw new ServiceException("该商户号已暂停交易");
|
||||
}
|
||||
if (StringUtil.isBlank(notifyUrl)) {
|
||||
|
||||
@@ -177,6 +177,10 @@ public class WxPayAccountConfigServiceImpl extends BaseServiceImpl<WxPayAccountC
|
||||
|
||||
@Override
|
||||
public WxPayAccountConfig getDefault() {
|
||||
return getOne(Wrappers.<WxPayAccountConfig>lambdaQuery().eq(WxPayAccountConfig::getStatus, 1));
|
||||
WxPayAccountConfig config = getOne(Wrappers.<WxPayAccountConfig>lambdaQuery().eq(WxPayAccountConfig::getStatus, 1));
|
||||
if (config == null) {
|
||||
throw new ServiceException("该商户号尚未接入");
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user