peng
8 天以前 75f9783d5a70a5f037e3b34dc0e479069e63c0e9
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package cn.lili.modules.payment.kit.plugin.unionpay;
 
import cn.hutool.core.net.URLEncoder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.SnowFlake;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.payment.kit.CashierSupport;
import cn.lili.modules.payment.kit.Payment;
import cn.lili.modules.payment.kit.core.enums.SignType;
import cn.lili.modules.payment.kit.core.kit.HttpKit;
import cn.lili.modules.payment.kit.core.kit.IpKit;
import cn.lili.modules.payment.kit.core.kit.WxPayKit;
import cn.lili.modules.payment.kit.dto.PayParam;
import cn.lili.modules.payment.kit.params.dto.CashierParam;
import cn.lili.modules.payment.kit.plugin.unionpay.enums.ServiceEnum;
import cn.lili.modules.payment.kit.plugin.unionpay.model.UnifiedOrderModel;
import cn.lili.modules.payment.service.PaymentService;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.dto.payment.UnionPaymentSetting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import cn.lili.modules.system.service.SettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Map;
 
/**
 * 银联云闪付 支付
 *
 * @author Bulbasaur
 * @since 2023/02/14 17:44
 */
@Slf4j
@Component
public class UnionPayPlugin implements Payment {
 
 
    /**
     * 收银台
     */
    @Autowired
    private CashierSupport cashierSupport;
    /**
     * 支付日志
     */
    @Autowired
    private PaymentService paymentService;
    /**
     * 配置
     */
    @Autowired
    private SettingService settingService;
 
    @Override
    public ResultMessage<Object> nativePay(HttpServletRequest request, PayParam payParam) {
        try {
            CashierParam cashierParam = cashierSupport.cashierParam(payParam);
            UnionPaymentSetting unionPaymentSetting = this.unionPaymentSetting();
            String notifyUrl = unionPaymentSetting.getUnionPayDomain().concat("/unionPay/payNotify");
            //用户ip
            String ip = IpKit.getRealIp(request);
            //第三方付款订单
            String outOrderNo = SnowFlake.getIdStr();
            String attach = URLEncoder.createDefault().encode(JSONUtil.toJsonStr(payParam), StandardCharsets.UTF_8);
            Map<String, String> params = UnifiedOrderModel.builder()
                    .service(ServiceEnum.NATIVE.toString())
                    .mch_id(unionPaymentSetting.getUnionPayMachId())
                    .out_trade_no(outOrderNo)
                    .body(cashierParam.getDetail())
                    .attach(attach)
                    .total_fee("0")
                    .mch_create_ip(ip)
                    .notify_url(notifyUrl)
                    .nonce_str(WxPayKit.generateStr())
                    .build().createSign(unionPaymentSetting.getUnionPayKey(), SignType.MD5);
 
            String xmlResult = UnionPayApi.execution(unionPaymentSetting.getUnionPayServerUrl(), params);
            log.info("xmlResult:" + xmlResult);
            Map<String, String> result = WxPayKit.xmlToMap(xmlResult);
            log.info(result.toString());
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    @Override
    public ResultMessage<Object> appPay(HttpServletRequest request, PayParam payParam) {
        try {
 
            CashierParam cashierParam = cashierSupport.cashierParam(payParam);
            UnionPaymentSetting unionPaymentSetting = this.unionPaymentSetting();
            String notifyUrl = unionPaymentSetting.getUnionPayDomain().concat("/unionPay/payNotify");
            String attach = URLEncoder.createDefault().encode(JSONUtil.toJsonStr(payParam), StandardCharsets.UTF_8);
 
            //用户ip
            String ip = IpKit.getRealIp(request);
            Map<String, String> params = UnifiedOrderModel.builder()
                    .service(ServiceEnum.WEI_XIN_APP_PAY.toString())
                    .mch_id(unionPaymentSetting.getUnionPayMachId())
                    .appid(unionPaymentSetting.getUnionPayAppId())
                    .out_trade_no(WxPayKit.generateStr())
                    .body(cashierParam.getDetail())
                    .attach(attach)
                    .total_fee("0")
                    .mch_create_ip(ip)
                    .notify_url(notifyUrl)
                    .nonce_str(WxPayKit.generateStr())
                    .build()
                    .createSign(unionPaymentSetting.getUnionPayKey(), SignType.MD5);
 
            System.out.println(params);
 
            String xmlResult = UnionPayApi.execution(unionPaymentSetting.getUnionPayServerUrl(), params);
            log.info("xmlResult:" + xmlResult);
            Map<String, String> result = WxPayKit.xmlToMap(xmlResult);
            if (!WxPayKit.verifyNotify(result, unionPaymentSetting.getUnionPayKey(), SignType.MD5)) {
                log.error("签名异常");
            }
            String status = result.get("status");
            String resultCode = result.get("result_code");
            if (!"0".equals(status) && !"0".equals(resultCode)) {
                 log.error(result.get("err_msg"));
                return null;
            }
            log.error(result.get("pay_info"));
            return null;
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
 
            return null;
 
        }
    }
 
    @Override
    public ResultMessage<Object> jsApiPay(HttpServletRequest request, PayParam payParam) {
        String buyerLogonId="";
        String buyerId="";
        CashierParam cashierParam = cashierSupport.cashierParam(payParam);
        UnionPaymentSetting unionPaymentSetting = this.unionPaymentSetting();
        String attach = URLEncoder.createDefault().encode(JSONUtil.toJsonStr(payParam), StandardCharsets.UTF_8);
        //用户ip
        String ip = IpKit.getRealIp(request);
        try {
            if (StrUtil.isEmpty(buyerLogonId) && StrUtil.isEmpty(buyerId)) {
                 log.error("buyer_logon_id buyer_id 不能同时为空");
                return null;
            }
 
            String notifyUrl = unionPaymentSetting.getUnionPayDomain().concat("/unionPay/payNotify");
 
 
            Map<String, String> params = UnifiedOrderModel.builder()
                    .service(ServiceEnum.ALI_PAY_JS_PAY.toString())
                    .mch_id(unionPaymentSetting.getUnionPayMachId())
                    .out_trade_no(WxPayKit.generateStr())
                    .body(cashierParam.getDetail())
                    .attach(attach)
                    .total_fee("0")
                    .mch_create_ip(ip)
                    .notify_url(notifyUrl)
                    .nonce_str(WxPayKit.generateStr())
                    .buyer_id(buyerId)
                    .buyer_logon_id(buyerLogonId)
                    .build()
                    .createSign(unionPaymentSetting.getUnionPayKey(), SignType.MD5);
 
            System.out.println(params);
 
            String xmlResult = UnionPayApi.execution(unionPaymentSetting.getUnionPayServerUrl(), params);
            log.info("xmlResult:" + xmlResult);
            Map<String, String> result = WxPayKit.xmlToMap(xmlResult);
            log.info(result.toString());
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
 
    @Override
    public void callBack(HttpServletRequest request) {
        try {
            verifyNotify(request);
        } catch (Exception e) {
            log.error("支付异常", e);
        }
    }
 
    @Override
    public void notify(HttpServletRequest request) {
        try {
            verifyNotify(request);
        } catch (Exception e) {
            log.error("支付异常", e);
        }
    }
 
    private void verifyNotify(HttpServletRequest request) throws Exception {
        String xmlMsg = HttpKit.readData(request);
        log.info("支付通知=" + xmlMsg);
        Map<String, String> params = WxPayKit.xmlToMap(xmlMsg);
 
        String status = params.get("status");
        String returnCode = params.get("result_code");
 
        log.info(status + " " + returnCode);
 
        if ("0".equals(status) && "0".equals(returnCode)) {
            UnionPaymentSetting unionPaymentSetting = this.unionPaymentSetting();
            // 注意重复通知的情况,同一订单号可能收到多次通知,请注意一定先判断订单状态
            // 注意此处签名方式需与统一下单的签名类型一致
            if (WxPayKit.verifyNotify(params, unionPaymentSetting.getUnionPayKey(), SignType.MD5)) {
                log.info("支付成功....");
                // 更新订单信息
                // 发送通知等
 
            }
        }
 
    }
    /**
     * 获取微信支付配置
     *
     * @return
     */
    private UnionPaymentSetting unionPaymentSetting() {
        try {
            Setting systemSetting = settingService.get(SettingEnum.UNIONPAY_PAYMENT.name());
            UnionPaymentSetting unionPaymentSetting = JSONUtil.toBean(systemSetting.getSettingValue(), UnionPaymentSetting.class);
            return unionPaymentSetting;
        } catch (Exception e) {
            log.error("微信支付暂不支持", e);
            throw new ServiceException(ResultCode.PAY_NOT_SUPPORT);
        }
    }
}