shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.iplatform.pay.support;
 
import com.iplatform.base.PlatformRuntimeException;
import com.iplatform.model.po.S_pay_notify;
import com.iplatform.model.po.S_pay_order;
import com.iplatform.pay.service.PlatformOrderServiceImpl;
import com.iplatform.pay.util.NotifyUtils;
import com.iplatform.pay.util.OrderUtils;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.pay.NotifyValue;
import com.walker.pay.Order;
import com.walker.pay.PayStatus;
import com.walker.pay.response.ScanOrderResponsePay;
import com.walker.pay.support.DefaultOrder;
import com.walker.pay.wechat.v2.AppOrderResponsePay;
import com.walker.pay.wechat.v2.H5ResponsePay;
import com.walker.pay.wechat.v2.NotifyOrder;
import com.walker.pay.wechat.v2.WechatV2OrderCallback;
 
/**
 * 微信v2版本支付订单回调实现。<p></p>
 * 注意:该对象设计不合理,不应该在底层包实现<code>AbstractOrderCallback</code>,后续应该直接在平台继承该对象,不能放到类库中,不利于复用。<p></p>
 * 正确方法参考: {@linkplain AllinpayCloudOrderCallback}
 * @author 时克英
 * @date 2023-03-01
 */
public class DefaultWechatV2OrderCallback extends WechatV2OrderCallback {
 
    @Override
    protected void doPrepareOrder(Order platformOrder, AppOrderResponsePay responsePay){
        logger.info("默认实现: 执行保存'AppOrderResponsePay'订单操作,order={}", platformOrder);
        try {
            String payInfo = JsonUtils.objectToJsonString(responsePay.getAppOrderInfo());
            this.savePayOrderPersistent(platformOrder, payInfo);
        } catch (Exception e) {
            throw new RuntimeException("保存'微信v2-App'订单错误,无法转成json:" + responsePay.getAppOrderInfo(), e);
        }
    }
 
    @Override
    protected void doPrepareOrder(Order platformOrder, ScanOrderResponsePay responsePay){
        logger.info("默认实现: 执行保存'ScanOrderResponsePay'订单操作,order={}", platformOrder);
        this.savePayOrderPersistent(platformOrder, responsePay.getCodeUrl());
    }
 
    @Override
    protected void doPrepareOrder(Order platformOrder, H5ResponsePay responsePay){
        logger.info("默认实现: 执行保存'H5ResponsePay'订单操作,order={}", platformOrder);
        try {
            this.savePayOrderPersistent(platformOrder, JsonUtils.objectToJsonString(responsePay));
        } catch (Exception e) {
            throw new PlatformRuntimeException("H5ResponsePay --> Json 错误:" + e.getMessage(), e);
        }
    }
 
    @Override
    protected void doNotifyOrderPay(NotifyValue<NotifyOrder> notifyValue){
        logger.info("默认实现: 接收支付通知,notifyValue={}", notifyValue);
        long orderId = Long.parseLong(notifyValue.getOrderId());
        S_pay_notify exist = this.orderService.get(new S_pay_notify(orderId));
        if(exist != null && exist.getPay_status().equals(PayStatus.Success.getIndex())){
            logger.warn("订单通知已经接收,而且支付成功,不再重复处理,notifyId = {}, orderId = {}", notifyValue.getId(), orderId);
            return;
        }
        S_pay_notify s_pay_notify = this.acquirePayNotify(notifyValue);
        if(exist != null){
            // 更新通知,更新订单状态
            this.orderService.execInsertOrderNotify(s_pay_notify, false);
        } else {
            // 写入通知,更新订单状态
            this.orderService.execInsertOrderNotify(s_pay_notify, true);
        }
 
        logger.debug("------> 保存新订单通知, orderId = " + orderId);
    }
 
    protected S_pay_notify acquirePayNotify(NotifyValue<NotifyOrder> notifyValue){
        S_pay_notify s_pay_notify = NotifyUtils.acquireCombineBaseNotify(notifyValue);
        if(!notifyValue.isStatus()){
            // 通知支付失败
            s_pay_notify.setPay_status(PayStatus.Error.getIndex());
        } else {
            s_pay_notify.setPay_status(PayStatus.Success.getIndex());
        }
        NotifyOrder notifyOrder = notifyValue.getData();
        if(notifyOrder != null){
            s_pay_notify.setNotify_status("暂未获取");
            s_pay_notify.setNotify_amount(notifyOrder.getTotalMoney());
            s_pay_notify.setNotify_acct(notifyOrder.getOpenId());
            try {
                s_pay_notify.setNotify_source(JsonUtils.objectToJsonString(notifyOrder));
            } catch (Exception e) {
                logger.error("NotifyOrder转json错误:" + e.getMessage(), e);
            }
        }
        return s_pay_notify;
    }
 
    protected void savePayOrderPersistent(Order platformOrder, String thirdPayInfo){
        S_pay_order s_pay_order = OrderUtils.acquireDefaultOrder(0
                , (DefaultOrder) platformOrder, platformOrder.getTradeNo(), thirdPayInfo);
        this.orderService.insert(s_pay_order);
    }
 
    public void setOrderService(PlatformOrderServiceImpl orderService) {
        this.orderService = orderService;
    }
 
    private PlatformOrderServiceImpl orderService;
}