update 实时分账

This commit is contained in:
2023-08-25 16:07:15 +08:00
parent 18548fb83d
commit 033ddb40e7
8 changed files with 141 additions and 44 deletions

View File

@@ -99,15 +99,12 @@ public class AdapayService {
if (memberBasicInfo == null) {
throw new BusinessException(ReturnCodeEnum.CODE_GET_OPEN_ID_BY_CODE_ERROR);
}
String openId =memberBasicInfo.getOpenId();
// String openId = wxAppletRemoteService.getOpenIdByCode(dto.getCode());
// if (StringUtils.isBlank(openId)) {
// throw new BusinessException(ReturnCodeEnum.CODE_GET_OPEN_ID_BY_CODE_ERROR);
// }
String openId = memberBasicInfo.getOpenId();
// 封装对象
String amount = AdapayUtil.formatAmount(dto.getPayAmount()); // 用户支付金额
String payMode = Constants.ADAPAY_PAY_MODE_DELAY; // 汇付延时分账
// String payMode = Constants.ADAPAY_PAY_MODE_DELAY; // 汇付延时分账
String payMode = pileMerchantInfoService.getDelayModeByWechatAppId(dto.getWechatAppId());
CreateAdaPaymentParam createAdaPaymentParam = new CreateAdaPaymentParam();
createAdaPaymentParam.setOrder_no(dto.getOrderCode());
createAdaPaymentParam.setPay_amt(amount);
@@ -126,14 +123,17 @@ public class AdapayService {
createAdaPaymentParam.setExpend(JSONObject.toJSONString(ImmutableMap.of("open_id", openId)));
// 延时分账
createAdaPaymentParam.setPay_mode(payMode);
if (StringUtils.isNotBlank(payMode)) {
createAdaPaymentParam.setPay_mode(payMode);
}
try {
log.info("创建汇付支付参数:{}", JSONObject.toJSONString(createAdaPaymentParam));
Map<String, Object> response = Payment.create(BeanMap.create(createAdaPaymentParam), config.getWechatAppId());
if (response != null && !response.isEmpty()) {
JSONObject expend = JSONObject.parseObject(response.get("expend").toString());
JSONObject pay_info = expend.getJSONObject("pay_info");
Map<String, Object> resultMap = JSONObject.parseObject(pay_info.toJSONString(), new TypeReference<Map<String, Object>>() {});
Map<String, Object> resultMap = JSONObject.parseObject(pay_info.toJSONString(), new TypeReference<Map<String, Object>>() {
});
if (resultMap != null) {
// 请求参数放入缓存15分钟以内返回同一个支付参数
redisCache.setCacheObject(redisKey, resultMap, 15, TimeUnit.MINUTES);
@@ -666,7 +666,7 @@ public class AdapayService {
// 发送提现申请
Map<String, Object> settleCountParams = Maps.newHashMap();
String orderNo = "drawcash_" + dto.getMerchantId() + "_" +System.currentTimeMillis();
String orderNo = "drawcash_" + dto.getMerchantId() + "_" + System.currentTimeMillis();
settleCountParams.put("order_no", orderNo);
settleCountParams.put("cash_amt", AdapayUtil.formatAmount(cashAmt));
settleCountParams.put("member_id", adapayAccountBalanceVO.getAdapayMemberId());
@@ -710,6 +710,7 @@ public class AdapayService {
/**
* 查询取现对象
*
* @param orderNo 请求订单号
*/
public DrawCashDetailVO queryDrawCashDetail(String orderNo, String wechatAppId) throws BaseAdaPayException {
@@ -877,15 +878,30 @@ public class AdapayService {
/**
* 创建退款请求
*/
public RefundResponse createRefundRequest(String paymentId, BigDecimal refundAmt) {
// 延迟分账确认的调退款接口
public RefundResponse createRefundRequest(String paymentId, BigDecimal refundAmt, String wechatAppId, String memberId, String scenarioType, String orderCode) {
AbstractAdapayConfig config = AdapayConfigFactory.getConfig(wechatAppId);
if (config == null) {
throw new BusinessException(ReturnCodeEnum.CODE_ADAPAY_CONFIG_IS_NULL_ERROR);
}
// 实时分账的调退款接口
Map<String, Object> refundParams = Maps.newHashMap();
refundParams.put("refund_amt", AdapayUtil.formatAmount(refundAmt));
refundParams.put("refund_order_no", IdUtils.fastSimpleUUID());
// expand 为扩展域
Map<String, String> expendMap = Maps.newHashMap();
expendMap.put("memberId", memberId);
expendMap.put("scenarioType", scenarioType);
if (StringUtils.isNotBlank(orderCode)) {
expendMap.put("orderCode", orderCode);
}
refundParams.put("expand", expendMap);
refundParams.put("reason", expendMap);
refundParams.put("notify_url", ADAPAY_CALLBACK_URL);
Map<String, Object> resultResponse = null;
try {
resultResponse = Refund.create(paymentId, refundParams);
resultResponse = Refund.create(paymentId, refundParams, config.getWechatAppId());
} catch (BaseAdaPayException e) {
log.error("汇付支付创建退款对象error", e);
}
@@ -897,10 +913,10 @@ public class AdapayService {
/**
* 创建交易撤销请求
* 延迟分账未确认, 调交易撤销接口退款
*
* @param wechatAppId 微信小程序appId
*/
public PaymentReverseResponse createPaymentReverseRequest(String paymentId, BigDecimal reverseAmt, String wechatAppId, String memberId, String scenarioType, String orderCode) {
PaymentReverseResponse response;
AbstractAdapayConfig config = AdapayConfigFactory.getConfig(wechatAppId);
if (config == null) {
throw new BusinessException(ReturnCodeEnum.CODE_ADAPAY_CONFIG_IS_NULL_ERROR);

View File

@@ -95,7 +95,7 @@ public interface PileMerchantInfoMapper {
* @param appId
* @return
*/
String getFirstLevelMerchantIdByAppId(String appId);
PileMerchantInfo getFirstLevelMerchantByAppId(String appId);
/**
* 通过appid查询运营商部门id

View File

@@ -66,7 +66,9 @@ public interface IPileMerchantInfoService {
*/
public int deletePileMerchantInfoById(Long id);
/**
PileMerchantInfo getFirstLevelMerchantByAppId(String appId);
/**
* 通过微信小程序appId查询一级运营商merchantId
* @param appId 微信小程序appId
* @return 一级运营商merchantId
@@ -105,4 +107,6 @@ public interface IPileMerchantInfoService {
* @return
*/
List<String> getDeptIdsByAppId(String appId);
String getDelayModeByWechatAppId(String wechatAppId);
}

View File

@@ -12,6 +12,7 @@ import com.jsowell.adapay.dto.QueryPaymentConfirmDTO;
import com.jsowell.adapay.response.PaymentConfirmResponse;
import com.jsowell.adapay.response.PaymentReverseResponse;
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
import com.jsowell.adapay.response.RefundResponse;
import com.jsowell.adapay.service.AdapayService;
import com.jsowell.adapay.vo.OrderSettleResult;
import com.jsowell.common.constant.CacheConstants;
@@ -2338,23 +2339,21 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
BigDecimal refundAmount = dto.getRefundAmount();
BigDecimal payAmt = callbackRecord.getPayAmt();
if (refundAmount.compareTo(payAmt) > 0) {
logger.error("汇付支付订单号:{}, 退款金额:{}(元),大于可退金额{}(元), 抛出异常", dto.getOrderCode(), refundAmount, payAmt);
logger.error("汇付支付订单号:{}, 退款金额:{}(元),大于付款金额{}(元), 抛出异常", dto.getOrderCode(), refundAmount, payAmt);
throw new BusinessException(ReturnCodeEnum.CODE_REFUND_ORDER_AMOUNT_ERROR);
}
// 创建汇付退款对象 在完成初始化设置情况下,调用方法,获取 Refund对象
// TODO 拿orderCode查询清分状态
String payMode = "delay";
if (StringUtils.equals("", payMode)) {
// 延迟分账确认的调退款接口
adapayService.createRefundRequest(paymentId, refundAmount);
} else {
// 拿orderCode查询延时分账模式 延时分账的使用撤销方法退款,实时分账的使用退款方法
String expend = callbackRecord.getExpend();
JSONObject expendJsonObject = JSON.parseObject(expend);
String payMode = expendJsonObject.getString("payMode");
// String payMode = "delay";
if (StringUtils.equalsIgnoreCase(payMode, Constants.ADAPAY_PAY_MODE_DELAY)) {
// 延迟分账未确认调撤销调撤销接口退款
PaymentReverseResponse response = adapayService.createPaymentReverseRequest(
paymentId, refundAmount, dto.getWechatAppId(), dto.getMemberId(), ScenarioEnum.ORDER.getValue(),
dto.getOrderCode());
if (response != null && response.isFailed()) {
if (response != null && response.isNotFailed()) {
MemberAdapayRecord record = memberAdapayRecordService.selectByPaymentId(paymentId);
BigDecimal reverseAmt = new BigDecimal(response.getReverse_amt());
// 更新此笔交易单的消费金额 = 支付金额 - 撤销金额
@@ -2370,7 +2369,52 @@ public class OrderBasicInfoServiceImpl implements IOrderBasicInfoService {
}
memberAdapayRecordService.updateByPrimaryKeySelective(record);
}
} else {
// 实时分账的调退款接口
RefundResponse refundRequest = adapayService.createRefundRequest(paymentId, refundAmount, dto.getWechatAppId(), dto.getMemberId(), ScenarioEnum.ORDER.getValue(),
dto.getOrderCode());
if (refundRequest != null && refundRequest.isNotFailed()) {
MemberAdapayRecord record = memberAdapayRecordService.selectByPaymentId(paymentId);
BigDecimal refundAmt = new BigDecimal(refundRequest.getRefund_amt());
// 更新此笔交易单的消费金额 = 支付金额 - 撤销金额
BigDecimal spendAmt = callbackRecord.getPayAmt().subtract(refundAmt);
record.setSpendAmt(spendAmt);
// 退款金额
record.setRefundAmt(refundAmt);
// 更新此笔交易单的剩余金额 = 支付金额 - 累计退款金额 - 累计消费金额
record.setBalanceAmt(record.getPayAmt().subtract(record.getRefundAmt()).subtract(record.getSpendAmt()));
if (BigDecimal.ZERO.compareTo(record.getBalanceAmt()) != 0) {
logger.error("订单分账结束后账不平paymentId:{}, orderCode:{}, 支付金额:{}, 消费金额:{}, 退款金额:{}",
paymentId, dto.getOrderCode(), payAmt, spendAmt, refundAmt);
}
memberAdapayRecordService.updateByPrimaryKeySelective(record);
}
}
// if (StringUtils.equals("", payMode)) {
// // 延迟分账确认的调退款接口
// adapayService.createRefundRequest(paymentId, refundAmount);
// } else {
// // 延迟分账未确认调撤销调撤销接口退款
// PaymentReverseResponse response = adapayService.createPaymentReverseRequest(
// paymentId, refundAmount, dto.getWechatAppId(), dto.getMemberId(), ScenarioEnum.ORDER.getValue(),
// dto.getOrderCode());
// if (response != null && response.isFailed()) {
// MemberAdapayRecord record = memberAdapayRecordService.selectByPaymentId(paymentId);
// BigDecimal reverseAmt = new BigDecimal(response.getReverse_amt());
// // 更新此笔交易单的消费金额 = 支付金额 - 撤销金额
// BigDecimal spendAmt = callbackRecord.getPayAmt().subtract(reverseAmt);
// record.setSpendAmt(spendAmt);
// // 退款金额
// record.setRefundAmt(reverseAmt);
// // 更新此笔交易单的剩余金额 = 支付金额 - 累计退款金额 - 累计消费金额
// record.setBalanceAmt(record.getPayAmt().subtract(record.getRefundAmt()).subtract(record.getSpendAmt()));
// if (BigDecimal.ZERO.compareTo(record.getBalanceAmt()) != 0) {
// logger.error("订单分账结束后账不平paymentId:{}, orderCode:{}, 支付金额:{}, 消费金额:{}, 退款金额:{}",
// paymentId, dto.getOrderCode(), payAmt, spendAmt, reverseAmt);
// }
// memberAdapayRecordService.updateByPrimaryKeySelective(record);
// }
// }
}
@Override

View File

@@ -260,7 +260,19 @@ public class PileMerchantInfoServiceImpl implements IPileMerchantInfoService {
// 逻辑删除部门
sysDeptService.deleteDeptById(Long.parseLong(deptId));
}
}
/**
* 通过微信小程序appId查询一级运营商merchant对象
* @param appId 微信小程序appId
* @return 一级运营商merchant对象
*/
@Override
public PileMerchantInfo getFirstLevelMerchantByAppId(String appId) {
if (StringUtils.isBlank(appId)) {
return null;
}
return pileMerchantInfoMapper.getFirstLevelMerchantByAppId(appId);
}
/**
@@ -270,18 +282,12 @@ public class PileMerchantInfoServiceImpl implements IPileMerchantInfoService {
*/
@Override
public String getFirstLevelMerchantIdByAppId(String appId) {
if (StringUtils.isBlank(appId)) {
return null;
String merchantId = null;
PileMerchantInfo merchant = getFirstLevelMerchantByAppId(appId);
if (Objects.nonNull(merchant)) {
merchantId = String.valueOf(merchant.getId());
}
try {
String merchantId = pileMerchantInfoMapper.getFirstLevelMerchantIdByAppId(appId);
if (StringUtils.isNotBlank(merchantId)) {
return merchantId;
}
} catch (Exception e) {
log.error("通过appid获取运营商id error", e);
}
return null;
return merchantId;
}
@Override
@@ -327,10 +333,6 @@ public class PileMerchantInfoServiceImpl implements IPileMerchantInfoService {
return pileMerchantInfoMapper.queryInfoListByIds(deptIds);
}
public List<String> getLoginUserMerchantIds() {
AuthorizedDeptVO authorizedMap = SecurityUtils.getAuthorizedMap();
return null;
}
/**
* 查询一级运营商列表
@@ -388,5 +390,21 @@ public class PileMerchantInfoServiceImpl implements IPileMerchantInfoService {
return pileMerchantInfoMapper.getDeptIdsByAppId(appId);
}
/**
* 通过wechatAppId查询一级运营商的延时支付模式配置
* @param wechatAppId 微信小程序id
* @return
*/
@Override
public String getDelayModeByWechatAppId(String wechatAppId) {
PileMerchantInfo merchant = getFirstLevelMerchantByAppId(wechatAppId);
if (merchant != null) {
String delayMode = merchant.getDelayMode();
if (StringUtils.equals(delayMode, Constants.ONE)) {
return Constants.ADAPAY_PAY_MODE_DELAY;
}
}
return null;
}
}