-
Notifications
You must be signed in to change notification settings - Fork 1
/
StripeApplicationIntegrationTests.java
341 lines (262 loc) · 11.4 KB
/
StripeApplicationIntegrationTests.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
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed 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.technochord.stripe;
import java.util.List;
import com.stripe.model.Charge;
import com.stripe.model.Coupon;
import com.stripe.model.Customer;
import com.stripe.model.Invoice;
import com.technochord.stripe.model.CustomerLite;
import com.technochord.stripe.service.StripeService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class StripeApplicationIntegrationTests {
private StripeService stripeService;
private String apiKey = System.getProperty("stripe.apiKey");
@Before
public void setup() {
this.stripeService = new StripeService(this.apiKey);
List<CustomerLite> customerList = this.stripeService.listAllCustomers();
for (CustomerLite c : customerList) {
// cancel all subscriptions
this.stripeService.cancelAllExistingSubscriptionsForCustomer(c.getId());
}
this.stripeService.deleteAllCustomers();
}
@Test(expected = StripeException.class)
public void test_creation_of_customer_with_same_email_should_fail() {
// Create a customer
String customerId = this.stripeService.createCustomer("[email protected]",
"a description");
// Create customer eith same email different description
this.stripeService.createCustomer("[email protected]", "a new description");
}
@Test
public void test_retrieval_of_non_existent_customerId() {
// Create a customer
String customerId = this.stripeService.createCustomer("[email protected]",
"a description");
Customer c = this.stripeService.retrieveCustomerById(customerId + "bogus");
Assert.assertTrue(c == null);
}
@Test
public void test_retrieval_of_non_existent_customer_email() {
// Create a customer
this.stripeService.createCustomer("[email protected]", "a description");
Customer c = this.stripeService.retrieveCustomerByEmail("[email protected]");
Assert.assertTrue(c == null);
}
@Test
public void test_successful_subscription() {
// Create a customer
String customerId = this.stripeService.createCustomer("[email protected]",
"a description");
// Add a payment source
this.stripeService.replacePaymentSourceForCustomer(customerId, "tok_amex");
// Create a subscription against a plan and charge the customer
String subscriptionId = this.stripeService.createSubscriptionForCustomerAndCharge(
"[email protected]", "monthly-plan");
// Invoice
Invoice invoice = this.stripeService
.getLatestInvoiceForSubscription(subscriptionId);
Assert.assertTrue(invoice.getAmountPaid() > 0);
}
@Test(expected = StripeException.class)
public void test_subscription_without_payment_source_on_customer_should_fail() {
// Create a customer
String customerId = this.stripeService.createCustomer("[email protected]",
"a description");
// Create a subscription against a plan and charge the customer
String subscriptionId = this.stripeService.createSubscriptionForCustomerAndCharge(
"[email protected]", "monthly-plan");
}
@Test(expected = StripeException.class)
public void test_subscription_after_removal_of_payment_source_on_customer_should_fail() {
// Create a customer
String customerId = this.stripeService.createCustomer("[email protected]",
"a description");
// Add a payment source
this.stripeService.replacePaymentSourceForCustomer(customerId, "tok_amex");
// Create a subscription against a plan and charge the customer
String subscriptionId = this.stripeService.createSubscriptionForCustomerAndCharge(
"[email protected]", "monthly-plan");
// Now remove the payment source
this.stripeService.removePaymentSourceFromCustomer(customerId);
// Try subscribing again
this.stripeService.createSubscriptionForCustomerAndCharge("[email protected]",
"monthly-plan");
}
@Test
public void test_that_coupon_gets_applied() {
// Create a customer
String customerId = this.stripeService.createCustomer("[email protected]",
"a description");
// Add a payment source
this.stripeService.replacePaymentSourceForCustomer(customerId, "tok_amex");
// Create a subscription against a plan and charge the customer
String subscriptionId = this.stripeService.createSubscriptionForCustomerAndCharge(
"[email protected]", "monthly-plan");
// Check the invoiced amount is correct
Invoice invoice = this.stripeService
.getLatestInvoiceForSubscription(subscriptionId);
Assert.assertTrue(invoice.getAmountPaid() == 100);
// Apply a coupon to the customer
this.stripeService.applyCouponToCustomer(customerId, "TEST_COUPON_ID");
// Subscribe again
String reSubscriptionId = this.stripeService
.createSubscriptionForCustomerAndCharge("[email protected]",
"monthly-plan");
// Check invoice.. should be the discounted amount
Invoice reInvoice = this.stripeService
.getLatestInvoiceForSubscription(reSubscriptionId);
Assert.assertTrue(reInvoice.getAmountPaid() == 90);
}
@Test
public void test_that_payment_source_can_get_changed() {
// Create a customer
String customerId = this.stripeService.createCustomer("[email protected]",
"a description");
// Add a payment source
this.stripeService.replacePaymentSourceForCustomer(customerId, "tok_amex");
// Create a subscription against a plan and charge the customer
String subscriptionId = this.stripeService.createSubscriptionForCustomerAndCharge(
"[email protected]", "monthly-plan");
// Invoice
Invoice invoice = this.stripeService
.getLatestInvoiceForSubscription(subscriptionId);
// Get the related charge
Charge charge = this.stripeService.getCharge(invoice.getCharge());
Assert.assertTrue(charge.getStatus().equals("succeeded"));
// Change the payment source
this.stripeService.replacePaymentSourceForCustomer(customerId, "tok_visa");
// Subscribe again
String reSubscriptionId = this.stripeService
.createSubscriptionForCustomerAndCharge("[email protected]",
"monthly-plan");
// Invoice
Invoice reInvoice = this.stripeService
.getLatestInvoiceForSubscription(reSubscriptionId);
// Get the related charge
Charge reCharge = this.stripeService.getCharge(reInvoice.getCharge());
// Assert success
Assert.assertTrue(reCharge.getStatus().equals("succeeded"));
}
@Test
public void test_get_customers_of_a_certain_category_and_updation_of_customer_category() {
// Create some customers, some with categories
String customerId1 = this.stripeService.createCustomer("[email protected]",
"a description1");
String customerId2 = this.stripeService.createCustomer("[email protected]",
"a description2");
String customerId3 = this.stripeService.createCustomer("[email protected]",
"a description3", "CAT1");
String customerId4 = this.stripeService.createCustomer("[email protected]",
"a description4");
String customerId5 = this.stripeService.createCustomer("[email protected]",
"a description5", "CAT1");
String customerId6 = this.stripeService.createCustomer("[email protected]",
"a description6", "CAT1");
String customerId7 = this.stripeService.createCustomer("[email protected]",
"a description7");
String customerId8 = this.stripeService.createCustomer("[email protected]",
"a description8");
// Retrieve that category of customers
List<CustomerLite> cat1CustomerLiteList = this.stripeService
.listAllCustomersByCategory("CAT1");
Assert.assertTrue(cat1CustomerLiteList != null);
Assert.assertTrue(cat1CustomerLiteList.size() == 3);
Assert.assertTrue(cat1CustomerLiteList.stream()
.filter((c) -> c.getEmail().equals("[email protected]")).count() == 1);
Assert.assertTrue(cat1CustomerLiteList.stream()
.filter((c) -> c.getEmail().equals("[email protected]")).count() == 1);
Assert.assertTrue(cat1CustomerLiteList.stream()
.filter((c) -> c.getEmail().equals("[email protected]")).count() == 1);
// Now update the category of one of the customers
this.stripeService.updateCustomerCategory(customerId5, "CAT2");
// Re-retrieve that category of customers
cat1CustomerLiteList = this.stripeService.listAllCustomersByCategory("CAT1");
Assert.assertTrue(cat1CustomerLiteList != null);
Assert.assertTrue(cat1CustomerLiteList.size() == 2);
// Re-retrieve that category of customers
List<CustomerLite> cat2CustomerLiteList = this.stripeService
.listAllCustomersByCategory("CAT2");
Assert.assertTrue(cat2CustomerLiteList != null);
Assert.assertTrue(cat2CustomerLiteList.size() == 1);
Assert.assertTrue(cat2CustomerLiteList.stream()
.filter((c) -> c.getEmail().equals("[email protected]")).count() == 1);
}
@Test
public void test_change_customers_email_before_subscription() {
// Create a customer
String customerId1 = this.stripeService.createCustomer("[email protected]",
"a description1");
this.stripeService.changeCustomerEmail(customerId1, "[email protected]");
Customer customer = this.stripeService
.retrieveCustomerByEmail("[email protected]");
Assert.assertTrue(customer != null);
}
@Test
public void test_change_customers_email_after_subscription() {
// Create a customer
String customerId = this.stripeService.createCustomer("[email protected]",
"a description");
// Add a payment source
this.stripeService.replacePaymentSourceForCustomer(customerId, "tok_amex");
// Create a subscription against a plan and charge the customer
String subscriptionId = this.stripeService.createSubscriptionForCustomerAndCharge(
"[email protected]", "monthly-plan");
this.stripeService.changeCustomerEmail(customerId, "[email protected]");
Customer customer = this.stripeService
.retrieveCustomerByEmail("[email protected]");
Assert.assertTrue(customer != null);
}
@Test(expected = StripeException.class)
public void test_change_customers_email_to_existing_email() {
// Create 2 customers
String customerId1 = this.stripeService.createCustomer("[email protected]",
"a description1");
String customerId2 = this.stripeService.createCustomer("[email protected]",
"a description2");
this.stripeService.changeCustomerEmail(customerId1, "[email protected]");
}
@Test
public void test_if_customer_has_valid_payment_source() {
// Create a customer
String customerId = this.stripeService.createCustomer("[email protected]",
"a description");
// Add a payment source
this.stripeService.replacePaymentSourceForCustomer(customerId, "tok_amex");
Boolean boo = this.stripeService.doesCustomerHaveActivePaymentSource(customerId);
Assert.assertTrue(boo);
this.stripeService.removePaymentSourceFromCustomer(customerId);
boo = this.stripeService.doesCustomerHaveActivePaymentSource(customerId);
Assert.assertTrue(!boo);
}
@Test
// @Ignore //external dep
public void test_list_all_coupons() {
List<Coupon> couponList = this.stripeService.listAllCoupons();
Assert.assertTrue(couponList != null);
Assert.assertTrue(couponList.size() == 1);
}
@Test
public void test_list_all_invoices() {
List<Invoice> invoiceList = this.stripeService.listAllInvoices();
Assert.assertTrue(invoiceList != null);
Assert.assertTrue(invoiceList.size() > 0);
}
}