Repository navigation
Expand file tree
/
Copy pathPayment.java
More file actions
619 lines (534 loc) · 25.2 KB
/
Copy pathPayment.java
File metadata and controls
619 lines (534 loc) · 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package co.dapi;
import co.dapi.response.CreateBeneficiaryResponse;
import co.dapi.response.CreateTransferResponse;
import co.dapi.response.GetBeneficiariesResponse;
import co.dapi.response.TransferAutoflowResponse;
import co.dapi.types.Beneficiary;
import co.dapi.types.UserInput;
import co.dapi.types.BeneficiaryAddress;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Optional;
public class Payment {
private final Config config;
public Payment(Config config) {
this.config = config;
}
public CreateBeneficiaryResponse createBeneficiary(BeneficiaryInfo beneficiary, String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
CreateBenefRequest bodyObj = new CreateBenefRequest(beneficiary, this.config.getAppSecret(), userSecret,
operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(bodyObj, CreateBenefRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + bodyObj.action, headers);
// Convert the got response to the wanted response type
CreateBeneficiaryResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, CreateBeneficiaryResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new CreateBeneficiaryResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
public GetBeneficiariesResponse getBeneficiaries(String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
GetBenefsRequest bodyObj = new GetBenefsRequest(this.config.getAppSecret(), userSecret, operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(bodyObj, GetBenefsRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + bodyObj.action, headers);
// Convert the got response to the wanted response type
GetBeneficiariesResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, GetBeneficiariesResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new GetBeneficiariesResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
public CreateTransferResponse createTransfer(Transfer transfer, String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
CreateTransferRequest body = new CreateTransferRequest(transfer, this.config.getAppSecret(), userSecret,
operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(body, CreateTransferRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + body.action, headers);
// Convert the got response to the wanted response type
CreateTransferResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, CreateTransferResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new CreateTransferResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
public TransferAutoflowResponse transferAutoflow(TransferAutoflow transferAutoflow, String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
TransferAutoflowRequest body = new TransferAutoflowRequest(transferAutoflow, this.config.getAppSecret(), userSecret,
operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(body, TransferAutoflowRequest.class);
// Construct the headers needed for this request
HashMap<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + accessToken);
// Make the request and get the response
String respJson = DapiRequest.Do(bodyJson, DapiRequest.Dapi_URL + "/v2" + body.action, headers);
// Convert the got response to the wanted response type
TransferAutoflowResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, TransferAutoflowResponse.class);
} catch (JsonSyntaxException e) {
// Empty catch, cause the handling code is below
}
// Check if the got response was of unexpected format, and return a suitable response
if (resp == null || (resp.getStatus() == null && !resp.getType().isPresent())) {
// If the got response wasn't a JSON string, resp will be null, and if
// it didn't have the 'status' field, getStatus() will return null.
return new TransferAutoflowResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
public static class Transfer {
private final String senderID;
private final float amount;
private final String receiverID;
private final String name;
private final String iban;
private final String accountNumber;
private final String remark;
private final String nickname;
/**
* Create an object that holds the info for a transfer from a bank that requires the receiver to be already
* registered as a beneficiary to perform a transaction.
*
* @param senderID the id of the account which the money should be sent from.
* retrieved from one of the accounts array returned from the getAccounts method.
* @param amount the amount of money which should be sent.
* @param receiverID the id of the beneficiary which the money should be sent to.
* retrieved from one of the beneficiaries array returned from the getBeneficiaries method.
*/
public Transfer(String senderID, float amount, String receiverID) {
this.senderID = senderID;
this.amount = amount;
this.receiverID = receiverID;
this.name = null;
this.iban = null;
this.accountNumber = null;
this.remark = null;
this.nickname = null;
}
/**
* Create an object that holds the info for a transfer from a bank that requires the receiver to be already
* registered as a beneficiary to perform a transaction.
*
* @param senderID the id of the account which the money should be sent from.
* retrieved from one of the accounts array returned from the getAccounts method.
* @param amount the amount of money which should be sent.
* @param receiverID the id of the beneficiary which the money should be sent to.
* retrieved from one of the beneficiaries array returned from the getBeneficiaries method.
* @param remark the remark string that will be sent with this transfer.
*/
public Transfer(String senderID, float amount, String receiverID, String remark) {
this.senderID = senderID;
this.amount = amount;
this.receiverID = receiverID;
this.remark = remark;
this.name = null;
this.iban = null;
this.accountNumber = null;
this.nickname = null;
}
/**
* Create an object that holds the info for a transfer from a bank that handles the creation of beneficiaries
* on its own, internally, and doesn't require the receiver to be already registered as a beneficiary to perform
* a transaction.
*
* @param senderID the id of the account which the money should be sent from.
* retrieved from one of the accounts array returned from the getAccounts method.
* @param amount the amount of money which should be sent.
* @param name the name of receiver.
* @param iban the IBAN of the receiver's account.
* @param accountNumber the Account Number of the receiver's account.
*/
public Transfer(String senderID, float amount, String name, String iban, String accountNumber) {
this.senderID = senderID;
this.amount = amount;
this.name = name;
this.iban = iban;
this.accountNumber = accountNumber;
this.receiverID = null;
this.remark = null;
this.nickname = null;
}
/**
* Create an object that holds the info for a transfer from a bank that handles the creation of beneficiaries
* on its own, internally, and doesn't require the receiver to be already registered as a beneficiary to perform
* a transaction.
*
* @param senderID the id of the account which the money should be sent from.
* retrieved from one of the accounts array returned from the getAccounts method.
* @param amount the amount of money which should be sent.
* @param name the name of receiver.
* @param iban the IBAN of the receiver's account.
* @param accountNumber the Account Number of the receiver's account.
* @param remark the remark string that will be sent with this transfer.
* @param nickname the nickname of the receiver.
*/
public Transfer(String senderID, float amount, String name, String iban, String accountNumber, String remark, String nickname) {
this.senderID = senderID;
this.amount = amount;
this.name = name;
this.iban = iban;
this.accountNumber = accountNumber;
this.remark = remark;
this.nickname = nickname;
this.receiverID = null;
}
public String getSenderID() {
return senderID;
}
public float getAmount() {
return amount;
}
public Optional<String> getReceiverID() {
return Optional.ofNullable(receiverID);
}
public Optional<String> getName() {
return Optional.ofNullable(name);
}
public Optional<String> getIban() {
return Optional.ofNullable(iban);
}
public Optional<String> getAccountNumber() {
return Optional.ofNullable(accountNumber);
}
public Optional<String> getRemark() {
return Optional.ofNullable(remark);
}
public Optional<String> getNickname() {
return Optional.ofNullable(nickname);
}
}
public static class TransferAutoflow {
private final String bundleID;
private final String appKey;
private final String userID;
private final String bankID;
private final String senderID;
private final float amount;
private final String remark;
private final BeneficiaryInfo beneficiary;
/**
* Create an object that holds the info needed for the transferAutoflow method.
*
* @param bundleID one of the bundleIDs set for this app.
* @param appKey the appKey of this app.
* @param userID the userID of the user which is initiating this transfer.
* @param bankID the bankID of the user which is initiating this transfer.
* @param senderID the id of the account which the money should be sent from.
* retrieved from one of the accounts array returned from the getAccounts method.
* @param amount the amount of money which should be sent.
* @param beneficiary the required info about the beneficiary.
*/
public TransferAutoflow(String bundleID, String appKey, String userID, String bankID, String senderID, float amount, BeneficiaryInfo beneficiary) {
this.bundleID = bundleID;
this.appKey = appKey;
this.userID = userID;
this.bankID = bankID;
this.senderID = senderID;
this.amount = amount;
this.beneficiary = beneficiary;
this.remark = null;
}
/**
* Create an object that holds the info needed for the transferAutoflow method.
*
* @param bundleID one of the bundleIDs set for this app.
* @param appKey the appKey of this app.
* @param userID the userID of the user which is initiating this transfer.
* @param bankID the bankID of the user which is initiating this transfer.
* @param senderID the id of the account which the money should be sent from.
* retrieved from one of the accounts array returned from the getAccounts method.
* @param amount the amount of money which should be sent.
* @param beneficiary the required info about the beneficiary.
* @param remark the remark string that will be sent with this transfer.
*/
public TransferAutoflow(String bundleID, String appKey, String userID, String bankID, String senderID, float amount, BeneficiaryInfo beneficiary, String remark) {
this.bundleID = bundleID;
this.appKey = appKey;
this.userID = userID;
this.bankID = bankID;
this.senderID = senderID;
this.amount = amount;
this.beneficiary = beneficiary;
this.remark = remark;
}
public String getBundleID() {
return bundleID;
}
public String getAppKey() {
return appKey;
}
public String getUserID() {
return userID;
}
public String getSenderID() {
return senderID;
}
public float getAmount() {
return amount;
}
public BeneficiaryInfo getBeneficiary() {
return beneficiary;
}
public Optional<String> getRemark() {
return Optional.ofNullable(remark);
}
}
public static class BeneficiaryInfo {
private final String name;
private final String accountNumber;
private final String iban;
private final String swiftCode;
private final Beneficiary.BeneficiaryType type;
private final BeneficiaryAddress address;
private final String country;
private final String branchAddress;
private final String branchName;
private final String bankName;
private final String phoneNumber;
private final String sortCode;
private final String nickname;
private final String routingNumber;
/**
* Creates an object that holds all the info about some beneficiary.
*/
public BeneficiaryInfo(String name,
String accountNumber,
String iban,
String swiftCode,
Beneficiary.BeneficiaryType type,
BeneficiaryAddress address,
String country,
String branchAddress,
String branchName,
String bankName,
String phoneNumber,
String sortCode,
String nickname,
String routingNumber) {
this.name = name;
this.accountNumber = accountNumber;
this.iban = iban;
this.swiftCode = swiftCode;
this.type = type;
this.address = address;
this.country = country;
this.branchAddress = branchAddress;
this.branchName = branchName;
this.bankName = bankName;
this.phoneNumber = phoneNumber;
this.sortCode = sortCode;
this.nickname = nickname;
this.routingNumber = routingNumber;
}
/**
* Creates an object that holds only the required info about some beneficiary.
*/
public BeneficiaryInfo(String name,
String accountNumber,
String iban,
String swiftCode,
Beneficiary.BeneficiaryType type,
BeneficiaryAddress address,
String country,
String branchAddress,
String branchName) {
this.name = name;
this.accountNumber = accountNumber;
this.iban = iban;
this.swiftCode = swiftCode;
this.type = type;
this.address = address;
this.country = country;
this.branchAddress = branchAddress;
this.branchName = branchName;
this.bankName = null;
this.phoneNumber = null;
this.sortCode = null;
this.nickname = null;
this.routingNumber = null;
}
public String getName() {
return name;
}
public String getIban() {
return iban;
}
public String getAccountNumber() {
return accountNumber;
}
public String getSwiftCode() {
return swiftCode;
}
public Beneficiary.BeneficiaryType getType() {
return type;
}
public BeneficiaryAddress getAddress() {
return address;
}
public String getCountry() {
return country;
}
public String getBranchAddress() {
return branchAddress;
}
public String getBranchName() {
return branchName;
}
public Optional<String> getBankName() {
return Optional.ofNullable(bankName);
}
public Optional<String> getPhoneNumber() {
return Optional.ofNullable(phoneNumber);
}
public Optional<String> getSortCode() {
return Optional.ofNullable(sortCode);
}
public Optional<String> getNickname() {
return Optional.ofNullable(nickname);
}
public Optional<String> getRoutingNumber() {
return Optional.ofNullable(routingNumber);
}
}
private static class CreateBenefRequest extends DapiRequest.BaseRequest {
private final String action = "/payment/beneficiaries/create";
private final String name;
private final String accountNumber;
private final String iban;
private final String swiftCode;
private final Beneficiary.BeneficiaryType type;
private final BeneficiaryAddress address;
private final String country;
private final String branchAddress;
private final String branchName;
private final String bankName;
private final String phoneNumber;
private final String sortCode;
private final String nickname;
private final String routingNumber;
public CreateBenefRequest(BeneficiaryInfo beneficiary,
String appSecret,
String userSecret,
String operationID,
UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
this.name = beneficiary.name;
this.accountNumber = beneficiary.accountNumber;
this.iban = beneficiary.iban;
this.swiftCode = beneficiary.swiftCode;
this.type = beneficiary.type;
this.address = beneficiary.address;
this.country = beneficiary.country;
this.branchAddress = beneficiary.branchAddress;
this.branchName = beneficiary.branchName;
this.bankName = beneficiary.bankName;
this.phoneNumber = beneficiary.phoneNumber;
this.sortCode = beneficiary.sortCode;
this.nickname = beneficiary.nickname;
this.routingNumber = beneficiary.routingNumber;
}
}
private static class GetBenefsRequest extends DapiRequest.BaseRequest {
private final String action = "/payment/beneficiaries/get";
public GetBenefsRequest(String appSecret,
String userSecret,
String operationID,
UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
}
}
private static class CreateTransferRequest extends DapiRequest.BaseRequest {
private final String action = "/payment/transfer/create";
private final String senderID;
private final float amount;
private final String receiverID;
private final String name;
private final String iban;
private final String accountNumber;
private final String remark;
private final String nickname;
public CreateTransferRequest(Transfer transfer,
String appSecret,
String userSecret,
String operationID,
UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
this.senderID = transfer.senderID;
this.amount = transfer.amount;
this.receiverID = transfer.receiverID;
this.name = transfer.name;
this.iban = transfer.iban;
this.accountNumber = transfer.accountNumber;
this.remark = transfer.remark;
this.nickname = transfer.nickname;
}
}
private static class TransferAutoflowRequest extends DapiRequest.BaseRequest {
private final String action = "/payment/transfer/autoflow";
private final String bundleID;
private final String appKey;
private final String userID;
private final String bankID;
private final String senderID;
private final float amount;
private final BeneficiaryInfo beneficiary;
private final String remark;
public TransferAutoflowRequest(TransferAutoflow transferAutoflow,
String appSecret,
String userSecret,
String operationID,
UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
this.bundleID = transferAutoflow.bundleID;
this.appKey = transferAutoflow.appKey;
this.userID = transferAutoflow.userID;
this.bankID = transferAutoflow.bankID;
this.senderID = transferAutoflow.senderID;
this.amount = transferAutoflow.amount;
this.beneficiary = transferAutoflow.beneficiary;
this.remark = transferAutoflow.remark;
}
}
}