支付宝支付调整
This commit is contained in:
parent
84bbf53ea4
commit
49f52d0845
|
|
@ -0,0 +1,66 @@
|
|||
package com.ghy.callback;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ghy.common.adapay.model.AdapayStatusEnum;
|
||||
import com.ghy.common.adapay.model.Event;
|
||||
import com.ghy.common.adapay.model.PaymentDTO;
|
||||
import com.ghy.common.utils.MoneyUtil;
|
||||
import com.ghy.payment.domain.PaymentConfirm;
|
||||
import com.ghy.payment.mapper.PaymentConfirmMapper;
|
||||
import com.ghy.payment.mapper.PaymentMapper;
|
||||
import com.ghy.payment.service.CallBackService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Service("paymentConfirmCallbackService")
|
||||
public class PaymentConfirmCallbackService implements CallBackService {
|
||||
|
||||
@Resource
|
||||
private PaymentMapper paymentMapper;
|
||||
@Resource
|
||||
private PaymentConfirmMapper paymentConfirmMapper;
|
||||
|
||||
@Override
|
||||
public void onCallback(Event event) {
|
||||
// 支付确认没有callback
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
if (AdapayStatusEnum.succeeded.code.equals(response.getString("status")) ||
|
||||
AdapayStatusEnum.pending.code.equals(response.getString("status"))) {
|
||||
// 支付确认 操作成功
|
||||
PaymentConfirm paymentConfirm = response.toJavaObject(PaymentConfirm.class);
|
||||
// 保存支付确认记录
|
||||
paymentConfirmMapper.insert(paymentConfirm);
|
||||
// 更新支付单信息
|
||||
updatePayment(paymentConfirm);
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void updatePayment(PaymentConfirm paymentConfirm) {
|
||||
String paymentId = paymentConfirm.getPaymentId();
|
||||
PaymentDTO payment = paymentMapper.selectById(paymentId);
|
||||
// 支付金额
|
||||
BigDecimal payAmt = new BigDecimal(payment.getPayAmt());
|
||||
// 已确认金额
|
||||
BigDecimal confirmedAmt = new BigDecimal(paymentConfirm.getConfirmedAmt());
|
||||
// 已退款金额
|
||||
BigDecimal refundedAmt = new BigDecimal(paymentConfirm.getRefundedAmt());
|
||||
// 已撤销金额
|
||||
BigDecimal reservedAmt = new BigDecimal(paymentConfirm.getReservedAmt());
|
||||
// 可支配金额 = 支付金额 - 已确认金额 - 已退款金额 - 已撤销金额
|
||||
BigDecimal availableAmt = payAmt.subtract(confirmedAmt).subtract(refundedAmt).subtract(reservedAmt);
|
||||
|
||||
// 更新支付单信息
|
||||
PaymentDTO payment2up = new PaymentDTO();
|
||||
payment2up.setId(paymentId);
|
||||
payment2up.setConfirmedAmt(paymentConfirm.getConfirmedAmt());
|
||||
payment2up.setRefundedAmt(paymentConfirm.getRefundedAmt());
|
||||
payment2up.setReservedAmt(paymentConfirm.getReservedAmt());
|
||||
payment2up.setAvailableAmt(MoneyUtil.toS(availableAmt));
|
||||
paymentMapper.updatePayment(payment2up);
|
||||
}
|
||||
}
|
||||
|
|
@ -14,9 +14,11 @@ import com.ghy.order.service.OrderDetailService;
|
|||
import com.ghy.order.service.OrderMasterService;
|
||||
import com.ghy.payment.domain.FinancialChangeRecord;
|
||||
import com.ghy.payment.domain.FinancialMaster;
|
||||
import com.ghy.payment.domain.PaymentRelation;
|
||||
import com.ghy.payment.service.AdapayService;
|
||||
import com.ghy.payment.service.FinancialChangeRecordService;
|
||||
import com.ghy.payment.service.FinancialMasterService;
|
||||
import com.ghy.payment.service.IPaymentRelationService;
|
||||
import com.huifu.adapay.core.exception.BaseAdaPayException;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
|
@ -25,8 +27,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
|
|
@ -42,12 +43,12 @@ public class AlipayController extends BaseController {
|
|||
private AdapayService adapayService;
|
||||
@Resource
|
||||
private OrderMasterService orderMasterService;
|
||||
@Resource
|
||||
private FinancialMasterService financialMasterService;
|
||||
|
||||
@Resource
|
||||
private OrderDetailService orderDetailService;
|
||||
|
||||
@Resource
|
||||
private FinancialMasterService financialMasterService;
|
||||
@Resource
|
||||
private IPaymentRelationService paymentRelationService;
|
||||
@Resource
|
||||
private FinancialChangeRecordService financialChangeRecordService;
|
||||
|
||||
|
|
@ -64,17 +65,18 @@ public class AlipayController extends BaseController {
|
|||
if (om.getPayStatus() == 1 || fm.getPayStatus() == 1) {
|
||||
return AjaxResult.error("订单已支付,不要重复付款!");
|
||||
}
|
||||
Map<String, Object> map;
|
||||
String payMoney = fm.getPayMoney().setScale(2, BigDecimal.ROUND_UNNECESSARY).toString();
|
||||
String payMoney = MoneyUtil.toS(fm.getPayMoney());
|
||||
// TODO 这里需要补充商品标题和商品描述信息
|
||||
PayParam payParam = PayParam.delayPayParam(om.getCode() + "_" + System.currentTimeMillis(), payMoney, "商品标题", "商品描述信息");
|
||||
try {
|
||||
map = adapayService.alipayQrPay(om.getDeptId(), payParam, null, null, null);
|
||||
JSONObject response = adapayService.alipayQrPay(om.getDeptId(), payParam, null, null, null);
|
||||
// 保存支付ID与主财务单ID到关系表
|
||||
paymentRelationService.insert(new PaymentRelation(null, fm.getId(), PaymentRelation.FINANCIAL_MASTER, fm.getPayMoney()));
|
||||
return AjaxResult.success(response);
|
||||
} catch (BaseAdaPayException e) {
|
||||
logger.error("创建支付失败", e);
|
||||
return AjaxResult.error("网络不佳 请稍后再试");
|
||||
}
|
||||
return AjaxResult.success(map);
|
||||
}
|
||||
|
||||
@PostMapping("/addMasterQr")
|
||||
|
|
@ -91,10 +93,12 @@ public class AlipayController extends BaseController {
|
|||
if (fm == null) {
|
||||
return AjaxResult.error("财务单不存在!");
|
||||
}
|
||||
ArrayList<PaymentRelation> relations = new ArrayList<>();
|
||||
// 主单是否付款 没付款的话一起付
|
||||
boolean fmPaid = Objects.equals(PayStatus.WAIT_PAY.getCode(), fm.getPayStatus());
|
||||
if (fmPaid) {
|
||||
payMoney = payMoney.add(fm.getPayMoney());
|
||||
relations.add(new PaymentRelation(null, fm.getId(), PaymentRelation.FINANCIAL_MASTER, fm.getPayMoney()));
|
||||
}
|
||||
|
||||
if (MoneyUtil.lte0(payMoney)) {
|
||||
|
|
@ -103,7 +107,7 @@ public class AlipayController extends BaseController {
|
|||
|
||||
// 付款
|
||||
PayParam payParam = PayParam.delayPayParam(orderMaster.getCode() + "_" + System.currentTimeMillis(),
|
||||
payMoney.setScale(2, RoundingMode.UNNECESSARY).toString(), "订单支付", "叮咚到家服务");
|
||||
MoneyUtil.toS(payMoney), "订单支付", "叮咚到家服务");
|
||||
|
||||
JSONObject response;
|
||||
try {
|
||||
|
|
@ -126,6 +130,11 @@ public class AlipayController extends BaseController {
|
|||
fm2update.setPayType(PayTypeEnum.ALIPAY_QR.getCode());
|
||||
financialMasterService.updateFinancialMaster(fm2update);
|
||||
}
|
||||
// 保存支付ID与订单ID到关系表
|
||||
for (PaymentRelation relation : relations) {
|
||||
relation.setPaymentId(paymentId);
|
||||
paymentRelationService.insert(relation);
|
||||
}
|
||||
return AjaxResult.success(response);
|
||||
}
|
||||
|
||||
|
|
@ -146,15 +155,18 @@ public class AlipayController extends BaseController {
|
|||
if (fm == null) {
|
||||
return AjaxResult.error("财务单不存在!");
|
||||
}
|
||||
ArrayList<PaymentRelation> relations = new ArrayList<>();
|
||||
// 主单是否付款 没付款的话一起付
|
||||
boolean fmPaid = Objects.equals(PayStatus.WAIT_PAY.getCode(), fm.getPayStatus());
|
||||
if (fmPaid) {
|
||||
payMoney = payMoney.add(fm.getPayMoney());
|
||||
relations.add(new PaymentRelation(null, fm.getId(), PaymentRelation.FINANCIAL_MASTER, fm.getPayMoney()));
|
||||
}
|
||||
// 查询关联的加价单
|
||||
FinancialChangeRecord financialChangeRecord = financialChangeRecordService.selectNotPayRecordByDetailId(orderDetailId);
|
||||
if (financialChangeRecord != null) {
|
||||
payMoney = payMoney.add(financialChangeRecord.getChangeMoney());
|
||||
FinancialChangeRecord fcr = financialChangeRecordService.selectNotPayRecordByDetailId(orderDetailId);
|
||||
if (fcr != null) {
|
||||
payMoney = payMoney.add(fcr.getChangeMoney());
|
||||
relations.add(new PaymentRelation(null, fcr.getId(), PaymentRelation.FINANCIAL_CHANGE, fcr.getChangeMoney()));
|
||||
}
|
||||
|
||||
if (MoneyUtil.lte0(payMoney)) {
|
||||
|
|
@ -163,12 +175,12 @@ public class AlipayController extends BaseController {
|
|||
|
||||
// 付款
|
||||
PayParam payParam;
|
||||
if (financialChangeRecord == null) {
|
||||
if (fcr == null) {
|
||||
payParam = PayParam.delayPayParam(orderMaster.getCode() + "_" + System.currentTimeMillis(),
|
||||
payMoney.setScale(2, BigDecimal.ROUND_UNNECESSARY).toString(),
|
||||
"订单支付", "叮咚到家服务");
|
||||
} else {
|
||||
payParam = PayParam.delayPayParam(orderMaster.getCode() + "_" + financialChangeRecord.getId() + "_" + System.currentTimeMillis(),
|
||||
payParam = PayParam.delayPayParam(orderMaster.getCode() + "_" + fcr.getId() + "_" + System.currentTimeMillis(),
|
||||
payMoney.setScale(2, BigDecimal.ROUND_UNNECESSARY).toString(),
|
||||
"加价付款", "叮咚到家服务");
|
||||
}
|
||||
|
|
@ -194,12 +206,17 @@ public class AlipayController extends BaseController {
|
|||
fm2update.setPayType(PayTypeEnum.ALIPAY_QR.getCode());
|
||||
financialMasterService.updateFinancialMaster(fm2update);
|
||||
}
|
||||
if (financialChangeRecord != null) {
|
||||
if (fcr != null) {
|
||||
FinancialChangeRecord fcr2update = new FinancialChangeRecord();
|
||||
fcr2update.setId(financialChangeRecord.getId());
|
||||
fcr2update.setId(fcr.getId());
|
||||
fcr2update.setPaymentId(paymentId);
|
||||
financialChangeRecordService.update(fcr2update);
|
||||
}
|
||||
// 保存支付ID与订单ID到关系表
|
||||
for (PaymentRelation relation : relations) {
|
||||
relation.setPaymentId(paymentId);
|
||||
paymentRelationService.insert(relation);
|
||||
}
|
||||
return AjaxResult.success(response);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ghy.common.adapay.model;
|
||||
package com.ghy.payment.domain;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import lombok.Data;
|
||||
|
|
@ -10,8 +10,13 @@ import lombok.Data;
|
|||
@Data
|
||||
public class PaymentConfirm {
|
||||
|
||||
@JSONField(name = "id")
|
||||
private String id;
|
||||
|
||||
private String paymentId;
|
||||
|
||||
private Long deptId;
|
||||
|
||||
@JSONField(name = "order_no")
|
||||
private String orderNo;
|
||||
|
||||
|
|
@ -51,18 +56,32 @@ public class PaymentConfirm {
|
|||
@JSONField(name = "fee_amt")
|
||||
private String feeAmt;
|
||||
|
||||
/**
|
||||
* 手续费收取模式:O-商户手续费账户扣取手续费,I-交易金额中扣取手续费;值为空时,默认值为I;若为O时,分账对象列表中不支持传入手续费承担方
|
||||
*/
|
||||
@JSONField(name = "fee_mode")
|
||||
private String feeMode;
|
||||
|
||||
@JSONField(name = "description")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 是否prod模式,true是prod模式,false是mock模式
|
||||
*/
|
||||
@JSONField(name = "prod_mode")
|
||||
private String prodMode;
|
||||
|
||||
@JSONField(name = "app_id")
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* pending 交易处理中
|
||||
* succeeded 交易成功
|
||||
* failed 交易失败
|
||||
*/
|
||||
@JSONField(name = "status")
|
||||
private String status;
|
||||
|
||||
// @JSONField(name = "div_members")
|
||||
// List<DivMember> divMembers;
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.ghy.payment.mapper;
|
||||
|
||||
import com.ghy.payment.domain.PaymentConfirm;
|
||||
|
||||
public interface PaymentConfirmMapper {
|
||||
|
||||
int insert(PaymentConfirm paymentConfirm);
|
||||
}
|
||||
|
|
@ -7,7 +7,6 @@ import com.ghy.common.adapay.AdapayProperties;
|
|||
import com.ghy.common.adapay.model.*;
|
||||
import com.ghy.common.enums.RefundType;
|
||||
import com.huifu.adapay.core.exception.BaseAdaPayException;
|
||||
import com.huifu.adapay.model.PaymentConfirm;
|
||||
import com.huifu.adapay.model.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
|
@ -35,6 +34,7 @@ public class AdapayService {
|
|||
private CallBackService refundCallbackService;
|
||||
private CallBackService drawCashCallbackService;
|
||||
private CallBackService payReverseCallbackService;
|
||||
private CallBackService paymentConfirmCallbackService;
|
||||
|
||||
@Autowired
|
||||
public void setExecutor(ThreadPoolTaskExecutor threadPoolTaskExecutor) {
|
||||
|
|
@ -71,6 +71,12 @@ public class AdapayService {
|
|||
this.payReverseCallbackService = payReverseCallbackService;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setPaymentConfirmCallbackService(CallBackService paymentConfirmCallbackService) {
|
||||
log.info("Adapay load callback: {}", paymentConfirmCallbackService.getClass().toString());
|
||||
this.paymentConfirmCallbackService = paymentConfirmCallbackService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询支付对象
|
||||
*
|
||||
|
|
@ -154,6 +160,9 @@ public class AdapayService {
|
|||
log.info("发起支付确认 dept[{}] param:{}", deptId, confirmParams.toJSONString());
|
||||
JSONObject response = (JSONObject) PaymentConfirm.create(confirmParams, deptId.toString());
|
||||
log.info("支付确认结果 dept[{}] response:{}", deptId, response.toJSONString());
|
||||
response.put("deptId", deptId);
|
||||
response.put("paymentId", paymentId);
|
||||
executor.execute(() -> paymentConfirmCallbackService.onResponse(response));
|
||||
return response;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ghy.payment.mapper.PaymentConfirmMapper">
|
||||
|
||||
<sql id="select_columns">
|
||||
SELECT id, dept_id, order_no, created_time, confirm_amt, confirmed_amt, reserved_amt, refunded_amt, fee_amt,
|
||||
fee_mode, description, prod_mode, app_id, status
|
||||
FROM adapay_payment_confirm
|
||||
</sql>
|
||||
|
||||
<insert id="insert" parameterType="com.ghy.payment.domain.PaymentConfirm">
|
||||
INSERT INTO adapay_payment_confirm
|
||||
(id, dept_id, order_no, created_time, confirm_amt, confirmed_amt, reserved_amt,
|
||||
refunded_amt, fee_amt, fee_mode, description, prod_mode, app_id, status)
|
||||
VALUES (#{id}, #{deptId}, #{orderNo}, #{createdTime}, #{confirmAmt}, #{confirmedAmt}, #{reservedAmt},
|
||||
#{refundedAmt}, #{feeAmt}, #{feeMode}, #{description}, #{prodMode}, #{appId}, #{status})
|
||||
</insert>
|
||||
|
||||
</mapper>
|
||||
Loading…
Reference in New Issue