-
Notifications
You must be signed in to change notification settings - Fork 97
/
TestRecurlyClient.java
2588 lines (2168 loc) · 126 KB
/
TestRecurlyClient.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
/*
* Copyright 2010-2014 Ning, Inc.
* Copyright 2014-2018 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.ning.billing.recurly;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import com.ning.billing.recurly.model.Account;
import com.ning.billing.recurly.model.AccountAcquisition;
import com.ning.billing.recurly.model.AccountBalance;
import com.ning.billing.recurly.model.Accounts;
import com.ning.billing.recurly.model.AcquisitionChannel;
import com.ning.billing.recurly.model.AddOn;
import com.ning.billing.recurly.model.AddOns;
import com.ning.billing.recurly.model.Address;
import com.ning.billing.recurly.model.Adjustment;
import com.ning.billing.recurly.model.AdjustmentRefund;
import com.ning.billing.recurly.model.Adjustments;
import com.ning.billing.recurly.model.BillingInfo;
import com.ning.billing.recurly.model.BillingInfoVerification;
import com.ning.billing.recurly.model.Coupon;
import com.ning.billing.recurly.model.Coupon.RedemptionResource;
import com.ning.billing.recurly.model.Coupons;
import com.ning.billing.recurly.model.CustomField;
import com.ning.billing.recurly.model.CustomFields;
import com.ning.billing.recurly.model.CustomFieldDefinition;
import com.ning.billing.recurly.model.CustomFieldDefinitions;
import com.ning.billing.recurly.model.DunningCampaignBulkUpdate;
import com.ning.billing.recurly.model.DunningCampaigns;
import com.ning.billing.recurly.model.GiftCard;
import com.ning.billing.recurly.model.Invoice;
import com.ning.billing.recurly.model.InvoiceCollection;
import com.ning.billing.recurly.model.InvoiceRefund;
import com.ning.billing.recurly.model.Invoices;
import com.ning.billing.recurly.model.Item;
import com.ning.billing.recurly.model.Plan;
import com.ning.billing.recurly.model.PlanCode;
import com.ning.billing.recurly.model.PlanCodes;
import com.ning.billing.recurly.model.Purchase;
import com.ning.billing.recurly.model.RecurlyAPIError;
import com.ning.billing.recurly.model.Redemption;
import com.ning.billing.recurly.model.Redemptions;
import com.ning.billing.recurly.model.RefundMethod;
import com.ning.billing.recurly.model.RefundOption;
import com.ning.billing.recurly.model.ShippingAddress;
import com.ning.billing.recurly.model.ShippingAddresses;
import com.ning.billing.recurly.model.Subscription;
import com.ning.billing.recurly.model.SubscriptionAddOns;
import com.ning.billing.recurly.model.SubscriptionNotes;
import com.ning.billing.recurly.model.SubscriptionUpdate;
import com.ning.billing.recurly.model.Subscriptions;
import com.ning.billing.recurly.model.Transaction;
import com.ning.billing.recurly.model.Transactions;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestRecurlyClient {
public static final String RECURLY_PAGE_SIZE = "recurly.page.size";
public static final String KILLBILL_PAYMENT_RECURLY_API_KEY = "killbill.payment.recurly.apiKey";
public static final String KILLBILL_PAYMENT_RECURLY_SUBDOMAIN = "killbill.payment.recurly.subDomain";
public static final String KILLBILL_PAYMENT_RECURLY_DEFAULT_CURRENCY_KEY = "killbill.payment.recurly.currency";
// Default to USD for all tests, which is expected to be supported by Recurly by default
// Multi Currency Support is an enterprise add-on
private static final String CURRENCY = System.getProperty(KILLBILL_PAYMENT_RECURLY_DEFAULT_CURRENCY_KEY, "USD");
private RecurlyClient recurlyClient;
@BeforeMethod(groups = {"integration", "enterprise"})
public void setUp() throws Exception {
final String apiKey = System.getProperty(KILLBILL_PAYMENT_RECURLY_API_KEY);
String subDomainTemp = System.getProperty(KILLBILL_PAYMENT_RECURLY_SUBDOMAIN);
if (apiKey == null) {
Assert.fail("You need to set your Recurly api key to run integration tests:" +
" -Dkillbill.payment.recurly.apiKey=...");
}
if (subDomainTemp == null) {
subDomainTemp = "api";
}
final String subDomain = subDomainTemp;
recurlyClient = new RecurlyClient(apiKey, subDomain);
recurlyClient.open();
}
@AfterMethod(groups = {"integration", "enterprise"})
public void tearDown() throws Exception {
recurlyClient.close();
}
@Test(groups = "integration")
public void testUnauthorizedException() throws Exception {
final String subdomain = System.getProperty(KILLBILL_PAYMENT_RECURLY_SUBDOMAIN);
RecurlyClient unauthorizedRecurlyClient = new RecurlyClient("invalid-api-key", subdomain);
unauthorizedRecurlyClient.open();
try {
unauthorizedRecurlyClient.getAccounts();
Assert.fail("getAccounts call should not succeed with invalid credentials.");
} catch (RecurlyAPIException expected) {
Assert.assertEquals(expected.getRecurlyError().getSymbol(), "unauthorized");
}
}
@Test(groups = "integration", description = "See https://github.com/killbilling/recurly-java-library/issues/21")
public void testGetEmptySubscriptions() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
try {
// Create a user
final Account account = recurlyClient.createAccount(accountData);
// Create BillingInfo
billingInfoData.setAccount(account);
final BillingInfo billingInfo = recurlyClient.createOrUpdateBillingInfo(billingInfoData);
Assert.assertNotNull(billingInfo);
final BillingInfo retrievedBillingInfo = recurlyClient.getBillingInfo(account.getAccountCode());
Assert.assertNotNull(retrievedBillingInfo);
final Subscriptions subs = recurlyClient.getAccountSubscriptions(accountData.getAccountCode(), "active");
Assert.assertEquals(subs.size(), 0);
} finally {
// Close the account
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testReopenAccount() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
try {
// Create account
final Account newAccount = recurlyClient.createAccount(accountData);
Assert.assertNull(newAccount.getClosedAt());
// Close the account
recurlyClient.closeAccount(accountData.getAccountCode());
final Account closedAccount = recurlyClient.getAccount(accountData.getAccountCode());
Assert.assertNotNull(closedAccount.getClosedAt());
// Reopen the account
final Account reopenedAccount = recurlyClient.reopenAccount(accountData.getAccountCode());
// Confirm that the reopened account is the same as the original
// (besides `updated_at`, which may differ)
newAccount.setUpdatedAt(reopenedAccount.getUpdatedAt());
Assert.assertEquals(reopenedAccount, newAccount);
} finally {
// Close the account
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testInvalidTokenError() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
RecurlyAPIException error = null;
try {
// Create account with invalid billing token
final BillingInfo billingInfo = new BillingInfo();
billingInfo.setTokenId("invalid token");
accountData.setBillingInfo(billingInfo);
final Subscription subscription = new Subscription();
subscription.setPlanCode("anything");
final Subscriptions subscriptions = new Subscriptions();
subscriptions.setRecurlyObject(subscription);
final Purchase purchase = new Purchase();
purchase.setCurrency(CURRENCY);
purchase.setAccount(accountData);
purchase.setSubscriptions(subscriptions);
recurlyClient.previewPurchase(purchase);
} catch (RecurlyAPIException expected) {
error = expected;
}
// Despite being a 422 error, this case returns a single Error
// object rather than Errors. Check that we're deserializing it
// properly.
Assert.assertEquals(error.getRecurlyError().getHttpStatusCode(), 422);
Assert.assertEquals(error.getRecurlyError().getSymbol(), "token_invalid");
}
@Test(groups = "integration")
public void testGetBillingInfo() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
billingInfoData.setAccount(null); // need to null out test account
accountData.setBillingInfo(billingInfoData);
try {
// Create account and fetch billing info
final Account account = recurlyClient.createAccount(accountData);
final BillingInfo retrievedBillingInfo = recurlyClient.getBillingInfo(account.getAccountCode());
Assert.assertNotNull(retrievedBillingInfo);
Assert.assertEquals(retrievedBillingInfo.getType(), "credit_card");
} finally {
// Close the account
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testVerifyBillingInfoWithGatewayCode() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
billingInfoData.setAccount(null); // need to null out test account
accountData.setBillingInfo(billingInfoData);
final BillingInfoVerification gateway = new BillingInfoVerification();
gateway.setGatewayCode("bad-code");
try {
final Account account = recurlyClient.createAccount(accountData);
final Transaction verifiedBillingInfo = recurlyClient.verifyBillingInfo(account.getAccountCode(), gateway);
Assert.fail("Should have thrown Recurly API exception");
} catch (RecurlyAPIException e) {
Assert.assertEquals(e.getRecurlyError().getSymbol(), "not_found");
} finally {
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testVerifyBillingInfo() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
billingInfoData.setAccount(null); // need to null out test account
accountData.setBillingInfo(billingInfoData);
try {
final Account account = recurlyClient.createAccount(accountData);
final Transaction verifiedBillingInfo = recurlyClient.verifyBillingInfo(account.getAccountCode());
Assert.assertEquals(verifiedBillingInfo.getOrigin(), "api_verify_card");
Assert.assertEquals(verifiedBillingInfo.getAction(), "verify");
} finally {
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testGetCustomFieldDefinitions() throws Exception {
final CustomFieldDefinitions definitions = recurlyClient.getCustomFieldDefinitions(CustomFieldDefinitions.CustomFieldDefinitionRelatedType.CHARGE);
final CustomFieldDefinition definition = definitions.get(0);
Assert.assertEquals(definition.getRelatedType(), "charge");
}
@Test(groups = "integration", description = "See https://github.com/killbilling/recurly-java-library/issues/23")
public void testRemoveSubscriptionAddons() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
final Plan planData = TestUtils.createRandomPlan(CURRENCY);
try {
// Create a user
final Account account = recurlyClient.createAccount(accountData);
// Create BillingInfo
billingInfoData.setAccount(account);
final BillingInfo billingInfo = recurlyClient.createOrUpdateBillingInfo(billingInfoData);
Assert.assertNotNull(billingInfo);
// Create a plan with addons
final Plan plan = recurlyClient.createPlan(planData);
Thread.sleep(1000); // TODO - can remove after Jan 18th 2017
final List<AddOn> addons = new ArrayList<AddOn>();
final int nbAddOns = 5;
for (int i = 0; i < nbAddOns; i++) {
final AddOn addOn = TestUtils.createRandomAddOn(CURRENCY);
final AddOn addOnRecurly = recurlyClient.createPlanAddOn(plan.getPlanCode(), addOn);
addons.add(addOnRecurly);
}
// Create a subscription with addons
final Subscription subscriptionDataWithAddons = TestUtils.createRandomSubscription(CURRENCY, plan, accountData, addons);
final Subscription subscriptionWithAddons = recurlyClient.createSubscription(subscriptionDataWithAddons);
Assert.assertEquals(subscriptionWithAddons.getAddOns().size(), nbAddOns);
for (int i = 0; i < nbAddOns; i++) {
Assert.assertEquals(subscriptionWithAddons.getAddOns().get(i).getAddOnCode(), addons.get(i).getAddOnCode());
}
// Fetch the corresponding invoice
final Invoice subInvoice = subscriptionWithAddons.getInvoice();
Assert.assertNotNull(subInvoice);
// Refetch the invoice using the getInvoice method
final String invoiceID = subInvoice.getId();
final Invoice gotInvoice = recurlyClient.getInvoice(invoiceID);
Assert.assertEquals(subInvoice.hashCode(), gotInvoice.hashCode());
// Remove all addons
final SubscriptionUpdate subscriptionUpdate = new SubscriptionUpdate();
subscriptionUpdate.setAddOns(new SubscriptionAddOns());
final Subscription subscriptionWithAddons1 = recurlyClient.updateSubscription(subscriptionWithAddons.getUuid(), subscriptionUpdate);
Assert.assertTrue(subscriptionWithAddons1.getAddOns().isEmpty());
// Add them again
final SubscriptionUpdate subscriptionUpdate1 = new SubscriptionUpdate();
final SubscriptionAddOns newAddons = new SubscriptionAddOns();
newAddons.addAll(subscriptionDataWithAddons.getAddOns());
subscriptionUpdate1.setAddOns(newAddons);
final Subscription subscriptionWithAddons2 = recurlyClient.updateSubscription(subscriptionWithAddons.getUuid(), subscriptionUpdate1);
Assert.assertEquals(subscriptionWithAddons2.getAddOns().size(), nbAddOns);
for (int i = 0; i < nbAddOns; i++) {
Assert.assertEquals(subscriptionWithAddons2.getAddOns().get(i).getAddOnCode(), addons.get(i).getAddOnCode());
}
} finally {
// Close the account
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testGetSiteSubscriptions() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
final Plan planData = TestUtils.createRandomPlan();
try {
final Account account = recurlyClient.createAccount(accountData);
billingInfoData.setAccount(account);
final BillingInfo billingInfo = recurlyClient.createOrUpdateBillingInfo(billingInfoData);
final Plan plan = recurlyClient.createPlan(planData);
final Subscription subscriptionData = new Subscription();
subscriptionData.setPlanCode(plan.getPlanCode());
subscriptionData.setAccount(accountData);
subscriptionData.setCurrency(CURRENCY);
subscriptionData.setUnitAmountInCents(1242);
subscriptionData.setRemainingBillingCycles(1);
// makes sure we have at least one subscription
recurlyClient.createSubscription(subscriptionData);
// make sure we return more than one subscription
Assert.assertTrue(recurlyClient.getSubscriptions().size() > 0);
} finally {
// Close the account
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testGetCoupons() throws Exception {
final Coupons retrievedCoupons = recurlyClient.getCoupons();
Assert.assertTrue(retrievedCoupons.size() >= 0);
}
@Test(groups = "integration")
public void testGetDunningCampaigns() throws Exception {
final DunningCampaigns retrievedDunningCampaigns = recurlyClient.getDunningCampaigns();
Assert.assertTrue(retrievedDunningCampaigns.size() >= 0);
}
@Test(groups="integration")
public void testGetAndDeleteAdjustment() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
final Plan planData = TestUtils.createRandomPlan();
try {
// Create a user
final Account account = recurlyClient.createAccount(accountData);
// Create BillingInfo
billingInfoData.setAccount(account);
final BillingInfo billingInfo = recurlyClient.createOrUpdateBillingInfo(billingInfoData);
// Create a plan
final Plan plan = recurlyClient.createPlan(planData);
// Subscribe the user to the plan
final Subscription subscriptionData = new Subscription();
subscriptionData.setPlanCode(plan.getPlanCode());
subscriptionData.setAccount(accountData);
subscriptionData.setCurrency(CURRENCY);
subscriptionData.setUnitAmountInCents(1242);
subscriptionData.setRemainingBillingCycles(null);
//Add some adjustments to the account's open invoice
final Adjustment adjustmentData = new Adjustment();
adjustmentData.setCurrency("USD");
adjustmentData.setUnitAmountInCents("100");
adjustmentData.setDescription("A description of an account adjustment");
Adjustment adjustment = recurlyClient.createAccountAdjustment(account.getAccountCode(), adjustmentData);
final String uuid = adjustment.getUuid();
adjustment = recurlyClient.getAdjustment(uuid);
Assert.assertEquals(adjustment.getUuid(), uuid);
recurlyClient.deleteAdjustment(uuid);
// Check that we deleted it
try {
recurlyClient.getAdjustment(uuid);
Assert.fail("Failed to delete the Adjustment");
} catch (final RecurlyAPIException e) {
Assert.assertEquals(e.getRecurlyError().getHttpStatusCode(), 404);
}
} finally {
// Close the account
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testGetAdjustments() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
final Plan planData = TestUtils.createRandomPlan();
try {
// Create a user
final Account account = recurlyClient.createAccount(accountData);
// Create BillingInfo
billingInfoData.setAccount(account);
final BillingInfo billingInfo = recurlyClient.createOrUpdateBillingInfo(billingInfoData);
Assert.assertNotNull(billingInfo);
final BillingInfo retrievedBillingInfo = recurlyClient.getBillingInfo(account.getAccountCode());
Assert.assertNotNull(retrievedBillingInfo);
// Create a plan
final Plan plan = recurlyClient.createPlan(planData);
// Subscribe the user to the plan
final Subscription subscriptionData = new Subscription();
subscriptionData.setPlanCode(plan.getPlanCode());
subscriptionData.setAccount(accountData);
subscriptionData.setCurrency(CURRENCY);
subscriptionData.setUnitAmountInCents(1242);
//Add some adjustments to the account's open invoice
final Adjustment adjustment = new Adjustment();
adjustment.setCurrency("USD");
adjustment.setUnitAmountInCents("100");
adjustment.setDescription("A description of an account adjustment");
//Use an "accounting code" for one of the adjustments
String adjustmentAccountCode = "example account code";
final Adjustment adjustmentWithCode = new Adjustment();
adjustmentWithCode.setAccountingCode(adjustmentAccountCode);
adjustmentWithCode.setCurrency("USD");
adjustmentWithCode.setUnitAmountInCents("200");
adjustmentWithCode.setDescription("A description of an account adjustment with a code");
//Create 2 new Adjustments
recurlyClient.createAccountAdjustment(accountData.getAccountCode(), adjustment);
recurlyClient.createAccountAdjustment(accountData.getAccountCode(), adjustmentWithCode);
// Test adjustment retrieval methods
Adjustments retrievedAdjustments = recurlyClient.getAccountAdjustments(accountData.getAccountCode(), null, null);
Assert.assertEquals(retrievedAdjustments.size(), 2, "Did not retrieve correct count of Adjustments of any type and state");
retrievedAdjustments = recurlyClient.getAccountAdjustments(accountData.getAccountCode(), Adjustments.AdjustmentType.CHARGE, null);
Assert.assertEquals(retrievedAdjustments.size(), 2, "Did not retrieve correct count of Adjustments of type Charge");
retrievedAdjustments = recurlyClient.getAccountAdjustments(accountData.getAccountCode(), Adjustments.AdjustmentType.CHARGE, Adjustments.AdjustmentState.INVOICED);
Assert.assertEquals(retrievedAdjustments.size(), 0, "Retrieved Adjustments of type Charge marked as invoiced although none should be.");
retrievedAdjustments = recurlyClient.getAccountAdjustments(accountData.getAccountCode(), null, Adjustments.AdjustmentState.INVOICED);
Assert.assertEquals(retrievedAdjustments.size(), 0, "Retrieved Adjustments marked as invoiced although none should be.");
retrievedAdjustments = recurlyClient.getAccountAdjustments(accountData.getAccountCode(), Adjustments.AdjustmentType.CHARGE, Adjustments.AdjustmentState.PENDING);
Assert.assertEquals(2, retrievedAdjustments.size(), "Did not retrieve correct count of Adjustments of type Charge in Pending state");
int adjAccountCodeCounter = 0;
for (Adjustment adj : retrievedAdjustments) {
if (adjustmentAccountCode.equals(adj.getAccountingCode())) {
adjAccountCodeCounter++;
}
}
Assert.assertEquals(adjAccountCodeCounter, 1, "An unexpected number of Adjustments were assigned the accountCode [" + adjustmentAccountCode + "]");
} finally {
// Close the account
recurlyClient.closeAccount(accountData.getAccountCode());
// Delete the Plan
recurlyClient.deletePlan(planData.getPlanCode());
}
}
@Test(groups = "integration")
public void testPagination() throws Exception {
System.setProperty(RECURLY_PAGE_SIZE, "1");
final int minNumberOfAccounts = 5;
for (int i = 0; i < minNumberOfAccounts; i++) {
final Account accountData = TestUtils.createRandomAccount();
recurlyClient.createAccount(accountData);
}
final Set<String> accountCodes = new HashSet<String>();
Accounts accounts = recurlyClient.getAccounts();
for (int i = 0; i < minNumberOfAccounts; i++) {
// If the environment is used, we will have more than the ones we created
Assert.assertEquals(accounts.size(), 1);
accountCodes.add(accounts.get(0).getAccountCode());
if (i < minNumberOfAccounts - 1) {
accounts = accounts.getNext();
}
}
Assert.assertEquals(accountCodes.size(), minNumberOfAccounts);
System.setProperty(RECURLY_PAGE_SIZE, "50");
}
@Test(groups = "integration")
public void testCreateAccountWithBadBillingInfo() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
// See http://docs.recurly.com/payment-gateways/test
billingInfoData.setNumber("4000-0000-0000-0093");
try {
final Account account = recurlyClient.createAccount(accountData);
billingInfoData.setAccount(account);
recurlyClient.createOrUpdateBillingInfo(billingInfoData);
Assert.fail("Should have thrown transaction exception");
} catch (TransactionErrorException e) {
Assert.assertEquals(e.getErrors().getTransactionError().getErrorCode(), "fraud_ip_address");
Assert.assertEquals(e.getErrors().getTransactionError().getMerchantMessage(), "The payment gateway declined the transaction because it originated from an IP address known for fraudulent transactions.");
Assert.assertEquals(e.getErrors().getTransactionError().getCustomerMessage(), "The transaction was declined. Please contact support.");
}
}
@Test(groups = "integration")
public void testCreateUpdateAccount() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
CustomFields customFields = new CustomFields();
// NOTE: acct_field and acct_field2 must be created on the integration server first
customFields.add(TestUtils.createRandomCustomField("acct_field"));
customFields.add(TestUtils.createRandomCustomField("acct_field2"));
accountData.setCustomFields(customFields);
final AccountAcquisition acquisitionData = TestUtils.createRandomAccountAcquisition();
accountData.setAccountAcquisition(acquisitionData);
try {
final DateTime creationDateTime = new DateTime(DateTimeZone.UTC);
final Account account = recurlyClient.createAccount(accountData);
// Test account creation
Assert.assertNotNull(account);
Assert.assertEquals(accountData.getAccountCode(), account.getAccountCode());
Assert.assertEquals(accountData.getEmail(), account.getEmail());
Assert.assertEquals(accountData.getFirstName(), account.getFirstName());
Assert.assertEquals(accountData.getLastName(), account.getLastName());
Assert.assertEquals(accountData.getUsername(), account.getUsername());
Assert.assertEquals(accountData.getAcceptLanguage(), account.getAcceptLanguage());
Assert.assertEquals(accountData.getCompanyName(), account.getCompanyName());
Assert.assertEquals(accountData.getAddress().getAddress1(), account.getAddress().getAddress1());
Assert.assertEquals(accountData.getAddress().getAddress2(), account.getAddress().getAddress2());
Assert.assertEquals(accountData.getAddress().getCity(), account.getAddress().getCity());
Assert.assertEquals(accountData.getAddress().getState(), account.getAddress().getState());
Assert.assertEquals(accountData.getAddress().getZip(), account.getAddress().getZip());
Assert.assertEquals(accountData.getAddress().getCountry(), account.getAddress().getCountry());
Assert.assertEquals(accountData.getAddress().getPhone(), account.getAddress().getPhone());
Assert.assertEquals(accountData.getCustomFields(), account.getCustomFields());
// fetch and check the acquisition data
final AccountAcquisition acquisition = recurlyClient.getAccountAcquisition(account.getAccountCode());
Assert.assertEquals(acquisition.getCurrency(), acquisitionData.getCurrency());
Assert.assertEquals(acquisition.getChannel(), acquisitionData.getChannel());
Assert.assertEquals(acquisition.getCampaign(), acquisitionData.getCampaign());
Assert.assertEquals(acquisition.getSubchannel(), acquisitionData.getSubchannel());
Assert.assertEquals(acquisition.getCostInCents(), acquisitionData.getCostInCents());
// Test getting all
final Accounts retrievedAccounts = recurlyClient.getAccounts();
Assert.assertTrue(retrievedAccounts.size() > 0);
// Test an account lookup
final Account retrievedAccount = recurlyClient.getAccount(account.getAccountCode());
Assert.assertEquals(retrievedAccount, account);
// Create a BillingInfo
billingInfoData.setAccount(account);
final BillingInfo billingInfo = recurlyClient.createOrUpdateBillingInfo(billingInfoData);
// Test BillingInfo creation
Assert.assertNotNull(billingInfo);
Assert.assertEquals(billingInfoData.getFirstName(), billingInfo.getFirstName());
Assert.assertEquals(billingInfoData.getLastName(), billingInfo.getLastName());
Assert.assertEquals(billingInfoData.getMonth(), billingInfo.getMonth());
Assert.assertEquals(billingInfoData.getYear(), billingInfo.getYear());
Assert.assertEquals(billingInfo.getCardType(), "Visa");
// Test BillingInfo lookup
final BillingInfo retrievedBillingInfo = recurlyClient.getBillingInfo(account.getAccountCode());
Assert.assertEquals(retrievedBillingInfo, billingInfo);
// Test Update Account
Account updateAccount = new Account();
updateAccount.setAccountCode(account.getAccountCode());
CustomFields fields = account.getCustomFields();
fields.get(0).setValue("");
fields.get(1).setValue("update this value");
updateAccount.setCustomFields(fields);
recurlyClient.updateAccount(updateAccount.getAccountCode(), updateAccount);
Account getAccount = recurlyClient.getAccount(updateAccount.getAccountCode());
Assert.assertEquals(getAccount.getCustomFields().size(), 1);
Assert.assertEquals(getAccount.getCustomFields().get(0).getValue(), "update this value");
} finally {
// Clean up
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testCreateAccountIbanBillingInfo() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomIbanBillingInfo();
try {
final Account account = recurlyClient.createAccount(accountData);
recurlyClient.createOrUpdateBillingInfo(account.getAccountCode(), billingInfoData);
Assert.fail("Should have thrown transaction exception");
} catch(TransactionErrorException e) {
Assert.assertEquals(e.getErrors().getTransactionError().getErrorCode(), "no_gateway");
Assert.assertEquals(e.getErrors().getTransactionError().getMerchantMessage(), "There is no available payment gateway on your account capable of processing this transaction.");
}
}
@Test(groups = "integration")
public void testCreateAccountBacsBillingInfo() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBacsBillingInfo();
try {
final Account account = recurlyClient.createAccount(accountData);
recurlyClient.createOrUpdateBillingInfo(account.getAccountCode(), billingInfoData);
Assert.fail("Should have thrown transaction exception");
} catch(TransactionErrorException e) {
Assert.assertEquals(e.getErrors().getTransactionError().getErrorCode(), "no_gateway");
Assert.assertEquals(e.getErrors().getTransactionError().getMerchantMessage(), "There is no available payment gateway on your account capable of processing this transaction.");
}
}
@Test(groups = "integration")
public void testCreateAccountBecsBillingInfo() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBecsBillingInfo();
try {
final Account account = recurlyClient.createAccount(accountData);
recurlyClient.createOrUpdateBillingInfo(account.getAccountCode(), billingInfoData);
Assert.fail("Should have thrown transaction exception");
} catch(TransactionErrorException e) {
Assert.assertEquals(e.getErrors().getTransactionError().getErrorCode(), "no_gateway");
Assert.assertEquals(e.getErrors().getTransactionError().getMerchantMessage(), "There is no available payment gateway on your account capable of processing this transaction.");
}
}
@Test(groups = "integration")
public void testGetAccountBalance() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
try {
final Account account = recurlyClient.createAccount(accountData);
// Create BillingInfo
billingInfoData.setAccount(account);
final BillingInfo billingInfo = recurlyClient.createOrUpdateBillingInfo(billingInfoData);
Assert.assertNotNull(billingInfo);
final BillingInfo retrievedBillingInfo = recurlyClient.getBillingInfo(account.getAccountCode());
Assert.assertNotNull(retrievedBillingInfo);
final Adjustment adjustment = new Adjustment();
adjustment.setUnitAmountInCents(150);
adjustment.setCurrency(CURRENCY);
recurlyClient.createAccountAdjustment(account.getAccountCode(), adjustment);
final AccountBalance balance = recurlyClient.getAccountBalance(account.getAccountCode());
Assert.assertEquals(balance.getBalanceInCents().getUnitAmountUSD(), new Integer(150));
Assert.assertEquals(balance.getProcessingPrepaymentBalanceInCents().getUnitAmountUSD(), new Integer(0));
Assert.assertEquals(balance.getAvailableCreditBalanceInCents().getUnitAmountUSD(), new Integer(0));
Assert.assertEquals(balance.getPastDue(), Boolean.FALSE);
} finally {
// Clean up
recurlyClient.clearBillingInfo(accountData.getAccountCode());
recurlyClient.closeAccount(accountData.getAccountCode());
}
}
@Test(groups = "integration")
public void testCreateItem() throws Exception {
final Item itemData = TestUtils.createRandomItem();
try {
// Create an item
final Item item = recurlyClient.createItem(itemData);
Assert.assertNotNull(item);
Assert.assertTrue(recurlyClient.getItems().size() > 0);
} finally {
// Delete the item
recurlyClient.deleteItem(itemData.getItemCode());
}
}
@Test(groups = "integration")
public void testUpdateItem() throws Exception {
final Item itemData = TestUtils.createRandomItem();
try {
// Create an item
final Item item = recurlyClient.createItem(itemData);
final Item itemChanges = new Item(); // Start with a fresh item object for changes
Assert.assertNotNull(item);
// Set the itemcode to identify which item to change
itemChanges.setItemCode(itemData.getItemCode());
// Change some attributes
itemChanges.setName("A new name");
itemChanges.setDescription("A new description");
// **custom fields must be configured through ui**
// final CustomFields customFields = new CustomFields();
// final CustomField field = new CustomField();
// field.setName("size");
// field.setValue("small");
// customFields.add(field);
// itemChanges.setCustomFields(customFields);
// Send off the changes and get the updated object
final Item updatedItem = recurlyClient.updateItem(itemChanges.getItemCode(), itemChanges);
Assert.assertNotNull(updatedItem);
Assert.assertEquals(updatedItem.getName(), "A new name");
Assert.assertEquals(updatedItem.getDescription(), "A new description");
// Assert.assertEquals(updatedItem.getCustomFields(), itemChanges.getCustomFields());
} finally {
// Delete the item
recurlyClient.deleteItem(itemData.getItemCode());
}
}
@Test(groups = "integration")
public void testReactivateItem() throws Exception {
final Item itemData = TestUtils.createRandomItem();
try {
// Create an item
final Item item = recurlyClient.createItem(itemData);
Assert.assertNotNull(item);
Assert.assertTrue(recurlyClient.getItems().size() > 0);
// Delete the item
recurlyClient.deleteItem(item.getItemCode());
final Item deletedItem = recurlyClient.getItem(item.getItemCode());
Assert.assertEquals(deletedItem.getState(), "inactive");
// Reactivate the item
recurlyClient.reactivateItem(item.getItemCode());
final Item reactivatedItem = recurlyClient.getItem(item.getItemCode());
Assert.assertEquals(reactivatedItem.getState(), "active");
} finally {
// Delete the item
recurlyClient.deleteItem(itemData.getItemCode());
}
}
@Test(groups = "integration")
public void testCreatePlan() throws Exception {
final Plan planData = TestUtils.createRandomPlan();
try {
// Create a plan
final Plan plan = recurlyClient.createPlan(planData);
// test creation of plan
Assert.assertNotNull(plan);
// Check that getting all the plans makes sense
Assert.assertTrue(recurlyClient.getPlans().size() > 0);
} finally {
// Delete the plan
recurlyClient.deletePlan(planData.getPlanCode());
// Check that we deleted it
try {
final Plan retrievedPlan2 = recurlyClient.getPlan(planData.getPlanCode());
Assert.fail("Failed to delete the Plan");
} catch (final RecurlyAPIException e) {
// good
}
}
}
@Test(groups = "integration")
public void testUpdatePlan() throws Exception {
final Plan planData = TestUtils.createRandomPlan();
try {
// Create a plan
final DateTime creationDateTime = new DateTime(DateTimeZone.UTC);
final Plan plan = recurlyClient.createPlan(planData);
final Plan planChanges = new Plan(); // Start with a fresh plan object for changes
Assert.assertNotNull(plan);
// Set the plancode to identify which plan to change
planChanges.setPlanCode(planData.getPlanCode());
// Change some attributes
planChanges.setName("A new name");
planChanges.setDescription("A new description");
// Send off the changes and get the updated object
final Plan updatedPlan = recurlyClient.updatePlan(planChanges);
Assert.assertNotNull(updatedPlan);
Assert.assertEquals(updatedPlan.getName(), "A new name");
Assert.assertEquals(updatedPlan.getDescription(), "A new description");
} finally {
// Delete the plan
recurlyClient.deletePlan(planData.getPlanCode());
// Check that we deleted it
try {
final Plan retrievedPlan2 = recurlyClient.getPlan(planData.getPlanCode());
Assert.fail("Failed to delete the Plan");
} catch (final RecurlyAPIException e) {
// good
}
}
}
@Test(groups = "integration")
public void testBulkUpdate() throws Exception {
//generate 3 random plans
final Plan planData1 = TestUtils.createRandomPlan();
final Plan planData2 = TestUtils.createRandomPlan();
final Plan planData3 = TestUtils.createRandomPlan();
try {
//create the random plans in recurly
final Plan plan1 = recurlyClient.createPlan(planData1);
final Plan plan2 = recurlyClient.createPlan(planData2);
final Plan plan3 = recurlyClient.createPlan(planData3);
Assert.assertNotNull(plan1);
Assert.assertNotNull(plan2);
Assert.assertNotNull(plan3);
//this test will only operate if there is at least one dunning campaign on the subdomain
DunningCampaigns dunningCampaigns = recurlyClient.getDunningCampaigns();
if (!dunningCampaigns.isEmpty()) {
//add the plancodes to a new dunning bulk update object
DunningCampaignBulkUpdate dunningCampaignBulkUpdate = new DunningCampaignBulkUpdate();
PlanCode planCode1 = new PlanCode(plan1.getPlanCode());
PlanCode planCode2 = new PlanCode(plan2.getPlanCode());
PlanCode planCode3 = new PlanCode(plan3.getPlanCode());
PlanCodes planCodes = new PlanCodes();
planCodes.setRecurlyObject(planCode1);
planCodes.setRecurlyObject(planCode2);
planCodes.setRecurlyObject(planCode3);
dunningCampaignBulkUpdate.setPlanCodes(planCodes);
//update the dunning campaign with the new plan codes, and verify that the plans were updated
String dunningCampaignId = dunningCampaigns.get(0).getId();
recurlyClient.bulkUpdate(dunningCampaignId, dunningCampaignBulkUpdate);
Plan updatedPlan1 = recurlyClient.getPlan(plan1.getPlanCode());
Plan updatedPlan2 = recurlyClient.getPlan(plan2.getPlanCode());
Plan updatedPlan3 = recurlyClient.getPlan(plan3.getPlanCode());
Assert.assertEquals(updatedPlan1.getDunningCampaignId(), dunningCampaignId);
Assert.assertEquals(updatedPlan2.getDunningCampaignId(), dunningCampaignId);
Assert.assertEquals(updatedPlan3.getDunningCampaignId(), dunningCampaignId);
}
} finally {
// Delete the plans
recurlyClient.deletePlan(planData1.getPlanCode());
recurlyClient.deletePlan(planData2.getPlanCode());
recurlyClient.deletePlan(planData3.getPlanCode());
}
}
@Test(groups = "integration")
public void testCreateSubscriptions() throws Exception {
final Account accountData = TestUtils.createRandomAccount();
final BillingInfo billingInfoData = TestUtils.createRandomBillingInfo();
final Plan planData = TestUtils.createRandomPlan();
final Coupon couponData = TestUtils.createRandomCoupon();
final Coupon couponDataForPlan = TestUtils.createRandomCoupon();
try {
// Create a user
final Account account = recurlyClient.createAccount(accountData);
// Create BillingInfo
billingInfoData.setAccount(account);
final BillingInfo billingInfo = recurlyClient.createOrUpdateBillingInfo(billingInfoData);
Assert.assertNotNull(billingInfo);
final BillingInfo retrievedBillingInfo = recurlyClient.getBillingInfo(account.getAccountCode());
Assert.assertNotNull(retrievedBillingInfo);
// Create a plan
final Plan plan = recurlyClient.createPlan(planData);
// Create a coupon
Coupon coupon = recurlyClient.createCoupon(couponData);
// Create a coupon for the plan
couponDataForPlan.setAppliesToAllPlans(false);
final PlanCodes planCodes = new PlanCodes();
planCodes.add(new PlanCode(plan.getPlanCode()));
couponDataForPlan.setPlanCodes(planCodes);
Coupon couponForPlan = recurlyClient.createCoupon(couponDataForPlan);
// Set up a subscription
final Subscription subscriptionData = new Subscription();
subscriptionData.setPlanCode(plan.getPlanCode());
subscriptionData.setAccount(accountData);
subscriptionData.setCurrency(CURRENCY);
subscriptionData.setUnitAmountInCents(1242);
subscriptionData.setRemainingBillingCycles(2);
// Apply a coupon at the time of subscription creation
subscriptionData.setCouponCode(couponData.getCouponCode());
// Create some notes on the subscription
subscriptionData.setCustomerNotes("Customer Notes");
subscriptionData.setTermsAndConditions("Terms and Conditions");
final DateTime creationDateTime = new DateTime(DateTimeZone.UTC);
// Preview the user subscribing to the plan
final Subscription subscriptionPreview = recurlyClient.previewSubscription(subscriptionData);
// Test the subscription preview
Assert.assertNotNull(subscriptionPreview);
Assert.assertEquals(subscriptionPreview.getCurrency(), subscriptionData.getCurrency());
if (null == subscriptionData.getQuantity()) {
Assert.assertEquals(subscriptionPreview.getQuantity(), new Integer(1));
} else {
Assert.assertEquals(subscriptionPreview.getQuantity(), subscriptionData.getQuantity());
}
//Assert.assertEquals(subscriptionPreview.getRemainingBillingCycles(), subscriptionData.getRemainingBillingCycles());
// Subscribe the user to the plan
final Subscription subscription = recurlyClient.createSubscription(subscriptionData);
// Test subscription creation
Assert.assertNotNull(subscription);
// Test invoice fetching via href
Assert.assertNotNull(subscription.getInvoice());
Assert.assertEquals(subscription.getCurrency(), subscriptionData.getCurrency());
if (null == subscriptionData.getQuantity()) {
Assert.assertEquals(subscription.getQuantity(), new Integer(1));
} else {
Assert.assertEquals(subscription.getQuantity(), subscriptionData.getQuantity());
}
//Assert.assertEquals(subscription.getRemainingBillingCycles(), subscriptionData.getRemainingBillingCycles());
// Test lookup for subscription
final Subscription sub1 = recurlyClient.getSubscription(subscription.getUuid());
Assert.assertNotNull(sub1);
Assert.assertEquals(sub1, subscription);
// Do a lookup for subs for given account
final Subscriptions subs = recurlyClient.getAccountSubscriptions(accountData.getAccountCode());
// Check that the newly created sub is in the list
Subscription found = null;
for (final Subscription s : subs) {
if (s.getUuid().equals(subscription.getUuid())) {
found = s;
break;
}
}
if (found == null) {
Assert.fail("Could not locate the subscription in the subscriptions associated with the account");