mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 19:45:09 +08:00
优化支付订单逻辑
This commit is contained in:
@@ -150,7 +150,6 @@ public class OrderService {
|
||||
* @param dto
|
||||
*/
|
||||
public Map<String, Object> payOrder(PayOrderDTO dto) throws Exception {
|
||||
Map<String, Object> resultMap = Maps.newHashMap();
|
||||
OrderBasicInfo orderInfo = orderBasicInfoService.getOrderInfoByOrderCode(dto.getOrderCode());
|
||||
if (orderInfo == null) {
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_QUERY_ORDER_NULL_ERROR);
|
||||
@@ -159,118 +158,150 @@ public class OrderService {
|
||||
// 订单已支付
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_ORDER_IS_NOT_TO_BE_PAID_ERROR);
|
||||
}
|
||||
String orderCode = orderInfo.getOrderCode();
|
||||
// 该订单充电金额
|
||||
BigDecimal chargeAmount = dto.getPayAmount();
|
||||
// 记录支付流水
|
||||
List<OrderPayRecord> payRecordList = Lists.newArrayList();
|
||||
if (StringUtils.equals(dto.getPayMode(), OrderPayModeEnum.PAYMENT_OF_BALANCE.getValue())) { // 余额支付
|
||||
// 查询该会员的余额
|
||||
MemberVO memberVO = memberService.getMemberInfoByMemberId(dto.getMemberId());
|
||||
BigDecimal totalAccountAmount = memberVO.getTotalAccountAmount();
|
||||
|
||||
if (totalAccountAmount.compareTo(chargeAmount) < 0) {
|
||||
// 总余额小于充电金额
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_BALANCE_IS_INSUFFICIENT);
|
||||
}
|
||||
BigDecimal principalAmount = memberVO.getPrincipalBalance(); // 会员剩余本金金额
|
||||
BigDecimal giftAmount = memberVO.getGiftBalance(); // 会员剩余赠送余额
|
||||
|
||||
BigDecimal principalPay = null; // 30
|
||||
BigDecimal giftPay = null; // 10
|
||||
// 先扣除本金金额,再扣除赠送金额
|
||||
BigDecimal subtract = principalAmount.subtract(chargeAmount);
|
||||
if (subtract.compareTo(BigDecimal.ZERO) >= 0) {
|
||||
principalPay = chargeAmount;
|
||||
} else {
|
||||
if (principalAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||
principalPay = principalAmount;
|
||||
}
|
||||
giftPay = subtract.negate();
|
||||
}
|
||||
|
||||
// 更新会员钱包
|
||||
UpdateMemberBalanceDTO updateMemberBalanceDTO = UpdateMemberBalanceDTO.builder()
|
||||
.memberId(dto.getMemberId())
|
||||
.type(MemberWalletEnum.TYPE_OUT.getValue())
|
||||
.subType(MemberWalletEnum.SUBTYPE_PAYMENT_FOR_ORDER.getValue())
|
||||
.updatePrincipalBalance(principalPay)
|
||||
.updateGiftBalance(giftPay)
|
||||
.relatedOrderCode(orderCode)
|
||||
.build();
|
||||
memberBasicInfoService.updateMemberBalance(updateMemberBalanceDTO);
|
||||
|
||||
// 记录流水
|
||||
if (principalPay != null) {
|
||||
payRecordList.add(OrderPayRecord.builder()
|
||||
.orderCode(dto.getOrderCode())
|
||||
.payMode(OrderPayRecordEnum.PRINCIPAL_BALANCE_PAYMENT.getValue())
|
||||
.payAmount(principalPay)
|
||||
.createBy(dto.getMemberId())
|
||||
.build());
|
||||
}
|
||||
if (giftPay != null) {
|
||||
payRecordList.add(OrderPayRecord.builder()
|
||||
.orderCode(dto.getOrderCode())
|
||||
.payMode(OrderPayRecordEnum.GIFT_BALANCE_PAYMENT.getValue())
|
||||
.payAmount(giftPay)
|
||||
.createBy(dto.getMemberId())
|
||||
.build());
|
||||
}
|
||||
// 余额支付可以直接调支付回调方法
|
||||
PayOrderSuccessCallbackDTO callbackDTO = PayOrderSuccessCallbackDTO.builder()
|
||||
.orderCode(orderCode)
|
||||
.payAmount(chargeAmount)
|
||||
.payMode(dto.getPayMode())
|
||||
.build();
|
||||
payOrderSuccessCallback(callbackDTO);
|
||||
|
||||
// 余额支付订单 记录会员交易流水
|
||||
MemberTransactionRecord record = MemberTransactionRecord.builder()
|
||||
.orderCode(orderCode)
|
||||
.scenarioType(ScenarioEnum.ORDER.getValue())
|
||||
.memberId(memberVO.getMemberId())
|
||||
.actionType(ActionTypeEnum.FORWARD.getValue())
|
||||
.payMode(PayModeEnum.PAYMENT_OF_BALANCE.getValue())
|
||||
.amount(dto.getPayAmount()) // 单位元
|
||||
.build();
|
||||
memberTransactionRecordService.insertSelective(record);
|
||||
} else if (StringUtils.equals(dto.getPayMode(), OrderPayModeEnum.PAYMENT_OF_WECHATPAY.getValue())) { // 微信支付
|
||||
String openId = memberService.getOpenIdByCode(dto.getCode());
|
||||
if (StringUtils.isBlank(openId)) {
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_GET_OPEN_ID_BY_CODE_ERROR);
|
||||
}
|
||||
WeixinPayDTO weixinPayDTO = new WeixinPayDTO();
|
||||
weixinPayDTO.setOpenId(openId);
|
||||
weixinPayDTO.setAmount(dto.getPayAmount().toString());
|
||||
// 支付订单 附加参数
|
||||
PaymentScenarioDTO paymentScenarioDTO = PaymentScenarioDTO.builder()
|
||||
.type(ScenarioEnum.ORDER.getValue())
|
||||
.orderCode(dto.getOrderCode())
|
||||
.memberId(orderInfo.getMemberId())
|
||||
.build();
|
||||
weixinPayDTO.setAttach(JSONObject.toJSONString(paymentScenarioDTO));
|
||||
weixinPayDTO.setDescription("充电费用");
|
||||
Map<String, Object> weixinMap = this.weixinPayV3(weixinPayDTO);
|
||||
Map<String, Object> resultMap = Maps.newHashMap();
|
||||
if (StringUtils.equals(dto.getPayMode(), OrderPayModeEnum.PAYMENT_OF_BALANCE.getValue())) {
|
||||
// 余额支付
|
||||
balancePayOrder(dto);
|
||||
} else if (StringUtils.equals(dto.getPayMode(), OrderPayModeEnum.PAYMENT_OF_WECHATPAY.getValue())) {
|
||||
// 微信支付
|
||||
Map<String, Object> weixinMap = wechatPayOrder(dto, orderInfo);
|
||||
// 返回微信支付参数
|
||||
resultMap.put("weixinMap", weixinMap);
|
||||
} else if (StringUtils.equals(dto.getPayMode(), OrderPayModeEnum.PAYMENT_OF_ALIPAY.getValue())) { // 支付宝支付
|
||||
// TODO 返回支付宝支付参数
|
||||
} else if (StringUtils.equals(dto.getPayMode(), OrderPayModeEnum.PAYMENT_OF_WHITELIST.getValue())) { // 白名单支付
|
||||
// 白名单支付可以直接调支付回调方法
|
||||
PayOrderSuccessCallbackDTO callbackDTO = PayOrderSuccessCallbackDTO.builder()
|
||||
.orderCode(orderCode)
|
||||
.payAmount(dto.getPayAmount())
|
||||
.payMode(dto.getPayMode())
|
||||
.build();
|
||||
payOrderSuccessCallback(callbackDTO);
|
||||
whiteListPayOrder(dto);
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 白名单支付订单逻辑
|
||||
* @param dto
|
||||
*/
|
||||
private void whiteListPayOrder(PayOrderDTO dto) {
|
||||
String orderCode = dto.getOrderCode();
|
||||
BigDecimal payAmount = dto.getPayAmount();
|
||||
String payMode = dto.getPayMode();
|
||||
PayOrderSuccessCallbackDTO callbackDTO = PayOrderSuccessCallbackDTO.builder()
|
||||
.orderCode(orderCode)
|
||||
.payAmount(payAmount)
|
||||
.payMode(payMode)
|
||||
.build();
|
||||
payOrderSuccessCallback(callbackDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付订单逻辑 获取支付参数
|
||||
* @param dto
|
||||
* @param orderInfo
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private Map<String, Object> wechatPayOrder(PayOrderDTO dto, OrderBasicInfo orderInfo) throws Exception {
|
||||
String openId = memberService.getOpenIdByCode(dto.getCode());
|
||||
if (StringUtils.isBlank(openId)) {
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_GET_OPEN_ID_BY_CODE_ERROR);
|
||||
}
|
||||
WeixinPayDTO weixinPayDTO = new WeixinPayDTO();
|
||||
weixinPayDTO.setOpenId(openId);
|
||||
weixinPayDTO.setAmount(dto.getPayAmount().toString());
|
||||
// 支付订单 附加参数
|
||||
PaymentScenarioDTO paymentScenarioDTO = PaymentScenarioDTO.builder()
|
||||
.type(ScenarioEnum.ORDER.getValue())
|
||||
.orderCode(dto.getOrderCode())
|
||||
.memberId(orderInfo.getMemberId())
|
||||
.build();
|
||||
weixinPayDTO.setAttach(JSONObject.toJSONString(paymentScenarioDTO));
|
||||
weixinPayDTO.setDescription("充电费用");
|
||||
return this.weixinPayV3(weixinPayDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 余额支付订单逻辑
|
||||
* @param dto
|
||||
*/
|
||||
private void balancePayOrder(PayOrderDTO dto) {
|
||||
// 记录支付流水
|
||||
List<OrderPayRecord> payRecordList = Lists.newArrayList();
|
||||
String orderCode = dto.getOrderCode();
|
||||
BigDecimal chargeAmount = dto.getPayAmount();
|
||||
// 查询该会员的余额
|
||||
MemberVO memberVO = memberService.getMemberInfoByMemberId(dto.getMemberId());
|
||||
BigDecimal totalAccountAmount = memberVO.getTotalAccountAmount();
|
||||
|
||||
if (totalAccountAmount.compareTo(chargeAmount) < 0) {
|
||||
// 总余额小于充电金额
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_BALANCE_IS_INSUFFICIENT);
|
||||
}
|
||||
BigDecimal principalAmount = memberVO.getPrincipalBalance(); // 会员剩余本金金额
|
||||
BigDecimal giftAmount = memberVO.getGiftBalance(); // 会员剩余赠送余额
|
||||
|
||||
BigDecimal principalPay = null; // 30
|
||||
BigDecimal giftPay = null; // 10
|
||||
// 先扣除本金金额,再扣除赠送金额
|
||||
BigDecimal subtract = principalAmount.subtract(chargeAmount);
|
||||
if (subtract.compareTo(BigDecimal.ZERO) >= 0) {
|
||||
principalPay = chargeAmount;
|
||||
} else {
|
||||
if (principalAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||
principalPay = principalAmount;
|
||||
}
|
||||
giftPay = subtract.negate();
|
||||
}
|
||||
|
||||
// 更新会员钱包
|
||||
UpdateMemberBalanceDTO updateMemberBalanceDTO = UpdateMemberBalanceDTO.builder()
|
||||
.memberId(dto.getMemberId())
|
||||
.type(MemberWalletEnum.TYPE_OUT.getValue())
|
||||
.subType(MemberWalletEnum.SUBTYPE_PAYMENT_FOR_ORDER.getValue())
|
||||
.updatePrincipalBalance(principalPay)
|
||||
.updateGiftBalance(giftPay)
|
||||
.relatedOrderCode(orderCode)
|
||||
.build();
|
||||
memberBasicInfoService.updateMemberBalance(updateMemberBalanceDTO);
|
||||
|
||||
// 记录流水
|
||||
if (principalPay != null) {
|
||||
payRecordList.add(OrderPayRecord.builder()
|
||||
.orderCode(orderCode)
|
||||
.payMode(OrderPayRecordEnum.PRINCIPAL_BALANCE_PAYMENT.getValue())
|
||||
.payAmount(principalPay)
|
||||
.createBy(dto.getMemberId())
|
||||
.build());
|
||||
}
|
||||
if (giftPay != null) {
|
||||
payRecordList.add(OrderPayRecord.builder()
|
||||
.orderCode(orderCode)
|
||||
.payMode(OrderPayRecordEnum.GIFT_BALANCE_PAYMENT.getValue())
|
||||
.payAmount(giftPay)
|
||||
.createBy(dto.getMemberId())
|
||||
.build());
|
||||
}
|
||||
// 余额支付可以直接调支付回调方法
|
||||
PayOrderSuccessCallbackDTO callbackDTO = PayOrderSuccessCallbackDTO.builder()
|
||||
.orderCode(orderCode)
|
||||
.payAmount(chargeAmount)
|
||||
.payMode(dto.getPayMode())
|
||||
.build();
|
||||
payOrderSuccessCallback(callbackDTO);
|
||||
|
||||
// 余额支付订单 记录会员交易流水
|
||||
MemberTransactionRecord record = MemberTransactionRecord.builder()
|
||||
.orderCode(orderCode)
|
||||
.scenarioType(ScenarioEnum.ORDER.getValue())
|
||||
.memberId(memberVO.getMemberId())
|
||||
.actionType(ActionTypeEnum.FORWARD.getValue())
|
||||
.payMode(PayModeEnum.PAYMENT_OF_BALANCE.getValue())
|
||||
.amount(dto.getPayAmount()) // 单位元
|
||||
.build();
|
||||
memberTransactionRecordService.insertSelective(record);
|
||||
|
||||
// 订单支付流水入库
|
||||
if (CollectionUtils.isNotEmpty(payRecordList)) {
|
||||
orderPayRecordService.batchInsert(payRecordList);
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user