-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPayloadTest.java
More file actions
292 lines (253 loc) · 9.59 KB
/
PayloadTest.java
File metadata and controls
292 lines (253 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.payload.test;
import com.payload.pl;
import com.payload.Session;
import com.payload.arm.ARMRequest;
import com.payload.Exceptions;
import java.util.List;
import java.lang.reflect.Field;
import java.io.FileNotFoundException;
import org.json.*;
import org.junit.Test;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import com.github.javafaker.Faker;
import com.payload.test.Factory;
import com.payload.test.Fixtures;
public class PayloadTest {
public static Faker faker = new Faker();
public static Fixtures fixtures;
public static Factory factory;
@BeforeClass
public static void setUpClass() throws Exception {
if (System.getenv("API_URL") != null)
pl.api_url = System.getenv("API_URL");
pl.api_key = System.getenv("API_KEY");
factory = new Factory();
fixtures = new Fixtures(factory);
}
@AfterClass
public static void tearDownClass() throws Exception {
pl.api_url = pl.URL;
pl.api_key = null;
}
@Test
public void testCardPayment() throws Exception {
pl.Payment pmt = new pl.Payment() {
{
set("amount", 100);
set("processing_id", fixtures.processing_account.getStr("id"));
set("payment_method", new pl.Card() {
{
set("card", new JSONObject() {
{
put("card_number", "4242 4242 4242 4242");
put("expiry", "12/29");
}
});
set("billing_address", new JSONObject() {
{
put("postal_code", "12345");
}
});
}
});
create();
}
};
assertNotNull(pmt.getStr("id"));
assertEquals(pmt.getFloat("amount"), 100., 0.0001);
assertEquals(pmt.getJObj("payment_method").getJSONObject("card").get("card_number"), "xxxxxxxxxxxx4242");
}
@Test
public void testCardPaymentMapping() throws Exception {
pl.Payment pmt = new pl.Payment() {
{
set("amount", 100);
set("processing_id", fixtures.processing_account.getStr("id"));
set("payment_method", new pl.Card() {
{
set("card_number", "4242 4242 4242 4242");
set("expiry", "12/29");
set("billing_address", new JSONObject() {
{
put("postal_code", "12345");
}
});
}
});
create();
}
};
assertNotNull(pmt.getStr("id"));
assertEquals(pmt.getFloat("amount"), 100., 0.0001);
assertEquals(pmt.paymentMethod().getStr("card_number"), "xxxxxxxxxxxx4242");
}
@Test
public void testBankAccountPayment() throws Exception {
pl.Payment pmt = new pl.Payment() {
{
set("amount", 100);
set("processing_id", fixtures.processing_account.getStr("id"));
set("payment_method", new pl.BankAccount() {
{
set("account_number", "1234567890");
set("routing_number", "036001808");
set("account_type", "checking");
}
});
create();
}
};
assertNotNull(pmt.getStr("id"));
assertEquals(pmt.getFloat("amount"), 100., 0.0001);
assertEquals(pmt.paymentMethod().getStr("account_number"), "xxxxxx7890");
assertEquals(pmt.paymentMethod().getStr("routing_number"), "xxxxx1808");
assertEquals(pmt.paymentMethod().getStr("account_type"), "checking");
}
@Test
public void testCreateCust() throws Exception {
pl.Customer accnt = new pl.Customer() {
{
set("email", "test@gmail.com");
set("name", "Test Account");
create();
}
};
assertNotNull(accnt.getStr("id"));
assertEquals(accnt.getStr("email"), "test@gmail.com");
assertEquals(accnt.getStr("name"), "Test Account");
}
@Test
public void testMultiCreate() throws Exception {
pl.Customer[] custs = new pl.Customer[] { new pl.Customer() {
{
set("email", "test3@gmail.com");
set("name", "Test Account");
}
}, new pl.Customer() {
{
set("email", "test4@gmail.com");
set("name", "Test Account");
}
} };
List<pl.Customer> out = pl.Customer.create(custs);
assertNotNull(out.get(0).getStr("id"));
assertEquals(out.get(0).getStr("email"), "test3@gmail.com");
assertEquals(out.get(0).getStr("name"), "Test Account");
assertNotNull(out.get(1).getStr("id"));
assertEquals(out.get(1).getStr("email"), "test4@gmail.com");
assertEquals(out.get(1).getStr("name"), "Test Account");
}
@Test
public void testSelectCust() throws Exception {
List<pl.Customer> out = pl.Customer.filter_by("email", fixtures.customer.getStr("email")).all();
assertEquals(out.get(0).getStr("id"), fixtures.customer.getStr("id"));
assertEquals(out.size(), 1);
}
@Test
public void testGetCust() throws Exception {
pl.Customer cust = pl.Customer.get(fixtures.customer.getStr("id"));
assertEquals(cust.getStr("id"), fixtures.customer.getStr("id"));
assertEquals(cust.getStr("email"), fixtures.customer.getStr("email"));
}
@Test(expected = NullPointerException.class)
public void testBadGet() throws Exception {
pl.Customer.get(null);
}
@Test
public void testUpdateCust() throws Exception {
final String origEmail = faker.internet().emailAddress();
pl.Customer c = new pl.Customer() {
{
set("email", origEmail);
set("name", "Test Account");
create();
}
};
assertEquals(c.getStr("email"), origEmail);
String newEmail = faker.internet().emailAddress();
c.update(pl.attr("email", newEmail));
assertEquals(c.getStr("email"), newEmail);
c = pl.Customer.get(c.getStr("id")); // Refresh
assertEquals(c.getStr("email"), newEmail);
}
@Test(expected = Exceptions.NotFound.class)
public void testDelCust() throws Exception {
pl.Customer cust = new pl.Customer() {
{
set("email", faker.internet().emailAddress());
set("name", "Test Account");
create();
}
};
cust.delete();
cust = pl.Customer.get(cust.getStr("id"));
}
@Test
public void testCreateInvPaymentLink() throws Exception {
final pl.Invoice inv = new pl.Invoice() {
{
set("description", "Test Invoice");
set("type", "Bill");
set("due_date", "2021-06-15");
set("processing_id", fixtures.processing_account.getStr("id"));
set("customer", new pl.Customer() {
{
set("email", "test2@gmail.com");
set("name", "Test Account");
}
});
set("items", new pl.LineItem[] { new pl.ChargeItem() {
{
set("description", "Test Charge");
set("amount", "29.99");
}
} });
}
}.create();
pl.PaymentLink lnk = new pl.PaymentLink() {
{
set("invoice_id", inv.getStr("id"));
}
}.create();
assertEquals(lnk.getStr("description"), "Test Invoice");
assertEquals(lnk.getFloat("amount"), 29.99, 0.0001);
}
@Test
public void testCreateClientToken() throws Exception {
pl.ClientToken clientToken = new pl.ClientToken().create();
assertNotNull(clientToken.getStr("id"));
assertEquals(clientToken.getStr("type"), "client");
}
@Test
public void testCreateClientTokenWithCheckoutPageIntent() throws Exception {
pl.ClientToken clientToken = new pl.ClientToken() {
{
set("intent", new JSONObject() {
{
put("checkout_page", new JSONObject() {
{
put("amount", 100);
put("description", "Test Payment");
put("redirects", new JSONObject() {
{
put("completed_url", "http://localhost/payment-complete");
put("return_url", "http://localhost/cart");
}
});
}
});
}
});
}
}.create();
assertNotNull(clientToken.getStr("id"));
assertNotNull(clientToken.getJObj("intent").getJSONObject("checkout_page").getString("url"));
}
}