mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 02:55:04 +08:00
update 订单分账
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.jsowell.adapay.dto;
|
||||
|
||||
import com.jsowell.adapay.common.DivMember;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 汇付分账所需参数
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class PaymentConfirmParam {
|
||||
/**
|
||||
* 支付单号
|
||||
*/
|
||||
private String paymentId;
|
||||
|
||||
/**
|
||||
* 参与分账的账户, 最多7个
|
||||
*/
|
||||
private List<DivMember> divMemberList;
|
||||
|
||||
/**
|
||||
* 确认金额
|
||||
*/
|
||||
private BigDecimal confirmAmt;
|
||||
|
||||
/**
|
||||
* 订单编号
|
||||
*/
|
||||
private String orderCode;
|
||||
|
||||
/**
|
||||
* appId
|
||||
*/
|
||||
private String wechatAppId;
|
||||
}
|
||||
@@ -959,50 +959,65 @@ public class AdapayService {
|
||||
* @param orderCode 订单编号
|
||||
*/
|
||||
public PaymentConfirmResponse createPaymentConfirmRequest(String paymentId, AdapayMemberAccount adapayMemberAccount, BigDecimal confirmAmt, String orderCode, String wechatAppId) {
|
||||
// 调汇付的分账接口 确认交易
|
||||
Map<String, Object> confirmParams = Maps.newHashMap();
|
||||
// Adapay生成的支付对象id
|
||||
confirmParams.put("payment_id", paymentId);
|
||||
// 请求订单号,只能为英文、数字或者下划线的一种或多种组合,保证在app_id下唯一
|
||||
// confirmParams.put("order_no", "PC" + System.currentTimeMillis());
|
||||
confirmParams.put("order_no", IdUtils.get16UUID("PC"));
|
||||
// 确认金额,必须大于0,保留两位小数点,如0.10、100.05等。必须小于等于原支付金额-已确认金额-已撤销金额。
|
||||
confirmParams.put("confirm_amt", AdapayUtil.formatAmount(confirmAmt));
|
||||
// 附加说明
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("orderCode", orderCode);
|
||||
jsonObject.put("adapayMemberId", adapayMemberAccount.getAdapayMemberId());
|
||||
confirmParams.put("description", jsonObject.toJSONString());
|
||||
// 分账对象信息 一次最多7个
|
||||
// // 调汇付的分账接口 确认交易
|
||||
// Map<String, Object> confirmParams = Maps.newHashMap();
|
||||
// // Adapay生成的支付对象id
|
||||
// confirmParams.put("payment_id", paymentId);
|
||||
// // 请求订单号,只能为英文、数字或者下划线的一种或多种组合,保证在app_id下唯一
|
||||
// // confirmParams.put("order_no", "PC" + System.currentTimeMillis());
|
||||
// confirmParams.put("order_no", IdUtils.get16UUID("PC"));
|
||||
// // 确认金额,必须大于0,保留两位小数点,如0.10、100.05等。必须小于等于原支付金额-已确认金额-已撤销金额。
|
||||
// confirmParams.put("confirm_amt", AdapayUtil.formatAmount(confirmAmt));
|
||||
// // 附加说明
|
||||
// JSONObject jsonObject = new JSONObject();
|
||||
// jsonObject.put("orderCode", orderCode);
|
||||
// jsonObject.put("adapayMemberId", adapayMemberAccount.getAdapayMemberId());
|
||||
// confirmParams.put("description", jsonObject.toJSONString());
|
||||
// // 分账对象信息 一次最多7个
|
||||
// DivMember divMember = new DivMember();
|
||||
// divMember.setMemberId(adapayMemberAccount.getAdapayMemberId());
|
||||
// divMember.setAmount(AdapayUtil.formatAmount(confirmAmt));
|
||||
// divMember.setFeeFlag(Constants.Y);
|
||||
// confirmParams.put("div_members", Lists.newArrayList(divMember));
|
||||
// Map<String, Object> paymentConfirm = null;
|
||||
// try {
|
||||
// paymentConfirm = PaymentConfirm.create(confirmParams, wechatAppId);
|
||||
// } catch (BaseAdaPayException e) {
|
||||
// log.error("创建交易确认请求error", e);
|
||||
// }
|
||||
// String jsonString = JSON.toJSONString(paymentConfirm);
|
||||
// log.info("调分账接口param:{}, result:{}", JSON.toJSONString(confirmParams), jsonString);
|
||||
//
|
||||
// // 删除支付确认信息缓存
|
||||
// redisCache.deleteObject(CacheConstants.PAYMENT_CONFIRM_LIST + paymentId);
|
||||
// return JSONObject.parseObject(jsonString, PaymentConfirmResponse.class);
|
||||
|
||||
DivMember divMember = new DivMember();
|
||||
divMember.setMemberId(adapayMemberAccount.getAdapayMemberId());
|
||||
divMember.setAmount(AdapayUtil.formatAmount(confirmAmt));
|
||||
divMember.setFeeFlag(Constants.Y);
|
||||
confirmParams.put("div_members", Lists.newArrayList(divMember));
|
||||
Map<String, Object> paymentConfirm = null;
|
||||
try {
|
||||
paymentConfirm = PaymentConfirm.create(confirmParams, wechatAppId);
|
||||
} catch (BaseAdaPayException e) {
|
||||
log.error("创建交易确认请求error", e);
|
||||
}
|
||||
String jsonString = JSON.toJSONString(paymentConfirm);
|
||||
log.info("调分账接口param:{}, result:{}", JSON.toJSONString(confirmParams), jsonString);
|
||||
|
||||
// 删除支付确认信息缓存
|
||||
redisCache.deleteObject(CacheConstants.PAYMENT_CONFIRM_LIST + paymentId);
|
||||
return JSONObject.parseObject(jsonString, PaymentConfirmResponse.class);
|
||||
PaymentConfirmParam param = PaymentConfirmParam.builder()
|
||||
.paymentId(paymentId)
|
||||
.divMemberList(Lists.newArrayList(divMember))
|
||||
.confirmAmt(confirmAmt)
|
||||
.orderCode(orderCode)
|
||||
.wechatAppId(wechatAppId)
|
||||
.build();
|
||||
return createPaymentConfirmRequest(param);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建交易确认请求/创建分账请求
|
||||
* 这个方法只适用于多用户分账
|
||||
*
|
||||
* @param paymentId 支付对象id
|
||||
* @param adapayMemberAccounts 收到该账的汇付会员信息
|
||||
* @param confirmAmt 确认的金额
|
||||
* @param orderCode 订单编号
|
||||
*/
|
||||
public PaymentConfirmResponse createPaymentConfirmRequestMultiuser(String paymentId, List<DivMember> adapayMemberAccounts, BigDecimal confirmAmt, String orderCode, String wechatAppId) {
|
||||
public PaymentConfirmResponse createPaymentConfirmRequest(PaymentConfirmParam param) {
|
||||
String paymentId = param.getPaymentId(); // 支付对象id
|
||||
List<DivMember> adapayMemberAccounts = param.getDivMemberList(); // 收到该账的汇付会员信息
|
||||
BigDecimal confirmAmt = param.getConfirmAmt(); // 确认的金额
|
||||
String orderCode = param.getOrderCode(); // 订单编号
|
||||
String wechatAppId = param.getWechatAppId(); // appId
|
||||
|
||||
// 调汇付的分账接口 确认交易
|
||||
Map<String, Object> confirmParams = Maps.newHashMap();
|
||||
// Adapay生成的支付对象id
|
||||
@@ -1019,10 +1034,6 @@ public class AdapayService {
|
||||
jsonObject.put("adapayMemberIds", StringUtils.join(collect, ","));
|
||||
confirmParams.put("description", jsonObject.toJSONString());
|
||||
// 分账对象信息 一次最多7个
|
||||
// DivMember divMember = new DivMember();
|
||||
// divMember.setMember_id(adapayMemberAccount.getAdapayMemberId());
|
||||
// divMember.setAmount(AdapayUtil.formatAmount(confirmAmt));
|
||||
// divMember.setFee_flag(Constants.Y);
|
||||
if (adapayMemberAccounts.size() > 7) {
|
||||
throw new BusinessException("", "分账人数不能超过7人");
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 站点分成配置表
|
||||
*/
|
||||
* 站点分成配置表
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@SuperBuilder
|
||||
|
||||
@@ -1379,6 +1379,59 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
|
||||
return result;
|
||||
}
|
||||
|
||||
public OrderSplitResult doBalancePaymentWithDelay(OrderBasicInfo orderBasicInfo, List<DivMember> divMemberList, String wechatAppId) throws BaseAdaPayException {
|
||||
// 订单结算金额
|
||||
BigDecimal settleAmount = orderBasicInfo.getSettleAmount();
|
||||
// 订单编号
|
||||
String orderCode = orderBasicInfo.getOrderCode();
|
||||
|
||||
BigDecimal confirmAmt = BigDecimal.ZERO; // 消费金额
|
||||
BigDecimal feeAmt = BigDecimal.ZERO; // 手续费金额
|
||||
String status = null; // 状态
|
||||
|
||||
// 获取余额支付扣除金额 也许存在一笔不够扣,需要扣多笔的情况
|
||||
List<BalanceDeductionAmountVO> list = calculateSplitTheBillAmount(orderBasicInfo.getMemberId(), settleAmount);
|
||||
// List<String> collect = list.stream().map(BalanceDeductionAmountVO::getPaymentId).collect(Collectors.toList());
|
||||
|
||||
List<OrderPayRecord> orderPayRecordList = orderPayRecordService.getOrderPayRecordList(orderBasicInfo.getOrderCode());
|
||||
List<PaymentInfo> paymentInfos = Lists.newArrayList();
|
||||
for (OrderPayRecord orderPayRecord : orderPayRecordList) {
|
||||
String deductionRecord = orderPayRecord.getDeductionRecord();
|
||||
paymentInfos.addAll(orderPayRecordService.parseDeductionRecord(deductionRecord));
|
||||
}
|
||||
List<String> collect = paymentInfos.stream().map(PaymentInfo::getPaymentId).collect(Collectors.toList());
|
||||
|
||||
// 校验是否分账
|
||||
OrderSplitResult result = verifyOrderConfirmAmount(collect, orderCode, settleAmount, wechatAppId);
|
||||
if (!AdapayStatusEnum.SUCCEEDED.getValue().equals(result.getStatus())) {
|
||||
// 没有分账,执行分账逻辑
|
||||
for (BalanceDeductionAmountVO vo : list) {
|
||||
String paymentId = vo.getPaymentId();
|
||||
BigDecimal deductionAmount = vo.getDeductionAmount();
|
||||
|
||||
// 延时分账,使用确认交易API
|
||||
PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequestMultiuser(paymentId,
|
||||
divMemberList, deductionAmount, orderCode, wechatAppId);
|
||||
|
||||
if (paymentConfirmResponse != null && paymentConfirmResponse.isNotFailed()) {
|
||||
confirmAmt = confirmAmt.add(new BigDecimal(paymentConfirmResponse.getConfirm_amt()));
|
||||
feeAmt = feeAmt.add(new BigDecimal(paymentConfirmResponse.getFee_amt()));
|
||||
status = paymentConfirmResponse.getStatus();
|
||||
memberAdapayRecordService.unfreezeAmountAndUpdateSpendAmount(paymentId, confirmAmt, confirmAmt);
|
||||
}
|
||||
}
|
||||
|
||||
// 分账接口返回的信息
|
||||
result = new OrderSplitResult();
|
||||
result.setConfirmAmt(confirmAmt.toString());
|
||||
result.setStatus(status);
|
||||
result.setFeeAmt(feeAmt.toString());
|
||||
result.setOrderCode(orderCode);
|
||||
result.setSettleAmt(settleAmount.toString());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算分账扣除金额
|
||||
*/
|
||||
@@ -1520,6 +1573,60 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟交易订单 交易确认 多账户
|
||||
* @param orderBasicInfo
|
||||
* @param divMemberList
|
||||
* @param wechatAppId
|
||||
* @return
|
||||
* @throws BaseAdaPayException
|
||||
*/
|
||||
public OrderSplitResult doPaymentConfirmWithDelay(OrderBasicInfo orderBasicInfo, List<DivMember> divMemberList, String wechatAppId) throws BaseAdaPayException {
|
||||
// 订单编号
|
||||
String orderCode = orderBasicInfo.getOrderCode();
|
||||
// 订单结算金额
|
||||
BigDecimal settleAmount = orderBasicInfo.getSettleAmount();
|
||||
|
||||
// 查询订单支付记录
|
||||
List<OrderPayRecord> orderPayRecordList = orderPayRecordService.getOrderPayRecordList(orderCode);
|
||||
|
||||
// 选出需要分账的支付id
|
||||
List<PaymentInfo> paymentInfos = Lists.newArrayList();
|
||||
for (OrderPayRecord orderPayRecord : orderPayRecordList) {
|
||||
BigDecimal payAmount = orderPayRecord.getPayAmount();
|
||||
BigDecimal refundAmount = orderPayRecord.getRefundAmount() == null ? BigDecimal.ZERO : orderPayRecord.getRefundAmount();
|
||||
// 如果相减等于0,说明这笔支付单退完了,就不用分账了
|
||||
BigDecimal subtract = payAmount.subtract(refundAmount);
|
||||
if (subtract.compareTo(BigDecimal.ZERO) > 0) {
|
||||
paymentInfos.addAll(orderPayRecordService.parseDeductionRecord(orderPayRecord.getDeductionRecord()));
|
||||
}
|
||||
}
|
||||
|
||||
List<String> paymentIds = paymentInfos.stream().map(PaymentInfo::getPaymentId).collect(Collectors.toList());
|
||||
|
||||
// 支付id
|
||||
String paymentId = paymentInfos.get(0).getPaymentId();
|
||||
|
||||
// 校验订单分账金额
|
||||
OrderSplitResult result = verifyOrderConfirmAmount(paymentIds, orderCode, settleAmount, wechatAppId);
|
||||
|
||||
// 校验订单是否分账 状态为非交易完成的时候,进行分账处理
|
||||
if (!AdapayStatusEnum.SUCCEEDED.getValue().equals(result.getStatus())) {
|
||||
// 调汇付的分账接口 确认交易
|
||||
PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequestMultiuser(paymentId,
|
||||
divMemberList, settleAmount, orderCode, wechatAppId);
|
||||
|
||||
// 分账接口返回的信息
|
||||
result = new OrderSplitResult();
|
||||
result.setConfirmAmt(paymentConfirmResponse.getConfirm_amt());
|
||||
result.setStatus(paymentConfirmResponse.getStatus());
|
||||
result.setFeeAmt(paymentConfirmResponse.getFee_amt());
|
||||
result.setOrderCode(orderCode);
|
||||
result.setSettleAmt(settleAmount.toString());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单实时分账方法
|
||||
* 2024年9月24日16点17分 新的分账方法
|
||||
@@ -1547,7 +1654,7 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 分账方法
|
||||
* TODO 分账方法
|
||||
* @param afterSettleOrderDTO
|
||||
*/
|
||||
private void splittingMethod(AfterSettleOrderDTO afterSettleOrderDTO) {
|
||||
@@ -1556,6 +1663,9 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
|
||||
if (orderSettleAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||
}
|
||||
|
||||
// 根据站点id查询分账配置
|
||||
List<StationSplitConfig> stationSplitConfigList = stationSplitConfigService.queryByStationId(afterSettleOrderDTO.getStationId());
|
||||
|
||||
// 订单分账
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user