|
|
|
|
@@ -18,6 +18,15 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 四方支付配置Service业务层处理
|
|
|
|
|
*
|
|
|
|
|
@@ -35,9 +44,26 @@ public class PayTrdConfigServiceImpl extends ServiceImpl<PayTrdConfigMapper, Pay
|
|
|
|
|
@Autowired
|
|
|
|
|
private OrderLogsService orderLogsService;
|
|
|
|
|
|
|
|
|
|
private final static Map<BigDecimal, AtomicLong> ACCOUNT_MAP = new ConcurrentHashMap<>();
|
|
|
|
|
private final static List<PayTrdConfig> PAY_TRD_CONFIG_LIST = new CopyOnWriteArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init(){
|
|
|
|
|
this.resetPayTrdConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void resetPayTrdConfig() {
|
|
|
|
|
PAY_TRD_CONFIG_LIST.clear();
|
|
|
|
|
List<PayTrdConfig> list = this.list(Wrappers.lambdaQuery(PayTrdConfig.class).orderByAsc(PayTrdConfig::getSortBy));
|
|
|
|
|
PAY_TRD_CONFIG_LIST.addAll(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public PayReturnResp createOrderAli(PayOrderInfoDTO dto){
|
|
|
|
|
PayTrdConfig payTrdConfig = getEnableStatus();
|
|
|
|
|
// PayTrdConfig payTrdConfig = getEnableStatus();
|
|
|
|
|
PayTrdConfig payTrdConfig = getConfig(dto.getPrice());
|
|
|
|
|
if(payTrdConfig == null){
|
|
|
|
|
throw new ServiceException("未开启支付,请联系客服");
|
|
|
|
|
}
|
|
|
|
|
@@ -45,44 +71,43 @@ public class PayTrdConfigServiceImpl extends ServiceImpl<PayTrdConfigMapper, Pay
|
|
|
|
|
if(typeEnum == null){
|
|
|
|
|
throw new ServiceException("未开启支付,请联系客服");
|
|
|
|
|
}
|
|
|
|
|
PayReturnResp orderAliMerge = trdPayManager.createOrderAliMerge(dto, payTrdConfig, typeEnum);
|
|
|
|
|
return orderAliMerge;
|
|
|
|
|
/*
|
|
|
|
|
JSONObject jsonObject = trdPayManager.createOrderAli(dto, payTrdConfig, typeEnum);
|
|
|
|
|
if(jsonObject == null){
|
|
|
|
|
log.error("第三方支付失败 返回数据为空");
|
|
|
|
|
throw new ServiceException("调用支付失败");
|
|
|
|
|
log.info("使用支付 {}", payTrdConfig.getType());
|
|
|
|
|
payManager.updatePayType(dto.getOrderNo(), payTrdConfig.getType());
|
|
|
|
|
return trdPayManager.createOrderAliMerge(dto, payTrdConfig, typeEnum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PayTrdConfig getConfig(BigDecimal amount){
|
|
|
|
|
List<PayTrdConfig> list = new ArrayList<>();
|
|
|
|
|
for(PayTrdConfig payTrdConfig : PAY_TRD_CONFIG_LIST){
|
|
|
|
|
if(payTrdConfig.getEnableStatus() == 0){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(payTrdConfig.getMaxAmount().compareTo(BigDecimal.ZERO) == 0 && payTrdConfig.getMinAmount().compareTo(BigDecimal.ZERO) == 0){
|
|
|
|
|
list.add(payTrdConfig);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// 在范围内
|
|
|
|
|
if(payTrdConfig.getMinAmount().compareTo(amount) <= 0 && payTrdConfig.getMaxAmount().compareTo(amount) >= 0){
|
|
|
|
|
list.add(payTrdConfig);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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(jsonObject));
|
|
|
|
|
throw new ServiceException("调用支付失败");
|
|
|
|
|
}
|
|
|
|
|
String payUrl = jsonObject.getJSONObject("payParams").getString("payUrl");
|
|
|
|
|
return PayReturnResp.createH5(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(jsonObject));
|
|
|
|
|
throw new ServiceException("调用支付失败");
|
|
|
|
|
}
|
|
|
|
|
String payMethod = jsonObject.getString("payMethod");
|
|
|
|
|
if("alipayApp".equals(payMethod)){
|
|
|
|
|
String appStr = jsonObject.getJSONObject("payParams").getString("appStr");
|
|
|
|
|
return PayReturnResp.createApp(appStr);
|
|
|
|
|
}
|
|
|
|
|
return PayReturnResp.createH5(JSON.toJSONString(jsonObject));
|
|
|
|
|
}else if(typeEnum == TrdPayTypeEnum.V3){
|
|
|
|
|
if(!"SUCCESS".equals(jsonObject.getString("retCode"))){
|
|
|
|
|
log.info("第三方支付失败 V3统一支付失败失败 dto={}, payTrdConfig={}, typeEnum={}", JSON.toJSONString(dto), JSON.toJSONString(payTrdConfig),JSON.toJSONString(jsonObject));
|
|
|
|
|
throw new ServiceException("调用支付失败");
|
|
|
|
|
}
|
|
|
|
|
String payUrl = jsonObject.getJSONObject("payParams").getString("payUrl");
|
|
|
|
|
return PayReturnResp.createH5(payUrl);
|
|
|
|
|
if(list.isEmpty()){
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return null;*/
|
|
|
|
|
AtomicLong index = ACCOUNT_MAP.get(amount);
|
|
|
|
|
if(index == null){
|
|
|
|
|
index = new AtomicLong(0);
|
|
|
|
|
index.incrementAndGet();
|
|
|
|
|
ACCOUNT_MAP.put(amount,index);
|
|
|
|
|
return list.get(0);
|
|
|
|
|
}
|
|
|
|
|
long in = index.getAndIncrement();
|
|
|
|
|
long l = in % list.size();
|
|
|
|
|
return list.get((int) l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
|
private PayTrdConfig getEnableStatus(){
|
|
|
|
|
return this.getOne(Wrappers.lambdaQuery(PayTrdConfig.class).eq(PayTrdConfig::getEnableStatus, 1)
|
|
|
|
|
.eq(PayTrdConfig::getDeleteFlag, 0).last("limit 1"));
|
|
|
|
|
@@ -109,87 +134,27 @@ public class PayTrdConfigServiceImpl extends ServiceImpl<PayTrdConfigMapper, Pay
|
|
|
|
|
TrdPayTypeEnum typeEnum = TrdPayTypeEnum.getByCode(trdPayType);
|
|
|
|
|
PayTrdConfig payTrdConfig = getConfigByPay(trdPayType);
|
|
|
|
|
if(typeEnum == null || payTrdConfig == null){
|
|
|
|
|
throw new ServiceException("配置数据为空");
|
|
|
|
|
throw new ServiceException("未检测到支付方式");
|
|
|
|
|
}
|
|
|
|
|
return trdPayManager.queryOrder(orderNo,payTrdConfig,typeEnum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public JSONObject resetOrder(String orderNo, String trdPayType,boolean updateData){
|
|
|
|
|
// 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("配置数据为空");
|
|
|
|
|
throw new ServiceException("未检测到支付方式");
|
|
|
|
|
}
|
|
|
|
|
return trdPayManager.resetQuery(orderNo,payTrdConfig,typeEnum,updateData);
|
|
|
|
|
/* 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,"V1", 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,"V2", PayTypeEnum.TRD);
|
|
|
|
|
}
|
|
|
|
|
return jsonObject;
|
|
|
|
|
}else if(typeEnum == TrdPayTypeEnum.V3){ // V1
|
|
|
|
|
if(!"SUCCESS".equals(jsonObject.getString("retCode"))){
|
|
|
|
|
log.info("第三方支付失败 V3统一支付失败失败 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,"V3", PayTypeEnum.TRD);
|
|
|
|
|
}
|
|
|
|
|
return jsonObject;
|
|
|
|
|
}
|
|
|
|
|
return jsonObject;*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void enableStatus(Long id, Integer enableStatus) {
|
|
|
|
|
if(enableStatus.equals(0)){
|
|
|
|
|
this.update(Wrappers.lambdaUpdate(PayTrdConfig.class).eq(PayTrdConfig::getId, id)
|
|
|
|
|
.set(PayTrdConfig::getEnableStatus, enableStatus));
|
|
|
|
|
/*if(enableStatus.equals(0)){
|
|
|
|
|
this.update(Wrappers.lambdaUpdate(PayTrdConfig.class).eq(PayTrdConfig::getId, id)
|
|
|
|
|
.set(PayTrdConfig::getEnableStatus, enableStatus));
|
|
|
|
|
} else if(enableStatus.equals(1)){
|
|
|
|
|
@@ -198,7 +163,7 @@ public class PayTrdConfigServiceImpl extends ServiceImpl<PayTrdConfigMapper, Pay
|
|
|
|
|
.set(PayTrdConfig::getEnableStatus, 0));
|
|
|
|
|
this.update(Wrappers.lambdaUpdate(PayTrdConfig.class).eq(PayTrdConfig::getId, id)
|
|
|
|
|
.set(PayTrdConfig::getEnableStatus, 1));
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|