摘要:支付流程以上是微信支付的流程用户进入选择商品进行购买,在内部结算时生成用户本系统订单待支付状态,此时返回订单信息与支付方式列表用户确认金额无误,并选择支付方式。
支付流程
以上是微信app支付的流程:
用户进入app选择商品进行购买,在app内部结算时生成用户本系统订单(待支付状态),此时返回订单信息与支付方式列表
用户确认金额无误,并选择支付方式。此时app将订单id与支付方式传给服务器,服务器根据订单金额与支付方式在外部支付系统下单(预支付订单),并给app返回可以唤起响应支付工具的‘支付数据’
app获取到‘支付数据’调用响应支付sdk,此时用户会看见微信支付或支付宝支付的页面,此时用户需要确认支付金额并输入正确的密码
sdk会检查用户的密码信息是否正确并返回支付结果,此时app收到sdk同步通知的支付结果,并提交到服务器,服务器会记录该笔支付的状态,切记不能使用app上传回来的支付结果作为最终的支付结果,不能信任前端数据
外部支付系统在处理完成该笔支付后会回调服务器设置的回调接口,通知服务器该笔支付的最终支付状态
收到通知时需要注意:
同样的通知可能会多次发送给商户系统。商户系统必须能够正确处理重复的通知。
后台通知交互时,如果微信收到商户的应答不符合规范或超时,微信会判定本次通知失败,重新发送通知,直到成功为止,但微信不保证通知最终一定能成功。
在订单状态不明或者没有收到微信支付结果通知的情况下,建议商户主动调用微信支付【查询订单API】确认订单状态。
商户系统对于支付结果通知的内容一定要做签名验证,并校验返回的订单金额是否与商户侧的订单金额一致,防止数据泄漏导致出现“假通知”,造成资金损失。
当收到通知进行处理时,首先检查对应业务数据的状态,判断该通知是否已经处理过,如果没有处理过再进行处理,如果处理过直接返回结果成功。在对业务数据进行状态检查和处理之前,要采用数据锁进行并发控制,以避免函数重入造成的数据混乱。
线上问题以上流程时微信与支付宝给出的官方流程,并且也是最标准的流程。但是当外部支付系统并没有微信与支付宝那么优秀的时候,我们的系统就不能按照该流程正常运行下去,下面我说说在使用‘建行支付’时遇到的问题
服务器收不到支付结果的回调
即使主动查询支付状态‘建行支付’依然返回未支付的状态
以上两个问题会引发更复杂的问题
由于没有支付回调,订单状态就不会发送改变用户就会再次支付,造成重复支付的现象
系统没有预料到会出现重复支付也就没有相应的策略去弥补
由于系统并不知道支付已经成功,用户在取消订单的时候就不会收到退款
根据这些线上出现的问题,我们决定进行重构,并深层次的处理整个支付流程!
设计思路1.确认支付方式
1.微信App支付 2.支付宝App支付 3.建行支付App支付
2.如何确保支付成功
1.外部支付系统(支付宝)成功后回调通知 2.本系统主动查询外部支付系统订单状态
3.如何避免用户重复支付
1.本系统在发起支付的时候检查‘订单号’是否有已经成功的支付记录 2.本系统在发起支付的时候检查‘订单号’是否有已经提交的支付记录,如果有需要同步查询外部支付系统该订单的支付状态
4.如果用户出现重复支付系统如何处理
1.系统需要定时检查是否有同一个‘订单号’出现多条成功支付的记录,如果有需要保留一笔支付,其余的进行退款处理
5.数据出现异常怎么办(例如:用户说支付完成,但是订单依然是待支付的状态)
1.所有的支付流程都需要进行数据记录,形成支付流水,这样可以直观的看到用户支付的路径,也方便外部支付系统查询具体逻辑
‘支付’是一次支付的记录,可能包含多个订单的支付金额,因为用户在购买商品生成订单的时候会根据商家拆单
‘支付与订单的隐射’表明该支付中所有的订单信息,每个‘映射’都记录了订单的金额与支付状态,并且重复支付也是发生在该‘映射’上的,因为一个订单智能有一次最终成功支付的记录,最终哪一个映射是有效的由‘是否当前使用’决定,任何时候一个订单只有一个映射有效
‘支付’可能有多条退款记录,退款的总金额不能超过支付金额,并且每一笔退款都需要一个唯一的退款交易号来保证不会重复退款,退款交易号由具体业务系统生成(比如退货,取消订单,重复支付)
所有的退款必须成功
系统需要主动查询支付状态是‘发起支付’,‘app同步通知成功’记录在外部支付系统的支付状态,如果在外部支付系统支付成功,这里需要重新设置支付状态为‘已完成’
支付的外部交易号与退款的退款交易号都是唯一的
为了保证系统的正常工作我们还需要一些定时器来作为最后的防线
接口实现1. 支付业务逻辑
public interface IPaymentApplicationService {
/** * 创建支付,待支付状态 * @param orders 订单JSONArray * @param paymentAmount 支付金额 * @param paymentChannel 支付渠道 * @param paymentType 支付方式 * @param accountId 账户 * @param serviceId 服务 * @param platformId 平台 * @param hockParams 回传参数 * @return */ PaymentResultDTO createPayment(Listorders, BigDecimal paymentAmount, PaymentChannelEnum paymentChannel, PaymentTypeEnum paymentType, String accountId, String serviceId, String platformId,String hockParams) throws InvalidOperationException; /** * app、小程序、H5收到回调 * @param paymentId 支付id * @param isSuccess 是否支付成功 */ void synchronizedCallback(String paymentId,boolean isSuccess) throws InvalidOperationException, PaymentQueryException, PaymentNotExistException; /** * app、小程序、H5收到回调 * @param orderIds 本次支付的所有订单id * @param isSuccess 是否成功 */ void synchronizedCallback(Collection orderIds,boolean isSuccess); /** * 服务器端收到回调 * @param paymentId 支付id * @param isSuccess 是否支付成功 * @param tradeNo 交易号 */ void asyncCallback(String paymentId,boolean isSuccess,String tradeNo) throws InvalidOperationException; /** * 服务器端收到回调 * @param outTradeNo 外部交易号 * @param isSuccess 是否支付成功 * @param tradeNo 交易号 */ void asyncCallbackForOutTradeNo(String outTradeNo,boolean isSuccess,String tradeNo);
}
2.退款业务逻辑
public interface IRefundApplicationService {
/** * 发起退款 * @param paymentId 支付id * @param sysRefundTradeNo 系统内部退款单号 * @param refundAmount 退款金额 * @param refundType 退款类型 * @param reason 退款理由 * @return 退款id */ void createRefund(String paymentId,String sysRefundTradeNo, BigDecimal refundAmount, RefundTypeEnum refundType, String reason) throws Exception; /** * 针对订单发起退款 * @param orderId 订单id * @param sysRefundTradeNo 系统内部退款单号 * @param refundAmount 退款金额 * @param refundType 退款类型 * @param reason 退款理由 * @return 退款id */ void createRefundByOrder(String orderId,String sysRefundTradeNo, BigDecimal refundAmount, RefundTypeEnum refundType, String reason) throws Exception; /** * 退款 * @param refund 退款实体 */ void refund(RefundDO refund) throws InvalidOperationException;
}
附件代码@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class PaymentApplicationServiceImpl implements IPaymentApplicationService {
private final PaymentConfigJpaRepository paymentConfigJpaRepository; private final PaymentJpaRepository paymentJpaRepository; private final PaymentOrderJpaRepository paymentOrderJpaRepository; private final PaymentFlowJpaRepository paymentFlowJpaRepository; private final OrderRepository orderJpaRepository; private final PayUtilRepository payUtilRepository; @Autowired public PaymentApplicationServiceImpl(PaymentConfigJpaRepository paymentConfigJpaRepository, PaymentJpaRepository paymentJpaRepository, PaymentOrderJpaRepository paymentOrderJpaRepository, PaymentFlowJpaRepository paymentFlowJpaRepository, OrderRepository orderJpaRepository, PayUtilRepository payUtilRepository) { this.paymentConfigJpaRepository = paymentConfigJpaRepository; this.paymentJpaRepository = paymentJpaRepository; this.paymentOrderJpaRepository = paymentOrderJpaRepository; this.paymentFlowJpaRepository = paymentFlowJpaRepository; this.orderJpaRepository = orderJpaRepository; this.payUtilRepository = payUtilRepository; } @Override public PaymentResultDTO createPayment(Listorders, BigDecimal paymentAmount, PaymentChannelEnum paymentChannel, PaymentTypeEnum paymentType, String accountId, String serviceId, String platformId, String hockParams,String wechatOpenId) throws Exception { Optional paymentConfigOptional = paymentConfigJpaRepository.findByPaymentChannelAndPaymentType(paymentChannel.getValue(),paymentType.getValue()); if(!paymentConfigOptional.isPresent()){ throw new InvalidOperationException("支付方式不存在"); } PaymentConfigDO paymentConfig = paymentConfigOptional.get(); //成功支付的订单不能再次支付 List orderIds = orders.stream().map(PaymentOrderDTO::getOrderId).collect(Collectors.toList()); List oldPaymentOrders = paymentOrderJpaRepository.findByOrderIdIn(orderIds); for (PaymentOrderDO po : oldPaymentOrders) { if(po.getPaymentStatus().equals(PaymentStatusEnum.ASYNC_CALLBACK_SUCCEED.getValue())){ throw new InvalidOperationException("订单:" + po.getOrderId() + "已成功支付"); }else if(po.getPaymentStatus().equals(PaymentStatusEnum.SYNCHRONIZE_CALLBACK_SUCCEED.getValue()) || po.getPaymentStatus().equals(PaymentStatusEnum.LAUNCH_PAY.getValue())){ //可能是重复支付 po.fundRepeatPay(); paymentOrderJpaRepository.save(po); //主动查询支付是否成功 PaymentQueryResult paymentQueryResult; paymentQueryResult = getPaymentResult(paymentConfig,po.getOutTradeNo()); if(paymentQueryResult != null && paymentQueryResult.getTradeStatus().equals(PaymentQueryResult.TradeStatus.SUCCESS)){ asyncCallback(po.getPaymentId(),true,paymentQueryResult.getTradeNo()); } } } PaymentDO payment = new PaymentDO(paymentAmount,paymentChannel,paymentType,paymentConfig.getMerchantId(),accountId,serviceId,platformId); paymentJpaRepository.save(payment); PaymentFlowDO paymentFlow = new PaymentFlowDO(payment.getId(),payment.getOutTradeNo(), PaymentFlowEnum.REQUEST_SIGNATURE, JSONArray.toJSONString(orders)); paymentFlowJpaRepository.save(paymentFlow); List paymentOrders = new ArrayList<>(); for (PaymentOrderDTO order : orders) { String orderId = order.getOrderId(); if (StringUtils.isBlank(orderId)) { throw new InvalidOperationException("orderId必传"); } PaymentOrderDO paymentOrder = new PaymentOrderDO(payment.getId(), orderId, payment.getOutTradeNo(), order.getOrderAmount(), JSONObject.toJSONString(order)); paymentOrders.add(paymentOrder); } paymentOrderJpaRepository.saveAll(paymentOrders); String sign = lunchPay(payment,paymentOrders,hockParams,wechatOpenId); return new PaymentResultDTO(payment.getId(),sign,hockParams,orderIds); } private String lunchPay(PaymentDO payment,List paymentOrders,String hockParams,String wechatOpenId) throws Exception { Optional paymentConfigOptional = paymentConfigJpaRepository.findByPaymentChannelAndPaymentType(payment.getPaymentChannel(),payment.getPaymentType()); if(!paymentConfigOptional.isPresent()){ throw new InvalidOperationException("支付配置不存在"); } PaymentConfigDO paymentConfig = paymentConfigOptional.get(); //计算签名字符串 String signString; PaymentChannelEnum paymentChannel = PaymentChannelEnum.getEnumByVal(paymentConfig.getPaymentChannel()); switch (paymentChannel){ case ALIPAY: AliPayUtil aliPayUtil = payUtilRepository.getAliPay(payment); signString = aliPayUtil.getSignStr(paymentConfig.getOrderNamePrefix() + ":" + payment.getId(),payment.getOutTradeNo(),payment.getPaymentAmount(),hockParams); break; case WECHATPAY: WechatPayUtil wechatPayUtil = payUtilRepository.getWechatPay(payment); if(StringUtils.isNotBlank(wechatOpenId)) { signString = wechatPayUtil.getJsApiSignStr(paymentConfig.getOrderNamePrefix() + ":" + payment.getId(), payment.getOutTradeNo(), payment.getPaymentAmount(), wechatOpenId, hockParams); }else { signString = wechatPayUtil.getSignStr(paymentConfig.getOrderNamePrefix() + ":" + payment.getId(), payment.getOutTradeNo(), payment.getPaymentAmount(), hockParams); } break; default: throw new InvalidOperationException("支付方式异常"); } payment.lunchPay(signString); for (PaymentOrderDO po : paymentOrders) { po.lunchPay(signString); } PaymentFlowDO paymentFlow = new PaymentFlowDO(payment.getId(),payment.getOutTradeNo(), PaymentFlowEnum.RECEIVE_SIGNATURE,signString); paymentJpaRepository.save(payment); paymentOrderJpaRepository.saveAll(paymentOrders); paymentFlowJpaRepository.save(paymentFlow); return signString; } @Override public void synchronizedCallback(String paymentId, boolean isSuccess) throws InvalidOperationException { Optional paymentOptional = paymentJpaRepository.findById(paymentId); if(!paymentOptional.isPresent()){ throw new InvalidOperationException("支付不存在"); } PaymentDO payment = paymentOptional.get(); payment.synchronizedCallback(isSuccess); List paymentOrders = paymentOrderJpaRepository.findByPaymentId(paymentId); for (PaymentOrderDO po : paymentOrders) { po.synchronizedCallback(isSuccess); } PaymentFlowDO paymentFlow = new PaymentFlowDO(payment.getId(),payment.getOutTradeNo(), PaymentFlowEnum.SYNCHRONIZE_CALLBACK,isSuccess + ""); //主动查询订单在第三方支付的状态,确保就算没有收到异步回调,支付状态依然正确 Optional paymentConfigOptional = paymentConfigJpaRepository.findByPaymentChannelAndPaymentType(payment.getPaymentChannel(),payment.getPaymentType()); if(!paymentConfigOptional.isPresent()){ throw new InvalidOperationException("支付方式不存在"); } PaymentConfigDO paymentConfig = paymentConfigOptional.get(); PaymentQueryResult paymentQueryResult = getPaymentResult(paymentConfig,payment.getOutTradeNo()); if(paymentQueryResult != null && paymentQueryResult.getTradeStatus().equals(PaymentQueryResult.TradeStatus.SUCCESS)){ asyncCallback(paymentId,true,paymentQueryResult.getTradeNo()); } paymentJpaRepository.save(payment); paymentOrderJpaRepository.saveAll(paymentOrders); paymentFlowJpaRepository.save(paymentFlow); } @Override public void synchronizedCallback(Collection orderIds, boolean isSuccess) { List paymentOrders = paymentOrderJpaRepository.findByOrderIdInAndPaymentStatus(orderIds,PaymentStatusEnum.LAUNCH_PAY.getValue()); Set paymentIds = paymentOrders.stream().map(PaymentOrderDO::getPaymentId).collect(Collectors.toSet()); for (String pId : paymentIds) { try { synchronizedCallback(pId,true); } catch (Exception e) { e.printStackTrace(); log.error("支付同步通知异常:" + pId + ":" + e.getMessage()); } } } @Override public void asyncCallback(String paymentId, boolean isSuccess, String tradeNo) throws InvalidOperationException { Optional paymentOptional = paymentJpaRepository.findById(paymentId); if(!paymentOptional.isPresent()){ throw new InvalidOperationException("支付不存在"); } PaymentDO payment = paymentOptional.get(); if(payment.getPaymentStatus().equals(PaymentStatusEnum.ASYNC_CALLBACK_SUCCEED.getValue())){ return; } List paymentOrders = paymentOrderJpaRepository.findByPaymentId(paymentId); List orderIds = paymentOrders.stream().map(PaymentOrderDO::getOrderId).collect(Collectors.toList()); //这些订单的其他支付:设置为重复 List oldOtherPaymentOrders = paymentOrderJpaRepository. findByOrderIdIn(orderIds).stream(). filter(po -> !po.getPaymentId().equalsIgnoreCase(paymentId)). collect(Collectors.toList()); if(!CollectionUtils.isEmpty(oldOtherPaymentOrders)){ for (PaymentOrderDO opo : oldOtherPaymentOrders) { if(opo.getPaymentStatus().equals(PaymentStatusEnum.ASYNC_CALLBACK_SUCCEED.getValue()) || opo.getPaymentStatus().equals(PaymentStatusEnum.SYNCHRONIZE_CALLBACK_SUCCEED.getValue()) || opo.getPaymentStatus().equals(PaymentStatusEnum.LAUNCH_PAY.getValue())) { opo.fundRepeatPay(); } } paymentOrderJpaRepository.saveAll(oldOtherPaymentOrders); } payment.asyncCallback(isSuccess,tradeNo); for (PaymentOrderDO po : paymentOrders) { po.asyncCallback(isSuccess); if(isSuccess){ //todo 需要考虑通知业务系统 Optional orderOptional = orderJpaRepository.findById(po.getOrderId()); if(orderOptional.isPresent()) { OrderDO order = orderOptional.get(); order.paymentSucceed(); orderJpaRepository.save(order); } } } PaymentFlowDO paymentFlow = new PaymentFlowDO(payment.getId(),payment.getOutTradeNo(), PaymentFlowEnum.ASYNC_CALLBACK,isSuccess + ""); paymentJpaRepository.save(payment); paymentFlowJpaRepository.save(paymentFlow); } @Override public void asyncCallbackForOutTradeNo(String outTradeNo, boolean isSuccess, String tradeNo) { Optional paymentOptional = paymentJpaRepository.findByOutTradeNo(outTradeNo); if(paymentOptional.isPresent()){ PaymentDO payment = paymentOptional.get(); try{ asyncCallback(payment.getId(),true,tradeNo); }catch (Exception e) { e.printStackTrace(); log.error("支付异步通知异常:" + payment.getId() + ":" + e.getMessage()); } } } private PaymentQueryResult getPaymentResult(PaymentConfigDO paymentConfig,String outTradeNo) { Optional paymentOptional = paymentJpaRepository.findByOutTradeNo(outTradeNo); if(!paymentOptional.isPresent()){ return null; } PaymentDO payment = paymentOptional.get(); PaymentQueryResult paymentQueryResult; PaymentChannelEnum paymentChannel = PaymentChannelEnum.getEnumByVal(paymentConfig.getPaymentChannel()); try{ switch (paymentChannel){ case ALIPAY: AliPayUtil aliPayUtil = payUtilRepository.getAliPay(payment); paymentQueryResult = aliPayUtil.paymentQuery(outTradeNo); break; case WECHATPAY: WechatPayUtil wechatPayUtil = payUtilRepository.getWechatPay(payment); paymentQueryResult = wechatPayUtil.paymentQuery(outTradeNo); break; default: throw new InvalidOperationException("支付方式异常"); } return paymentQueryResult; }catch (Exception e){ log.error(e.getMessage()); return null; } }
}
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class RefundApplicationServiceImpl implements IRefundApplicationService {
private final RefundJpaRepository refundJpaRepository; private final RefundFlowJpaRepository refundFlowJpaRepository; private final PaymentJpaRepository paymentJpaRepository; private final PaymentOrderJpaRepository paymentOrderJpaRepository; private final WechatPayUtil wechatPayUtil; private final AliPayUtil aliPayUtil; private final CcbPayUtil ccbPayUtil; @Autowired public RefundApplicationServiceImpl(RefundJpaRepository refundJpaRepository, RefundFlowJpaRepository refundFlowJpaRepository, PaymentJpaRepository paymentJpaRepository, PaymentOrderJpaRepository paymentOrderJpaRepository, WechatPayUtil wechatPayUtil, AliPayUtil aliPayUtil, CcbPayUtil ccbPayUtil) { this.refundJpaRepository = refundJpaRepository; this.refundFlowJpaRepository = refundFlowJpaRepository; this.paymentJpaRepository = paymentJpaRepository; this.paymentOrderJpaRepository = paymentOrderJpaRepository; this.wechatPayUtil = wechatPayUtil; this.aliPayUtil = aliPayUtil; this.ccbPayUtil = ccbPayUtil; } @Override public void createRefund(String paymentId,String sysRefundTradeNo, BigDecimal refundAmount, RefundTypeEnum refundType, String reason) throws Exception { OptionalpaymentOptional = paymentJpaRepository.findById(paymentId); if(!paymentOptional.isPresent()){ throw new InvalidOperationException("支付不存在,不可退款"); } PaymentDO payment = paymentOptional.get(); //检查支付是否完成,未完成就不可退款 PaymentChannelEnum paymentChannel = PaymentChannelEnum.getEnumByVal(payment.getPaymentChannel()); PaymentQueryResult paymentQueryResult = getPaymentResult(paymentChannel,payment.getOutTradeNo()); log.info("pay query : {}",paymentQueryResult); if(paymentQueryResult == null || paymentQueryResult.getTradeStatus().equals(PaymentQueryResult.TradeStatus.FAILED) || paymentQueryResult.getTradeStatus().equals(PaymentQueryResult.TradeStatus.CLOSED) || paymentQueryResult.getTradeStatus().equals(PaymentQueryResult.TradeStatus.PAYING)){ throw new InvalidOperationException("支付状态异常,不可退款"); } List refundStatus = Arrays.asList(RefundStatusEnum.START.getValue(),RefundStatusEnum.SUCCESS.getValue(),RefundStatusEnum.FAIL.getValue()); List oldRefunds = refundJpaRepository.findByPaymentIdAndRefundStatusIn(paymentId,refundStatus); BigDecimal oldRefundsAmount = oldRefunds.stream().map(RefundDO::getRefundAmount).reduce(BigDecimal.ZERO,BigDecimal::add); if(oldRefundsAmount.add(refundAmount).compareTo(payment.getPaymentAmount()) > 0){ throw new InvalidOperationException("退款总金额不可超过支付金额"); } RefundDO refund; Optional refundOptional = refundJpaRepository.findBySysRefundTradeNo(sysRefundTradeNo); if(refundOptional.isPresent()){ RefundDO oldRefund = refundOptional.get(); if(oldRefund.getRefundStatus().equals(RefundStatusEnum.SUCCESS.getValue())){ throw new InvalidOperationException("退款已完成"); }else if(oldRefund.getRefundStatus().equals(RefundStatusEnum.START.getValue())){ throw new InvalidOperationException("退款处理中"); } }else { refund = new RefundDO(paymentId,payment.getOutTradeNo(),sysRefundTradeNo,refundAmount, refundType,reason,payment.getPaymentChannel(), payment.getPaymentType(),payment.getPaymentAmount()); refundJpaRepository.save(refund); } } @Override public void createRefundByOrder(String orderId, String sysRefundTradeNo, BigDecimal refundAmount, RefundTypeEnum refundType, String reason) throws Exception { Optional paymentOrderOptional = paymentOrderJpaRepository.findByOrderIdAndIsCurrent(orderId,true); if(paymentOrderOptional.isPresent()){ PaymentOrderDO paymentOrder = paymentOrderOptional.get(); createRefund(paymentOrder.getPaymentId(),sysRefundTradeNo,refundAmount,refundType,reason); paymentOrderJpaRepository.save(paymentOrder); } } @Override public void refund(RefundDO refund) throws InvalidOperationException { PaymentChannelEnum paymentChannel = PaymentChannelEnum.getEnumByVal(refund.getPaymentChannel()); RefundTypeEnum refundType = RefundTypeEnum.getEnumByVal(refund.getRefundType()); String respData; try { switch (paymentChannel){ case ALIPAY: respData = aliPayUtil.orderPayRefund(refund.getOutTradeNo(),refund.getOutRefundTradeNo(),refund.getPaymentAmount(),refund.getRefundAmount(),refundType); break; case WECHATPAY: respData = wechatPayUtil.orderPayRefund(refund.getOutTradeNo(),refund.getOutRefundTradeNo(),refund.getPaymentAmount(),refund.getRefundAmount(),refundType); break; case CCBPAY: respData = ccbPayUtil.orderPayRefund(refund.getOutTradeNo(),refund.getOutRefundTradeNo(),refund.getPaymentAmount(),refund.getRefundAmount(),refundType); break; default: throw new InvalidOperationException("支付方式异常"); } refundSuccess(refund,respData); }catch (Exception e){ refundFail(refund,e.getMessage()); } } private void refundSuccess(RefundDO refund,String respData) throws InvalidOperationException { refund.asyncCallback(true); refundJpaRepository.save(refund); RefundFlowDO refundSyncFlow = new RefundFlowDO(refund.getId(), RefundFlowEnum.SYNCHRONIZE_CALLBACK_SUCCESS,respData); refundFlowJpaRepository.save(refundSyncFlow); } private void refundFail(RefundDO refund,String errorMessage) throws InvalidOperationException{ refund.asyncCallback(false); refundJpaRepository.save(refund); RefundFlowDO refundSyncFlow = new RefundFlowDO(refund.getId(), RefundFlowEnum.SYNCHRONIZE_CALLBACK_FAIL,errorMessage); refundFlowJpaRepository.save(refundSyncFlow); } private PaymentQueryResult getPaymentResult(PaymentChannelEnum paymentChannel,String outTradeNo) { try { switch (paymentChannel){ case ALIPAY: return aliPayUtil.paymentQuery(outTradeNo); case WECHATPAY: return wechatPayUtil.paymentQuery(outTradeNo); case CCBPAY: return ccbPayUtil.paymentQuery(outTradeNo); default: return null; } }catch (Exception e){ log.error(e.getMessage()); return null; } }
}
@Component
@Slf4j
@DisallowConcurrentExecution
public class CheckPaymentCloseSchedule extends QuartzJobBean {
@Autowired private PaymentJpaRepository paymentJpaRepository; @Autowired private IPaymentQueryService paymentQueryService; @Override public void executeInternal(JobExecutionContext context) throws JobExecutionException { log.info("检查关闭支付定时器启动"); Listpayments = paymentJpaRepository.findByPaymentStatus(PaymentStatusEnum.LAUNCH_PAY.getValue()); LocalDateTime now = LocalDateTime.now(); List closePayments = new ArrayList<>(); for (PaymentDO p : payments) { long minutes = 10L; if(!now.minusMinutes(minutes).isBefore(p.getLaunchPayTime())){ //超过十分钟没有支付,就进行关闭 try { PaymentQueryResult result = paymentQueryService.getPaymentResult(p.getId()); if(result == null){ p.close(); closePayments.add(p); if(closePayments.size() > 100){ paymentJpaRepository.saveAll(closePayments); closePayments.clear(); p = null; } } } catch (Exception e) { log.error("关闭支付异常:" + p.getId() + ":" + e.getMessage()); } } } paymentJpaRepository.saveAll(closePayments); }
}
@Component
@Slf4j
@DisallowConcurrentExecution
public class CheckPaymentRepeatSchedule extends QuartzJobBean {
@Autowired private PaymentOrderJpaRepository paymentOrderJpaRepository; @Autowired private IRefundApplicationService refundApplicationService; @Override public void executeInternal(JobExecutionContext context)throws JobExecutionException { log.info("检查重复支付定时器启动"); //找到尚未处理的重复支付记录 ListpaymentOrders = paymentOrderJpaRepository.findByPaymentStatusAndIsCurrentAndRepeatPayProcess(PaymentStatusEnum.ASYNC_CALLBACK_SUCCEED.getValue(),false,false); paymentOrders.forEach(po -> { try { refundApplicationService.createRefund(po.getPaymentId(), ObjectId.get().toString(),po.getPaymentAmount(), RefundTypeEnum.REPEAT_PAY,RefundTypeEnum.REPEAT_PAY.getDescription()); po.refundRepeatPay(); paymentOrderJpaRepository.save(po); } catch (Exception e) { log.error("支付订单:" + po.getId() + ":重复支付退款异常:" + e.getMessage()); } }); }
}
@Component
@Slf4j
@DisallowConcurrentExecution
public class CheckPaymentSuccessSchedule extends QuartzJobBean {
@Autowired private PaymentJpaRepository paymentJpaRepository; @Autowired private PaymentFlowJpaRepository paymentFlowJpaRepository; @Autowired private IPaymentApplicationService paymentApplicationService; @Autowired private IPaymentQueryService paymentQuery; @Override public void executeInternal(JobExecutionContext context) throws JobExecutionException { log.info("检查支付成功定时器启动"); Listpayments = paymentJpaRepository.findByPaymentStatusIn(Arrays.asList(PaymentStatusEnum.LAUNCH_PAY.getValue(),PaymentStatusEnum.SYNCHRONIZE_CALLBACK_SUCCEED.getValue())); for (PaymentDO p : payments) { PaymentQueryResult result = paymentQuery.getPaymentResult(p.getId()); PaymentFlowDO paymentFlow; if(result != null) { paymentFlow = new PaymentFlowDO(p.getId(), p.getOutTradeNo(), PaymentFlowEnum.RECEIVE_PAYMENT_DETAIL, JSONObject.toJSONString(result)); }else { if(p.getPaymentStatus().equals(PaymentStatusEnum.SYNCHRONIZE_CALLBACK_SUCCEED.getValue())) { String error = "支付:" + p.getId() + "app同步通知成功,但是第三方支付查询:未支付!"; log.error(error); p.close(); paymentJpaRepository.save(p); paymentFlow = new PaymentFlowDO(p.getId(), p.getOutTradeNo(), PaymentFlowEnum.RECEIVE_PAYMENT_DETAIL, error); }else { paymentFlow = new PaymentFlowDO(p.getId(), p.getOutTradeNo(), PaymentFlowEnum.RECEIVE_PAYMENT_DETAIL, "未支付"); } } paymentFlowJpaRepository.save(paymentFlow); if(result != null){ if(result.getTradeStatus().equals(PaymentQueryResult.TradeStatus.PAYING) || result.getTradeStatus().equals(PaymentQueryResult.TradeStatus.SUCCESS)){ //第三方支付成功,直接修改支付状态 try { paymentApplicationService.asyncCallback(p.getId(),true,result.getTradeNo()); PaymentFlowDO successPaymentFlow = new PaymentFlowDO(p.getId(),p.getOutTradeNo(), PaymentFlowEnum.ASYNC_CALLBACK_SUCCESS, JSONObject.toJSONString(result)); paymentFlowJpaRepository.save(successPaymentFlow); } catch (InvalidOperationException e) { log.error("支付:" + p.getId() + "第三方交易成功,本地修改异常:" + e.getMessage()); } }else if(result.getTradeStatus().equals(PaymentQueryResult.TradeStatus.FAILED)){ try { paymentApplicationService.asyncCallback(p.getId(),false,""); PaymentFlowDO failPaymentFlow = new PaymentFlowDO(p.getId(),p.getOutTradeNo(), PaymentFlowEnum.ASYNC_CALLBACK_FAILED, JSONObject.toJSONString(result)); paymentFlowJpaRepository.save(failPaymentFlow); } catch (InvalidOperationException e) { log.error("支付:" + p.getId() + "第三方交易失败,本地修改异常:" + e.getMessage()); } } } } }
}
@Component
@Slf4j
@DisallowConcurrentExecution
public class CheckRefundSuccessSchedule extends QuartzJobBean {
@Autowired private RefundJpaRepository refundJpaRepository; @Autowired private IRefundApplicationService refundApplicationService; @Override public void executeInternal(JobExecutionContext context) { log.info("检查退款成功定时器启动"); Listrefunds = refundJpaRepository.findByRefundStatusIn(Arrays.asList(RefundStatusEnum.FAIL.getValue(),RefundStatusEnum.START.getValue())); for (RefundDO r : refunds) { try { refundApplicationService.refund(r); } catch (InvalidOperationException e) { log.error("退款失败:" + r.getId() + ":" + e.getMessage()); } } }
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/74326.html
摘要:建立后台触发熔断操作入口,人工录入熔断配置或资损防控检测出异常新增并生效熔断配置,应急情况生效熔断,日常支付链路不会过熔断判断。确认无误或故障处理完成后,触发解熔断操作,业务继续处理或驳回。 1. 资损盲区 随着有赞支付体量的增大,资产部门承担的资金管理,风险把控的责任也越大。我们一方面要小步快跑,快速支撑业务,又要稳住底盘,守好底线。支付业务底线就是守护用户的每一分钱,不能有资金损失...
摘要:为了帮助用户更好地完成消费决策闭环,马蜂窝上线了大交通业务。现在,用户在马蜂窝也可以完成购买机票火车票等操作。第二阶段架构转变及服务化初探从年开始,整个大交通业务开始从架构向服务化演变。 交通方式是用户旅行前要考虑的核心要素之一。为了帮助用户更好地完成消费决策闭环,马蜂窝上线了大交通业务。现在,用户在马蜂窝也可以完成购买机票、火车票等操作。 与大多数业务系统相同,我们一样经历着从无到有...
阅读 1096·2021-10-12 10:11
阅读 878·2019-08-30 15:53
阅读 2288·2019-08-30 14:15
阅读 2963·2019-08-30 14:09
阅读 1198·2019-08-29 17:24
阅读 973·2019-08-26 18:27
阅读 1285·2019-08-26 11:57
阅读 2146·2019-08-23 18:23