MrShi
2023-12-12 9088209cdd7e3158d2a04caae05e6726ba3b3ad6
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
package com.doumee.api.web;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.doumee.core.model.ApiResponse;
import com.doumee.core.wx.WxMiniConfig;
import com.doumee.service.business.GoodsorderService;
import com.doumee.service.business.RefundService;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
/**
 * Created by IntelliJ IDEA.
 * 支付回调
 * @Author : Rk
 * @create 2023/2/23 13:49
 */
@Slf4j
@RestController
@CrossOrigin
public class PaymentCallback {
 
    @Autowired
    private GoodsorderService goodsorderService;
 
    /**
     * 【微信支付】异步通知
     *
     * @return
     */
    @PostMapping("/api/wxPayNotify")
    public ApiResponse wxPay_notify(HttpServletRequest request) {
        try {
            ServletInputStream inputStream = request.getInputStream();
            StringBuffer stringBuffer = new StringBuffer();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String s;
            //读取回调请求体
            while ((s = bufferedReader.readLine()) != null) {
                stringBuffer.append(s);
            }
            String body = stringBuffer.toString();
            String timestamp = request.getHeader("Wechatpay-Timestamp");
            String nonce = request.getHeader("Wechatpay-Nonce");
            String signType = request.getHeader("Wechatpay-Signature-Type");
            String serialNo = request.getHeader("wechatpay-Serial");
            String signature = request.getHeader("Wechatpay-Signature");
            RequestParam requestParam = new RequestParam.Builder()
                    .serialNumber(serialNo)
                    .nonce(nonce)
                    .signType(signType)
                    .signature(signature)
                    .timestamp(String.valueOf(timestamp))
                    .body(body)
                    .build();
 
            NotificationConfig config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(WxMiniConfig.wxProperties.getMchId())
                    .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath())
                    .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer())
                    .apiV3Key(WxMiniConfig.wxProperties.getApiV3Key())
                    .build();
 
 
            NotificationParser parser = new NotificationParser(config);
            Transaction result = parser.parse(requestParam, Transaction.class);
            //自定义订单号
            String outTradeNo = result.getOutTradeNo();
            //微信订单号
            String paymentNo = result.getTransactionId();
            if ("SUCCESS".equals(result.getTradeState().name())) {
                // 支付成功ge
                switch (result.getAttach()) {
                    //支付订单回调
                    case "createGoodsOrder": {
                        goodsorderService.payNotify(outTradeNo,paymentNo);
                        break;
                    }
                }
            } else {
                // 支付失败
                switch (result.getAttach()) {
                    case "createGoodsOrder": {
                        break;
                    }
                }
            }
            return ApiResponse.success("处理成功!");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("微信回调结果异常,异常原因{}", e.getLocalizedMessage());
            return ApiResponse.failed("");
        }
    }
    @PostMapping("/api/wxRefundNotify")
    public ApiResponse wxRefundNotify(HttpServletRequest request) {
        try {
            ServletInputStream inputStream = request.getInputStream();
            StringBuffer stringBuffer = new StringBuffer();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String s;
            //读取回调请求体
            while ((s = bufferedReader.readLine()) != null) {
                stringBuffer.append(s);
            }
            String body = stringBuffer.toString();
            String timestamp = request.getHeader("Wechatpay-Timestamp");
            String nonce = request.getHeader("Wechatpay-Nonce");
            String signType = request.getHeader("Wechatpay-Signature-Type");
            String serialNo = request.getHeader("wechatpay-Serial");
            String signature = request.getHeader("Wechatpay-Signature");
            RequestParam requestParam = new RequestParam.Builder()
                    .serialNumber(serialNo)
                    .nonce(nonce)
                    .signType(signType)
                    .signature(signature)
                    .timestamp(String.valueOf(timestamp))
                    .body(body)
                    .build();
 
            NotificationConfig config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(WxMiniConfig.wxProperties.getMchId())
                    .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath())
                    .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer())
                    .apiV3Key(WxMiniConfig.wxProperties.getApiV3Key())
                    .build();
 
 
            NotificationParser parser = new NotificationParser(config);
            RefundNotification result = parser.parse(requestParam, RefundNotification.class);
//            if ("SUCCESS".equals(result.getRefundStatus().name())) {
                // 支付成功ge
                     goodsorderService.closeGoodsorderDone(result);
//            }
            return ApiResponse.success("处理成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("微信回调结果异常,异常原因{}", e.getLocalizedMessage());
            return ApiResponse.failed("");
        }
    }
 
 
}