This commit is contained in:
77
2024-11-25 18:09:52 +08:00
parent ab685878b7
commit fa1d6df347
16 changed files with 542 additions and 111 deletions

View File

@@ -0,0 +1,20 @@
package com.ruoyi.cai.service;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.cai.domain.PayTrdConfig;
import com.ruoyi.cai.pay.PayOrderInfoDTO;
/**
* 四方支付配置Service接口
*
* @author ruoyi
* @date 2024-11-25
*/
public interface PayTrdConfigService extends IService<PayTrdConfig> {
String createOrderAli(PayOrderInfoDTO dto);
JSONObject queryOrder(String orderNo, String trdPayType);
JSONObject resetOrder(String orderNo, String trdPayType);
}

View File

@@ -0,0 +1,155 @@
package com.ruoyi.cai.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.PayTrdConfig;
import com.ruoyi.cai.mapper.PayTrdConfigMapper;
import com.ruoyi.cai.pay.PayManager;
import com.ruoyi.cai.pay.PayOrderInfoDTO;
import com.ruoyi.cai.pay.PayTypeEnum;
import com.ruoyi.cai.service.PayTrdConfigService;
import com.ruoyi.cai.trdpay.TrdPayManager;
import com.ruoyi.cai.trdpay.TrdPayTypeEnum;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* 四方支付配置Service业务层处理
*
* @author ruoyi
* @date 2024-11-25
*/
@Service
@Slf4j
public class PayTrdConfigServiceImpl extends ServiceImpl<PayTrdConfigMapper, PayTrdConfig> implements PayTrdConfigService {
@Autowired
private TrdPayManager trdPayManager;
@Autowired
private PayManager payManager;
@Override
public String createOrderAli(PayOrderInfoDTO dto){
PayTrdConfig payTrdConfig = getEnableStatus();
if(payTrdConfig == null){
throw new ServiceException("未开启支付,请联系客服");
}
TrdPayTypeEnum typeEnum = TrdPayTypeEnum.getByCode(payTrdConfig.getType());
if(typeEnum == null){
throw new ServiceException("未开启支付,请联系客服");
}
JSONObject jsonObject = trdPayManager.createOrderAli(dto, payTrdConfig, typeEnum);
if(jsonObject == null){
log.error("第三方支付失败 返回数据为空");
throw new ServiceException("调用支付失败");
}
if(typeEnum == TrdPayTypeEnum.V1){ // V1
if(!"SUCCESS".equals(jsonObject.getString("retCode"))){
log.info("第三方支付失败 V1统一支付失败失败 dto={}, payTrdConfig={}, typeEnum={}", JSON.toJSONString(dto), JSON.toJSONString(payTrdConfig),JSON.toJSONString(typeEnum));
throw new ServiceException("调用支付失败");
}
return jsonObject.getJSONObject("payParams").getString("payUrl");
}else if(typeEnum == TrdPayTypeEnum.V2){
if(!"0".equals(jsonObject.getString("retCode"))){
log.info("第三方支付失败 V2统一支付失败失败 dto={}, payTrdConfig={}, typeEnum={}", JSON.toJSONString(dto), JSON.toJSONString(payTrdConfig),JSON.toJSONString(typeEnum));
throw new ServiceException("调用支付失败");
}
return JSON.toJSONString(jsonObject);
}
return null;
}
private PayTrdConfig getEnableStatus(){
return this.getOne(Wrappers.lambdaQuery(PayTrdConfig.class).eq(PayTrdConfig::getEnableStatus, 1)
.eq(PayTrdConfig::getDeleteFlag, 0).last("limit 1"));
}
private PayTrdConfig getConfigByPay(String type){
PayTrdConfig one = this.getOne(Wrappers.lambdaQuery(PayTrdConfig.class).eq(PayTrdConfig::getEnableStatus, 1)
.eq(PayTrdConfig::getType, type).eq(PayTrdConfig::getDeleteFlag, 0).last("limit 1"));
if(one != null){
return one;
}
one = this.getOne(Wrappers.lambdaQuery(PayTrdConfig.class)
.eq(PayTrdConfig::getType, type).eq(PayTrdConfig::getDeleteFlag, 0).last("limit 1"));
if(one != null){
return one;
}
one = this.getOne(Wrappers.lambdaQuery(PayTrdConfig.class)
.eq(PayTrdConfig::getType, type).last("limit 1"));
return one;
}
@Override
public JSONObject queryOrder(String orderNo, String trdPayType){
TrdPayTypeEnum typeEnum = TrdPayTypeEnum.getByCode(trdPayType);
PayTrdConfig payTrdConfig = getConfigByPay(trdPayType);
if(typeEnum == null || payTrdConfig == null){
throw new ServiceException("配置数据为空");
}
return trdPayManager.queryOrder(orderNo,payTrdConfig,typeEnum);
}
@Override
public JSONObject resetOrder(String orderNo, String trdPayType){
// PayOrderInfoDTO orderInfo = payManager.getOrderInfo(orderNo);
// if(orderInfo == null){
// throw new ServiceException("订单不存在");
// }
// if(!PayTypeEnum.TRD.getCode().toString().equals(orderInfo.getPlatformType())){
// throw new ServiceException("订单不属于第三方支付");
// }
// String appid = orderInfo.getAppid();
TrdPayTypeEnum typeEnum = TrdPayTypeEnum.getByCode(trdPayType);
PayTrdConfig payTrdConfig = getConfigByPay(trdPayType);
if(typeEnum == null || payTrdConfig == null){
throw new ServiceException("配置数据为空");
}
JSONObject jsonObject = trdPayManager.queryOrder(orderNo,payTrdConfig,typeEnum);
if(typeEnum == TrdPayTypeEnum.V1){ // V1
if(!"SUCCESS".equals(jsonObject.getString("retCode"))){
log.info("第三方支付失败 V1统一支付失败失败 orderNo={}, payTrdConfig={}, typeEnum={}", orderNo, JSON.toJSONString(payTrdConfig),JSON.toJSONString(typeEnum));
throw new ServiceException("调用支付失败");
}
Integer status = jsonObject.getInteger("status");
if(status != null && status.equals(2)){
String mchOrderNo = jsonObject.getString("mchOrderNo");
String payOrderId = jsonObject.getString("payOrderId");
String productId = jsonObject.getString("productId");
Map<String,String> objectJson = new HashMap<>();
for (String key : jsonObject.keySet()) {
objectJson.put(key, jsonObject.getString(key));
}
payManager.callBack(mchOrderNo,payOrderId,objectJson,productId, PayTypeEnum.TRD);
}
return jsonObject;
}else if(typeEnum == TrdPayTypeEnum.V2){
if(!"0".equals(jsonObject.getString("retCode"))){
log.info("第三方支付失败 V2统一支付失败失败 orderNo={}, payTrdConfig={}, typeEnum={}", orderNo, JSON.toJSONString(payTrdConfig),JSON.toJSONString(typeEnum));
throw new ServiceException("调用支付失败");
}
Integer status = jsonObject.getInteger("status");
if(status != null && (status.equals(2) || status.equals(3))){
String mchOrderNo = jsonObject.getString("mchOrderNo");
String payOrderId = jsonObject.getString("payOrderId");
String productId = jsonObject.getString("productId");
Map<String,String> objectJson = new HashMap<>();
for (String key : jsonObject.keySet()) {
objectJson.put(key, jsonObject.getString(key));
}
payManager.callBack(mchOrderNo,payOrderId,objectJson,productId, PayTypeEnum.TRD);
}
return jsonObject;
}
return jsonObject;
}
}