/
StripeTest.java
1973 lines (1715 loc) · 78.5 KB
/
StripeTest.java
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.stripe;
import com.google.common.collect.ImmutableMap;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.APIException;
import com.stripe.exception.AuthenticationException;
import com.stripe.exception.CardException;
import com.stripe.exception.InvalidRequestException;
import com.stripe.exception.StripeException;
import com.stripe.model.Account;
import com.stripe.model.Address;
import com.stripe.model.ApplicationFee;
import com.stripe.model.Balance;
import com.stripe.model.BalanceTransaction;
import com.stripe.model.BalanceTransactionCollection;
import com.stripe.model.Card;
import com.stripe.model.Charge;
import com.stripe.model.ChargeCollection;
import com.stripe.model.ChargeRefundCollection;
import com.stripe.model.Coupon;
import com.stripe.model.Customer;
import com.stripe.model.CustomerSubscriptionCollection;
import com.stripe.model.DeletedCard;
import com.stripe.model.DeletedCoupon;
import com.stripe.model.DeletedCustomer;
import com.stripe.model.DeletedInvoiceItem;
import com.stripe.model.DeletedPlan;
import com.stripe.model.DeletedRecipient;
import com.stripe.model.Dispute;
import com.stripe.model.Event;
import com.stripe.model.EvidenceDetails;
import com.stripe.model.EvidenceSubObject;
import com.stripe.model.FileUpload;
import com.stripe.model.FraudDetails;
import com.stripe.model.Invoice;
import com.stripe.model.InvoiceItem;
import com.stripe.model.InvoiceLineItemCollection;
import com.stripe.model.MetadataStore;
import com.stripe.model.Plan;
import com.stripe.model.Recipient;
import com.stripe.model.Refund;
import com.stripe.model.ShippingDetails;
import com.stripe.model.Subscription;
import com.stripe.model.Token;
import com.stripe.model.Transfer;
import com.stripe.net.RequestOptions;
import com.stripe.model.BitcoinReceiver;
import com.stripe.model.DeletedBitcoinReceiver;
import com.stripe.model.BitcoinTransaction;
import com.stripe.model.PaymentSource;
import com.stripe.model.PaymentSourceCollection;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class StripeTest {
static Map<String, Object> defaultCardParams = new HashMap<String, Object>();
static Map<String, Object> defaultDebitCardParams = new HashMap<String, Object>();
static Map<String, Object> defaultChargeParams = new HashMap<String, Object>();
static Map<String, Object> defaultCustomerParams = new HashMap<String, Object>();
static Map<String, Object> defaultPlanParams = new HashMap<String, Object>();
static Map<String, Object> defaultCouponParams = new HashMap<String, Object>();
static Map<String, Object> defaultTokenParams = new HashMap<String, Object>();
static Map<String, Object> defaultDebitTokenParams = new HashMap<String, Object>();
static Map<String, Object> defaultBankAccountParams = new HashMap<String, Object>();
static Map<String, Object> defaultRecipientParams = new HashMap<String, Object>();
static Map<String, Object> defaultBitcoinReceiverParams = new HashMap<String, Object>();
static RequestOptions cardSupportedRequestOptions;
static String getUniquePlanId() {
return String.format("MY-J-PLAN-%s", UUID.randomUUID().toString().substring(24));
}
static String getUniqueCouponId() {
return String.format("MY-J-COUPON-%s", UUID.randomUUID().toString().substring(24));
}
static Map<String, Object> getUniquePlanParams() {
Map<String, Object> uniqueParams = new HashMap<String, Object>();
uniqueParams.putAll(defaultPlanParams);
uniqueParams.put("id", getUniquePlanId());
return uniqueParams;
}
static Map<String, Object> getUniqueCouponParams() {
Map<String, Object> uniqueParams = new HashMap<String, Object>();
uniqueParams.putAll(defaultCouponParams);
uniqueParams.put("id", getUniqueCouponId());
return uniqueParams;
}
static Map<String, Object> getInvoiceItemParams() throws StripeException {
Map<String, Object> params = new HashMap<String, Object>();
Customer customer = Customer.create(defaultCustomerParams);
params.put("amount", 100);
params.put("currency", "usd");
params.put("customer", customer.getId());
return params;
}
static Map<String, Object> getTransferParams() throws StripeException {
Map<String, Object> params = new HashMap<String, Object>();
Recipient recipient = Recipient.create(defaultRecipientParams);
params.put("amount", 100);
params.put("currency", "usd");
params.put("recipient", recipient.getId());
params.put("card", recipient.getDefaultCard());
return params;
}
static InvoiceItem createDefaultInvoiceItem(Customer customer)
throws StripeException {
Map<String, Object> invoiceItemParams = new HashMap<String, Object>();
invoiceItemParams.put("amount", 100);
invoiceItemParams.put("currency", "usd");
invoiceItemParams.put("customer", customer.getId());
return InvoiceItem.create(invoiceItemParams);
}
static Customer createDefaultCustomerWithPlan(Plan plan)
throws StripeException {
Map<String, Object> customerWithPlanParams = new HashMap<String, Object>();
customerWithPlanParams.putAll(defaultCustomerParams);
customerWithPlanParams.put("plan", plan.getId());
Customer customer = Customer.create(customerWithPlanParams);
return customer;
}
static Recipient createDefaultRecipient()
throws StripeException {
Map<String, Object> recipientParams = new HashMap<String, Object>();
recipientParams.putAll(defaultRecipientParams);
return Recipient.create(recipientParams);
}
static Customer createDefaultCustomerWithDefaultBitcoinReceiver()
throws StripeException {
Customer customer = Customer.create(defaultCustomerParams);
BitcoinReceiver receiver = BitcoinReceiver.create(defaultBitcoinReceiverParams);
Map<String, Object> createParams = new HashMap<String, Object>();
createParams.put("source", receiver.getId());
customer.getSources().create(createParams);
return customer;
}
static Map<String, Object> getSubscriptionParams() throws StripeException {
Plan plan = Plan.create(getUniquePlanParams());
Map<String, Object> subscriptionParams = new HashMap<String, Object>();
subscriptionParams.put("plan", plan.getId());
return subscriptionParams;
}
@Before
public void before() {
Stripe.apiVersion = null;
}
@BeforeClass
public static void setUp() {
Stripe.apiKey = "tGN0bIwXnHdwOa85VABjPdSn8nWY7G7I"; // stripe public
// test key
cardSupportedRequestOptions = RequestOptions.builder().setStripeVersion("2015-02-16").build();
defaultCardParams.put("number", "4242424242424242");
defaultCardParams.put("exp_month", 12);
defaultCardParams.put("exp_year", 2015);
defaultCardParams.put("cvc", "123");
defaultCardParams.put("name", "J Bindings Cardholder");
defaultCardParams.put("address_line1", "140 2nd Street");
defaultCardParams.put("address_line2", "4th Floor");
defaultCardParams.put("address_city", "San Francisco");
defaultCardParams.put("address_zip", "94105");
defaultCardParams.put("address_state", "CA");
defaultCardParams.put("address_country", "USA");
defaultDebitCardParams.put("number", "4000056655665556");
defaultDebitCardParams.put("exp_month", 12);
defaultDebitCardParams.put("exp_year", 2015);
defaultDebitCardParams.put("cvc", "123");
defaultDebitCardParams.put("name", "J Bindings Debitholder");
defaultDebitCardParams.put("address_line1", "140 2nd Street");
defaultDebitCardParams.put("address_line2", "4th Floor");
defaultDebitCardParams.put("address_city", "San Francisco");
defaultDebitCardParams.put("address_zip", "94105");
defaultDebitCardParams.put("address_state", "CA");
defaultDebitCardParams.put("address_country", "USA");
defaultChargeParams.put("amount", 100);
defaultChargeParams.put("currency", "usd");
defaultChargeParams.put("card", defaultCardParams);
defaultTokenParams.put("card", defaultCardParams);
defaultDebitTokenParams.put("card", defaultDebitCardParams);
defaultCustomerParams.put("card", defaultCardParams);
defaultCustomerParams.put("description", "J Bindings Customer");
defaultPlanParams.put("amount", 100);
defaultPlanParams.put("currency", "usd");
defaultPlanParams.put("interval", "month");
defaultPlanParams.put("interval_count", 2);
defaultPlanParams.put("name", "J Bindings Plan");
defaultCouponParams.put("duration", "once");
defaultCouponParams.put("percent_off", 10);
defaultBankAccountParams.put("country", "US");
defaultBankAccountParams.put("routing_number", "110000000");
defaultBankAccountParams.put("account_number", "000123456789");
defaultRecipientParams.put("name", "J Test");
defaultRecipientParams.put("type", "individual");
defaultRecipientParams.put("tax_id", "000000000");
defaultRecipientParams.put("bank_account", defaultBankAccountParams);
defaultRecipientParams.put("card", defaultDebitCardParams);
defaultBitcoinReceiverParams.put("amount", 100);
defaultBitcoinReceiverParams.put("currency", "usd");
defaultBitcoinReceiverParams.put("description", "some details");
defaultBitcoinReceiverParams.put("email", "do+fill_now@stripe.com");
}
@Test
public void testAPIBase() throws StripeException {
assertEquals("https://api.stripe.com", Stripe.getApiBase());
}
@Test
public void testAccountRetrieve() throws StripeException {
Account retrievedAccount = Account.retrieve();
assertEquals("test+bindings@stripe.com", retrievedAccount.getEmail());
assertEquals(false, retrievedAccount.getChargesEnabled());
assertEquals(false, retrievedAccount.getDetailsSubmitted());
assertEquals(null, retrievedAccount.getStatementDescriptor());
assertEquals(false, retrievedAccount.getTransfersEnabled());
assertEquals("usd", retrievedAccount.getDefaultCurrency());
}
@Test
public void testBalanceRetrieve() throws StripeException {
Balance retrievedBalance = Balance.retrieve();
assertEquals(false, retrievedBalance.getLivemode());
assertEquals(1, retrievedBalance.getPending().size());
assertEquals(1, retrievedBalance.getAvailable().size());
}
@Test
public void testChargeCreate() throws StripeException {
Charge createdCharge = Charge.create(defaultChargeParams);
assertFalse(createdCharge.getRefunded());
}
@Test
public void testChargeCreateWithStatementDescriptor() throws StripeException {
Map<String, Object> chargeWithStatementDescriptorParams = new HashMap<String, Object>();
chargeWithStatementDescriptorParams.putAll(defaultChargeParams);
chargeWithStatementDescriptorParams.put("description", "hahaha1234");
chargeWithStatementDescriptorParams.put("statement_descriptor", "Stripe");
Charge createdCharge = Charge.create(chargeWithStatementDescriptorParams);
assertEquals("Stripe", createdCharge.getStatementDescriptor());
}
@Test
public void testChargeCreateWithShippingDetails() throws StripeException {
ShippingDetails shippingDetails = new ShippingDetails();
shippingDetails.setName("name");
shippingDetails.setPhone("123-456-7890");
Address address = new Address()
.setCity("Washington")
.setCountry("USA")
.setLine1("1600 Pennsylvania Ave.")
.setLine2("line 2 address")
.setPostalCode("20500")
.setState("D.C.");
shippingDetails.setAddress(address);
Map<String, Object> params = ImmutableMap.<String, Object>builder()
.putAll(defaultChargeParams)
.put("shipping", ImmutableMap.builder().put("address", ImmutableMap.builder().put("line1", address.getLine1()).put("line2", address.getLine2()).put("city", address.getCity()).put("country", address.getCountry()).put("postal_code", address.getPostalCode()).put("state", address.getState()).build()).put("name", shippingDetails.getName()).put("phone", shippingDetails.getPhone()).build())
.build();
Charge createdCharge = Charge.create(params);
assertEquals(createdCharge.getShipping(), shippingDetails);
}
@Test
public void testBalanceTransactionRetrieval() throws StripeException {
Charge.create(defaultChargeParams);
BalanceTransactionCollection balanceTransactions = BalanceTransaction.all(null);
assertFalse(balanceTransactions.getData().isEmpty());
BalanceTransaction first = balanceTransactions.getData().get(0);
assertNotNull(first.getStatus());
HashMap<String, Object> fetchParams = new HashMap<String, Object>();
fetchParams.put("count", 2);
assertEquals(BalanceTransaction.all(fetchParams).getData().size(), 2);
BalanceTransaction retrieved = BalanceTransaction.retrieve(first.getId());
assertEquals(retrieved.getId(), first.getId());
assertEquals(retrieved.getSource(), first.getSource());
}
@Test
public void testChargeRetrieve() throws StripeException {
Charge createdCharge = Charge.create(defaultChargeParams);
Charge retrievedCharge = Charge.retrieve(createdCharge.getId());
assertEquals(createdCharge.getCreated(), retrievedCharge.getCreated());
assertEquals(createdCharge.getId(), retrievedCharge.getId());
assertNotNull(retrievedCharge.getSource());
assertEquals("card", retrievedCharge.getSource().getObject());
Card card = (Card) retrievedCharge.getSource();
assertEquals(defaultCardParams.get("address_city"), card.getAddressCity());
}
@Test
public void testChargeRetrieveNullId() throws StripeException {
try {
Charge.retrieve(null);
assertTrue(false);
}
catch (InvalidRequestException e) {
// Expected
}
}
@Test
public void testChargeRefund() throws StripeException {
Charge createdCharge = Charge.create(defaultChargeParams);
Charge refundedCharge = createdCharge.refund();
assertTrue(refundedCharge.getRefunded());
ChargeRefundCollection refunds = refundedCharge.getRefunds();
assertTrue(refunds.getData() instanceof List);
assertEquals(1, refunds.getData().size());
assertTrue(refunds.getData().get(0) instanceof Refund);
}
@Test
public void testChargeRefundUpdateApiKey() throws StripeException {
Charge createdCharge = Charge.create(defaultChargeParams);
ChargeRefundCollection refunds = createdCharge.refund().getRefunds();
Refund refund = refunds.getData().get(0);
Map<String, String> metadata = new HashMap<String, String>();
metadata.put("foo", "bar");
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("metadata", metadata);
refund = refund.update(updateParams, Stripe.apiKey);
assertEquals("bar", refund.getMetadata().get("foo"));
}
@Test
public void testChargeRefundCreate() throws StripeException {
Charge ch = Charge.create(defaultChargeParams);
Map<String, Object> params = new HashMap<String, Object>();
params.put("amount", 10);
ChargeRefundCollection refunds = ch.getRefunds();
Refund created = refunds.create(params);
Refund retrieved = ch.getRefunds().retrieve(created.getId());
assertEquals(created.getId(), retrieved.getId());
}
@Test
public void testChargeRefundCreateApiKey() throws StripeException {
Charge ch = Charge.create(defaultChargeParams);
Map<String, Object> params = new HashMap<String, Object>();
params.put("amount", 10);
Refund created = ch.getRefunds().create(params, Stripe.apiKey);
Refund retrieved = ch.getRefunds().retrieve(created.getId(), Stripe.apiKey);
assertEquals(created.getId(), retrieved.getId());
}
@Test
public void testChargeRefundListAndRetrieve()
throws StripeException {
Charge ch = Charge.create(defaultChargeParams);
ch = ch.refund();
Map<String, Object> listParams = new HashMap<String, Object>();
listParams.put("count", 1);
Refund created = ch.getRefunds().all(listParams).getData().get(0);
Refund retrieved = ch.getRefunds().retrieve(created.getId());
assertEquals(created.getId(), retrieved.getId());
}
@Test
public void testChargeRefundListAndRetrievePerCallAPIKey()
throws StripeException {
Charge ch = Charge.create(defaultChargeParams);
ch = ch.refund();
Map<String, Object> listParams = new HashMap<String, Object>();
listParams.put("count", 1);
Refund created = ch.getRefunds().all(listParams,
Stripe.apiKey).getData().get(0);
Refund retrieved = ch.getRefunds().retrieve(created.getId(), Stripe.apiKey);
assertEquals(created.getId(), retrieved.getId());
}
@Test
public void testChargeCapture() throws StripeException {
Map<String, Object> options = new HashMap<String, Object>(defaultChargeParams);
options.put("capture", false);
Charge created = Charge.create(options);
assertFalse(created.getCaptured());
Charge captured = created.capture();
assertTrue(captured.getCaptured());
}
@Test
public void testChargePartialRefund() throws StripeException {
Charge createdCharge = Charge.create(defaultChargeParams);
Map<String, Object> refundParams = new HashMap<String, Object>();
final Integer REFUND_AMOUNT = 50;
refundParams.put("amount", REFUND_AMOUNT);
Charge refundedCharge = createdCharge.refund(refundParams);
assertFalse(refundedCharge.getRefunded());
assertEquals(refundedCharge.getAmountRefunded(), REFUND_AMOUNT);
}
@Test
public void testChargeList() throws StripeException {
Map<String, Object> listParams = new HashMap<String, Object>();
listParams.put("count", 1);
List<Charge> charges = Charge.all(listParams).getData();
assertEquals(charges.size(), 1);
}
@Test
public void testChargeListWithTotalCount() throws StripeException {
Map<String, Object> listParams = new HashMap<String, Object>();
listParams.put("include[]", "total_count");
ChargeCollection chargeCollection = Charge.all(listParams);
assertNotNull(chargeCollection.getTotalCount());
assertTrue(chargeCollection.getTotalCount() > 0);
}
@Test(expected = CardException.class)
public void testInvalidCard() throws StripeException {
Map<String, Object> invalidChargeParams = new HashMap<String, Object>();
invalidChargeParams.putAll(defaultChargeParams);
Map<String, Object> invalidCardParams = new HashMap<String, Object>();
invalidCardParams.put("number", "4242424242424241");
invalidCardParams.put("exp_month", 12);
invalidCardParams.put("exp_year", 2015);
invalidChargeParams.put("card", invalidCardParams);
Charge.create(invalidChargeParams);
}
@Test
public void testInvalidAddressZipTest() throws StripeException {
Map<String, Object> invalidChargeParams = new HashMap<String, Object>();
invalidChargeParams.putAll(defaultChargeParams);
Map<String, Object> invalidCardParams = new HashMap<String, Object>();
// See https://stripe.com/docs/testing
invalidCardParams.put("number", "4000000000000036");
invalidCardParams.put("address_zip", "94024");
invalidCardParams.put("address_line1", "42 Foo Street");
invalidCardParams.put("exp_month", 12);
invalidCardParams.put("exp_year", 2015);
invalidChargeParams.put("card", invalidCardParams);
Charge charge = Charge.create(invalidChargeParams, cardSupportedRequestOptions);
assertEquals(charge.getPaid(), true);
assertEquals(charge.getCard().getAddressZipCheck(), "fail");
assertEquals(charge.getCard().getAddressLine1Check(), "pass");
}
@Test
public void testInvalidAddressLine1Test() throws StripeException {
Map<String, Object> invalidChargeParams = new HashMap<String, Object>();
invalidChargeParams.putAll(defaultChargeParams);
Map<String, Object> invalidCardParams = new HashMap<String, Object>();
// See https://stripe.com/docs/testing
invalidCardParams.put("number", "4000000000000028");
invalidCardParams.put("address_zip", "94024");
invalidCardParams.put("address_line1", "42 Foo Street");
invalidCardParams.put("exp_month", 12);
invalidCardParams.put("exp_year", 2015);
invalidChargeParams.put("card", invalidCardParams);
Charge charge = Charge.create(invalidChargeParams, cardSupportedRequestOptions);
assertEquals(charge.getPaid(), true);
assertEquals(charge.getCard().getAddressZipCheck(), "pass");
assertEquals(charge.getCard().getAddressLine1Check(), "fail");
}
@Test
public void testDisputedCharge() throws StripeException, InterruptedException {
int chargeValueCents = 100;
Charge disputedCharge = createDisputedCharge(chargeValueCents);
Dispute dispute = disputedCharge.getDispute();
assertNotNull(dispute);
assertFalse(dispute.getIsChargeRefundable());
assertEquals(1, dispute.getBalanceTransactions().size());
assertEquals(-chargeValueCents, dispute.getBalanceTransactions().get(0).getAmount().intValue());
}
@Test
public void testFraudDetails() throws StripeException, InterruptedException {
Charge charge = Charge.create(defaultChargeParams);
FraudDetails expected = new FraudDetails();
assertEquals(expected, charge.getFraudDetails());
Charge refundedCharge = charge.refund();
assertEquals(expected, refundedCharge.getFraudDetails());
Charge updatedCharge = charge.update(ImmutableMap.<String, Object>of("fraud_details", ImmutableMap.of("user_report", "fraudulent")));
FraudDetails expectedReported = new FraudDetails();
expectedReported.setUserReport("fraudulent");
assertEquals(expectedReported, updatedCharge.getFraudDetails());
Charge nowSafe = updatedCharge.markSafe(null);
expectedReported.setUserReport("safe");
assertEquals(expectedReported, nowSafe.getFraudDetails());
Charge nowFraudulent = nowSafe.markFraudulent(null);
expectedReported.setUserReport("fraudulent");
assertEquals(expectedReported, nowFraudulent.getFraudDetails());
}
@Test
public void testCreateFileUpload() throws StripeException,
InterruptedException, URISyntaxException {
URL url = getClass().getResource("minimal.pdf");
File file = new File(url.getPath());
Map<String, Object> fileUploadParams = new HashMap<String, Object>();
fileUploadParams.put("purpose", "dispute_evidence");
fileUploadParams.put("file", file);
FileUpload fileUpload = FileUpload.create(fileUploadParams);
assertEquals(file.length(), fileUpload.getSize().longValue());
assertEquals("pdf", fileUpload.getType());
FileUpload retrievedUpload = FileUpload.retrieve(fileUpload.getId());
assertEquals(fileUpload.getId(), retrievedUpload.getId());
assertEquals(file.length(), retrievedUpload.getSize().longValue());
assertEquals("pdf", retrievedUpload.getType());
}
@Test
public void testFileUploadList() throws StripeException {
Map<String, Object> listParams = new HashMap<String, Object>();
listParams.put("limit", 1);
List<FileUpload> uploads = FileUpload.all(listParams).getData();
assertEquals(uploads.size(), 1);
}
@Test
public void testSubmitOldStyleEvidence() throws StripeException, InterruptedException {
int chargeValueCents = 100;
Stripe.apiVersion = "2014-11-20";
Charge disputedCharge = createDisputedCharge(chargeValueCents);
String myEvidence = "Here's evidence showing this charge is legitimate.";
Dispute initialDispute = disputedCharge.getDispute();
assertNull(initialDispute.getEvidence());
assertNull(initialDispute.getEvidenceSubObject());
Map<String, Object> disputeParams = ImmutableMap.<String, Object>of("evidence", myEvidence);
Dispute updatedDispute = disputedCharge.updateDispute(disputeParams);
assertNotNull(updatedDispute);
assertEquals(myEvidence, updatedDispute.getEvidence());
assertNull(updatedDispute.getEvidenceSubObject());
}
@Test
public void testSubmitEvidence() throws StripeException, InterruptedException {
int chargeValueCents = 100;
Charge disputedCharge = createDisputedCharge(chargeValueCents);
Dispute initialDispute = disputedCharge.getDispute();
assertNull(initialDispute.getEvidence());
EvidenceSubObject emptyEvidence = new EvidenceSubObject();
assertEquals(emptyEvidence, initialDispute.getEvidenceSubObject());
assertEquals(0, initialDispute.getEvidenceDetails().getSubmissionCount().intValue());
Map<String, Object> evidenceHashParams = new HashMap<String, Object>();
// TODO: assert on all param types
evidenceHashParams.put("product_description", "my productDescription");
evidenceHashParams.put("customer_name", "my customerName");
evidenceHashParams.put("uncategorized_text", "my uncategorizedText");
Map<String, Object> providedEvidenceParams = ImmutableMap.<String, Object>of("evidence", evidenceHashParams);
Dispute updatedDispute = disputedCharge.updateDispute(providedEvidenceParams);
assertNotNull(updatedDispute);
EvidenceSubObject evidenceSubObject = updatedDispute.getEvidenceSubObject();
assertNotSame(emptyEvidence, evidenceSubObject);
assertEquals(1, updatedDispute.getEvidenceDetails().getSubmissionCount().intValue());
assertNull(updatedDispute.getEvidence());
assertEquals("my productDescription", evidenceSubObject.getProductDescription());
assertEquals("my customerName", evidenceSubObject.getCustomerName());
assertEquals("my uncategorizedText", evidenceSubObject.getUncategorizedText());
EvidenceDetails evidenceDetails = updatedDispute.getEvidenceDetails();
assertNotNull(evidenceDetails);
assertEquals(1, evidenceDetails.getSubmissionCount().intValue());
}
private Charge createDisputedCharge(int chargeValueCents) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException, InterruptedException {
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.putAll(defaultChargeParams);
chargeParams.put("amount", chargeValueCents);
Map<String, Object> testModeDisputeCardParams = new HashMap<String, Object>();
testModeDisputeCardParams.put("number", "4000000000000259");
testModeDisputeCardParams.put("exp_month", 12);
testModeDisputeCardParams.put("exp_year", 2020);
chargeParams.put("card", testModeDisputeCardParams);
Charge charge = Charge.create(chargeParams);
// This test relies on the server asynchronously marking the charge as disputed.
// TODO: find a more reliable way to do this instead of sleeping
Thread.sleep(10000);
return Charge.retrieve(charge.getId());
}
@Test
public void testCustomerCreate() throws StripeException {
Customer customer = Customer.create(defaultCustomerParams, cardSupportedRequestOptions);
assertEquals(customer.getDescription(), "J Bindings Customer");
List<Card> customerCards = customer.getCards().getData();
assertEquals(1, customerCards.size());
assertEquals("4242", customerCards.get(0).getLast4());
}
@Test
public void testCustomerRetrieve() throws StripeException {
Customer createdCustomer = Customer.create(defaultCustomerParams);
Customer retrievedCustomer = Customer.retrieve(createdCustomer.getId());
assertEquals(createdCustomer.getCreated(),
retrievedCustomer.getCreated());
assertEquals(createdCustomer.getId(), retrievedCustomer.getId());
}
@Test
public void testCustomerList() throws StripeException {
Map<String, Object> listParams = new HashMap<String, Object>();
listParams.put("count", 1);
List<Customer> Customers = Customer.all(listParams).getData();
assertEquals(Customers.size(), 1);
}
@Test
public void testCustomerUpdate() throws StripeException {
Customer createdCustomer = Customer.create(defaultCustomerParams);
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("description", "Updated Description");
Customer updatedCustomer = createdCustomer.update(updateParams);
assertEquals(updatedCustomer.getDescription(), "Updated Description");
}
@Test(expected=InvalidRequestException.class)
public void testCustomerUpdateToBlank() throws StripeException {
Customer createdCustomer = Customer.create(defaultCustomerParams);
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("description", "");
createdCustomer.update(updateParams);
}
@Test
public void testCustomerUpdateToNull() throws StripeException {
Customer createdCustomer = Customer.create(defaultCustomerParams);
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("description", null);
Customer updatedCustomer = createdCustomer.update(updateParams);
assertEquals(updatedCustomer.getDescription(), null);
}
@Test
public void testCustomerSourceList() throws StripeException {
Customer customer = createDefaultCustomerWithDefaultBitcoinReceiver();
HashMap<String, Object> listParams = new HashMap<String, Object>();
List<PaymentSource> customerSourceList = customer.getSources().all(listParams).getData();
assertEquals(2, customerSourceList.size());
assert(customerSourceList.get(0) instanceof Card);
assertEquals("4242", ((Card) customerSourceList.get(0)).getLast4());
assert(customerSourceList.get(1) instanceof BitcoinReceiver);
assertEquals(true, ((BitcoinReceiver) customerSourceList.get(1)).getFilled());
}
@Test
public void testCustomerSourceRetrieveCard() throws StripeException {
Customer customer = Customer.create(defaultCustomerParams);
PaymentSourceCollection customerSources = customer.getSources();
String paymentSourceId = customerSources.getData().get(0).getId();
PaymentSource paymentSource = customerSources.retrieve(paymentSourceId);
assertNotNull(paymentSource);
assertEquals(paymentSourceId, paymentSource.getId());
}
@Test
public void testCustomerSourceRetrieveBitcoinReceiver() throws StripeException {
Customer customer = Customer.create(new HashMap<String, Object>());
BitcoinReceiver receiver = BitcoinReceiver.create(defaultBitcoinReceiverParams);
PaymentSourceCollection customerSources = customer.getSources();
Map<String, Object> createParams = new HashMap<String, Object>();
createParams.put("source", receiver.getId());
customerSources.create(createParams);
customerSources = customerSources.all(new HashMap<String, Object>());
String paymentSourceId = customerSources.getData().get(0).getId();
PaymentSource paymentSource = customerSources.retrieve(paymentSourceId);
assertNotNull(paymentSource);
assertEquals(paymentSourceId, paymentSource.getId());
assertTrue(paymentSource instanceof BitcoinReceiver);
assertTrue(((BitcoinReceiver) paymentSource).getFilled());
}
@Test
public void testCustomerCreateWithSource() throws StripeException {
HashMap<String, Object> customerCreationParams = new HashMap<String, Object>();
HashMap<String, Object> cardParams = new HashMap<String, Object>(defaultCardParams);
cardParams.put("object", "card");
customerCreationParams.put("source", cardParams);
Customer customer = Customer.create(customerCreationParams);
assertNotNull(customer);
assertNotNull(customer.getId());
assertNotNull(customer.getSources());
assert(customer.getSources().getData().get(0) instanceof Card);
}
@Test
public void testCustomerCreateSourceWithCardHash() throws StripeException {
Customer customer = Customer.create(new HashMap<String, Object>());
PaymentSourceCollection customerSources = customer.getSources();
HashMap<String, Object> cardParams = new HashMap<String, Object>(defaultCardParams);
cardParams.put("object", "card");
HashMap<String, Object> createParams = new HashMap<String, Object>();
createParams.put("source", cardParams);
PaymentSource paymentSource = customerSources.create(createParams);
assertNotNull(paymentSource);
assertNotNull(paymentSource.getId());
assert(paymentSource instanceof Card);
}
@Test
public void testCustomerCreateSourceWithBitcoinReceiverToken() throws StripeException {
Customer customer = Customer.create(new HashMap<String, Object>());
PaymentSourceCollection customerSources = customer.getSources();
BitcoinReceiver receiver = BitcoinReceiver.create(defaultBitcoinReceiverParams);
HashMap<String, Object> createParams = new HashMap<String, Object>();
createParams.put("source", receiver.getId());
PaymentSource paymentSource = customerSources.create(createParams);
assertNotNull(paymentSource);
assertNotNull(paymentSource.getId());
assert(paymentSource instanceof BitcoinReceiver);
}
@Test
public void testCustomerSourceUpdate() throws StripeException {
Customer customer = Customer.create(defaultCustomerParams);
PaymentSourceCollection customerSources = customer.getSources();
PaymentSource paymentSource = customerSources.getData().get(0);
assert(paymentSource instanceof Card);
Card card = (Card) paymentSource;
HashMap<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("address_line1", "some address details");
Card updatedCard = card.update(updateParams);
assertEquals("some address details", updatedCard.getAddressLine1());
}
@Test
public void testCustomerSourceDelete() throws StripeException {
Customer customer = Customer.create(defaultCustomerParams);
PaymentSourceCollection customerSources = customer.getSources();
PaymentSource paymentSource = customerSources.getData().get(0);
paymentSource.delete();
HashMap<String, Object> listParams = new HashMap<String, Object>();
assertEquals(0, customerSources.all(listParams).getData().size());
}
@Test
public void testCustomerCardAddition() throws StripeException {
Customer createdCustomer = Customer.create(defaultCustomerParams, cardSupportedRequestOptions);
String originalDefaultCard = createdCustomer.getDefaultCard();
Map<String, Object> creationParams = new HashMap<String, Object>();
creationParams.put("card", defaultCardParams);
Card addedCard = createdCustomer.createCard(creationParams);
Token token = Token.create(defaultTokenParams);
createdCustomer.createCard(token.getId());
Customer updatedCustomer = Customer.retrieve(createdCustomer.getId(), cardSupportedRequestOptions);
assertEquals((Integer) updatedCustomer.getCards().getData().size(), (Integer) 3);
assertEquals(updatedCustomer.getDefaultCard(), originalDefaultCard);
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("default_card", addedCard.getId());
Customer customerAfterDefaultCardUpdate = updatedCustomer.update(updateParams, cardSupportedRequestOptions);
assertEquals((Integer) customerAfterDefaultCardUpdate.getCards().getData().size(), (Integer) 3);
assertEquals(customerAfterDefaultCardUpdate.getDefaultCard(), addedCard.getId());
assertEquals(customerAfterDefaultCardUpdate.getCards().retrieve(originalDefaultCard).getId(), originalDefaultCard);
assertEquals(customerAfterDefaultCardUpdate.getCards().retrieve(addedCard.getId()).getId(), addedCard.getId());
}
@Test
public void testCreateCardThroughCollection() throws StripeException {
Customer createdCustomer = Customer.create(defaultCustomerParams, cardSupportedRequestOptions);
Map<String, Object> creationParams = new HashMap<String, Object>();
creationParams.put("card", defaultCardParams);
Card addedCard = createdCustomer.getCards().create(creationParams);
assertEquals(createdCustomer.getId(), addedCard.getCustomer());
Customer updatedCustomer = Customer.retrieve(createdCustomer.getId(), cardSupportedRequestOptions);
assertEquals((Integer) updatedCustomer.getCards().getData().size(), (Integer) 2);
}
@Test
public void testCustomerCardUpdate() throws StripeException {
Customer customer = Customer.create(defaultCustomerParams, cardSupportedRequestOptions);
Card originalCard = customer.getCards().getData().get(0);
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("name", "J Bindings Cardholder, Jr.");
Card updatedCard = originalCard.update(updateParams);
assertEquals(updatedCard.getName(), "J Bindings Cardholder, Jr.");
}
@Test
public void testCustomerCardDelete() throws StripeException {
Customer customer = Customer.create(defaultCustomerParams, cardSupportedRequestOptions);
Map<String, Object> creationParams = new HashMap<String, Object>();
creationParams.put("card", defaultCardParams);
customer.createCard(creationParams);
Card card = customer.getCards().getData().get(0);
DeletedCard deletedCard = card.delete();
Customer retrievedCustomer = Customer.retrieve(customer.getId(), cardSupportedRequestOptions);
assertTrue(deletedCard.getDeleted());
assertEquals(deletedCard.getId(), card.getId());
for(Card retrievedCard : retrievedCustomer.getCards().getData()) {
assertFalse("Card was not actually deleted: " + card.getId(), card.getId().equals(retrievedCard.getId()));
}
}
@Test
public void testCustomerDelete() throws StripeException {
Customer createdCustomer = Customer.create(defaultCustomerParams);
DeletedCustomer deletedCustomer = createdCustomer.delete();
Customer deletedRetrievedCustomer = Customer.retrieve(createdCustomer
.getId());
assertTrue(deletedCustomer.getDeleted());
assertEquals(deletedCustomer.getId(), createdCustomer.getId());
assertTrue(deletedRetrievedCustomer.getDeleted());
}
@Test
public void testPlanCreate() throws StripeException {
Plan plan = Plan.create(getUniquePlanParams());
assertEquals(plan.getInterval(), "month");
assertEquals(plan.getIntervalCount(), (Integer) 2);
}
@Test
public void testPlanCreateWithStatementDescriptor() throws StripeException {
Map<String, Object> planParamsWithStatementDescriptor = getUniquePlanParams();
planParamsWithStatementDescriptor.put("statement_descriptor", "Stripe");
Plan plan = Plan.create(planParamsWithStatementDescriptor);
assertEquals(plan.getStatementDescriptor(), "Stripe");
}
@Test
public void testPlanUpdate() throws StripeException {
Plan createdPlan = Plan.create(getUniquePlanParams());
Map<String, Object> updateParams = new HashMap<String, Object>();
updateParams.put("name", "Updated Plan Name");
Plan updatedplan = createdPlan.update(updateParams);
assertEquals(updatedplan.getName(), "Updated Plan Name");
}
@Test
public void testPlanRetrieve() throws StripeException {
Plan createdPlan = Plan.create(getUniquePlanParams());
Plan retrievedPlan = Plan.retrieve(createdPlan.getId());
assertEquals(createdPlan.getId(), retrievedPlan.getId());
}
@Test
public void testPlanList() throws StripeException {
Map<String, Object> listParams = new HashMap<String, Object>();
listParams.put("count", 1);
List<Plan> Plans = Plan.all(listParams).getData();
assertEquals(Plans.size(), 1);
}
@Test
public void testPlanDelete() throws StripeException {
Plan createdPlan = Plan.create(getUniquePlanParams());
DeletedPlan deletedPlan = createdPlan.delete();
assertTrue(deletedPlan.getDeleted());
assertEquals(deletedPlan.getId(), createdPlan.getId());
}
@Test
public void testCustomerCreateWithPlan() throws StripeException {
Plan plan = Plan.create(getUniquePlanParams());
Customer customer = createDefaultCustomerWithPlan(plan);
assertEquals(customer.getSubscriptions().getData().get(0).getPlan().getId(), plan.getId());
}
@Test
public void testUpdateSubscription() throws StripeException {
Plan plan = Plan.create(getUniquePlanParams());
Customer customer = Customer.create(defaultCustomerParams);
Map<String, Object> subscriptionParams = new HashMap<String, Object>();
subscriptionParams.put("plan", plan.getId());
Subscription sub = customer.updateSubscription(subscriptionParams);
assertEquals(sub.getPlan().getId(), plan.getId());
assertEquals(sub.getCustomer(), customer.getId());
}
@Test
public void testCancelSubscription() throws StripeException {
Plan plan = Plan.create(getUniquePlanParams());
Customer customer = createDefaultCustomerWithPlan(plan);
assertEquals(customer.getSubscriptions().getData().get(0).getStatus(), "active");
Subscription canceledSubscription = customer.cancelSubscription();
assertEquals(canceledSubscription.getStatus(), "canceled");
}
@Test
public void testCancelSubscriptionAtPeriodEnd() throws StripeException {
Plan plan = Plan.create(getUniquePlanParams());
Customer customer = createDefaultCustomerWithPlan(plan);
assertEquals(customer.getSubscriptions().getData().get(0).getStatus(), "active");
Map<String, Object> cancelParams = new HashMap<String, Object>();
cancelParams.put("at_period_end", true);
Subscription canceledSubscription = customer
.cancelSubscription(cancelParams);
assertEquals(canceledSubscription.getStatus(), "active");
assertEquals(canceledSubscription.getCancelAtPeriodEnd(), true);
}
@Test
public void testNewStyleSubscriptionAPI() throws StripeException {
Plan plan = Plan.create(getUniquePlanParams());
Plan plan2 = Plan.create(getUniquePlanParams());
Customer customer = Customer.create(defaultCustomerParams);
// Create
Map<String, Object> subCreateParams = new HashMap<String, Object>();
subCreateParams.put("plan", plan.getId());
Subscription sub = customer.createSubscription(subCreateParams);
assertEquals(plan.getId(), sub.getPlan().getId());
customer = Customer.retrieve(customer.getId());
assertEquals(1, customer.getSubscriptions().getData().size());
assertEquals(sub.getId(), customer.getSubscriptions().getData().get(0).getId());
// Retrieve
Subscription retrievedSub = customer.getSubscriptions().retrieve(sub.getId());
assertEquals(sub.getId(), retrievedSub.getId());
// List
CustomerSubscriptionCollection list = customer.getSubscriptions().all(null);
assertEquals(1, list.getData().size());
assertEquals(sub.getId(), list.getData().get(0).getId());
// Update
Map<String, Object> subUpdateParams = new HashMap<String, Object>();
subUpdateParams.put("plan", plan2.getId());
sub = sub.update(subUpdateParams);
assertEquals(plan2.getId(), sub.getPlan().getId());
// Cancel
sub = sub.cancel(null);
assertNotNull(sub.getCanceledAt());
}
@Test
public void testCreateSubscriptionThroughCollection() throws StripeException {
Plan plan = Plan.create(getUniquePlanParams());
Customer customer = Customer.create(defaultCustomerParams);
// Create
Map<String, Object> subCreateParams = new HashMap<String, Object>();
subCreateParams.put("plan", plan.getId());