diff --git a/.classpath b/.classpath
index b4c1ab3..e25c007 100644
--- a/.classpath
+++ b/.classpath
@@ -10,6 +10,7 @@
+
@@ -17,7 +18,7 @@
-
+
@@ -27,5 +28,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.project b/.project
index 7e5cee3..b5dd879 100644
--- a/.project
+++ b/.project
@@ -20,4 +20,15 @@
org.eclipse.m2e.core.maven2Nature
org.eclipse.jdt.core.javanature
+
+
+ 1665500716441
+
+ 30
+
+ org.eclipse.core.resources.regexFilterMatcher
+ node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__
+
+
+
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3938dbc..fd3534e 100755
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,10 @@
+# 2.0.0
+- Added: API Open Finance
+- Added: API Pagamentos
+- Added: new endpoints of API Pix
+- Updated: PixSend example
+- Updated: rename "pix_cert" to "certificate" in credentials.json
+
# 1.0.2
- Fix: Error message
diff --git a/README.md b/README.md
index b7167b6..c4c9cd7 100755
--- a/README.md
+++ b/README.md
@@ -22,7 +22,7 @@ java 7.0 and 8.0
**Via gradle:**
```gradle
-compile 'br.com.gerencianet.gnsdk:gn-api-sdk-java:1.0.2'
+compile 'br.com.gerencianet.gnsdk:gn-api-sdk-java:2.0.0'
```
**Via maven:**
@@ -31,7 +31,7 @@ compile 'br.com.gerencianet.gnsdk:gn-api-sdk-java:1.0.2'
br.com.gerencianet.gnsdk
gn-api-sdk-java
- 1.0.2
+ 2.0.0
```
@@ -60,7 +60,7 @@ Instantiate the module passing using your client_id, client_secret and sandbox e
JSONObject options = new JSONObject();
options.put("client_id", "client_id");
options.put("client_secret", "client_secret");
-options.put("pix_cert", "./certs/developmentCertificate.p12");
+options.put("certificate", "./certs/developmentCertificate.p12");
options.put("sandbox", true);
Gerencianet gn = new Gerencianet($options);
@@ -71,7 +71,7 @@ Or
Map options = new HashMap();
options.put("client_id", "client_id");
options.put("client_secret", "client_secret");
-options.put("pix_cert", "./certs/developmentCertificate.p12");
+options.put("certificate", "./certs/developmentCertificate.p12");
options.put("sandbox", true);
Gerencianet gn = new Gerencianet($options);
@@ -83,7 +83,7 @@ To change the environment to production, just set the third sandbox to false:
JSONObject options = new JSONObject();
options.put("client_id", "client_id");
options.put("client_secret", "client_secret");
-options.put("pix_cert", "./certs/productionCertificate.p12");
+options.put("certificate", "./certs/productionCertificate.p12");
options.put("sandbox", false);
Gerencianet gn = new Gerencianet($options);
@@ -94,7 +94,7 @@ Or
Map options = new HashMap();
options.put("client_id", "client_id");
options.put("client_secret", "client_secret");
-options.put("pix_cert", "./certs/productionCertificate.p12");
+options.put("certificate", "./certs/productionCertificate.p12");
options.put("sandbox", false);
Gerencianet gn = new Gerencianet($options);
diff --git a/pom.xml b/pom.xml
index 05488ef..72e9211 100755
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
4.0.0
br.com.gerencianet.gnsdk
gn-api-sdk-java
- 1.0.2
+ 2.0.0
GN API SDK JAVA
Java SDK for integrating with Gerencianet API
@@ -148,7 +148,7 @@
org.apache.maven.plugins
maven-surefire-plugin
- 2.19.1
+ 2.22.0
true
@@ -157,7 +157,7 @@
org.jacoco
jacoco-maven-plugin
- 0.7.7.201606060606
+ 0.7.8
**/exceptions/*
@@ -210,7 +210,7 @@
org.apache.maven.plugins
maven-gpg-plugin
- 1.5
+ 3.0.1
sign-artifacts
diff --git a/src/main/java/br/com/gerencianet/gnsdk/APIRequest.java b/src/main/java/br/com/gerencianet/gnsdk/APIRequest.java
index ea35369..2b45fb3 100644
--- a/src/main/java/br/com/gerencianet/gnsdk/APIRequest.java
+++ b/src/main/java/br/com/gerencianet/gnsdk/APIRequest.java
@@ -12,8 +12,9 @@
/**
* This class instance a Auth Object, to authenticate client credentials in
- * Gerencianet API. After client's credentials are validated a client Object
- * send a given request body to a given endpoint throw a given route.
+ * Gerencianet API. After client's credentials
+ * are validated a client Object send a given request body to a given endpoint
+ * throw a given route.
*
* @author Filipe Mata
*
@@ -24,10 +25,10 @@ public class APIRequest {
private String route;
private JSONObject body;
- public APIRequest(String method, String route, JSONObject body, Config config) throws Exception {
+ public APIRequest(String method, String route, JSONObject body, JSONObject auth, Config config) throws Exception {
this.route = route;
- String authenticateRoute = config.getEndpoints().getJSONObject("authorize").getString("route");
- String authenticateMethod = config.getEndpoints().getJSONObject("authorize").getString("method");
+ String authenticateRoute = auth.getString("route");
+ String authenticateMethod = auth.getString("method");
this.authenticator = new Auth(config.getOptions(), authenticateMethod, authenticateRoute);
String url = config.getOptions().getString("baseUri") + route;
@@ -42,6 +43,7 @@ public APIRequest(String method, String route, JSONObject body, Config config) t
if (config.getOptions().has("headers")) {
this.requester.addHeader("x-skip-mtls-checking", config.getOptions().getString("headers"));
+ this.requester.addHeader("x-idempotency-key", config.getOptions().getString("headers"));
}
this.body = body;
@@ -56,10 +58,8 @@ public APIRequest(Auth auth, Request request, JSONObject body) {
public JSONObject send() throws AuthorizationException, GerencianetException, IOException {
Date expiredDate = this.authenticator.getExpires();
if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) {
-
this.authenticator.authorize();
}
-
this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken());
try {
return this.requester.send(this.body);
@@ -69,6 +69,20 @@ public JSONObject send() throws AuthorizationException, GerencianetException, IO
}
}
+ public String sendString() throws AuthorizationException, GerencianetException, IOException {
+ Date expiredDate = this.authenticator.getExpires();
+ if (this.authenticator.getExpires() == null || expiredDate.compareTo(new Date()) <= 0) {
+ this.authenticator.authorize();
+ }
+ this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken());
+ try {
+ return this.requester.sendString(this.body);
+ } catch (AuthorizationException e) {
+ this.authenticator.authorize();
+ return this.requester.sendString(body);
+ }
+ }
+
public Request getRequester() {
return requester;
}
diff --git a/src/main/java/br/com/gerencianet/gnsdk/Auth.java b/src/main/java/br/com/gerencianet/gnsdk/Auth.java
index 85a85ce..d7a8bff 100644
--- a/src/main/java/br/com/gerencianet/gnsdk/Auth.java
+++ b/src/main/java/br/com/gerencianet/gnsdk/Auth.java
@@ -31,8 +31,8 @@ public Auth(JSONObject credentials, String method, String authorizeRoute) throws
throw new Exception("Client_Id or Client_Secret not found");
}
- if (credentials.has("certificadoPix")) {
- System.setProperty("javax.net.ssl.keyStore", credentials.getString("certificadoPix"));
+ if (credentials.has("certificate")) {
+ System.setProperty("javax.net.ssl.keyStore", credentials.getString("certificate"));
}
String url = credentials.getString("baseUri") + authorizeRoute;
diff --git a/src/main/java/br/com/gerencianet/gnsdk/Config.java b/src/main/java/br/com/gerencianet/gnsdk/Config.java
index 5907ae1..0bbef3c 100755
--- a/src/main/java/br/com/gerencianet/gnsdk/Config.java
+++ b/src/main/java/br/com/gerencianet/gnsdk/Config.java
@@ -11,44 +11,19 @@
*
*/
public class Config {
- private final static String version = "1.0.2";
+ private final static String version = "2.0.0";
private JSONObject conf = new JSONObject();
private JSONObject endpoints = new JSONObject();
private JSONObject urls = new JSONObject();
+ private JSONObject options = new JSONObject();
public Config(JSONObject options, JSONObject config) throws Exception {
-
- if (config.has("ENDPOINTS")) {
- this.endpoints = (JSONObject) config.get("ENDPOINTS");
- if (setPix(options)) {
- if (this.endpoints.has("PIX")) {
- this.endpoints = (JSONObject) this.endpoints.get("PIX");
- }
- } else {
- if (this.endpoints.has("DEFAULT")) {
- this.endpoints = (JSONObject) this.endpoints.get("DEFAULT");
- }
- }
- } else
- throw new Exception("Problems to get ENDPOINTS in file config.json");
-
- if (config.has("URL")) {
- this.urls = (JSONObject) config.get("URL");
- if (setPix(options)) {
- if (this.urls.has("PIX")) {
- this.urls = (JSONObject) this.urls.get("PIX");
- }
- } else if (this.urls.has("DEFAULT")) {
- this.urls = (JSONObject) this.urls.get("DEFAULT");
- }
- } else
- throw new Exception("Problems to get URLs in file config.json");
-
- this.setConf(options);
+ this.endpoints = config;
+ this.options = options;
}
public JSONObject getEndpoints() {
- return endpoints;
+ return (JSONObject)endpoints.get("APIs");
}
public JSONObject getUrls() {
@@ -71,15 +46,13 @@ public void setConf(JSONObject options) {
this.conf.put("clientId", options.getString("client_id"));
if (options.has("client_secret"))
this.conf.put("clientSecret", options.getString("client_secret"));
- if (options.has("pix_cert"))
- this.conf.put("certificadoPix", options.getString("pix_cert"));
+ if (options.has("certificate"))
+ this.conf.put("certificate", options.getString("certificate"));
if (options.has("partner_token"))
this.conf.put("partnerToken", options.getString("partner_token"));
if (options.has("url")) {
this.conf.put("baseUri", options.getString("url"));
- }
-
- else {
+ } else {
String baseUri = this.urls.getString("production");
if (this.conf.getBoolean("sandbox") == true)
baseUri = this.urls.getString("sandbox");
@@ -90,6 +63,10 @@ public void setConf(JSONObject options) {
if (options.has("x-skip-mtls-checking")) {
this.conf.put("headers", options.getString("x-skip-mtls-checking"));
}
+
+ if (options.has("x-idempotency-key")) {
+ this.conf.put("headers", options.getString("x-idempotency-key"));
+ }
}
public JSONObject getOptions() {
@@ -100,8 +77,9 @@ public static String getVersion() {
return Config.version;
}
- public boolean setPix(JSONObject options) {
- return options.has("pix_cert");
+ public void setURLs(JSONObject parURLs) {
+ this.urls = parURLs;
+ this.setConf(this.options);
}
}
\ No newline at end of file
diff --git a/src/main/java/br/com/gerencianet/gnsdk/Endpoints.java b/src/main/java/br/com/gerencianet/gnsdk/Endpoints.java
index 3d149a8..dbd6dbb 100644
--- a/src/main/java/br/com/gerencianet/gnsdk/Endpoints.java
+++ b/src/main/java/br/com/gerencianet/gnsdk/Endpoints.java
@@ -1,8 +1,10 @@
package br.com.gerencianet.gnsdk;
+import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
+import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@@ -13,102 +15,172 @@
import org.json.JSONTokener;
/**
- * This is the mains class of Gerencianet SDK JAVA. It's responsible to instance an APIRequester,
+ * This is the mains class of Gerencianet SDK JAVA. It's responsible to instance
+ * an APIRequester,
* send the right data to a given endpoint, and return a response to SDK client.
+ *
* @author Filipe Mata
- *
*/
public class Endpoints {
private APIRequest requester;
private Config config;
-
- public Endpoints(JSONObject options) throws Exception
- {
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- InputStream configFile = classLoader.getResourceAsStream("config.json");
- JSONTokener tokener = new JSONTokener(configFile);
- JSONObject config = new JSONObject(tokener);
- configFile.close();
+
+ /**
+ * Constructor JSONObject
+ *
+ * @param options Objeto JSON com as definições da API
+ * @throws Exception
+ * @author Jessica Gava
+ */
+ public Endpoints(JSONObject options) throws Exception {
+ JSONObject config = readJSONFile();
this.config = new Config(options, config);
}
-
- public Endpoints(Config config, APIRequest request) throws Exception
- {
+
+ /**
+ * Constructor Map
+ *
+ * @param options Map com as definiçõess da API
+ * @throws Exception
+ * @author Jessica Gava
+ */
+ public Endpoints(Map options) throws Exception {
+ JSONObject credentials = (JSONObject) JSONObject.wrap(options);
+ JSONObject config = readJSONFile();
+ this.config = new Config(credentials, config);
+ }
+
+ public Endpoints(Config config, APIRequest request) throws Exception {
this.config = config;
this.requester = request;
}
-
- public Endpoints(Config config){
+
+ public Endpoints(Config config) {
this.config = config;
}
-
- public Endpoints(Map options) throws Exception{
- JSONObject credentials = (JSONObject) JSONObject.wrap(options);
+
+ /**
+ * Read the JSON settings file
+ *
+ * @return JSONObject
+ * @throws IOException
+ * @author Jessica Gava
+ */
+ protected JSONObject readJSONFile() throws IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream configFile = classLoader.getResourceAsStream("config.json");
JSONTokener tokener = new JSONTokener(configFile);
JSONObject config = new JSONObject(tokener);
configFile.close();
- this.config = new Config(credentials, config);
+ return config;
}
-
+
public APIRequest getRequester() {
return requester;
}
-
- public JSONObject call(String endpoint, Map params, JSONObject body) throws Exception{
+
+ public JSONObject call(String endpoint, Map params, JSONObject body) throws Exception {
return kernelCall(endpoint, params, body);
}
-
- public Map call(String endpoint, Map params, Map mapBody) throws Exception{
+
+ public String callString(String endpoint, Map params, JSONObject body) throws Exception {
+ return kernelCallString(endpoint, params, body);
+ }
+
+ public Map call(String endpoint, Map params, Map mapBody)
+ throws Exception {
JSONObject body = (JSONObject) JSONObject.wrap(mapBody);
JSONObject response = kernelCall(endpoint, params, body);
return response.toMap();
}
-
- private JSONObject kernelCall(String endpointName, Map params, JSONObject body) throws Exception{
+
+ protected HashMap getRoute(JSONObject parEndpointsObj, String parEndpointName) {
+ HashMap hash = new HashMap();
+ for (String ApiName : parEndpointsObj.keySet()) {
+ JSONObject urls = (JSONObject) ((JSONObject) parEndpointsObj.get(ApiName)).get("URL");
+ JSONObject endpoints = (JSONObject) ((JSONObject) parEndpointsObj.get(ApiName)).get("ENDPOINTS");
+ if (endpoints.has(parEndpointName)) {
+ hash.put("URL", urls);
+ hash.put("ENDPOINTS", endpoints);
+ }
+ }
+ return hash;
+ }
+
+ private JSONObject kernelCall(String endpointName, Map params, JSONObject body) throws Exception {
JSONObject endpoints = this.config.getEndpoints();
- if(!endpoints.has(endpointName))
+ HashMap api = getRoute(endpoints, endpointName);
+ if (!api.containsKey("ENDPOINTS"))
throw new Exception("nonexistent endpoint");
-
- JSONObject endpoint = (JSONObject)endpoints.get(endpointName);
- String route = getRoute(endpoint, params);
- route += getQueryString(params);
- if(this.requester == null)
- requester = new APIRequest(endpoint.get("method").toString(), route, body, this.config);
+
+ JSONObject api_url = (JSONObject) api.get("URL");
+ this.config.setURLs(api_url);
+
+ JSONObject api_endpoints = (JSONObject) api.get("ENDPOINTS");
+ JSONObject api_auth = (JSONObject) api_endpoints.get("authorize");
+ JSONObject endpoint = (JSONObject) api_endpoints.get(endpointName);
+ String routeName = getRoute(endpoint, params);
+ routeName += getQueryString(params);
+
+ if (this.requester == null) {
+ requester = new APIRequest(endpoint.get("method").toString(), routeName, body, api_auth, this.config);
+ }
JSONObject response = this.requester.send();
this.requester = null;
-
+
return response;
}
-
+
+ private String kernelCallString(String endpointName, Map params, JSONObject body) throws Exception {
+ JSONObject endpoints = this.config.getEndpoints();
+ HashMap api = getRoute(endpoints, endpointName);
+ if (!api.containsKey("ENDPOINTS"))
+ throw new Exception("nonexistent endpoint");
+
+ JSONObject api_url = (JSONObject) api.get("URL");
+ this.config.setURLs(api_url);
+
+ JSONObject api_endpoints = (JSONObject) api.get("ENDPOINTS");
+ JSONObject api_auth = (JSONObject) api_endpoints.get("authorize");
+ JSONObject endpoint = (JSONObject) api_endpoints.get(endpointName);
+ String routeName = getRoute(endpoint, params);
+ routeName += getQueryString(params);
+ if (this.requester == null)
+ requester = new APIRequest(endpoint.get("method").toString(), routeName, body, api_auth, this.config);
+ String response = this.requester.sendString();
+ this.requester = null;
+
+ return response;
+ }
+
private String getQueryString(Map params) throws UnsupportedEncodingException {
Set> set = params.entrySet();
- String query="";
- for(Entry entry : set){
- if(!query.isEmpty())query +="&";
- else query +="?";
- query += entry.getKey() + "=" + URLEncoder.encode(entry.getValue(),"UTF-8");
- }
- return query;
+ String query = "";
+ for (Entry entry : set) {
+ if (!query.isEmpty())
+ query += "&";
+ else
+ query += "?";
+ query += entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8");
+ }
+ return query;
}
-
- private String getRoute(JSONObject endpoint, Map params)
- {
+
+ private String getRoute(JSONObject endpoint, Map params) {
Pattern pattern = Pattern.compile("/:(\\w+)/");
- String route = endpoint.get("route").toString();
- route += "/";
- Matcher matcher = pattern.matcher(route);
- while(matcher.find()){
- String value = route.substring(matcher.start()+2, matcher.end()-1);
- if(params.containsKey(value)){
- route = route.replace(":"+value, params.get(value));
- params.remove(value);
- matcher = pattern.matcher(route);
- }
- }
- route = route.substring(0, route.length()-1);
- return route;
+ String route = endpoint.get("route").toString();
+ route += "/";
+ Matcher matcher = pattern.matcher(route);
+ while (matcher.find()) {
+ String value = route.substring(matcher.start() + 2, matcher.end() - 1);
+ if (params.containsKey(value)) {
+ route = route.replace(":" + value, params.get(value));
+ params.remove(value);
+ matcher = pattern.matcher(route);
+ }
+ }
+ route = route.substring(0, route.length() - 1);
+ return route;
}
}
diff --git a/src/main/java/br/com/gerencianet/gnsdk/Gerencianet.java b/src/main/java/br/com/gerencianet/gnsdk/Gerencianet.java
index 8396682..c89d951 100644
--- a/src/main/java/br/com/gerencianet/gnsdk/Gerencianet.java
+++ b/src/main/java/br/com/gerencianet/gnsdk/Gerencianet.java
@@ -12,9 +12,8 @@
public class Gerencianet extends Endpoints{
public Gerencianet(JSONObject options) throws Exception {
super(options);
- }
-
+ }
public Gerencianet(Map options) throws Exception {
super(options);
- }
+ }
}
diff --git a/src/main/java/br/com/gerencianet/gnsdk/Request.java b/src/main/java/br/com/gerencianet/gnsdk/Request.java
index 97d4d5e..d2f1b96 100644
--- a/src/main/java/br/com/gerencianet/gnsdk/Request.java
+++ b/src/main/java/br/com/gerencianet/gnsdk/Request.java
@@ -4,6 +4,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
+import java.nio.charset.StandardCharsets;
+
import org.json.JSONObject;
import org.json.JSONTokener;
@@ -52,15 +54,40 @@ public JSONObject send(JSONObject requestOptions) throws AuthorizationException,
os.flush();
os.close();
}
-
+
int responseCode = client.getResponseCode();
-
- if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
+ if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED){
InputStream responseStream = client.getInputStream();
JSONTokener responseTokener = new JSONTokener(responseStream);
return new JSONObject(responseTokener);
- }
- else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED || responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
+ } else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED || responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
+ throw new AuthorizationException();
+ } else {
+ InputStream responseStream = client.getErrorStream();
+ JSONTokener responseTokener = new JSONTokener(responseStream);
+ JSONObject response = new JSONObject(responseTokener);
+ throw new GerencianetException(response);
+ }
+ }
+
+ public String sendString(JSONObject requestOptions) throws AuthorizationException, GerencianetException, IOException {
+ byte[] postDataBytes;
+ postDataBytes = requestOptions.toString().getBytes("UTF-8");
+ this.client.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
+ if (!client.getRequestMethod().toLowerCase().equals("get")) {
+ client.setDoOutput(true);
+ OutputStream os = client.getOutputStream();
+ os.write(postDataBytes);
+ os.flush();
+ os.close();
+ }
+
+ int responseCode = client.getResponseCode();
+ if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_ACCEPTED){
+ InputStream responseStream = client.getInputStream();
+ String response = new String(responseStream.readAllBytes(), StandardCharsets.UTF_8);
+ return new String(response);
+ } else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED || responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
throw new AuthorizationException();
} else {
InputStream responseStream = client.getErrorStream();
diff --git a/src/main/java/br/com/gerencianet/gnsdk/exceptions/GerencianetException.java b/src/main/java/br/com/gerencianet/gnsdk/exceptions/GerencianetException.java
index 2f0db29..016a4a7 100644
--- a/src/main/java/br/com/gerencianet/gnsdk/exceptions/GerencianetException.java
+++ b/src/main/java/br/com/gerencianet/gnsdk/exceptions/GerencianetException.java
@@ -1,5 +1,6 @@
package br.com.gerencianet.gnsdk.exceptions;
+import org.json.JSONArray;
import org.json.JSONObject;
/**
@@ -17,8 +18,7 @@ public class GerencianetException extends Exception {
public GerencianetException(JSONObject response) {
String message = "";
- if (response.has("error_description"))
- {
+ if (response.has("error_description")) {
if (response.get("error_description").getClass().getSimpleName().equals("JSONObject")) {
JSONObject errorDescription = response.getJSONObject("error_description");
if (errorDescription.has("message"))
@@ -32,13 +32,26 @@ public GerencianetException(JSONObject response) {
message = response.get("error_description").toString();
if (response.has("code"))
- this.code = Integer.parseInt(response.get("code").toString());
+ this.code = Integer.parseInt(response.get("code").toString());
this.error = response.get("error").toString();
this.errorDescription = message;
- } else {
+ } else if (response.has("nome")) {
this.error = response.get("nome").toString();
this.errorDescription = response.get("mensagem").toString();
+ } else if (response.has("violacoes")) {
+ if (response.get("violacoes").getClass().getSimpleName().equals("JSONArray")) {
+ JSONArray violacoes = response.getJSONArray("violacoes");
+ for (int i = 0; i < violacoes.length(); i++) {
+ JSONObject json = violacoes.getJSONObject(i);
+ if (json.has("razao")) {
+ this.error = json.get("razao").toString();
+ this.errorDescription = json.get("propriedade").toString();
+ }
+ }
+ }
+ } else {
+ System.out.println(response);
}
}
diff --git a/src/main/resources/config.json b/src/main/resources/config.json
index 2f45f44..26b8534 100755
--- a/src/main/resources/config.json
+++ b/src/main/resources/config.json
@@ -1,271 +1,363 @@
{
- "URL": {
- "DEFAULT" : {
- "production": "https://api.gerencianet.com.br",
- "sandbox": "https://sandbox.gerencianet.com.br"
- },
- "PIX": {
- "production": "https://api-pix.gerencianet.com.br",
- "sandbox": "https://api-pix-h.gerencianet.com.br"
- }
- },
- "ENDPOINTS": {
- "DEFAULT" : {
- "authorize": {
- "route": "/v1/authorize",
- "method": "post"
- },
- "createCharge": {
- "route": "/v1/charge",
- "method": "post"
- },
- "oneStep": {
- "route": "/v1/charge/one-step",
- "method": "post"
- },
- "detailCharge": {
- "route": "/v1/charge/:id",
- "method": "get"
- },
- "updateChargeMetadata": {
- "route": "/v1/charge/:id/metadata",
- "method": "put"
- },
- "updateBillet": {
- "route": "/v1/charge/:id/billet",
- "method": "put"
- },
- "payCharge": {
- "route": "/v1/charge/:id/pay",
- "method": "post"
- },
- "cancelCharge": {
- "route": "/v1/charge/:id/cancel",
- "method": "put"
- },
- "createCarnet": {
- "route": "/v1/carnet",
- "method": "post"
- },
- "detailCarnet": {
- "route": "/v1/carnet/:id",
- "method": "get"
- },
- "updateParcel": {
- "route": "/v1/carnet/:id/parcel/:parcel",
- "method": "put"
- },
- "updateCarnetMetadata": {
- "route": "/v1/carnet/:id/metadata",
- "method": "put"
- },
- "getNotification": {
- "route": "/v1/notification/:token",
- "method": "get"
- },
- "getPlans": {
- "route": "/v1/plans",
- "method": "get"
- },
- "createPlan": {
- "route": "/v1/plan",
- "method": "post"
- },
- "deletePlan": {
- "route": "/v1/plan/:id",
- "method": "delete"
- },
- "createSubscription": {
- "route": "/v1/plan/:id/subscription",
- "method": "post"
- },
- "detailSubscription": {
- "route": "/v1/subscription/:id",
- "method": "get"
- },
- "paySubscription": {
- "route": "/v1/subscription/:id/pay",
- "method": "post"
- },
- "cancelSubscription": {
- "route": "/v1/subscription/:id/cancel",
- "method": "put"
- },
- "updateSubscriptionMetadata": {
- "route": "/v1/subscription/:id/metadata",
- "method": "put"
- },
- "getInstallments": {
- "route": "/v1/installments",
- "method": "get"
- },
- "resendBillet": {
- "route": "/v1/charge/:id/billet/resend",
- "method": "post"
- },
- "createChargeHistory": {
- "route": "/v1/charge/:id/history",
- "method": "post"
- },
- "resendCarnet": {
- "route": "/v1/carnet/:id/resend",
- "method": "post"
- },
- "resendParcel": {
- "route": "/v1/carnet/:id/parcel/:parcel/resend",
- "method": "post"
- },
- "createCarnetHistory": {
- "route": "/v1/carnet/:id/history",
- "method": "post"
- },
- "cancelCarnet": {
- "route": "/v1/carnet/:id/cancel",
- "method": "put"
- },
- "cancelParcel": {
- "route": "/v1/carnet/:id/parcel/:parcel/cancel",
- "method": "put"
- },
- "linkCharge": {
- "route": "/v1/charge/:id/link",
- "method": "post"
- },
- "chargeLink": {
- "route": "/v1/charge/:id/link",
- "method": "post"
- },
- "updateChargeLink": {
- "route": "/v1/charge/:id/link",
- "method": "put"
- },
- "updatePlan": {
- "route": "/v1/plan/:id",
- "method": "put"
- },
- "createSubscriptionHistory": {
- "route": "/v1/subscription/:id/history",
- "method": "post"
- },
- "createChargeBalanceSheet": {
- "route": "/v1/charge/:id/balance-sheet",
- "method": "post"
- },
- "settleCharge": {
- "route": "/v1/charge/:id/settle",
- "method": "put"
- },
- "settleCarnetParcel": {
- "route": "/v1/carnet/:id/parcel/:parcel/settle",
- "method": "put"
+ "APIs":{
+ "DEFAULT": {
+ "URL": {
+ "production": "https://api.gerencianet.com.br",
+ "sandbox": "https://sandbox.gerencianet.com.br"
+ },
+ "ENDPOINTS": {
+ "authorize": {
+ "route": "/v1/authorize",
+ "method": "post"
+ },
+ "createCharge": {
+ "route": "/v1/charge",
+ "method": "post"
+ },
+ "oneStep": {
+ "route": "/v1/charge/one-step",
+ "method": "post"
+ },
+ "detailCharge": {
+ "route": "/v1/charge/:id",
+ "method": "get"
+ },
+ "updateChargeMetadata": {
+ "route": "/v1/charge/:id/metadata",
+ "method": "put"
+ },
+ "updateBillet": {
+ "route": "/v1/charge/:id/billet",
+ "method": "put"
+ },
+ "payCharge": {
+ "route": "/v1/charge/:id/pay",
+ "method": "post"
+ },
+ "cancelCharge": {
+ "route": "/v1/charge/:id/cancel",
+ "method": "put"
+ },
+ "createCarnet": {
+ "route": "/v1/carnet",
+ "method": "post"
+ },
+ "detailCarnet": {
+ "route": "/v1/carnet/:id",
+ "method": "get"
+ },
+ "updateParcel": {
+ "route": "/v1/carnet/:id/parcel/:parcel",
+ "method": "put"
+ },
+ "updateCarnetMetadata": {
+ "route": "/v1/carnet/:id/metadata",
+ "method": "put"
+ },
+ "getNotification": {
+ "route": "/v1/notification/:token",
+ "method": "get"
+ },
+ "getPlans": {
+ "route": "/v1/plans",
+ "method": "get"
+ },
+ "createPlan": {
+ "route": "/v1/plan",
+ "method": "post"
+ },
+ "deletePlan": {
+ "route": "/v1/plan/:id",
+ "method": "delete"
+ },
+ "createSubscription": {
+ "route": "/v1/plan/:id/subscription",
+ "method": "post"
+ },
+ "detailSubscription": {
+ "route": "/v1/subscription/:id",
+ "method": "get"
+ },
+ "paySubscription": {
+ "route": "/v1/subscription/:id/pay",
+ "method": "post"
+ },
+ "cancelSubscription": {
+ "route": "/v1/subscription/:id/cancel",
+ "method": "put"
+ },
+ "updateSubscriptionMetadata": {
+ "route": "/v1/subscription/:id/metadata",
+ "method": "put"
+ },
+ "getInstallments": {
+ "route": "/v1/installments",
+ "method": "get"
+ },
+ "resendBillet": {
+ "route": "/v1/charge/:id/billet/resend",
+ "method": "post"
+ },
+ "createChargeHistory": {
+ "route": "/v1/charge/:id/history",
+ "method": "post"
+ },
+ "resendCarnet": {
+ "route": "/v1/carnet/:id/resend",
+ "method": "post"
+ },
+ "resendParcel": {
+ "route": "/v1/carnet/:id/parcel/:parcel/resend",
+ "method": "post"
+ },
+ "createCarnetHistory": {
+ "route": "/v1/carnet/:id/history",
+ "method": "post"
+ },
+ "cancelCarnet": {
+ "route": "/v1/carnet/:id/cancel",
+ "method": "put"
+ },
+ "cancelParcel": {
+ "route": "/v1/carnet/:id/parcel/:parcel/cancel",
+ "method": "put"
+ },
+ "linkCharge": {
+ "route": "/v1/charge/:id/link",
+ "method": "post"
+ },
+ "updateChargeLink": {
+ "route": "/v1/charge/:id/link",
+ "method": "put"
+ },
+ "updatePlan": {
+ "route": "/v1/plan/:id",
+ "method": "put"
+ },
+ "createSubscriptionHistory": {
+ "route": "/v1/subscription/:id/history",
+ "method": "post"
+ },
+ "createChargeBalanceSheet": {
+ "route": "/v1/charge/:id/balance-sheet",
+ "method": "post"
+ },
+ "settleCharge": {
+ "route": "/v1/charge/:id/settle",
+ "method": "put"
+ },
+ "settleCarnetParcel": {
+ "route": "/v1/carnet/:id/parcel/:parcel/settle",
+ "method": "put"
+ },
+ "oneStepSubscription": {
+ "route": "/v1/plan/:id/subscription/one-step",
+ "method": "post"
+ },
+ "oneStepSubscriptionLink": {
+ "route": "/v1/plan/:id/subscription/one-step/link",
+ "method": "post"
+ },
+ "oneStepLink": {
+ "route": "/v1/charge/one-step/link",
+ "method": "post"
+ }
}
},
"PIX": {
- "authorize": {
- "route": "/oauth/token",
- "method": "post"
- },
-
- "pixConfigWebhook": {
+ "URL": {
+ "production": "https://api-pix.gerencianet.com.br",
+ "sandbox": "https://api-pix-h.gerencianet.com.br"
+ },
+ "ENDPOINTS": {
+ "authorize": {
+ "route": "/oauth/token",
+ "method": "post"
+ },
+ "pixConfigWebhook": {
"route": "/v2/webhook/:chave",
"method": "put"
- },
- "pixDetailWebhook": {
- "route": "/v2/webhook/:chave",
+ },
+ "pixDetailWebhook": {
+ "route": "/v2/webhook/:chave",
+ "method": "get"
+ },
+ "pixListWebhook": {
+ "route": "/v2/webhook",
+ "method": "get"
+ },
+ "pixDeleteWebhook": {
+ "route": "/v2/webhook/:chave",
+ "method": "delete"
+ },
+ "pixCreateCharge": {
+ "route": "/v2/cob/:txid",
+ "method": "put"
+ },
+ "pixCreateImmediateCharge": {
+ "route": "/v2/cob",
+ "method": "post"
+ },
+ "pixDetailCharge": {
+ "route": "/v2/cob/:txid",
"method": "get"
- },
- "pixListWebhook": {
- "route": "/v2/webhook",
+ },
+ "pixUpdateCharge": {
+ "route": "/v2/cob/:txid",
+ "method": "patch"
+ },
+ "pixListCharges": {
+ "route": "/v2/cob",
"method": "get"
- },
- "pixDeleteWebhook": {
- "route": "/v2/webhook/:chave",
+ },
+ "pixGenerateQRCode": {
+ "route": "/v2/loc/:id/qrcode",
+ "method": "get"
+ },
+ "pixDevolution": {
+ "route": "/v2/pix/:e2eId/devolucao/:id",
+ "method": "put"
+ },
+ "pixDetailDevolution": {
+ "route": "/v2/pix/:e2eId/devolucao/:id",
+ "method": "get"
+ },
+ "pixSend": {
+ "route": "/v2/gn/pix/:idEnvio",
+ "method": "put"
+ },
+ "pixDetail": {
+ "route": "/v2/pix/:e2eId",
+ "method": "get"
+ },
+ "pixListReceived": {
+ "route": "/v2/pix",
+ "method": "get"
+ },
+ "pixCreateLocation": {
+ "route": "/v2/loc",
+ "method": "post"
+ },
+ "pixListLocation": {
+ "route": "/v2/loc",
+ "method": "get"
+ },
+ "pixDetailLocation": {
+ "route": "/v2/loc/:id",
+ "method": "get"
+ },
+ "pixUnsetTxid": {
+ "route": "/v2/loc/:id/txid",
"method": "delete"
- },
-
- "pixCreateCharge": {
- "route": "/v2/cob/:txid",
- "method": "put"
- },
- "pixCreateImmediateCharge": {
- "route": "/v2/cob",
- "method": "post"
- },
- "pixDetailCharge": {
- "route": "/v2/cob/:txid",
- "method": "get"
- },
- "pixUpdateCharge": {
- "route": "/v2/cob/:txid",
- "method": "patch"
- },
- "pixListCharges": {
- "route": "/v2/cob",
- "method": "get"
- },
- "pixGenerateQRCode": {
- "route": "/v2/loc/:id/qrcode",
- "method": "get"
- },
- "pixDevolution": {
- "route": "/v2/pix/:e2eId/devolucao/:id",
- "method": "put"
- },
- "pixDetailDevolution": {
- "route": "/v2/pix/:e2eId/devolucao/:id",
- "method": "get"
- },
- "pixSend": {
- "route": "/v2/pix",
- "method": "post"
- },
- "pixDetail": {
- "route": "/v2/pix/:e2eId",
- "method": "get"
- },
- "pixListReceived": {
- "route": "/v2/pix",
- "method": "get"
- },
- "pixCreateLocation": {
- "route": "/v2/loc",
- "method": "post"
- },
- "pixListLocation": {
- "route": "/v2/loc",
- "method": "get"
- },
- "pixDetailLocation": {
- "route": "/v2/loc/:id",
- "method": "get"
- },
- "pixUnsetTxid": {
- "route": "/v2/loc/:id/txid",
- "method": "delete"
- },
- "pixCreateEvp": {
- "route": "/v2/gn/evp",
- "method": "post"
- },
- "pixListEvp": {
- "route": "/v2/gn/evp",
- "method": "get"
- },
- "pixDeleteEvp": {
- "route": "/v2/gn/evp/:chave",
- "method": "delete"
- },
- "pixDetailBalance": {
- "route": "/v2/gn/saldo",
- "method": "get"
- },
- "pixUpdateSettings": {
- "route": "/v2/gn/config",
- "method": "put"
- },
- "pixDetailSettings": {
- "route": "/v2/gn/config",
- "method": "get"
+ },
+ "pixCreateEvp": {
+ "route": "/v2/gn/evp",
+ "method": "post"
+ },
+ "pixListEvp": {
+ "route": "/v2/gn/evp",
+ "method": "get"
+ },
+ "pixDeleteEvp": {
+ "route": "/v2/gn/evp/:chave",
+ "method": "delete"
+ },
+ "pixDetailBalance": {
+ "route": "/v2/gn/saldo",
+ "method": "get"
+ },
+ "pixUpdateSettings": {
+ "route": "/v2/gn/config",
+ "method": "put"
+ },
+ "pixDetailSettings": {
+ "route": "/v2/gn/config",
+ "method": "get"
+ },
+ "pixCreateDueCharge": {
+ "route": "/v2/cobv/:txid",
+ "method": "put"
+ },
+ "pixDetailDueCharge": {
+ "route": "/v2/cobv/:txid",
+ "method": "get"
+ },
+ "pixListDueCharges": {
+ "route": "/v2/cobv",
+ "method": "get"
+ },
+ "pixUpdateDueCharge": {
+ "route": "/v2/cobv/:txid",
+ "method": "patch"
+ },
+ "createReport": {
+ "route": "/v2/gn/relatorios/extrato-conciliacao",
+ "method": "post"
+ },
+ "detailReport": {
+ "route": "/v2/gn/relatorios/:id",
+ "method": "get"
+ }
+ }
+ },
+ "OPEN-FINANCE": {
+ "URL": {
+ "production": "https://apis.gerencianet.com.br/open-finance",
+ "sandbox": "https://apis.gerencianet.com.br/open-finance"
+ },
+ "ENDPOINTS": {
+ "authorize": {
+ "route": "/oauth/token",
+ "method": "post"
+ },
+ "ofUpdateSettings": {
+ "route": "/config",
+ "method": "put"
+ },
+ "ofDetailSettings": {
+ "route": "/config",
+ "method": "get"
+ },
+ "ofListParticipants": {
+ "route": "/participantes/",
+ "method": "get"
+ },
+ "ofStartPixPayment": {
+ "route": "/pagamentos/pix",
+ "method": "post"
+ },
+ "ofListPixPayment": {
+ "route": "/pagamentos/pix",
+ "method": "get"
+ }
+ }
+ },
+ "PAGAMENTOS": {
+ "URL": {
+ "production": "https://apis.gerencianet.com.br/pagamento",
+ "sandbox": "https://apis.gerencianet.com.br/pagamento"
+ },
+ "ENDPOINTS": {
+ "authorize": {
+ "route": "/oauth/token",
+ "method": "post"
+ },
+ "payDetailPayment": {
+ "route": "/:idPagamento",
+ "method": "get"
+ },
+ "payRequestBarCode": {
+ "route": "/codBarras/:codBarras",
+ "method": "post"
+ },
+ "payDetailBarCode": {
+ "route": "/codBarras/:codBarras",
+ "method": "get"
+ },
+ "payListPayments": {
+ "route": "/resumo",
+ "method": "get"
+ }
}
}
}
diff --git a/src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java b/src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java
index 9428c64..6ee611a 100644
--- a/src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java
+++ b/src/test/java/br/com/gerencianet/gnsdk/APIRequestTest.java
@@ -125,7 +125,7 @@ public void shouldSetPropertiesCorrectly() throws Exception{
when(credentials.getString("baseUri")).thenReturn("https://sandbox.gerencianet.com.br");
when(config.getEndpoints()).thenReturn(endpoints);
when(config.getOptions()).thenReturn(credentials);
- apiRequester = new APIRequest("post", "/v1/charge", body, config);
+ apiRequester = new APIRequest("post", "/v1/charge", body, authorize, config);
Assert.assertTrue(apiRequester.getRequester() != null);
Assert.assertTrue(apiRequester.getBody().has("item"));
Assert.assertTrue(apiRequester.getBody().getInt("item") == 12);
diff --git a/src/test/java/br/com/gerencianet/gnsdk/EndpointsTest.java b/src/test/java/br/com/gerencianet/gnsdk/EndpointsTest.java
index e7a99ca..e25cc75 100644
--- a/src/test/java/br/com/gerencianet/gnsdk/EndpointsTest.java
+++ b/src/test/java/br/com/gerencianet/gnsdk/EndpointsTest.java
@@ -143,7 +143,7 @@ public void shouldForwarWithParamsAndQueryString() throws Exception{
Mockito.when(config.getOptions()).thenReturn(options);
JSONObject body = mock(JSONObject.class);
- endpoints = new Endpoints(config);
+ endpoints = new Endpoints(config, apiRequester);
HashMap params = new HashMap();
params.put("id", "1");
params.put("parcel", "1");
@@ -176,7 +176,7 @@ public void shouldForwarWithParams() throws Exception{
Mockito.when(config.getEndpoints()).thenReturn(ep);
Mockito.when(config.getOptions()).thenReturn(options);
- endpoints = new Endpoints(config);
+ endpoints = new Endpoints(config, apiRequester);
HashMap params = new HashMap();
params.put("id", "1");
try