mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-05-17 00:08:35 +08:00
订单分账逻辑
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package com.jsowell.adapay.operation;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 汇付相关操作需要的信息
|
||||||
|
* Operation
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class AdapayOperationInfo {
|
||||||
|
/**
|
||||||
|
* 分账用户 Member对象 的 id;若是商户本身时,传入0
|
||||||
|
*/
|
||||||
|
private String adapayMemberId;
|
||||||
|
|
||||||
|
// 商户唯一标识,通常使用微信小程序appId
|
||||||
|
private String merchantKey;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.jsowell.adapay.operation;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支付确认操作对象
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class PaymentConfirmOperation extends AdapayOperationInfo{
|
||||||
|
private String paymentId;
|
||||||
|
|
||||||
|
private BigDecimal confirmAmt;
|
||||||
|
|
||||||
|
private String orderCode;
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ import com.jsowell.adapay.common.DivMember;
|
|||||||
import com.jsowell.adapay.config.AbstractAdapayConfig;
|
import com.jsowell.adapay.config.AbstractAdapayConfig;
|
||||||
import com.jsowell.adapay.dto.*;
|
import com.jsowell.adapay.dto.*;
|
||||||
import com.jsowell.adapay.factory.AdapayConfigFactory;
|
import com.jsowell.adapay.factory.AdapayConfigFactory;
|
||||||
|
import com.jsowell.adapay.operation.PaymentConfirmOperation;
|
||||||
import com.jsowell.adapay.response.*;
|
import com.jsowell.adapay.response.*;
|
||||||
import com.jsowell.adapay.vo.*;
|
import com.jsowell.adapay.vo.*;
|
||||||
import com.jsowell.common.constant.CacheConstants;
|
import com.jsowell.common.constant.CacheConstants;
|
||||||
@@ -785,13 +786,51 @@ public class AdapayService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建交易确认请求/创建分账请求
|
||||||
|
* 这个方法只适用于给一个用户分账
|
||||||
|
*/
|
||||||
|
public PaymentConfirmResponse createPaymentConfirmRequest(PaymentConfirmOperation operation) {
|
||||||
|
// 调汇付的分账接口 确认交易
|
||||||
|
Map<String, Object> confirmParams = Maps.newHashMap();
|
||||||
|
// Adapay生成的支付对象id
|
||||||
|
confirmParams.put("payment_id", operation.getPaymentId());
|
||||||
|
// 请求订单号,只能为英文、数字或者下划线的一种或多种组合,保证在app_id下唯一
|
||||||
|
confirmParams.put("order_no", "PC" + System.currentTimeMillis());
|
||||||
|
// 确认金额,必须大于0,保留两位小数点,如0.10、100.05等。必须小于等于原支付金额-已确认金额-已撤销金额。
|
||||||
|
String amount = AdapayUtil.formatAmount(operation.getConfirmAmt());
|
||||||
|
confirmParams.put("confirm_amt", amount);
|
||||||
|
|
||||||
|
// 附加说明
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put("orderCode", operation.getOrderCode());
|
||||||
|
jsonObject.put("adapayMemberId", operation.getAdapayMemberId());
|
||||||
|
confirmParams.put("description", jsonObject.toJSONString());
|
||||||
|
|
||||||
|
// 分账对象信息 一次最多7个
|
||||||
|
DivMember divMember = new DivMember();
|
||||||
|
divMember.setMember_id(operation.getAdapayMemberId());
|
||||||
|
divMember.setAmount(amount);
|
||||||
|
divMember.setFee_flag(Constants.Y);
|
||||||
|
confirmParams.put("div_members", Lists.newArrayList(divMember));
|
||||||
|
|
||||||
|
Map<String, Object> paymentConfirm = null;
|
||||||
|
try {
|
||||||
|
paymentConfirm = PaymentConfirm.create(confirmParams, operation.getMerchantKey());
|
||||||
|
} catch (BaseAdaPayException e) {
|
||||||
|
log.error("创建交易确认请求error", e);
|
||||||
|
}
|
||||||
|
String jsonString = JSON.toJSONString(paymentConfirm);
|
||||||
|
log.info("调分账接口param:{}, result:{}", JSON.toJSONString(confirmParams), jsonString);
|
||||||
|
return JSONObject.parseObject(jsonString, PaymentConfirmResponse.class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建交易确认请求/创建分账请求
|
* 创建交易确认请求/创建分账请求
|
||||||
* 这个方法只适用于给一个用户分账
|
* 这个方法只适用于给一个用户分账
|
||||||
*
|
*
|
||||||
* @param paymentId 支付对象id
|
* @param paymentId 支付对象id
|
||||||
* @param adapayMemberAccount 收到该张的汇付会员信息
|
* @param adapayMemberAccount 收到该账的汇付会员信息
|
||||||
* @param confirmAmt 确认的金额
|
* @param confirmAmt 确认的金额
|
||||||
* @param orderCode 订单编号
|
* @param orderCode 订单编号
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import com.google.common.collect.Maps;
|
|||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
import com.huifu.adapay.core.exception.BaseAdaPayException;
|
import com.huifu.adapay.core.exception.BaseAdaPayException;
|
||||||
import com.jsowell.adapay.dto.QueryPaymentConfirmDTO;
|
import com.jsowell.adapay.dto.QueryPaymentConfirmDTO;
|
||||||
|
import com.jsowell.adapay.operation.PaymentConfirmOperation;
|
||||||
import com.jsowell.adapay.response.PaymentConfirmResponse;
|
import com.jsowell.adapay.response.PaymentConfirmResponse;
|
||||||
import com.jsowell.adapay.response.PaymentReverseResponse;
|
import com.jsowell.adapay.response.PaymentReverseResponse;
|
||||||
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
|
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
|
||||||
@@ -761,17 +762,11 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
*/
|
*/
|
||||||
OrderSettleResult orderSettleResult = null;
|
OrderSettleResult orderSettleResult = null;
|
||||||
if (StringUtils.equals(delayMode, Constants.ADAPAY_PAY_MODE_DELAY)) {
|
if (StringUtils.equals(delayMode, Constants.ADAPAY_PAY_MODE_DELAY)) {
|
||||||
// 延时分账的处理逻辑
|
// 延迟商家订单处理逻辑
|
||||||
if (OrderPayModeEnum.PAYMENT_OF_BALANCE.getValue().equals(orderBasicInfo.getPayMode())) {
|
orderSettleResult = delayMerchantOrderProcessingLogic(orderBasicInfo, adapayMemberAccount, appId);
|
||||||
// 余额支付的订单
|
|
||||||
orderSettleResult = doBalancePaymentWithDelay(orderBasicInfo, adapayMemberAccount, appId);
|
|
||||||
} else {
|
|
||||||
// 在线支付,进行支付确认分账
|
|
||||||
orderSettleResult = doPaymentConfirmWithDelay(orderBasicInfo, adapayMemberAccount, appId);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 不使用延时分账的汇付商户,钱已经到基本账户中了,只需要退款就可以
|
// 不延迟商家订单处理逻辑 不使用延时分账的汇付商户,钱已经到基本账户中了,只需要退款就可以
|
||||||
orderSettleResult = notDelayOrder(orderBasicInfo, adapayMemberAccount, appId);
|
orderSettleResult = notDelayMerchantOrderProcessingLogic(orderBasicInfo, adapayMemberAccount, appId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 退款逻辑/需要退款的情况
|
// 退款逻辑/需要退款的情况
|
||||||
@@ -789,16 +784,41 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param orderBasicInfo
|
* 不延迟商家订单处理逻辑
|
||||||
* @param adapayMemberAccount
|
* 例如:希晓
|
||||||
* @param wechatAppId
|
*
|
||||||
|
* @param orderBasicInfo 订单信息
|
||||||
|
* @param adapayMemberAccount 汇付用户信息
|
||||||
|
* @param wechatAppId 小程序appId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private OrderSettleResult notDelayOrder(OrderBasicInfo orderBasicInfo, AdapayMemberAccount adapayMemberAccount, String wechatAppId) {
|
private OrderSettleResult notDelayMerchantOrderProcessingLogic(OrderBasicInfo orderBasicInfo, AdapayMemberAccount adapayMemberAccount, String wechatAppId) {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 延迟商家订单处理逻辑
|
||||||
|
* 例如:举视以及下面的二级运营商
|
||||||
|
*
|
||||||
|
* @param orderBasicInfo 订单信息
|
||||||
|
* @param adapayMemberAccount 汇付用户信息
|
||||||
|
* @param wechatAppId 小程序appId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private OrderSettleResult delayMerchantOrderProcessingLogic(OrderBasicInfo orderBasicInfo, AdapayMemberAccount adapayMemberAccount, String wechatAppId) {
|
||||||
|
OrderSettleResult orderSettleResult;
|
||||||
|
// 延时分账的处理逻辑
|
||||||
|
if (OrderPayModeEnum.PAYMENT_OF_BALANCE.getValue().equals(orderBasicInfo.getPayMode())) {
|
||||||
|
// 余额支付的订单
|
||||||
|
orderSettleResult = doBalancePaymentWithDelay(orderBasicInfo, adapayMemberAccount, wechatAppId);
|
||||||
|
} else {
|
||||||
|
// 在线支付,进行支付确认分账
|
||||||
|
orderSettleResult = doPaymentConfirmWithDelay(orderBasicInfo, adapayMemberAccount, wechatAppId);
|
||||||
|
}
|
||||||
|
return orderSettleResult;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回更新后的OrderBasicInfo对象
|
* 返回更新后的OrderBasicInfo对象
|
||||||
* 专用方法,其他地方如果要用请仔细检查
|
* 专用方法,其他地方如果要用请仔细检查
|
||||||
@@ -1255,12 +1275,14 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public OrderSettleResult doBalancePaymentWithDelay(OrderBasicInfo orderBasicInfo, AdapayMemberAccount adapayMemberAccount, String wechatAppId) {
|
public OrderSettleResult doBalancePaymentWithDelay(OrderBasicInfo orderBasicInfo, AdapayMemberAccount adapayMemberAccount, String wechatAppId) {
|
||||||
|
// 订单结算金额
|
||||||
BigDecimal settleAmount = orderBasicInfo.getSettleAmount();
|
BigDecimal settleAmount = orderBasicInfo.getSettleAmount();
|
||||||
|
// 订单编号
|
||||||
String orderCode = orderBasicInfo.getOrderCode();
|
String orderCode = orderBasicInfo.getOrderCode();
|
||||||
|
|
||||||
BigDecimal confirmAmt = BigDecimal.ZERO;
|
BigDecimal confirmAmt = BigDecimal.ZERO; // 消费金额
|
||||||
BigDecimal feeAmt = BigDecimal.ZERO;
|
BigDecimal feeAmt = BigDecimal.ZERO; // 手续费金额
|
||||||
String status = null;
|
String status = null; // 状态
|
||||||
|
|
||||||
// 获取余额支付扣除金额 也许存在一笔不够扣,需要扣多笔的情况
|
// 获取余额支付扣除金额 也许存在一笔不够扣,需要扣多笔的情况
|
||||||
List<BalanceDeductionAmountVO> list = calculateTheBalanceDeductionAmount(orderBasicInfo.getMemberId(), settleAmount);
|
List<BalanceDeductionAmountVO> list = calculateTheBalanceDeductionAmount(orderBasicInfo.getMemberId(), settleAmount);
|
||||||
@@ -1269,9 +1291,17 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
BigDecimal deductionAmount = vo.getDeductionAmount();
|
BigDecimal deductionAmount = vo.getDeductionAmount();
|
||||||
|
|
||||||
// 延时分账,使用确认交易API
|
// 延时分账,使用确认交易API
|
||||||
PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, deductionAmount, orderCode, wechatAppId);
|
PaymentConfirmOperation operation = new PaymentConfirmOperation();
|
||||||
|
operation.setPaymentId(paymentId);
|
||||||
|
operation.setConfirmAmt(deductionAmount);
|
||||||
|
operation.setOrderCode(orderCode);
|
||||||
|
operation.setAdapayMemberId(adapayMemberAccount.getAdapayMemberId());
|
||||||
|
operation.setMerchantKey(wechatAppId);
|
||||||
|
PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(operation);
|
||||||
|
|
||||||
|
// PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, deductionAmount, orderCode, wechatAppId);
|
||||||
if (paymentConfirmResponse != null && paymentConfirmResponse.isNotFailed()) {
|
if (paymentConfirmResponse != null && paymentConfirmResponse.isNotFailed()) {
|
||||||
confirmAmt = confirmAmt.add(new BigDecimal(paymentConfirmResponse.getConfirmed_amt()));
|
confirmAmt = confirmAmt.add(new BigDecimal(paymentConfirmResponse.getConfirm_amt()));
|
||||||
feeAmt = feeAmt.add(new BigDecimal(paymentConfirmResponse.getFee_amt()));
|
feeAmt = feeAmt.add(new BigDecimal(paymentConfirmResponse.getFee_amt()));
|
||||||
status = paymentConfirmResponse.getStatus();
|
status = paymentConfirmResponse.getStatus();
|
||||||
memberAdapayRecordService.updateSpendAmount(paymentId, deductionAmount);
|
memberAdapayRecordService.updateSpendAmount(paymentId, deductionAmount);
|
||||||
@@ -1347,27 +1377,36 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
|
|||||||
* @param adapayMemberAccount 结算账户
|
* @param adapayMemberAccount 结算账户
|
||||||
*/
|
*/
|
||||||
public OrderSettleResult doPaymentConfirmWithDelay(OrderBasicInfo orderBasicInfo, AdapayMemberAccount adapayMemberAccount, String wechatAppId) {
|
public OrderSettleResult doPaymentConfirmWithDelay(OrderBasicInfo orderBasicInfo, AdapayMemberAccount adapayMemberAccount, String wechatAppId) {
|
||||||
// 查询订单的交易id
|
|
||||||
String orderCode = orderBasicInfo.getOrderCode();
|
String orderCode = orderBasicInfo.getOrderCode();
|
||||||
|
// 查询该笔订单的支付交易回调
|
||||||
AdapayCallbackRecord adapayCallbackRecord = adapayCallbackRecordService.selectByOrderCode(orderCode);
|
AdapayCallbackRecord adapayCallbackRecord = adapayCallbackRecordService.selectByOrderCode(orderCode);
|
||||||
if (adapayCallbackRecord == null) {
|
if (adapayCallbackRecord == null) {
|
||||||
logger.error("根据订单号:{}, 未查询到汇付支付回调信息", orderCode);
|
logger.error("根据订单号:{}, 未查询到汇付支付回调信息", orderCode);
|
||||||
throw new BusinessException(ReturnCodeEnum.CODE_ADAPAY_CALLBACK_IS_NULL_ERROR);
|
throw new BusinessException(ReturnCodeEnum.CODE_ADAPAY_CALLBACK_IS_NULL_ERROR);
|
||||||
}
|
}
|
||||||
|
// 支付id
|
||||||
String paymentId = adapayCallbackRecord.getPaymentId();
|
String paymentId = adapayCallbackRecord.getPaymentId();
|
||||||
|
|
||||||
// 金额
|
// 订单结算金额
|
||||||
BigDecimal settleAmount = orderBasicInfo.getSettleAmount();
|
BigDecimal settleAmount = orderBasicInfo.getSettleAmount();
|
||||||
|
|
||||||
// 调汇付的分账接口 确认交易
|
// 调汇付的分账接口 确认交易
|
||||||
PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, settleAmount, orderCode, wechatAppId);
|
// PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, settleAmount, orderCode, wechatAppId);
|
||||||
|
|
||||||
|
PaymentConfirmOperation operation = new PaymentConfirmOperation();
|
||||||
|
operation.setPaymentId(paymentId);
|
||||||
|
operation.setConfirmAmt(settleAmount);
|
||||||
|
operation.setOrderCode(orderCode);
|
||||||
|
operation.setAdapayMemberId(adapayMemberAccount.getAdapayMemberId());
|
||||||
|
operation.setMerchantKey(wechatAppId);
|
||||||
|
PaymentConfirmResponse paymentConfirmResponse = adapayService.createPaymentConfirmRequest(operation);
|
||||||
|
|
||||||
|
|
||||||
// 分账接口返回的信息
|
// 分账接口返回的信息
|
||||||
OrderSettleResult result = new OrderSettleResult();
|
OrderSettleResult result = new OrderSettleResult();
|
||||||
result.setConfirmAmt(paymentConfirmResponse.getConfirm_amt());
|
result.setConfirmAmt(paymentConfirmResponse.getConfirm_amt());
|
||||||
result.setStatus(paymentConfirmResponse.getStatus());
|
result.setStatus(paymentConfirmResponse.getStatus());
|
||||||
result.setFeeAmt(paymentConfirmResponse.getFee_amt());
|
result.setFeeAmt(paymentConfirmResponse.getFee_amt());
|
||||||
// result.setDescription(paymentConfirmResponse.getDescription());
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user