Skip to content

Commit e75e322

Browse files
New endpoints for Pix API and Open Finance API
1 parent 7c5731c commit e75e322

File tree

10 files changed

+292
-37
lines changed

10 files changed

+292
-37
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 1.2.3
2+
3+
- Added endpoint and example to get receipts of Pix transactions
4+
- Added endpoints and examples for Automatic Pix - Pix API
5+
- Added endpoints and examples for Automatic Payments - Open Finance API
6+
- Added endpoints and examples for Payments by biometrics - Open Finance API
7+
18
# 1.2.2
29

310
- Added Statements API endpoints and examples

README-en.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ For more informations about [parameters](http://dev.sejaefi.com.br) and [values]
2222
**Via gradle:**
2323

2424
```gradle
25-
implementation 'br.com.efipay.efisdk:sdk-java-apis-efi:1.2.2'
25+
implementation 'br.com.efipay.efisdk:sdk-java-apis-efi:1.2.3'
2626
```
2727

2828
**Via maven:**
@@ -31,7 +31,7 @@ implementation 'br.com.efipay.efisdk:sdk-java-apis-efi:1.2.2'
3131
<dependency>
3232
    <groupId>br.com.efipay.efisdk</groupId>
3333
<artifactId>sdk-java-apis-efi</artifactId>
34-
<version>1.2.2</version>
34+
<version>1.2.3</version>
3535
</dependency>
3636
```
3737

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Para mais informações sobre [parâmetros](http://sejaefi.com.br/api) e [valore
2323
**Via gradle:**
2424

2525
```gradle
26-
implementation 'br.com.efipay.efisdk:sdk-java-apis-efi:1.2.2'
26+
implementation 'br.com.efipay.efisdk:sdk-java-apis-efi:1.2.3'
2727
```
2828

2929
**Via maven:**
@@ -32,7 +32,7 @@ implementation 'br.com.efipay.efisdk:sdk-java-apis-efi:1.2.2'
3232
<dependency>
3333
<groupId>br.com.efipay.efisdk</groupId>
3434
<artifactId>sdk-java-apis-efi</artifactId>
35-
<version>1.2.2</version>
35+
<version>1.2.3</version>
3636
</dependency>
3737
```
3838

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>br.com.efipay.efisdk</groupId>
55
<artifactId>sdk-java-apis-efi</artifactId>
6-
<version>1.2.2</version>
6+
<version>1.2.3</version>
77

88
<name>SDK JAVA APIS EFI</name>
99
<description>Java SDK for integrating with Efí API</description>

src/main/java/br/com/efi/efisdk/APIRequest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ public JSONArray sendArray() throws AuthorizationException, EfiPayException, IOE
109109
return this.requester.sendArray(this.bodyArray);
110110
}
111111

112+
public byte[] sendAsBytes() throws AuthorizationException, EfiPayException, IOException {
113+
Date expiredDate = this.authenticator.getExpires();
114+
if (expiredDate == null || expiredDate.compareTo(new Date()) <= 0) {
115+
this.authenticator.authorize();
116+
}
117+
this.requester.addHeader("Authorization", "Bearer " + this.authenticator.getAccessToken());
118+
try {
119+
return this.requester.sendAsBytes(this.body);
120+
} catch (AuthorizationException e) {
121+
this.authenticator.authorize();
122+
return this.requester.sendAsBytes(this.body);
123+
}
124+
}
125+
112126
public Request getRequester() {
113127
return requester;
114128
}

src/main/java/br/com/efi/efisdk/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*/
1313
public class Config {
14-
private final static String version = "1.2.2";
14+
private final static String version = "1.2.3";
1515
private JSONObject conf = new JSONObject();
1616
private JSONObject endpoints = new JSONObject();
1717
private JSONObject urls = new JSONObject();

src/main/java/br/com/efi/efisdk/Endpoints.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public JSONArray callArray(String endpoint, Map<String, String> params, JSONArra
9393
return kernelCallArray(endpoint, params, body);
9494
}
9595

96+
public byte[] callImage(String endpointName, Map<String, String> params, JSONObject body) throws Exception {
97+
return kernelCallImage(endpointName, params, body);
98+
}
99+
96100
public Map<String, Object> call(String endpoint, Map<String, String> params, Map<String, Object> mapBody)
97101
throws Exception {
98102
JSONObject body = (JSONObject) JSONObject.wrap(mapBody);
@@ -203,6 +207,33 @@ private JSONArray kernelCallArray(String endpointName, Map<String, String> param
203207
return response;
204208
}
205209

210+
private byte[] kernelCallImage(String endpointName, Map<String, String> params, JSONObject body) throws Exception {
211+
JSONObject endpoints = this.config.getEndpoints();
212+
HashMap<String, JSONObject> api = getRoute(endpoints, endpointName);
213+
214+
if (!api.containsKey("ENDPOINTS")) {
215+
throw new Exception("nonexistent endpoint");
216+
}
217+
218+
JSONObject api_url = (JSONObject) api.get("URL");
219+
this.config.setURLs(api_url);
220+
221+
JSONObject api_endpoints = (JSONObject) api.get("ENDPOINTS");
222+
JSONObject api_auth = (JSONObject) api_endpoints.get("authorize");
223+
JSONObject endpoint = (JSONObject) api_endpoints.get(endpointName);
224+
String routeName = getRoute(endpoint, params);
225+
routeName += getQueryString(params);
226+
227+
if (this.requester == null) {
228+
requester = new APIRequest(endpoint.get("method").toString(), routeName, body, api_auth, this.config);
229+
}
230+
231+
byte[] response = this.requester.sendAsBytes(); // <- método novo que retorna `byte[]`
232+
this.requester = null;
233+
234+
return response;
235+
}
236+
206237
private String getQueryString(Map<String, String> params) throws UnsupportedEncodingException {
207238
Set<Entry<String, String>> set = params.entrySet();
208239
String query = "";

src/main/java/br/com/efi/efisdk/Request.java

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package br.com.efi.efisdk;
22

33
import java.io.BufferedReader;
4+
import java.io.ByteArrayOutputStream;
45
import java.io.IOException;
56
import java.io.InputStream;
67
import java.io.InputStreamReader;
@@ -26,8 +27,10 @@
2627
public class Request {
2728

2829
private HttpURLConnection client;
30+
private String method;
2931

3032
public Request(String method, HttpURLConnection conn) throws IOException {
33+
this.method = method.toUpperCase();
3134
this.client = conn;
3235
this.client.setRequestProperty("Content-Type", "application/json");
3336
this.client.setRequestProperty("charset", "UTF-8");
@@ -46,15 +49,14 @@ public void addHeader(String key, String value) {
4649
}
4750

4851
public JSONObject send(JSONObject requestOptions) throws AuthorizationException, EfiPayException, IOException {
49-
byte[] postDataBytes;
50-
postDataBytes = requestOptions.toString().getBytes("UTF-8");
51-
this.client.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
52-
if (!client.getRequestMethod().toLowerCase().equals("get")) {
52+
if (!requestOptions.isEmpty()) {
53+
byte[] postDataBytes = requestOptions.toString().getBytes(StandardCharsets.UTF_8);
54+
client.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
5355
client.setDoOutput(true);
54-
OutputStream os = client.getOutputStream();
55-
os.write(postDataBytes);
56-
os.flush();
57-
os.close();
56+
try (OutputStream os = client.getOutputStream()) {
57+
os.write(postDataBytes);
58+
os.flush();
59+
}
5860
}
5961

6062
int responseCode = client.getResponseCode();
@@ -85,8 +87,6 @@ public JSONObject send(JSONObject requestOptions) throws AuthorizationException,
8587

8688
}
8789
case HttpURLConnection.HTTP_UNAUTHORIZED:
88-
case HttpURLConnection.HTTP_FORBIDDEN:
89-
throw new AuthorizationException();
9090
case 422: {
9191
InputStream responseStream = client.getErrorStream();
9292
JSONTokener responseTokener = new JSONTokener(responseStream);
@@ -171,6 +171,53 @@ public JSONArray sendArray(JSONArray bodyArray)
171171
}
172172
}
173173

174+
public byte[] sendAsBytes(JSONObject requestOptions)
175+
throws AuthorizationException, EfiPayException, IOException {
176+
177+
if (!"GET".equalsIgnoreCase(this.method)) {
178+
String bodyString = requestOptions.toString();
179+
this.client.setRequestProperty("Content-Length", String.valueOf(bodyString.getBytes("UTF-8").length));
180+
client.setDoOutput(true);
181+
182+
try (OutputStream os = client.getOutputStream()) {
183+
os.write(bodyString.getBytes("UTF-8"));
184+
os.flush();
185+
}
186+
} else {
187+
client.setDoOutput(false);
188+
}
189+
190+
int responseCode = client.getResponseCode();
191+
192+
if (responseCode == HttpURLConnection.HTTP_OK
193+
|| responseCode == HttpURLConnection.HTTP_CREATED
194+
|| responseCode == HttpURLConnection.HTTP_ACCEPTED) {
195+
196+
try (InputStream responseStream = client.getInputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
197+
198+
byte[] data = new byte[4096];
199+
int nRead;
200+
while ((nRead = responseStream.read(data, 0, data.length)) != -1) {
201+
buffer.write(data, 0, nRead);
202+
}
203+
buffer.flush();
204+
205+
byte[] responseBytes = buffer.toByteArray();
206+
207+
return responseBytes;
208+
}
209+
210+
} else {
211+
212+
try (InputStream errorStream = client.getErrorStream()) {
213+
JSONTokener responseTokener = new JSONTokener(errorStream);
214+
JSONObject response = new JSONObject(responseTokener);
215+
throw new EfiPayException(response);
216+
}
217+
218+
}
219+
}
220+
174221
private String readInputStreamToString(InputStream inputStream) throws IOException {
175222
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
176223
StringBuilder stringBuilder = new StringBuilder();

src/main/java/br/com/efi/efisdk/exceptions/EfiPayException.java

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,48 @@ public class EfiPayException extends Exception {
1818

1919
public EfiPayException(JSONObject response) {
2020
String message = "";
21-
if (response.has("error_description")) {
21+
if (response.has("violacoes")) {
22+
if (response.get("violacoes").getClass().getSimpleName().equals("JSONArray")) {
23+
JSONArray violacoes = response.getJSONArray("violacoes");
24+
for (int i = 0; i < violacoes.length(); i++) {
25+
JSONObject json = violacoes.getJSONObject(i);
26+
if (json.has("razao")) {
27+
this.error = json.get("razao").toString();
28+
this.errorDescription = json.get("propriedade").toString();
29+
}
30+
}
31+
}
32+
} else if (response.has("error_description")) {
2233
if (response.get("error_description").getClass().getSimpleName().equals("JSONObject")) {
2334
JSONObject errorDescription = response.getJSONObject("error_description");
24-
if (errorDescription.has("message"))
35+
if (errorDescription.has("message")) {
2536
message = errorDescription.getString("message");
26-
else
37+
} else {
2738
message = response.get("error_description").toString();
39+
}
2840

29-
if (errorDescription.has("property"))
41+
if (errorDescription.has("property")) {
3042
message += ":" + errorDescription.get("property");
31-
} else
43+
}
44+
} else {
3245
message = response.get("error_description").toString();
46+
}
3347

34-
if (response.has("code"))
35-
this.code = Integer.parseInt(response.get("code").toString());
48+
if (response.has("code")) {
49+
this.code = Integer.parseInt(response.get("code").toString());
50+
}
3651
this.error = response.get("error").toString();
3752
this.errorDescription = message;
3853

3954
} else if (response.has("nome")) {
4055
this.error = response.get("nome").toString();
4156
this.errorDescription = response.get("mensagem").toString();
42-
} else if (response.has("violacoes")) {
43-
if (response.get("violacoes").getClass().getSimpleName().equals("JSONArray")) {
44-
JSONArray violacoes = response.getJSONArray("violacoes");
45-
for (int i = 0; i < violacoes.length(); i++) {
46-
JSONObject json = violacoes.getJSONObject(i);
47-
if (json.has("razao")) {
48-
this.error = json.get("razao").toString();
49-
this.errorDescription = json.get("propriedade").toString();
50-
}
51-
}
52-
}
57+
} else if (response.has("title") && response.has("detail")) {
58+
this.error = response.getString("title");
59+
this.errorDescription = response.getString("detail");
5360
} else {
5461
System.out.println(response);
62+
5563
}
5664
}
5765

@@ -69,9 +77,10 @@ public int getCode() {
6977

7078
@Override
7179
public String getMessage() {
72-
if (this.code != 0)
80+
if (this.code != 0) {
7381
return "Error " + this.code + " - " + this.error + ": " + this.errorDescription;
74-
else
82+
} else {
7583
return "Error: " + this.errorDescription;
84+
}
7685
}
7786
}

0 commit comments

Comments
 (0)