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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.iplatform.pay.support;
 
import com.iplatform.model.po.S_pay_notify;
import com.iplatform.model.po.S_pay_order;
import com.iplatform.pay.PlatformOrderCallback;
import com.iplatform.pay.util.NotifyUtils;
import com.iplatform.pay.util.OrderUtils;
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.pay.CallBackException;
import com.walker.pay.NotifyValue;
import com.walker.pay.Order;
import com.walker.pay.PayStatus;
import com.walker.pay.ResponsePay;
import com.walker.pay.allinpaycloud.Constants;
import com.walker.pay.allinpaycloud.NotifyOrder;
import com.walker.pay.allinpaycloud.generator.OrderResponsePay;
import com.walker.pay.support.BankCardOrder;
import com.walker.pay.support.allinpaycloud.WechatJsOrder;
import com.walker.pay.support.allinpaycloud.WechatMiniProgramOrder;
 
/**
 * 通商云订单回调业务实现。
 * @author 时克英
 * @date 2023-02-26
 */
public class AllinpayCloudOrderCallback extends PlatformOrderCallback {
 
    @Override
    public void onOrderPrepare(Order platformOrder, ResponsePay responsePay) {
        OrderResponsePay orderResponsePay = (OrderResponsePay) responsePay;
        logger.debug("保存'通商云订单':{}", orderResponsePay);
 
        S_pay_order s_pay_order = null;
        //
//        String tradeNo = orderResponsePay.getTradeNo();
        String tradeNo = orderResponsePay.getOrderNo();
        String thirdPayInfo = orderResponsePay.getPayInfo();
        /**
         * {
         * "orderNo":"1632077606083895296",
         * "tradeNo":"{\"sign\":\"\",\"tphtrxcrtime\":\"\",\"tphtrxid\":0,\"trxflag\":\"trx\",\"trxsn\":\"\"}",
         * "validationType":1,
         * "extendInfo":"test",
         * "bizUserId":"shikeying",
         * "bizOrderNo":"8400705969744373"}
         * 返回的通联订单号不是'tradeNo',而是'orderNo'
         */
        if(platformOrder instanceof BankCardOrder){
            BankCardOrder bankCardOrder = (BankCardOrder) platformOrder;
//            s_pay_order = OrderUtils.acquireBankCardOrder(0, bankCardOrder, orderResponsePay);
            s_pay_order = OrderUtils.acquireBankCardOrder(0, bankCardOrder, tradeNo, thirdPayInfo);
        } else if(platformOrder instanceof WechatJsOrder){
            WechatJsOrder wechatJsOrder = (WechatJsOrder) platformOrder;
            s_pay_order = OrderUtils.acquireWechatJsOrder(0, wechatJsOrder, tradeNo, thirdPayInfo);
        } else if(platformOrder instanceof WechatMiniProgramOrder){
            WechatMiniProgramOrder wechatMiniProgramOrder = (WechatMiniProgramOrder) platformOrder;
            s_pay_order = OrderUtils.acquireWechatJsOrder(0, wechatMiniProgramOrder, tradeNo, thirdPayInfo);
        } else {
            throw new UnsupportedOperationException("未实现保存该类型订单代码: " + platformOrder.getClass().getName());
        }
        this.orderService.insert(s_pay_order);
    }
 
    @Override
    public void onOrderNotify(NotifyValue<?> notifyValue) throws CallBackException {
        if(!(notifyValue.getData() instanceof NotifyOrder)){
            throw new IllegalArgumentException("notifyValue.getData()必须是 NotifyOrder 对象!");
        }
        this.doNotifyOrderPay((NotifyValue<NotifyOrder>)notifyValue);
    }
 
    protected void doNotifyOrderPay(NotifyValue<NotifyOrder> notifyValue){
//        logger.info("默认实现: 接收支付通知,notifyValue={}", notifyValue);
        if(StringUtils.isEmpty(notifyValue.getOrderId())){
            throw new IllegalArgumentException("接收订单通知,系统订单'orderId'为空");
        }
        long orderId = Long.parseLong(notifyValue.getOrderId());
//        S_pay_notify exist = this.orderService.get(new S_pay_notify(orderId));
//        if(exist != null){
//            logger.warn("订单通知已经接收,不再重复处理,notifyId = {}", notifyValue.getId());
//            return;
//        }
        S_pay_notify exist = this.getExistNotifyRecord(orderId);
        if(this.isAlreadySuccessNotify(exist)){
            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);
        }
//        this.orderService.execInsertOrderNotify(s_pay_notify);
        logger.debug("------> 保存新订单通知, orderId = " + orderId);
    }
 
    private S_pay_notify acquirePayNotify(NotifyValue<NotifyOrder> notifyValue){
//        S_pay_notify s_pay_notify = new S_pay_notify();
//        s_pay_notify.setOrder_id(Long.parseLong(notifyValue.getOrderId()));
//        s_pay_notify.setNotify_id(notifyValue.getId());
//        s_pay_notify.setCreate_time(DateUtils.getDateTimeNumber(System.currentTimeMillis()));
//        s_pay_notify.setNotify_type(notifyValue.getDataType());
//        s_pay_notify.setTrade_no(notifyValue.getTradeNo());
 
        S_pay_notify s_pay_notify = NotifyUtils.acquireCombineBaseNotify(notifyValue);
 
        NotifyOrder notifyOrder = notifyValue.getData();
        if(notifyOrder != null){
            if(notifyOrder.getStatus().equals(Constants.SUB_CODE_OK)){
                s_pay_notify.setPay_status(PayStatus.Success.getIndex());
            } else {
                s_pay_notify.setPay_status(PayStatus.Error.getIndex());
            }
            s_pay_notify.setNotify_status(notifyOrder.getStatus());
            s_pay_notify.setNotify_amount(notifyOrder.getAmount());
            s_pay_notify.setNotify_acct(notifyOrder.getAcct());
 
            try {
                s_pay_notify.setNotify_source(JsonUtils.objectToJsonString(notifyOrder));
            } catch (Exception e) {
                logger.error("NotifyOrder转json错误:" + e.getMessage(), e);
            }
        }
        return s_pay_notify;
    }
 
//    public void setOrderService(OrderServiceImpl orderService) {
//        this.orderService = orderService;
//    }
//    private OrderServiceImpl orderService;
}