Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
[RELEASE] v1.0.0-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
mbnakaya authored Aug 21, 2018
1 parent 19315a2 commit e81bd7c
Show file tree
Hide file tree
Showing 64 changed files with 5,498 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# moip-sdk-java-2
# moip-sdk-java

<img src="https://user-images.githubusercontent.com/32847427/42348249-1e007adc-807f-11e8-975f-5075ec3b13ab.png" align="left" />
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'signing'

group 'br.com.moip'
archivesBaseName = "sdk-java"
version '0.1.0'
version '1.0.0-beta'

description = "Java SDK for Moip APIs integration"

Expand Down
77 changes: 54 additions & 23 deletions src/main/java/br/com/moip/api/APIResources.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,66 @@
package br.com.moip.api;

import br.com.moip.models.Customer;
import br.com.moip.models.Order;
import br.com.moip.models.Payment;
import br.com.moip.models.Refund;
import br.com.moip.models.NotificationPreference;
import br.com.moip.models.Webhook;
import br.com.moip.models.Customers;
import br.com.moip.models.Orders;
import br.com.moip.models.Payments;
import br.com.moip.models.Refunds;
import br.com.moip.models.NotificationPreferences;
import br.com.moip.models.Webhooks;
import br.com.moip.models.Accounts;
import br.com.moip.models.Connect;
import br.com.moip.models.Multiorders;
import br.com.moip.models.Multipayments;
import br.com.moip.models.BankAccounts;
import br.com.moip.models.Balances;
import br.com.moip.models.Entries;
import br.com.moip.models.Transfers;
import br.com.moip.models.Escrows;

public class APIResources {

private static Customer customerInstance = new Customer();
private static Order orderInstance = new Order();
private static Payment paymentInstance = new Payment();
private static Refund refundInstance = new Refund();
private static NotificationPreference notificationPreferenceInstance = new NotificationPreference();
private static Webhook webhookInstance = new Webhook();
private static Customers customerInstance = new Customers();
private static Orders orderInstance = new Orders();
private static Payments paymentInstance = new Payments();
private static Refunds refundInstance = new Refunds();
private static NotificationPreferences notificationPreferenceInstance = new NotificationPreferences();
private static Webhooks webhookInstance = new Webhooks();
private static Accounts accountInstance = new Accounts();
private static Connect connectInstance = new Connect();
private static Multiorders multiorderInstance = new Multiorders();
private static Multipayments multipaymentsInstance = new Multipayments();
private static BankAccounts bankAccountsInstance = new BankAccounts();
private static Balances balancesInstance = new Balances();
private static Entries entriesInstance = new Entries();
private static Transfers transfersInstance = new Transfers();
private static Escrows escrowsInstance = new Escrows();

/**
* This method is used to get a instance of customer object.
*
* @return {@code Customer}
*/
public static Customer customers() { return customerInstance; }
public static Customers customers() { return customerInstance; }

public static Order orders() { return orderInstance; }
public static Orders orders() { return orderInstance; }

public static Payment payments() { return paymentInstance; }
public static Payments payments() { return paymentInstance; }

public static Refund refunds() { return refundInstance; }
public static Refunds refunds() { return refundInstance; }

public static NotificationPreference notificationPreferences() { return notificationPreferenceInstance; }
public static NotificationPreferences notificationPreferences() { return notificationPreferenceInstance; }

public static Webhook webhooks() { return webhookInstance; }
public static Webhooks webhooks() { return webhookInstance; }

public static Accounts accounts() { return accountInstance; }

public static Connect connect() { return connectInstance; }

public static Multiorders multiorders() { return multiorderInstance; }

public static Multipayments multipayments() { return multipaymentsInstance; }

public static BankAccounts bankAccounts() { return bankAccountsInstance; }

public static Balances balances() { return balancesInstance; }

public static Entries entries() { return entriesInstance; }

public static Transfers transfers() { return transfersInstance; }

public static Escrows escrows() { return escrowsInstance; }
}
4 changes: 0 additions & 4 deletions src/main/java/br/com/moip/api/request/RequestMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ public Map<String, Object> doRequest(final RequestProperties requestProps) {
LOGGER.debug("---> {} {}", requestProps.method, connection.getURL().toString());
logHeaders(connection.getRequestProperties().entrySet());

// Verificar essa parte do código, pois o objeto serializado deve ser um Map, não mais um String

if (requestProps.body != null) {
connection.setDoOutput(true);
String body = tools.getBody(requestProps.body, requestProps.contentType);
Expand All @@ -108,8 +106,6 @@ public Map<String, Object> doRequest(final RequestProperties requestProps) {
wr.close();
}

// ----------------------------------------------------------------------------------------------

LOGGER.debug("---> END HTTP");

int responseCode = connection.getResponseCode();
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/br/com/moip/api/response/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ public class Response extends HashMap<String, Object> {
* @return {@code Map}
*/
public Map<String, Object> jsonToMap(String json) {

/*
* This if block treats the /v2/accounts/exists response. Currently the endpoint returns the status
* code on its response body, breaking the JSON conversion.
*/
if ("200".equals(json)) {
this.responseBody.put("code", 200);
return this.responseBody;
}

if ("400".equals(json)) {
this.responseBody.put("code", 400);
return this.responseBody;
}

if ("404".equals(json)) {
this.responseBody.put("code", 404);
return this.responseBody;
}

if (!json.equals("")) {
ObjectMapper mapper = new ObjectMapper();
try {
Expand All @@ -49,11 +69,3 @@ public List<Map<String, Object>> jsonToList(String json) {
return this.responseBodyList;
}
}

/* Este método resolve grande parte do problema, porém não resolve o problema por completo.
* Precisamos fazer com que cada nó do JSON se converta em um objeto do seu respectivo model.
* Ex: o nó "taxDocument" precisa ser convertido em um objeto da classe TaxDocument.
*
* Talvez seja possível trabalhar com o [type], o atributo da classe RequestProperties, que tem
* como objetivo indicar a qual classe/model aquele objeto pertence.
*/
183 changes: 183 additions & 0 deletions src/main/java/br/com/moip/models/Accounts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package br.com.moip.models;

import br.com.moip.api.request.RequestMaker;
import br.com.moip.api.request.RequestProperties;
import br.com.moip.api.request.RequestPropertiesBuilder;
import org.apache.http.entity.ContentType;

import java.util.List;
import java.util.Map;

public class Accounts {

private static final String ENDPOINT = "/v2/accounts";
private static final ContentType CONTENT_TYPE = ContentType.APPLICATION_JSON;
private RequestMaker requestMaker;

/**
* This method is used to validate the argument of bellow method, if probably it's a tax document or not.
*
* @param argument
* {@code String} the received argument.
*
* @return {@code boolean}
*/
private boolean isTaxDocument(String argument) {
try {
Integer.parseInt(argument.substring(0,1));
} catch (Exception ex) {
return false;
}
return true;
}

/**
* This method allows you to check if a person already has a Moip account, by it's tax document or e-mail.
* The tax document must be write with punctuation, for example: 123.456.789-00.
*
* @param argument
* {@code String} the person's tax document or e-mail.
*
* @param setup
* {@code Setup} the setup object.
*
* @return {@code Map<String, Object>}
*/
public Map<String, Object> checkExistence(String argument, Setup setup) {
this.requestMaker = new RequestMaker(setup);
String argumentType;

if (isTaxDocument(argument)) argumentType = "tax_document";

else argumentType = "email";

RequestProperties props = new RequestPropertiesBuilder()
.method("GET")
.endpoint(String.format("%s/exists?%s=%s", ENDPOINT, argumentType, argument))
.type(Accounts.class)
.contentType(CONTENT_TYPE)
.build();

return this.requestMaker.doRequest(props);
}

/**
* This method allows you to create a Moip account (classical or transparent). To differentiate the
* two accounts types you have to set the boolean attribute {@code transparentAccount}, <b>true</b> value
* (you will create a transparent accounts) or <b>false</b> value (you will create a classical accounts).
*
* @param body
* {@code Map<String, Object>} the request body.
*
* @param setup
* {@code Setup} the setup object.
*
* @return {@code Map<String, Object>}
*/
public Map<String, Object> create(Map<String, Object> body, Setup setup) {
this.requestMaker = new RequestMaker(setup);
RequestProperties props = new RequestPropertiesBuilder()
.method("POST")
.endpoint(ENDPOINT)
.body(body)
.type(Accounts.class)
.contentType(CONTENT_TYPE)
.build();

return this.requestMaker.doRequest(props);
}

/**
* This method is used to get a created accounts by Moip account external ID.
*
* @param id
* {@code String} the Moip account external ID.
*
* @param setup
* {@code Setup} the setup object.
*
* @return {@code Map<String, Object>}
*/
public Map<String, Object> get(String id, Setup setup) {
this.requestMaker = new RequestMaker(setup);
RequestProperties props = new RequestPropertiesBuilder()
.method("GET")
.endpoint(String.format("%s/%s", ENDPOINT, id))
.type(Accounts.class)
.contentType(CONTENT_TYPE)
.build();

return this.requestMaker.doRequest(props);
}

/**
* This method is used to get the Basic Auth keys and the public key of a Moip account.
*
* @param setup
* {@code Setup} the setup object.
*
* @return {@code Map<String, Object>}
*/
public Map<String, Object> getKeys(Setup setup) {
this.requestMaker = new RequestMaker(setup);
RequestProperties props = new RequestPropertiesBuilder()
.method("GET")
.endpoint("/v2/keys")
.type(Accounts.class)
.contentType(CONTENT_TYPE)
.build();

return this.requestMaker.doRequest(props);
}

/**
* This method allows you to create a bank account to a Moip account. You can use a bank account to make
* transfers and refunds on Moip.
*
* @param body
* {@code Map<String, Object>} the request body.
*
* @param moipAccountId
* {@code String} the Moip account external ID.
*
* @param setup
* {@code Setup} the setup object.
*
* @return {@code Map<String, Object>}
*/
public Map<String, Object> createBankAccount(Map<String, Object> body, String moipAccountId, Setup setup) {
this.requestMaker = new RequestMaker(setup);
RequestProperties props = new RequestPropertiesBuilder()
.method("POST")
.endpoint(String.format("%s/%s/bankaccounts", ENDPOINT, moipAccountId))
.body(body)
.type(Accounts.class)
.contentType(CONTENT_TYPE)
.build();

return this.requestMaker.doRequest(props);
}

/**
* This method allows you to list all bank account of a Moip account.
*
* @param moipAccountId
* {@code String} the Moip account external ID.
*
* @param setup
* {@code Setup} the setup object.
*
* @return {@code Map<String, Object>}
*/
public List<Map<String, Object>> listBankAccounts(String moipAccountId, Setup setup) {
this.requestMaker = new RequestMaker(setup);
RequestProperties props = new RequestPropertiesBuilder()
.method("GET")
.endpoint(String.format("%s/%s/bankaccounts", ENDPOINT, moipAccountId))
.type(BankAccounts.class)
.contentType(CONTENT_TYPE)
.build();

return this.requestMaker.getList(props);
}
}
37 changes: 37 additions & 0 deletions src/main/java/br/com/moip/models/Balances.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package br.com.moip.models;

import br.com.moip.api.request.RequestMaker;
import br.com.moip.api.request.RequestProperties;
import br.com.moip.api.request.RequestPropertiesBuilder;
import org.apache.http.entity.ContentType;

import java.util.Map;

public class Balances {

private static final String ENDPOINT = "/v2/balances";
private static final ContentType CONTENT_TYPE = ContentType.APPLICATION_JSON;
private RequestMaker requestMaker;

/**
* This method is used to get the balances values of a Moip account (unavailable, future, current). The
* request uses the accept version {@code 2.1}.
*
* @param setup
* {@code Setup} the setup object.
*
* @return {@code Map<String, Object>}
*/
public Map<String, Object> get(Setup setup) {
this.requestMaker = new RequestMaker(setup);
RequestProperties props = new RequestPropertiesBuilder()
.method("GET")
.endpoint(ENDPOINT)
.type(Balances.class)
.contentType(CONTENT_TYPE)
.accept("2.1")
.build();

return this.requestMaker.doRequest(props);
}
}
Loading

0 comments on commit e81bd7c

Please sign in to comment.