-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathRiskifiedClient.java
More file actions
1120 lines (1015 loc) · 51.6 KB
/
RiskifiedClient.java
File metadata and controls
1120 lines (1015 loc) · 51.6 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.riskified;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.riskified.models.*;
import com.riskified.validations.FieldBadFormatException;
import com.riskified.validations.IValidated;
import com.riskified.validations.Validation;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AUTH;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.MalformedChallengeException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
/**
* Riskified API Client
* The client implements the API for Riskified as described in:
* http://apiref.riskified.com/
*/
public class RiskifiedClient {
private Validation validation = Validation.ALL;
private Environment environment = Environment.SANDBOX;
private String baseUrl;
private String baseUrlSyncAnalyze;
private String decoBaseUrl;
private String screenBaseUrl;
private String accountBaseUrl;
private String shopUrl;
private SHA256Handler sha256Handler;
private int requestTimeout = 10000;
private int connectionTimeout = 5000;
private String authKey;
private String proxyUrl;
private int proxyPort;
private String proxyUsername;
private String proxyPassword;
private HttpClientContext context;
private List<RiskifiedLogListener> logListeners = new ArrayList<RiskifiedLogListener>();
/**
* Riskified API client
* use configuration file: "src/main/resources/riskified_sdk.properties"
* uses the keys: shopUrl, authKey, environment, debugRiskifiedHostUrl
* see full doc on GitHub
* @throws RiskifiedError When there was a critical error, look at the exception to see more data
*/
public RiskifiedClient() throws RiskifiedError {
Properties properties = new Properties();
try {
properties.load(getClass().getClassLoader().getResourceAsStream("riskified_sdk.properties"));
} catch (IOException e) {
throw new RiskifiedError("There was an error reading the config file in: src/main/resources/riskified_sdk.properties");
}
String shopUrl = properties.getProperty("shopUrl");
String authKey = properties.getProperty("authKey");
String environmentType = properties.getProperty("environment");
String validationType = properties.getProperty("validation");
String proxyUrl = properties.getProperty("proxyUrl");
String proxyPort = properties.getProperty("proxyPort");
String proxyUserName = properties.getProperty("proxyUsername");
String proxyPassword = properties.getProperty("proxyPassword");
if (validationType.equals("NONE")) {
validation = Validation.NONE;
} else if (validationType.equals("IGNORE_MISSING")) {
validation = Validation.IGNORE_MISSING;
} else if(validationType.equals("ALL")) {
validation = Validation.ALL;
}
if (environmentType.equals("DEBUG")) {
environment = Environment.DEBUG;
} else if (environmentType.equals("PRODUCTION")) {
environment = Environment.PRODUCTION;
} else if (environmentType.equals("SANDBOX")) {
environment = Environment.SANDBOX;
} else if (environmentType.equals("CHINA_PRODUCTION")) {
environment = Environment.CHINA_PRODUCTION;
}
init(shopUrl, authKey, Utils.getBaseUrlFromEnvironment(environment), Utils.getBaseUrlSyncAnalyzeFromEnvironment(environment), Utils.getDecoBaseFromEnvironment(environment), Utils.getAccountBaseFromEnvironment(environment), Utils.getScreenBaseFromEnvironment(environment),validation);
if (proxyUrl != null) {
initProxy(proxyUrl, proxyPort, proxyUserName, proxyPassword);
}
}
/**
* Riskified API client
* don't use config file
* @param shopUrl The shop URL as registered in Riskified
* @param authKey From the advance settings in Riskified web site
* @param environment The Riskified environment (SANDBOX / PRODUCTION)
* @throws RiskifiedError When there was a critical error, look at the exception to see more data
*/
public RiskifiedClient(String shopUrl, String authKey, Environment environment) throws RiskifiedError {
init(shopUrl, authKey, Utils.getBaseUrlFromEnvironment(environment), Utils.getBaseUrlSyncAnalyzeFromEnvironment(environment), Utils.getDecoBaseFromEnvironment(environment), Utils.getAccountBaseFromEnvironment(environment), Utils.getScreenBaseFromEnvironment(environment), Validation.ALL);
}
/**
* Riskified API client (with proxy)
* don't use config file
* @param shopUrl The shop URL as registered in Riskified
* @param authKey From the advance settings in Riskified web site
* @param environment The Riskified environment (SANDBOX / PRODUCTION)
* @param proxyClientDetails proxy details
* @throws RiskifiedError When there was a critical error, look at the exception to see more data
*/
public RiskifiedClient(String shopUrl, String authKey, Environment environment, ProxyClientDetails proxyClientDetails) throws RiskifiedError {
init(shopUrl, authKey, Utils.getBaseUrlFromEnvironment(environment), Utils.getBaseUrlSyncAnalyzeFromEnvironment(environment), Utils.getDecoBaseFromEnvironment(environment), Utils.getAccountBaseFromEnvironment(environment), Utils.getScreenBaseFromEnvironment(environment), Validation.ALL);
initProxy(proxyClientDetails);
}
/**
* Riskified API client
* don't use config file
* @param shopUrl The shop URL as registered in Riskified
* @param authKey From the advance settings in Riskified web site
* @param environment The Riskified environment (SANDBOX / PRODUCTION)
* @param validation The sdk's validation strategy
* @throws RiskifiedError When there was a critical error, look at the exception to see more data
*/
public RiskifiedClient(String shopUrl, String authKey, Environment environment, Validation validation) throws RiskifiedError {
init(shopUrl, authKey, Utils.getBaseUrlFromEnvironment(environment), Utils.getBaseUrlSyncAnalyzeFromEnvironment(environment), Utils.getDecoBaseFromEnvironment(environment), Utils.getAccountBaseFromEnvironment(environment), Utils.getScreenBaseFromEnvironment(environment), validation);
}
/**
* Riskified API client (with proxy)
* don't use config file
* @param shopUrl The shop URL as registered in Riskified
* @param authKey From the advance settings in Riskified web site
* @param environment The Riskifed environment (SANDBOX / PRODUCTION)
* @param validation The sdk's validation strategy
* @param proxyClientDetails proxy details
* @throws RiskifiedError When there was a critical error, look at the exception to see more data
*/
public RiskifiedClient(String shopUrl, String authKey, Environment environment, Validation validation, ProxyClientDetails proxyClientDetails) throws RiskifiedError {
init(shopUrl, authKey, Utils.getBaseUrlFromEnvironment(environment), Utils.getBaseUrlSyncAnalyzeFromEnvironment(environment), Utils.getDecoBaseFromEnvironment(environment), Utils.getAccountBaseFromEnvironment(environment), Utils.getScreenBaseFromEnvironment(environment), validation);
initProxy(proxyClientDetails);
}
/**
* Riskified API client
* don't use config file
* @param shopUrl The shop URL as registered in Riskified
* @param authKey From the advance settings in Riskified web site
* @param validation The sdk's validation strategy
* @param baseUrl base riskified host
* @param baseUrlSyncAnalyze base riskified sync-api host
* @param decoBaseUrl deco host
* @param accountBaseUrl account host
* @throws RiskifiedError When there was a critical error, look at the exception to see more data
*/
public RiskifiedClient(String shopUrl, String authKey, Validation validation, String baseUrl, String baseUrlSyncAnalyze, String decoBaseUrl, String accountBaseUrl, String screenBaseUrl) throws RiskifiedError {
init(shopUrl, authKey, baseUrl, baseUrlSyncAnalyze, decoBaseUrl, accountBaseUrl, screenBaseUrl, validation);
}
private void init(String shopUrl, String authKey, String baseUrl, String baseUrlSyncAnalyze, String decoBaseUrl, String accountBaseUrl, String screenBaseUrl, Validation validationType) throws RiskifiedError {
this.baseUrl = baseUrl;
this.baseUrlSyncAnalyze = baseUrlSyncAnalyze;
this.decoBaseUrl = decoBaseUrl;
this.accountBaseUrl = accountBaseUrl;
this.screenBaseUrl = screenBaseUrl;
this.shopUrl = shopUrl;
this.sha256Handler = new SHA256Handler(authKey);
this.validation = validationType;
}
private void initProxy(String proxyUrl, String proxyPort, String proxyUsername, String proxyPassword) {
this.proxyUrl = proxyUrl;
this.proxyPort = Integer.parseInt(proxyPort);
this.proxyUsername = proxyUsername;
this.proxyPassword = proxyPassword;
}
private void initProxy(ProxyClientDetails proxyClientDetails) {
this.proxyUrl = proxyClientDetails.getProxyUrl();
this.proxyPort = proxyClientDetails.getProxyPort();
this.proxyUsername = proxyClientDetails.getProxyUsername();
this.proxyPassword = proxyClientDetails.getProxyPassword();
}
/**
* Send a new checkout order to Riskified
* @param order The checkout order to create (Checkout order has the same fields like Order but ALL fields are optional)
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response checkoutOrder(CheckoutOrder order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/checkout_create";
// Validation.ALL is not relevant when checkout.
if(validation != validation.NONE) {
validate(order, Validation.IGNORE_MISSING);
}
return postCheckoutOrder(new CheckoutOrderWrapper<CheckoutOrder>(order), url);
}
// TODO add other paramaters Riskified server will return
/**
* Send a new advise order to Riskified
* @param order The advise order to create
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response adviseOrder(CheckoutOrder order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/advise";
// Validation.ALL is not relevant when checkout.
if(validation != validation.NONE) {
validate(order, Validation.IGNORE_MISSING);
}
return postCheckoutOrder(new CheckoutOrderWrapper<CheckoutOrder>(order), url);
}
/**
* Send a new checkout order to Riskified
* @param order The checkout order to create (Checkout order has the same fields like Order but ALL fields are optional)
* @param validation Determines what type of validation will take place
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response checkoutOrder(CheckoutOrder order, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/checkout_create";
validate(order, validation);
return postCheckoutOrder(new CheckoutOrderWrapper<CheckoutOrder>(order), url);
}
/**
* Mark a previously checkout order has been denied.
* @param order The checkout denied order details, mark as denied and also can specify why it was denied.
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response checkoutDeniedOrder(CheckoutDeniedOrder order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/checkout_denied";
validate(order);
return postCheckoutOrder(new CheckoutOrderWrapper<CheckoutDeniedOrder>(order), url);
}
/**
* Mark a previously checkout order has been denied.
* @param order The checkout denied order details, mark as denied and also can specify why it was denied.
* @param validation Determines what type of validation will take place
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response checkoutDeniedOrder(CheckoutDeniedOrder order, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/checkout_denied";
validate(order, validation);
return postCheckoutOrder(new CheckoutOrderWrapper<CheckoutDeniedOrder>(order), url);
}
/**
* Send a new order to Riskified
* Depending on your current plan, the newly created order might not be submitted automatically for review.
* @param order An order to create
* Any missing fields (such as BIN number or AVS result code) that are unavailable during the time of the request should be skipped or passed as null
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response createOrder(Order order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/create";
validate(order);
return postOrder(new OrderWrapper<Order>(order), url);
}
/**
* Send a new order to Riskified
* Depending on your current plan, the newly created order might not be submitted automatically for review.
* @param order An order to create
* @param validation Determines what type of validation will take place
* Any missing fields (such as BIN number or AVS result code) that are unavailable during the time of the request should be skipped or passed as null
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response createOrder(Order order, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/create";
validate(order, validation);
return postOrder(new OrderWrapper<Order>(order), url);
}
/**
* Submit a new or existing order to Riskified for review
* Forces the order to be submitted for review, regardless of your current plan.
* @param order An order to submit for review.
* Any missing fields (such as BIN number or AVS result code) that are unavailable during the time of the request should be skipped or passed as null.
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response submitOrder(Order order) throws IOException, FieldBadFormatException {
return submitOrder(order, validation);
}
/**
* Submit a new or existing order to Riskified for review
* Forces the order to be submitted for review, regardless of your current plan.
* @param order An order to submit for review.
* Any missing fields (such as BIN number or AVS result code) that are unavailable during the time of the request should be skipped or passed as null.
* @param validation Determines what type of validation will take place
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response submitOrder(Order order, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/submit";
validate(order, validation);
return postOrder(new OrderWrapper<Order>(order), url);
}
/**
* Update details of an existing order.
* Orders are differentiated by their id field. To update an existing order, include its id and any up-to-date data.
* @param order A (possibly incomplete) order to update
* The order must have an id field referencing an existing order and at least one additional field to update.
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response updateOrder(Order order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/update";
// Validation.ALL is not relevant when updating.
if(validation != validation.NONE) {
validate(order, Validation.IGNORE_MISSING);
}
return postOrder(new OrderWrapper<Order>(order), url);
}
/**
* Update details of an existing order.
* Orders are differentiated by their id field. To update an existing order, include its id and any up-to-date data.
* @param order A (possibly incomplete) order to update
* The order must have an id field referencing an existing order and at least one additional field to update.
* @param validation Determines what type of validation will take place
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response updateOrder(Order order, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/update";
validate(order, validation);
return postOrder(new OrderWrapper<Order>(order), url);
}
/**
* Mark a previously submitted order as cancelled.
* If the order has not yet been reviewed, it is excluded from future review.
* If the order has already been reviewed and approved, canceling it will also trigger a full refund on any associated charges.
* An order can only be cancelled during a relatively short time window after its creation.
* @param order The order to cancel
* @see CancelOrder
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response cancelOrder(CancelOrder order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/cancel";
validate(order);
return postOrder(new OrderWrapper<CancelOrder>(order), url);
}
/**
* Mark a previously submitted order as cancelled.
* If the order has not yet been reviewed, it is excluded from future review.
* If the order has already been reviewed and approved, canceling it will also trigger a full refund on any associated charges.
* An order can only be cancelled during a relatively short time window after its creation.
* @param order The order to cancel
* @param validation Determines what type of validation will take place
* @see CancelOrder
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response cancelOrder(CancelOrder order, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/cancel";
validate(order, validation);
return postOrder(new OrderWrapper<CancelOrder>(order), url);
}
/**
* Issue a partial refund for an existing order.
* Any associated charges will be updated to reflect the new order total amount.
* @param order The refund Order
* @see RefundOrder
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response refundOrder(RefundOrder order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/refund";
validate(order);
return postOrder(new OrderWrapper<RefundOrder>(order), url);
}
/**
* Issue a partial refund for an existing order.
* Any associated charges will be updated to reflect the new order total amount.
* @param order The refund Order
* @param validation Determines what type of validation will take place
* @see RefundOrder
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response refundOrder(RefundOrder order, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/refund";
validate(order, validation);
return postOrder(new OrderWrapper<RefundOrder>(order), url);
}
/**
* Mark a previously submitted order that is was fulfilled.
* @param order The fulfillment order details
* @see FulfillmentOrder
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response fulfillOrder(FulfillmentOrder order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/fulfill";
validate(order);
return postOrder(new OrderWrapper<FulfillmentOrder>(order), url);
}
/**
* Mark a previously submitted order that is was fulfilled.
* @param order The fulfillment order details
* @param validation Determines what type of validation will take place
* @see FulfillmentOrder
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response fulfillOrder(FulfillmentOrder order, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/fulfill";
validate(order, validation);
return postOrder(new OrderWrapper<FulfillmentOrder>(order), url);
}
/**
* Set the decision made for order that was not submitted.
* @param order The decision order details
* @see DecisionOrder
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response decisionOrder(DecisionOrder order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/decision";
validate(order);
return postOrder(new OrderWrapper<DecisionOrder>(order), url);
}
/**
* Set the decision made for order that was not submitted.
* @param order The decision order details
* @param validation Determines what type of validation will take place
* @see DecisionOrder
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response decisionOrder(DecisionOrder order, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/decision";
validate(order, validation);
return postOrder(new OrderWrapper<DecisionOrder>(order), url);
}
/**
* Screen a new order to Riskified
* Analyzes the order synchronicly, the returned status is Riskified's screen review result.
* @param order An order to screen
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response screenOrder(CheckoutOrder order) throws IOException, FieldBadFormatException {
String url = screenBaseUrl + "/api/screen";
validate(order, validation);
return postCheckoutOrder(new CheckoutOrderWrapper<CheckoutOrder>(order), url);
}
/**
* Send and analyze a new order to Riskified
* Analyzes the order synchronicly, the returned status is Riskified's analysis review result.
* @param order An order to create and analyze
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response analyzeOrder(Order order) throws IOException, FieldBadFormatException {
String url = baseUrlSyncAnalyze + "/api/decide";
validate(order);
return postOrder(new OrderWrapper<Order>(order), url);
}
/**
* Check eligibility for Deco
* After checkout_denied, Inquiry if order is eligible for Deco.
* @param order An order to check Deco eligibility (only the order id is needed)
* @see Response
* @return Response object, including the status from the Deco server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response eligible(Order order) throws IOException, FieldBadFormatException {
String url = decoBaseUrl + "/api/eligible";
return postOrder(new OrderWrapper<Order>(order), url);
}
/**
* Opt-in to Deco
* Notifies Deco the customer has chosen to utilize Deco’s service
* @param order An order to opt-in to Deco payment (only the order id is needed)
* @see Response
* @return Response object, including the status from the Deco server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response opt_in(Order order) throws IOException, FieldBadFormatException {
String url = decoBaseUrl + "/api/opt_in";
return postOrder(new OrderWrapper<Order>(order), url);
}
/**
* The chargeback API will allow merchants to request a fraud-related chargeback reimbursement.
* The submitted request will be processed within 48 hours.
* Eligible requests will trigger an automatic credit refund by Riskified.
* An eligible chargeback reimbursement request must match the details provided originally within the order JSON
* and contain a fraudulent chargeback reason code. For tangible goods,
* Riskified uses the tracking number provided in the fulfillment parameter to ensure the parcel was delivered
* to the address provided within the order JSON. Riskified reserves the right to request additional documentation
* pertaining to submitted chargebacks as part of the eligibility review process.
* @param order The order to mark as chargeback
* @see ChargebackOrder
* @see Response
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response chargebackOrder(ChargebackOrder order) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/chargeback";
validate(order);
return postOrder(new OrderWrapper<ChargebackOrder>(order), url);
}
/**
* Send an array (batch) of existing/historical orders to Riskified.
* Use the decision field to provide information regarding each order status.
*
* Orders sent will be used to build analysis models to better analyze newly received orders.
*
* @param orders A list of historical orders to send
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response historicalOrders(ArrayOrders orders) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/historical";
validate(orders);
return postOrder(orders, url);
}
/**
* Send an array (batch) of existing/historical orders to Riskified.
* Use the financial_status field to provide information regarding each order status:
* * 'approved' - approved orders
* * 'declined-fraud' - declined orders (refunded or voided) as suspected fraud
* * 'declined' - declined orders (refunded or voided) without connection to fraud
* * 'chargeback' - orders that received a chargeback
*
* Orders sent will be used to build analysis models to better analyze newly received orders.
*
* @param orders A list of historical orders to send
* @param validation Determines what type of validation will take place
* @return Response object, including the status from Riskified server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response historicalOrders(ArrayOrders orders, Validation validation) throws IOException, FieldBadFormatException {
String url = baseUrl + "/api/historical";
validate(orders, validation);
return postOrder(orders, url);
}
/**
* Login Account Action
* Notifies Riskified that there has been a login account action
* @param login A login object
* @see Response
* @return Response object, including the status from the Deco server
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response login(Login login) throws IOException, FieldBadFormatException {
String url = accountBaseUrl + "/customers/login";
validate(login, validation);
return postOrder(login, url);
}
/**
* Customer Create Account Action
* Notifies Riskified that there has been a customer create account action
* @param customerCreate A customer create object
* @see Response
* @return OK if good, object with error if bad request
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response customerCreate(CustomerCreate customerCreate) throws IOException, FieldBadFormatException {
String url = accountBaseUrl + "/customers/customer_create";
validate(customerCreate, validation);
return postOrder(customerCreate, url);
}
/**
* Customer Update Account Action
* Notifies Riskified that there has been a customer update account action
* @param customerUpdate A customer create object
* @see Response
* @return OK if good, object with error if bad request
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response customerUpdate(CustomerUpdate customerUpdate) throws IOException, FieldBadFormatException {
String url = accountBaseUrl + "/customers/customer_update";
validate(customerUpdate, validation);
return postOrder(customerUpdate, url);
}
/**
* Logout Account Action
* Notifies Riskified that there has been a logout account action
* @param logout A logout object
* @see Response
* @return OK if good, object with error if bad request
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response logout(Logout logout) throws IOException, FieldBadFormatException {
String url = accountBaseUrl + "/customers/logout";
validate(logout, validation);
return postOrder(logout, url);
}
/**
* ResetPassword Account Action
* Notifies Riskified that there has been a reset password account action
* @param resetPassword A resetPassword object
* @see Response
* @return OK if good, object with error if bad request
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response resetPassword(ResetPassword resetPassword) throws IOException, FieldBadFormatException {
String url = accountBaseUrl + "/customers/reset_password";
validate(resetPassword, validation);
return postOrder(resetPassword, url);
}
/**
* Wishlist Account Action
* Notifies Riskified that there has been a wishlist account action
* @param wishlist A Wishlist object
* @see Response
* @return OK if good, object with error if bad request
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response wishlist(Wishlist wishlist) throws IOException, FieldBadFormatException {
String url = accountBaseUrl + "/customers/wishlist";
validate(wishlist, validation);
return postOrder(wishlist, url);
}
/**
* Redeem Account Action
* Notifies Riskified that there has been a redeem account action
* @param redeem A Redeem object
* @see Response
* @return OK if good, object with error if bad request
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response redeem(Redeem redeem) throws IOException, FieldBadFormatException {
String url = accountBaseUrl + "/customers/redeem";
validate(redeem, validation);
return postOrder(redeem, url);
}
/**
* Contact Account Action
* Notifies Riskified that there has been a contact account action
* @param contact A Contact object
* @see Response
* @return OK if good, object with error if bad request
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response contact(Contact contact) throws IOException, FieldBadFormatException {
String url = accountBaseUrl + "/customers/contact";
validate(contact, validation);
return postOrder(contact, url);
}
/**
* Verification Account Action
* Notifies Riskified that a verification attempt has been made
* @param verification A Verification object
* @see Response
* @return OK if good, object with error if bad request
* @throws ClientProtocolException in case of a problem or the connection was aborted
* @throws IOException in case of an http protocol error
* @throws HttpResponseException The server respond status wasn't 200
* @throws FieldBadFormatException bad format found on field
*/
public Response verification(Verification verification) throws IOException, FieldBadFormatException {
String url = accountBaseUrl + "/customers/verification";
validate(verification, validation);
return postOrder(verification, url);
}
private Response postCheckoutOrder(Object data, String url) throws IOException, FieldBadFormatException {
HttpPost request = createPostRequest(url);
addDataToRequest(data, request);
HttpResponse response;
HttpClient client = constructHttpClient();
response = executeClient(client, request);
String postBody = EntityUtils.toString(response.getEntity(), "UTF-8");
int status = response.getStatusLine().getStatusCode();
Response responseObject = getCheckoutResponseObject(postBody);
switch (status) {
case 200:
return responseObject;
case 400:
throw new HttpResponseException(status, responseObject.getError().getMessage());
case 401:
throw new HttpResponseException(status, responseObject.getError().getMessage());
case 404:
throw new HttpResponseException(status, responseObject.getError().getMessage());
case 504:
throw new HttpResponseException(status, "Temporary error, please retry");
default:
throw new HttpResponseException(500, "Contact Riskified support");
}
}
private HttpClient constructHttpClient() {
RequestConfig.Builder requestBuilder = RequestConfig.custom()
.setConnectTimeout(connectionTimeout)
.setConnectionRequestTimeout(requestTimeout);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(requestBuilder.build());
if (this.proxyUrl != null) {
setProxyWithAuth(builder);
}
return builder.build();
}
private HttpResponse executeClient(HttpClient client, HttpPost request)
throws IOException {
HttpResponse response;
if (context != null) {
response = client.execute(request, context);
} else {
response = client.execute(request);
}
return response;
}
private CredentialsProvider getHttpProxyCredentials() {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(new HttpHost(this.proxyUrl, this.proxyPort)),
new UsernamePasswordCredentials(this.proxyUsername, this.proxyPassword));
return credsProvider;
}
private void setProxyWithAuth(HttpClientBuilder builder) {
builder.setProxy(new HttpHost(proxyUrl, proxyPort));
builder.setDefaultCredentialsProvider(getHttpProxyCredentials());
builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
if (this.context == null) {
try {
setProxyContext();
} catch (MalformedChallengeException e) {
System.out.println("Error: failed to process challenge for proxy");
}
}
}
private void setProxyContext() throws MalformedChallengeException {
BasicScheme proxyAuth = new BasicScheme();
proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH,
"BASIC realm=default"));
BasicAuthCache authCache = new BasicAuthCache();
authCache.put(new HttpHost(this.proxyUrl, this.proxyPort), proxyAuth);
HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
context.setCredentialsProvider(getHttpProxyCredentials());
this.context = context;
}
private Response postOrder(Object data, String url) throws IOException {
HttpPost request = createPostRequest(url);
addDataToRequest(data, request);
HttpResponse response;
HttpClient client = constructHttpClient();
response = executeClient(client, request);
String postBody = EntityUtils.toString(response.getEntity());
int status = response.getStatusLine().getStatusCode();
Response responseObject = getResponseObject(postBody);
switch (status) {
case 200:
return responseObject;
case 400:
throw new HttpResponseException(status, postBody);
case 401:
throw new HttpResponseException(status, postBody);
case 404:
throw new HttpResponseException(status, postBody);
case 504:
throw new HttpResponseException(status, "Temporary error, please retry");
default:
throw new HttpResponseException(500, "Contact Riskified support");
}
}
private Response getResponseObject(String postBody) throws IOException {
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
Response res = gson.fromJson(postBody, Response.class);
return res;
}
private CheckoutResponse getCheckoutResponseObject(String postBody) throws IOException {
Gson gson = new Gson();
CheckoutResponse res = gson.fromJson(postBody, CheckoutResponse.class);
res.setOrder(res.getCheckout());
return res;
}
public void addListener(RiskifiedLogListener riskifiedListener) {
logListeners.add(riskifiedListener);
}
private void addDataToRequest(Object data, HttpPost postRequest) throws IllegalStateException, UnsupportedEncodingException {
String jsonData = JSONFormater.toJson(data);
for (RiskifiedLogListener riskifiedLogListener : logListeners)
riskifiedLogListener.getRequestLogs(jsonData);
byte[] body = jsonData.getBytes("UTF-8");
String hmac = sha256Handler.createSHA256(body);
postRequest.setHeader("X-RISKIFIED-HMAC-SHA256", hmac);
ByteArrayEntity input;
input = new ByteArrayEntity(body, ContentType.APPLICATION_JSON);
postRequest.setEntity(input);
}
private HttpPost createPostRequest(String url) {
HttpPost postRequest = new HttpPost(url);
postRequest.setHeader(HttpHeaders.ACCEPT, "application/vnd.riskified.com; version=2");
postRequest.setHeader("X-RISKIFIED-SHOP-DOMAIN", shopUrl);
postRequest.setHeader("User-Agent","riskified_java_sdk/1.3.16"); // TODO: take the version automatically