1
+ /*
2
+ * Ant Group
3
+ * Copyright (c) 2004-2024 All Rights Reserved.
4
+ */
5
+ package com .alipay .global .api .example ;
6
+
7
+ import com .alipay .global .api .AlipayClient ;
8
+ import com .alipay .global .api .DefaultAlipayClient ;
9
+ import com .alipay .global .api .exception .AlipayApiException ;
10
+ import com .alipay .global .api .model .ams .Address ;
11
+ import com .alipay .global .api .model .ams .Amount ;
12
+ import com .alipay .global .api .model .ams .Buyer ;
13
+ import com .alipay .global .api .model .ams .CardVerificationResult ;
14
+ import com .alipay .global .api .model .ams .Env ;
15
+ import com .alipay .global .api .model .ams .Goods ;
16
+ import com .alipay .global .api .model .ams .OsType ;
17
+ import com .alipay .global .api .model .ams .RiskThreeDSResult ;
18
+ import com .alipay .global .api .model .ams .Shipping ;
19
+ import com .alipay .global .api .model .ams .TerminalType ;
20
+ import com .alipay .global .api .model .ams .UserName ;
21
+ import com .alipay .global .api .model .risk .AuthorizationPhase ;
22
+ import com .alipay .global .api .model .risk .Order ;
23
+ import com .alipay .global .api .model .risk .PaymentMethodMetaData ;
24
+ import com .alipay .global .api .model .risk .Merchant ;
25
+ import com .alipay .global .api .model .risk .PaymentDetail ;
26
+ import com .alipay .global .api .model .risk .PaymentMethod ;
27
+ import com .alipay .global .api .request .ams .risk .RiskDecideRequest ;
28
+ import com .alipay .global .api .request .ams .risk .RiskReportRequest ;
29
+ import com .alipay .global .api .request .ams .risk .SendPaymentResultRequest ;
30
+ import com .alipay .global .api .request .ams .risk .SendRefundResultRequest ;
31
+ import com .alipay .global .api .request .ams .risk .tee .exception .CryptoException ;
32
+ import com .alipay .global .api .response .ams .risk .RiskDecideResponse ;
33
+ import com .alipay .global .api .response .ams .risk .RiskReportResponse ;
34
+ import com .alipay .global .api .response .ams .risk .SendPaymentResultResponse ;
35
+ import com .alipay .global .api .response .ams .risk .SendRefundResultResponse ;
36
+ import com .alipay .global .api .request .ams .risk .tee .encryptstrategy .RiskDecideEncryptStrategy ;
37
+ import com .alipay .global .api .request .ams .risk .tee .enums .EncryptKeyEnum ;
38
+
39
+ import java .util .Arrays ;
40
+ import java .util .Collections ;
41
+ import java .util .Date ;
42
+ import java .util .List ;
43
+
44
+
45
+ public class RiskDecideTeeDemoCode {
46
+ private static final String CLIENT_ID = "" ;
47
+ private static final String GATE_WAY_URL = "" ;
48
+ private static final String merchantPrivateKey = "" ;
49
+ private static final String alipayPublicKey = "" ;
50
+ private static final AlipayClient defaultAlipayClient = new DefaultAlipayClient (GATE_WAY_URL , merchantPrivateKey , alipayPublicKey , CLIENT_ID );
51
+
52
+ private static final String DATA_KEY = "" ;
53
+ public static void main (String [] args ) {
54
+ preAuthDecide ();
55
+ }
56
+
57
+ public static RiskDecideResponse preAuthDecide () {
58
+ RiskDecideRequest request = new RiskDecideRequest ();
59
+ request .setReferenceTransactionId ("test_20231012091493242" );
60
+ request .setAuthorizationPhase (AuthorizationPhase .PRE_AUTHORIZATION );
61
+ // 1. build plaintext request
62
+ buildRiskDecideRequest (request );
63
+ RiskDecideResponse response = null ;
64
+ try {
65
+ // 2. encrypt request
66
+ encryptRequest (request );
67
+ // 3. send request
68
+ response = defaultAlipayClient .execute (request );
69
+ } catch (CryptoException e ) {
70
+ // TODO Handle CryptoException and log
71
+ } catch (AlipayApiException e ) {
72
+ // TODO Handle AlipayApiException and log
73
+ }
74
+ return response ;
75
+ }
76
+
77
+ public static RiskDecideResponse postAuthDecide () {
78
+ RiskDecideRequest request = new RiskDecideRequest ();
79
+ request .setReferenceTransactionId ("test_20231012091493242" );
80
+ request .setAuthorizationPhase (AuthorizationPhase .POST_AUTHORIZATION );
81
+ // 1. build plaintext request
82
+ buildRiskDecideRequest (request );
83
+ PaymentDetail paymentDetail = request .getPaymentDetails ().get (0 );
84
+ PaymentMethodMetaData paymentMethodMetaData = paymentDetail .getPaymentMethod ().getPaymentMethodMetaData ();
85
+ CardVerificationResult cardVerificationResult = new CardVerificationResult ();
86
+ cardVerificationResult .setAuthenticationType ("3D" );
87
+ cardVerificationResult .setAuthorizationCode ("10000" );
88
+ RiskThreeDSResult riskThreeDSResult = new RiskThreeDSResult ();
89
+ riskThreeDSResult .setEci ("00" );
90
+ riskThreeDSResult .setThreeDSVersion ("2.0" );
91
+ riskThreeDSResult .setCavv ("0" );
92
+ riskThreeDSResult .setThreeDSInteractionMode ("CHALLENGED" );
93
+ cardVerificationResult .setThreeDSResult (riskThreeDSResult );
94
+ paymentMethodMetaData .setCardVerificationResult (cardVerificationResult );
95
+ RiskDecideResponse response = null ;
96
+ try {
97
+ // 2. encrypt request
98
+ encryptRequest (request );
99
+ // 3. send request
100
+ response = defaultAlipayClient .execute (request );
101
+ } catch (CryptoException e ) {
102
+ // TODO Handle CryptoException and log
103
+ } catch (AlipayApiException e ) {
104
+ // TODO Handle AlipayApiException and log
105
+ }
106
+ return response ;
107
+ }
108
+
109
+ public static void encryptRequest (RiskDecideRequest request ) throws CryptoException {
110
+ // 2.1. build encryptList
111
+ List <EncryptKeyEnum > encryptList = Arrays .asList (
112
+ EncryptKeyEnum .BUYER_EMAIL ,
113
+ EncryptKeyEnum .BUYER_PHONE_NO ,
114
+ EncryptKeyEnum .BUYER_REGISTRATION_TIME ,
115
+ EncryptKeyEnum .CARDHOLDER_NAME ,
116
+ EncryptKeyEnum .SHIPPING_ADDRESS1 ,
117
+ EncryptKeyEnum .SHIPPING_ADDRESS2 ,
118
+ EncryptKeyEnum .SHIPPING_NAME ,
119
+ EncryptKeyEnum .SHIP_TO_EMAIL ,
120
+ EncryptKeyEnum .SHIPPING_PHONE_NO
121
+ );
122
+ // 2.2. encrypt request by using RiskDecideEncryptStrategy
123
+ RiskDecideEncryptStrategy strategy = new RiskDecideEncryptStrategy ();
124
+ strategy .encrypt (DATA_KEY , request , encryptList );
125
+ }
126
+
127
+ public static SendPaymentResultResponse sendPaymentResult () {
128
+ SendPaymentResultRequest request = new SendPaymentResultRequest ();
129
+ request .setReferenceTransactionId ("test_20231012091493242" );
130
+
131
+ request .setPaymentStatus ("SUCCESS" );
132
+ request .setReferenceTransactionId ("test_20231012091493242" );
133
+ CardVerificationResult cardVerificationResult = new CardVerificationResult ();
134
+ cardVerificationResult .setAuthenticationType ("3D" );
135
+ cardVerificationResult .setAuthorizationCode ("10000" );
136
+ RiskThreeDSResult threeDSResult = new RiskThreeDSResult ();
137
+ threeDSResult .setEci ("05" );
138
+ threeDSResult .setThreeDSVersion ("2.0" );
139
+ threeDSResult .setCavv ("0" );
140
+ threeDSResult .setThreeDSInteractionMode ("CHALLENGED" );
141
+ cardVerificationResult .setThreeDSResult (threeDSResult );
142
+ request .setCardVerificationResult (cardVerificationResult );
143
+ SendPaymentResultResponse response = null ;
144
+ try {
145
+ response = defaultAlipayClient .execute (request );
146
+ } catch (AlipayApiException e ) {
147
+ // TODO Handle AlipayApiException and log
148
+ }
149
+ return response ;
150
+ }
151
+
152
+ public static SendRefundResultResponse sendPaymentRefund () {
153
+ SendRefundResultRequest request = new SendRefundResultRequest ();
154
+ request .setReferenceTransactionId ("test_20231012091493242" );
155
+
156
+ SendRefundResultResponse response = null ;
157
+ try {
158
+ response = defaultAlipayClient .execute (request );
159
+ } catch (AlipayApiException e ) {
160
+ // TODO Handle AlipayApiException and log
161
+ }
162
+ return response ;
163
+ }
164
+
165
+ public static RiskReportResponse reportRisk () {
166
+ RiskReportRequest request = new RiskReportRequest ();
167
+ request .setReferenceTransactionId ("test_20231012091493242" );
168
+ request .setReportReason ("test" );
169
+ request .setRiskType ("FRAUD" );
170
+ request .setRiskOccurrenceTime (new Date ());
171
+
172
+ RiskReportResponse response = null ;
173
+ try {
174
+ response = defaultAlipayClient .execute (request );
175
+ } catch (AlipayApiException e ) {
176
+ // TODO Handle AlipayApiException and log
177
+ }
178
+ return response ;
179
+ }
180
+
181
+ private static void buildRiskDecideRequest (RiskDecideRequest request ) {
182
+ Order order = new Order ();
183
+ order .setReferenceOrderId ("test_202310120914932421" );
184
+ Amount orderAmount = new Amount ();
185
+ orderAmount .setCurrency ("BRL" );
186
+ orderAmount .setValue ("30000" );
187
+ order .setOrderAmount (orderAmount );
188
+ order .setOrderDescription ("Cappuccino #grande (Mika's coffee shop)" );
189
+ Merchant merchant = new Merchant ();
190
+ merchant .setReferenceMerchantId ("SM_001" );
191
+ merchant .
setMerchantEmail (
"[email protected] " );
192
+ order .setMerchant (merchant );
193
+ Goods goods = new Goods ();
194
+ goods .setReferenceGoodsId ("383382011_SGAMZ-904520356" );
195
+ goods .setGoodsName ("[3 Boxes] Starbucks Cappuccino Milk Coffee Pods / Coffee Capsules by Nescafe Dolce Gusto" );
196
+ goods .setGoodsCategory ("Digital Goods/Digital Vouchers/Food and Beverages" );
197
+ goods .setDeliveryMethodType ("DIGITAL" );
198
+ goods .setGoodsQuantity ("1" );
199
+ Amount goodsAmount = new Amount ();
200
+ goodsAmount .setValue ("30000" );
201
+ goodsAmount .setCurrency ("BRL" );
202
+ goods .setGoodsUnitAmount (goodsAmount );
203
+ order .setGoods (Collections .singletonList (goods ));
204
+ Shipping shipping = new Shipping ();
205
+ shipping .setShippingCarrier ("FedEx" );
206
+ shipping .setShippingPhoneNo ("12345678912" );
207
+ shipping .
setShipToEmail (
"[email protected] " );
208
+ UserName shippingName = new UserName ();
209
+ shippingName .setFirstName ("Dehua" );
210
+ shippingName .setLastName ("Liu" );
211
+ shippingName .setMiddleName ("Skr" );
212
+ shippingName .setFullName ("Dehua Skr Liu" );
213
+ shipping .setShippingName (shippingName );
214
+ Address shippingAddress = new Address ();
215
+ shippingAddress .setRegion ("CN" );
216
+ shippingAddress .setState ("Zhejiang" );
217
+ shippingAddress .setCity ("Hangzhou" );
218
+ shippingAddress .setAddress1 ("Wuchang road" );
219
+ shippingAddress .setAddress2 ("Xihu" );
220
+ shippingAddress .setZipCode ("310000" );
221
+ shipping .setShippingAddress (shippingAddress );
222
+ order .setShipping (shipping );
223
+ request .setOrders (Collections .singletonList (order ));
224
+
225
+ Buyer buyer = new Buyer ();
226
+ buyer .setReferenceBuyerId ("test12345678" );
227
+ buyer .setBuyerPhoneNo ("12345678912" );
228
+ buyer .
setBuyerEmail (
"[email protected] " );
229
+ buyer .setIsAccountVerified (true );
230
+ buyer .setSuccessfulOrderCount (100 );
231
+ UserName buyerName = new UserName ();
232
+ buyerName .setFirstName ("Dehua" );
233
+ buyerName .setLastName ("Liu" );
234
+ buyerName .setMiddleName ("Skr" );
235
+ buyerName .setFullName ("Dehua Skr Liu" );
236
+ buyer .setBuyerName (buyerName );
237
+ buyer .setBuyerRegistrationTime ("2023-01-01T12:08:56+08:00" );
238
+ request .setBuyer (buyer );
239
+
240
+ Amount actualPaymentAmount = new Amount ();
241
+ actualPaymentAmount .setCurrency ("BRL" );
242
+ actualPaymentAmount .setValue ("300000" );
243
+ request .setActualPaymentAmount (actualPaymentAmount );
244
+
245
+ PaymentDetail paymentDetail = new PaymentDetail ();
246
+ Amount paymentAmount = new Amount ();
247
+ paymentAmount .setCurrency ("BRL" );
248
+ paymentAmount .setValue ("300000" );
249
+ paymentDetail .setAmount (paymentAmount );
250
+ PaymentMethod paymentMethod = new PaymentMethod ();
251
+ paymentMethod .setPaymentMethodId ("0656XXXXXXX0001" );
252
+ paymentMethod .setPaymentMethodType ("CARD" );
253
+ PaymentMethodMetaData paymentMethodMetaData = new PaymentMethodMetaData ();
254
+ paymentMethodMetaData .setCpf ("02987654320" );
255
+ paymentMethodMetaData .setExpiryMonth ("12" );
256
+ paymentMethodMetaData .setExpiryYear ("29" );
257
+ paymentMethodMetaData .setCardNo ("4117347806156383" );
258
+ UserName cardHolderName = new UserName ();
259
+ cardHolderName .setFirstName ("Tom" );
260
+ cardHolderName .setLastName ("Jay" );
261
+ paymentMethodMetaData .setCardHolderName (cardHolderName );
262
+ Address billingAddress = new Address ();
263
+ billingAddress .setRegion ("CN" );
264
+ billingAddress .setState ("Zhejiang" );
265
+ billingAddress .setCity ("Hangzhou" );
266
+ billingAddress .setAddress1 ("test_address1" );
267
+ billingAddress .setAddress2 ("test_address2" );
268
+ billingAddress .setZipCode ("310000" );
269
+ paymentMethodMetaData .setBillingAddress (billingAddress );
270
+ paymentMethod .setPaymentMethodMetaData (paymentMethodMetaData );
271
+ paymentDetail .setPaymentMethod (paymentMethod );
272
+ request .setPaymentDetails (Collections .singletonList (paymentDetail ));
273
+
274
+ Env env = new Env ();
275
+ env .setTerminalType (TerminalType .APP );
276
+ env .setClientIp ("00.00.00.00" );
277
+ env .setOsType (OsType .IOS );
278
+ env .setDeviceLanguage ("zh_CN" );
279
+ env .setDeviceId ("test_deviceId" );
280
+ request .setEnv (env );
281
+
282
+ Amount discountAmount = new Amount ();
283
+ discountAmount .setCurrency ("BRL" );
284
+ discountAmount .setValue ("0" );
285
+ request .setDiscountAmount (discountAmount );
286
+ }
287
+
288
+ }
0 commit comments