Pu Zhibing
1 天以前 ddf438536267b9c9bb77369cdccce4e67206842a
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
package com.stylefeng.guns.modular.system.util;
 
import com.alibaba.fastjson.JSON;
import com.alipay.global.api.AlipayClient;
import com.alipay.global.api.DefaultAlipayClient;
import com.alipay.global.api.exception.AlipayApiException;
import com.alipay.global.api.model.ams.*;
import com.alipay.global.api.model.constants.EndPointConstants;
import com.alipay.global.api.request.ams.notify.AlipayPayResultNotify;
import com.alipay.global.api.request.ams.pay.AlipayPayQueryRequest;
import com.alipay.global.api.request.ams.pay.AlipayPayRequest;
import com.alipay.global.api.response.ams.pay.AlipayPayQueryResponse;
import com.alipay.global.api.response.ams.pay.AlipayPayResponse;
import com.alipay.global.api.tools.WebhookTool;
import lombok.Data;
 
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Date;
 
/**
 * 支付宝国际版支付工具类
 */
public class AntomPaymentUtil {
    /**
     * replace with your client id <br>
     * find your client id here: <a href="https://dashboard.antom.com/global-payments/developers/quickStart">quickStart</a>
     */
    public static final String CLIENT_ID = "your_client_id";
 
    /**
     * replace with your antom public key (used to verify signature) <br>
     * find your antom public key here: <a href="https://dashboard.antom.com/global-payments/developers/quickStart">quickStart</a>
     */
    public static final String ANTOM_PUBLIC_KEY = "antom_public_key";
 
    /**
     * replace with your private key (used to sign) <br>
     * please ensure the secure storage of your private key to prevent leakage
     */
    public static final String MERCHANT_PRIVATE_KEY = "your_private_key";
 
    private final static AlipayClient CLIENT = new DefaultAlipayClient(EndPointConstants.SG, MERCHANT_PRIVATE_KEY, ANTOM_PUBLIC_KEY, CLIENT_ID);
 
 
    /**
     * 发起付款请求
     * @param payment
     * @return
     */
    public static ResultUtil<AlipayPayResponse> pay(PaymentVO payment){
        AlipayPayRequest alipayPayRequest = new AlipayPayRequest();
        alipayPayRequest.setClientId(CLIENT_ID);
        alipayPayRequest.setProductCode(ProductCodeType.CASHIER_PAYMENT);
        alipayPayRequest.setPaymentRequestId(payment.getPaymentRequestId());
 
        Amount paymentAmount = new Amount();
        paymentAmount.setCurrency(payment.getCurrency());
        paymentAmount.setValue(payment.getAmountValue());
        alipayPayRequest.setPaymentAmount(paymentAmount);
 
        PaymentMethod paymentMethod = new PaymentMethod();
        paymentMethod.setPaymentMethodType(WalletPaymentMethodType.ALIPAY_CN.name());
        alipayPayRequest.setPaymentMethod(paymentMethod);
 
        alipayPayRequest.setPaymentNotifyUrl(payment.getPaymentNotifyUrl());
        alipayPayRequest.setPaymentRedirectUrl(payment.getPaymentRedirectUrl());
 
        SettlementStrategy settlementStrategy  = new SettlementStrategy();
        settlementStrategy.setSettlementCurrency(payment.getCurrency());
        alipayPayRequest.setSettlementStrategy(settlementStrategy);
 
        Order order = new Order();
        order.setReferenceOrderId(payment.getReferenceOrderId());
        order.setOrderDescription(payment.getOrderDescription());
        Amount orderAmount = new Amount();
        orderAmount.setCurrency(payment.getCurrency());
        orderAmount.setValue(payment.getAmountValue());
        order.setOrderAmount(orderAmount);
        alipayPayRequest.setOrder(order);
 
        Env env = new Env();
        env.setTerminalType(TerminalType.APP);
        alipayPayRequest.setEnv(env);
        AlipayPayResponse alipayPayResponse = null;
        try {
            alipayPayResponse = CLIENT.execute(alipayPayRequest);
        } catch (AlipayApiException e) {
            return ResultUtil.error(e.getErrMsg());
        }
        return ResultUtil.success(alipayPayResponse);
    }
 
 
    /**
     * 查询付款结果
     * @param paymentRequestId
     * @return
     */
    public static ResultUtil<AlipayPayQueryResponse> inquiryPayment(String paymentRequestId){
        AlipayPayQueryRequest alipayPayQueryRequest = new AlipayPayQueryRequest();
        alipayPayQueryRequest.setPaymentRequestId(paymentRequestId);
 
        AlipayPayQueryResponse alipayPayQueryResponse;
        try {
            long startTime = System.currentTimeMillis();
            System.out.println("inquiry payment request: " + JSON.toJSONString(alipayPayQueryRequest));
            alipayPayQueryResponse = CLIENT.execute(alipayPayQueryRequest);
            System.out.println("inquiry payment response: " + JSON.toJSONString(alipayPayQueryResponse));
            System.out.println("inquiry payment request cost time: " + (System.currentTimeMillis() - startTime) + "ms\n");
        } catch (AlipayApiException e) {
            return ResultUtil.error(e.getErrMsg());
        }
        return ResultUtil.success(alipayPayQueryResponse);
    }
 
    /**
     * 支付回调处理
     * @param request
     * @return
     */
    public static ResultUtil<AlipayPayResultNotify> receivePaymentNotify(HttpServletRequest request){
        // retrieve the required parameters from http request
        String requestUri = request.getRequestURI();
        String requestMethod = request.getMethod();
        // retrieve the required parameters from request header
        String requestTime = request.getHeader("request-time");
        String clientId = request.getHeader("client-id");
        String signature = request.getHeader("signature");
 
        try {
            ServletInputStream inputStream = request.getInputStream();
            StringBuilder sb = new StringBuilder();
            String s;
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            while ((s = in.readLine()) != null) {
                sb.append(s);
            }
            in.close();
            inputStream.close();
            String notifyBody = sb.toString();
            // verify the signature of notification
            boolean verifyResult = WebhookTool.checkSignature(requestUri, requestMethod, clientId,
                    requestTime, signature, notifyBody, ANTOM_PUBLIC_KEY);
            if (!verifyResult) {
                return ResultUtil.error("Invalid notify signature");
            }
            // deserialize the notification body
            AlipayPayResultNotify paymentNotify = JSON.parseObject(notifyBody, AlipayPayResultNotify.class);
            if (paymentNotify != null && "SUCCESS".equals(paymentNotify.getResult().getResultCode())) {
                // handle your own business logic.
                // e.g. The relationship between payment information and users is kept in the database.
                System.out.println("receive payment notify: " + JSON.toJSONString(paymentNotify));
                return ResultUtil.success(paymentNotify);
            }
            return ResultUtil.error("system_error");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(e.getMessage());
        }
    }
 
 
 
    @Data
    public static class PaymentVO {
        /**
         * 支付请求流水号
         */
        private String paymentRequestId;
        /**
         * 付款的结算货币
         */
        private String currency;
        /**
         * 支付金额
         */
        private String amountValue;
        /**
         * 商户端支付结果页面
         */
        private String paymentRedirectUrl;
        /**
         * 支付结果通知地址
         */
        private String paymentNotifyUrl;
        /**
         * 订单描述
         */
        private String orderDescription;
        /**
         * 商户订单唯一标识
         */
        private String referenceOrderId;
    }
 
 
    @Data
    public static class PaymentNotify {
        /**
         * 支付状态通知的类型
         * PAYMENT_RESULT: 表示通知是关于支付结果的。
         * PAYMENT_PENDING: 表示用户已完成支付。商家需要等待最终的支付结果。
         */
        private String notifyType;
        /**
         * 支付结果的详细信息,如支付状态、结果代码和结果消息
         */
        private String result;
        /**
         * 商户为识别支付请求而分配的专属 ID
         */
        private String paymentRequestId;
        /**
         * Antom 为识别支付而分配的支付 ID。paymentId 与 paymentRequestId 之间存在一对一对应关系。
         */
        private String paymentId;
        /**
         * 商家请求在订单币种中接收的支付金额。
         */
        private String paymentAmount;
        /**
         * 支付创建的日期和时间。
         */
        private Date paymentCreateTime;
        /**
         * 支付成功达到最终状态的日期和时间。
         */
        private Date paymentTime;
        /**
         * 支付结果信息
         */
        private String paymentResultInfo;
    }
}