-
Notifications
You must be signed in to change notification settings - Fork 2
CS-1982 | CS-2170: Added ACH endpoints #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 3 commits
f5d218f
a07c51e
c69f630
d34e467
b38f593
b06d400
e377868
c78847c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package co.dapi; | ||
|
|
||
| import co.dapi.response.CreatePullResponse; | ||
| import co.dapi.response.GetPullResponse; | ||
| 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 CreatePullResponse createPull(CreatePull 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 | ||
| CreatePullResponse resp = null; | ||
| try { | ||
| resp = DapiRequest.jsonAgent.fromJson(respJson, CreatePullResponse.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 CreatePullResponse("UNEXPECTED_RESPONSE", "Unexpected response body"); | ||
| } | ||
|
|
||
| return resp; | ||
| } | ||
|
|
||
| public GetPullResponse 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 | ||
| GetPullResponse resp = null; | ||
| try { | ||
| resp = DapiRequest.jsonAgent.fromJson(respJson, GetPullResponse.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 GetPullResponse("UNEXPECTED_RESPONSE", "Unexpected response body"); | ||
| } | ||
|
|
||
| return resp; | ||
| } | ||
|
|
||
| public static class CreatePull { | ||
| 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 CreatePull(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 CreatePull transfer; | ||
|
|
||
| public CreatePullRequest(CreatePull 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); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,12 @@ | |
| import co.dapi.types.UserInput; | ||
| import com.google.gson.JsonSyntaxException; | ||
| import okhttp3.Response; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.LocalDate; | ||
| import java.util.HashMap; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * {@link DapiApp} represents a client app that's using one or more of the Dapi products. | ||
| */ | ||
|
|
@@ -19,6 +19,7 @@ public class DapiApp { | |
| private final Auth a; | ||
| private final Data d; | ||
| private final Payment p; | ||
| private final ACH ach; | ||
| private final Metadata m; | ||
|
|
||
| public DapiApp(Config config) { | ||
|
|
@@ -27,6 +28,7 @@ public DapiApp(Config config) { | |
| this.d = new Data(config); | ||
| this.p = new Payment(config); | ||
| this.m = new Metadata(config); | ||
| this.ach = new ACH(config); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -297,6 +299,40 @@ public GetAccountsMetadataResponse getAccountsMetadata(String accessToken, Strin | |
| return this.m.getAccountsMetadata(accessToken, userSecret, operationID, userInputs); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * createPull talks to the CreatePull endpoint of Dapi, with this {@link DapiApp}'s appSecret, | ||
| * to continue a previous operation that required to provide some userInputs. | ||
| * | ||
| * @param transfer the transfer details that we want to initiate. | ||
| * @param accessToken retrieved from the ExchangeToken process. | ||
| * @param userSecret retrieved from the user login. | ||
| * @param operationID retrieved from the previous call's response. | ||
| * @param userInputs built from the previous call's response, and the required user input. | ||
| * @return an {@link CreatePullResponse}. | ||
| * @throws IOException in case of trouble happened while executing the request or reading the response. | ||
| */ | ||
| public CreatePullResponse createPull(ACH.CreatePull transfer, String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return this.ach.createPull(transfer, accessToken, userSecret, operationID, userInputs); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * getPull talks to the GetPull endpoint of Dapi, with this {@link DapiApp}'s appSecret, | ||
| * to continue a previous operation that required to provide some userInputs. | ||
| * | ||
| * @param accessToken retrieved from the ExchangeToken process. | ||
| * @param userSecret retrieved from the user login. | ||
| * @param operationID OperationID of the createPull request | ||
| * @param userInputs built from the previous call's response, and the required user input. | ||
| * @return an {@link GetPullResponse}. | ||
| * @throws IOException in case of trouble happened while executing the request or reading the response. | ||
| */ | ||
| public GetPullResponse getPull(String accessToken, String userSecret, String operationID, UserInput[] userInputs) throws IOException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return this.ach.getPull(accessToken, userSecret, operationID, userInputs); | ||
| } | ||
|
Comment on lines
+303
to
+359
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each method should an extra overloaded version, one that doesn't accept the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
|
|
||
| /** | ||
| * handleSDKRequest injects this {@link DapiApp}'s appSecret in the provided request body, bodyJson, and then | ||
| * forwards the request to Dapi, with the passed headers, headersMap, and returns the RAW response got. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package co.dapi.response; | ||
|
|
||
| public class CreatePullResponse extends BaseResponse { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| CreatePullResponse() { | ||
| super(); | ||
| } | ||
|
|
||
| public CreatePullResponse(String errType, String errMsg) { | ||
| super(errType, errMsg); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package co.dapi.response; | ||
|
|
||
| import co.dapi.types.PullTransfer; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public class GetPullResponse extends BaseResponse { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| GetPullResponse() { | ||
| super(); | ||
| } | ||
|
|
||
| private PullTransfer transfer; | ||
|
|
||
| public GetPullResponse(String errType, String errMsg) { | ||
| super(errType, errMsg); | ||
| } | ||
|
|
||
| public Optional<PullTransfer> getTransfer() { | ||
| return Optional.ofNullable(transfer); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package co.dapi.types; | ||
|
|
||
| public class PullTransfer { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| private final Float amount; | ||
| private final String status; | ||
| private final Currency currency; | ||
|
|
||
| public PullTransfer(Float amount, String status, Currency currency) { | ||
| this.amount = amount; | ||
| this.status = status; | ||
| this.currency = currency; | ||
| } | ||
|
|
||
| public Float getAmount() { | ||
| return amount; | ||
| } | ||
|
|
||
| public String getStatus() { | ||
| return status; | ||
| } | ||
|
|
||
| public Currency getCurrency() { | ||
| return currency; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to
PullTransfer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b38f593