-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathACH.java
More file actions
145 lines (113 loc) · 5.58 KB
/
ACH.java
File metadata and controls
145 lines (113 loc) · 5.58 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
package co.dapi;
import co.dapi.response.CreateACHPullResponse;
import co.dapi.response.GetACHPullResponse;
import co.dapi.types.UserInput;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Optional;
public class ACH {
private final Config config;
public ACH(Config config) {
this.config = config;
}
public CreateACHPullResponse createPull(PullTransfer transfer, String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
CreatePullRequest bodyObj = new CreatePullRequest(transfer, this.config.getAppSecret(), userSecret,
operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(bodyObj, CreatePullRequest.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
CreateACHPullResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, CreateACHPullResponse.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 CreateACHPullResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
public GetACHPullResponse getPull(String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException {
// Create the request body of this call
GetPullRequest bodyObj = new GetPullRequest(this.config.getAppSecret(), userSecret, operationID, userInputs);
// Convert the request body to a JSON string
String bodyJson = DapiRequest.jsonAgent.toJson(bodyObj, GetPullRequest.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
GetACHPullResponse resp = null;
try {
resp = DapiRequest.jsonAgent.fromJson(respJson, GetACHPullResponse.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 GetACHPullResponse("UNEXPECTED_RESPONSE", "Unexpected response body");
}
return resp;
}
public static class PullTransfer {
private final String senderID;
private final float amount;
private final String description;
/**
* Create an object that holds the info for an ACH create pull
*
* @param senderID the id of the account which the money should be pulled from.
* retrieved from one of the accounts array returned from the getAccounts method.
* @param amount the amount of money which should be pulled.
* @param description description for the ACH pull.
*/
public PullTransfer(String senderID, float amount, String description) {
this.senderID = senderID;
this.amount = amount;
this.description = description;
}
public String getSenderID() {
return senderID;
}
public float getAmount() {
return amount;
}
public Optional<String> getDescription() {
return Optional.ofNullable(description);
}
}
private static class CreatePullRequest extends DapiRequest.BaseRequest {
private final String action = "/ach/pull/create";
private final PullTransfer transfer;
public CreatePullRequest(PullTransfer transfer,
String appSecret,
String userSecret,
String operationID,
UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
this.transfer = transfer;
}
}
private static class GetPullRequest extends DapiRequest.BaseRequest {
private final String action = "/ach/pull/get";
public GetPullRequest(String appSecret,
String userSecret,
String operationID,
UserInput[] userInputs) {
super(appSecret, userSecret, operationID, userInputs);
}
}
}