Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions src/main/java/co/dapi/ACH.java
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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to PullTransfer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}
}

}
38 changes: 37 additions & 1 deletion src/main/java/co/dapi/DapiApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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) {
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to createACHPull.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to getACHPull.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 operationID and userInputs, for the case of createPull, but for getPull is should not accept only userInputs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/co/dapi/response/CreatePullResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.dapi.response;

public class CreatePullResponse extends BaseResponse {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to CreateACHPullResponse.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreatePullResponse() {
super();
}

public CreatePullResponse(String errType, String errMsg) {
super(errType, errMsg);
}
}
21 changes: 21 additions & 0 deletions src/main/java/co/dapi/response/GetPullResponse.java
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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to GetACHPullResponse.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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);
}
}
26 changes: 26 additions & 0 deletions src/main/java/co/dapi/types/PullTransfer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package co.dapi.types;

public class PullTransfer {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to ACHPullTransferInfo.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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;
}

}