From c209cbd3f58e91cbf438b71882d5411ceb03ac44 Mon Sep 17 00:00:00 2001 From: Diego Merjildo Date: Wed, 30 Sep 2020 19:07:40 -0300 Subject: [PATCH 1/6] Adiciona campo sub-merchant (MCC dinamico) no request --- .../userede/erede/AbstractTransaction.java | 37 +++++++ .../br/com/userede/erede/SubMerchant.java | 103 ++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 src/main/java/br/com/userede/erede/SubMerchant.java diff --git a/src/main/java/br/com/userede/erede/AbstractTransaction.java b/src/main/java/br/com/userede/erede/AbstractTransaction.java index 0a53aca..58b6b22 100644 --- a/src/main/java/br/com/userede/erede/AbstractTransaction.java +++ b/src/main/java/br/com/userede/erede/AbstractTransaction.java @@ -81,6 +81,15 @@ public class AbstractTransaction { @SerializedName("links") private List links; + @SerializedName("SubMerchant") + private SubMerchant submerchant; + + @SerializedName("PaymentFacilitatorID") + private Long paymentFacilitatorID; + + @SerializedName("IndependentSalesOrganizationID") + private String independentSalesOrganizationID; + private void prepareUrls() { if (urls == null) { urls = new ArrayList<>(); @@ -446,4 +455,32 @@ public AbstractTransaction setLinks(List links) { this.links = links; return this; } + + public SubMerchant getSubmerchant() { + return submerchant; + } + + public AbstractTransaction setSubmerchant(SubMerchant submerchant) { + this.submerchant = submerchant; + return this; + } + + public Long getPaymentFacilitatorID() { + return paymentFacilitatorID; + } + + public AbstractTransaction setPaymentFacilitatorID(Long paymentFacilitatorID) { + this.paymentFacilitatorID = paymentFacilitatorID; + return this; + } + + public String getIndependentSalesOrganizationID() { + return independentSalesOrganizationID; + } + + public AbstractTransaction setIndependentSalesOrganizationID(String independentSalesOrganizationID) { + this.independentSalesOrganizationID = independentSalesOrganizationID; + return this; + } + } diff --git a/src/main/java/br/com/userede/erede/SubMerchant.java b/src/main/java/br/com/userede/erede/SubMerchant.java new file mode 100644 index 0000000..ec3c815 --- /dev/null +++ b/src/main/java/br/com/userede/erede/SubMerchant.java @@ -0,0 +1,103 @@ +package br.com.userede.erede; + +import com.google.gson.annotations.SerializedName; + +public class SubMerchant { + + @SerializedName("MCC") + private Long mcc; + + @SerializedName("SubMerchantID") + private String submerchantID; + + @SerializedName("Address") + private String address; + + @SerializedName("City") + private String city; + + @SerializedName("State") + private String state; + + @SerializedName("Country") + private String country; + + @SerializedName("Cep") + private String cep; + + @SerializedName("Cnpj") + private String cnpj; + + public Long getMcc() { + return mcc; + } + + public SubMerchant setMcc(Long mcc) { + this.mcc = mcc; + return this; + } + + public String getSubmerchantID() { + return submerchantID; + } + + public SubMerchant setSubmerchantID(String submerchantID) { + this.submerchantID = submerchantID; + return this; + } + + public String getAddress() { + return address; + } + + public SubMerchant setAddress(String address) { + this.address = address; + return this; + } + + public String getCity() { + return city; + } + + public SubMerchant setCity(String city) { + this.city = city; + return this; + } + + public String getState() { + return state; + } + + public SubMerchant setState(String state) { + this.state = state; + return this; + } + + public String getCountry() { + return country; + } + + public SubMerchant setCountry(String country) { + this.country = country; + return this; + } + + public String getCep() { + return cep; + } + + public SubMerchant setCep(String cep) { + this.cep = cep; + return this; + } + + public String getCnpj() { + return cnpj; + } + + public SubMerchant setCnpj(String cnpj) { + this.cnpj = cnpj; + return this; + } + +} From f49b8220821d7b33ff6cd9efb248847e27cd7e05 Mon Sep 17 00:00:00 2001 From: Diego Merjildo Date: Thu, 1 Oct 2020 13:58:42 -0300 Subject: [PATCH 2/6] Formata o conteudo da mensagem no UTF-8. Habilita envio dos exception pro usuario do SDK --- src/main/java/br/com/userede/erede/eRede.java | 21 ++++---- .../service/AbstractTransactionService.java | 53 ++++++++----------- .../service/CancelTransactionService.java | 21 +++----- .../service/CaptureTransactionService.java | 23 ++++---- .../service/CreateTransactionService.java | 24 ++++----- .../erede/service/GetTransactionService.java | 11 ++-- 6 files changed, 63 insertions(+), 90 deletions(-) diff --git a/src/main/java/br/com/userede/erede/eRede.java b/src/main/java/br/com/userede/erede/eRede.java index 565fe42..df87595 100644 --- a/src/main/java/br/com/userede/erede/eRede.java +++ b/src/main/java/br/com/userede/erede/eRede.java @@ -1,10 +1,13 @@ package br.com.userede.erede; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.logging.Logger; + import br.com.userede.erede.service.CancelTransactionService; import br.com.userede.erede.service.CaptureTransactionService; import br.com.userede.erede.service.CreateTransactionService; import br.com.userede.erede.service.GetTransactionService; -import java.util.logging.Logger; public class eRede { @@ -24,46 +27,46 @@ public eRede(Store store, Logger logger) { this.logger = logger; } - public TransactionResponse authorize(Transaction transaction) { + public TransactionResponse authorize(Transaction transaction) throws URISyntaxException, IOException { return create(transaction); } - public TransactionResponse create(Transaction transaction) { + public TransactionResponse create(Transaction transaction) throws URISyntaxException, IOException { CreateTransactionService createTransactionService = new CreateTransactionService(store, transaction, logger); return createTransactionService.execute(); } - public TransactionResponse cancel(Transaction transaction) { + public TransactionResponse cancel(Transaction transaction) throws IOException, URISyntaxException { CancelTransactionService cancelTransactionService = new CancelTransactionService(store, transaction, logger); return cancelTransactionService.execute(); } - public TransactionResponse capture(Transaction transaction) { + public TransactionResponse capture(Transaction transaction) throws IOException, URISyntaxException { CaptureTransactionService captureTransactionService = new CaptureTransactionService(store, transaction, logger); return captureTransactionService.execute(); } - public TransactionResponse get(String tid) { + public TransactionResponse get(String tid) throws IOException, URISyntaxException { GetTransactionService getTransactionService = new GetTransactionService(store, null, logger); getTransactionService.setTid(tid); return getTransactionService.execute(); } - public TransactionResponse getByReference(String reference) { + public TransactionResponse getByReference(String reference) throws IOException, URISyntaxException { GetTransactionService getTransactionService = new GetTransactionService(store, null, logger); getTransactionService.setReference(reference); return getTransactionService.execute(); } - public TransactionResponse getRefunds(String tid) { + public TransactionResponse getRefunds(String tid) throws IOException, URISyntaxException { GetTransactionService getTransactionService = new GetTransactionService(store, null, logger); getTransactionService.setTid(tid); getTransactionService.setRefund(true); @@ -71,7 +74,7 @@ public TransactionResponse getRefunds(String tid) { return getTransactionService.execute(); } - public TransactionResponse zero(Transaction transaction) { + public TransactionResponse zero(Transaction transaction) throws Exception { transaction.setAmount(0); transaction.capture(); diff --git a/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java b/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java index dc3a9df..4ba6400 100644 --- a/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java +++ b/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java @@ -17,6 +17,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.GZIPInputStream; + import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; @@ -38,48 +39,38 @@ abstract class AbstractTransactionService { this.logger = logger; } - abstract public TransactionResponse execute(); + public abstract TransactionResponse execute() throws URISyntaxException, IOException; URIBuilder getUri() throws URISyntaxException { return new URIBuilder(store.getEnvironment().getEndpoint("transactions")); } - TransactionResponse sendRequest(HttpUriRequest request) { - String credentials = Base64.getEncoder() - .encodeToString(String.format("%s:%s", store.getFiliation(), store.getToken()).getBytes( - StandardCharsets.US_ASCII)); - - request - .addHeader(HttpHeaders.USER_AGENT, String.format(eRede.USER_AGENT, store.getFiliation())); - request.addHeader(HttpHeaders.ACCEPT, "application/json"); - request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); - request.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + credentials); + TransactionResponse sendRequest(HttpUriRequest request) throws IOException { + String credentials = Base64.getEncoder().encodeToString( + String.format("%s:%s", store.getFiliation(), store.getToken()).getBytes(StandardCharsets.US_ASCII)); - logger.log(Level.FINE, request.getRequestLine().toString()); + request.addHeader(HttpHeaders.USER_AGENT, String.format(eRede.USER_AGENT, store.getFiliation())); + request.addHeader(HttpHeaders.ACCEPT, "application/json"); + request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); + request.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + credentials); - try { - HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); - int status = httpResponse.getStatusLine().getStatusCode(); + logger.log(Level.FINE, request.getRequestLine().toString()); - String response = parseResponse(httpResponse); - TransactionResponse transactionResponse = new Gson() - .fromJson(response, TransactionResponse.class); + HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); + int status = httpResponse.getStatusLine().getStatusCode(); - if (status < 200 || status >= 400) { - RedeError redeError = new RedeError(transactionResponse.getReturnCode(), - transactionResponse.getReturnMessage()); + String response = parseResponse(httpResponse); + TransactionResponse transactionResponse = new Gson().fromJson(response, TransactionResponse.class); - throw new RedeException(httpResponse.getStatusLine().toString(), redeError, - transactionResponse); - } + if (status < 200 || status >= 400) { + RedeError redeError = new RedeError(transactionResponse.getReturnCode(), + transactionResponse.getReturnMessage() + " Original Request:" + request.getRequestLine().toString() + " Response: " + + response); - return transactionResponse; - } catch (IOException e) { - e.printStackTrace(); - } - - return null; - } + throw new RedeException(httpResponse.getStatusLine().toString(), redeError, transactionResponse); + } + return transactionResponse; + } private String parseResponse(HttpResponse response) throws IOException { HttpEntity responseEntity = response.getEntity(); diff --git a/src/main/java/br/com/userede/erede/service/CancelTransactionService.java b/src/main/java/br/com/userede/erede/service/CancelTransactionService.java index 7e390a8..1487300 100644 --- a/src/main/java/br/com/userede/erede/service/CancelTransactionService.java +++ b/src/main/java/br/com/userede/erede/service/CancelTransactionService.java @@ -4,9 +4,10 @@ import br.com.userede.erede.Transaction; import br.com.userede.erede.TransactionResponse; import com.google.gson.GsonBuilder; -import java.io.UnsupportedEncodingException; +import java.io.IOException; import java.net.URISyntaxException; import java.util.logging.Logger; + import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; @@ -27,18 +28,10 @@ public URIBuilder getUri() throws URISyntaxException { } @Override - public TransactionResponse execute() { - try { - HttpPost request = new HttpPost(getUri().build()); - String entity = new GsonBuilder().create().toJson(transaction); - - request.setEntity(new StringEntity(entity)); - - return sendRequest(request); - } catch (URISyntaxException | UnsupportedEncodingException e) { - e.printStackTrace(); - } - - return null; + public TransactionResponse execute() throws IOException, URISyntaxException { + HttpPost request = new HttpPost(getUri().build()); + String entity = new GsonBuilder().create().toJson(transaction); + request.setEntity(new StringEntity(entity)); + return sendRequest(request); } } diff --git a/src/main/java/br/com/userede/erede/service/CaptureTransactionService.java b/src/main/java/br/com/userede/erede/service/CaptureTransactionService.java index 23573c5..c64de32 100644 --- a/src/main/java/br/com/userede/erede/service/CaptureTransactionService.java +++ b/src/main/java/br/com/userede/erede/service/CaptureTransactionService.java @@ -4,13 +4,16 @@ import br.com.userede.erede.Transaction; import br.com.userede.erede.TransactionResponse; import com.google.gson.GsonBuilder; -import java.io.UnsupportedEncodingException; +import java.io.IOException; import java.net.URISyntaxException; import java.util.logging.Logger; + import org.apache.http.client.methods.HttpPut; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; + + public class CaptureTransactionService extends AbstractTransactionService { public CaptureTransactionService(Store store, @@ -26,18 +29,10 @@ public URIBuilder getUri() throws URISyntaxException { } @Override - public TransactionResponse execute() { - try { - HttpPut request = new HttpPut(getUri().build()); - String entity = new GsonBuilder().create().toJson(transaction); - - request.setEntity(new StringEntity(entity)); - - return sendRequest(request); - } catch (URISyntaxException | UnsupportedEncodingException e) { - e.printStackTrace(); - } - - return null; + public TransactionResponse execute() throws IOException, URISyntaxException { + HttpPut request = new HttpPut(getUri().build()); + String entity = new GsonBuilder().create().toJson(transaction); + request.setEntity(new StringEntity(entity)); + return sendRequest(request); } } diff --git a/src/main/java/br/com/userede/erede/service/CreateTransactionService.java b/src/main/java/br/com/userede/erede/service/CreateTransactionService.java index ea0f492..e71f879 100644 --- a/src/main/java/br/com/userede/erede/service/CreateTransactionService.java +++ b/src/main/java/br/com/userede/erede/service/CreateTransactionService.java @@ -3,13 +3,16 @@ import br.com.userede.erede.Store; import br.com.userede.erede.Transaction; import br.com.userede.erede.TransactionResponse; +import java.io.IOException; import com.google.gson.GsonBuilder; -import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.util.logging.Logger; + import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; + public class CreateTransactionService extends AbstractTransactionService { public CreateTransactionService(Store store, Transaction transaction, Logger logger) { @@ -17,18 +20,11 @@ public CreateTransactionService(Store store, Transaction transaction, Logger log } @Override - public TransactionResponse execute() { - try { - HttpPost request = new HttpPost(getUri().build()); - String entity = new GsonBuilder().create().toJson(transaction); - - request.setEntity(new StringEntity(entity)); - - return sendRequest(request); - } catch (URISyntaxException | UnsupportedEncodingException e) { - e.printStackTrace(); - } - - return null; + public TransactionResponse execute() throws URISyntaxException, IOException { + HttpPost request; + request = new HttpPost(getUri().build()); + String entity = new GsonBuilder().create().toJson(transaction); + request.setEntity(new StringEntity(entity, StandardCharsets.UTF_8)); + return sendRequest(request); } } diff --git a/src/main/java/br/com/userede/erede/service/GetTransactionService.java b/src/main/java/br/com/userede/erede/service/GetTransactionService.java index d491353..99ab63b 100644 --- a/src/main/java/br/com/userede/erede/service/GetTransactionService.java +++ b/src/main/java/br/com/userede/erede/service/GetTransactionService.java @@ -3,8 +3,10 @@ import br.com.userede.erede.Store; import br.com.userede.erede.Transaction; import br.com.userede.erede.TransactionResponse; +import java.io.IOException; import java.net.URISyntaxException; import java.util.logging.Logger; + import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; @@ -47,15 +49,8 @@ public URIBuilder getUri() throws URISyntaxException { } @Override - public TransactionResponse execute() { - try { + public TransactionResponse execute() throws IOException, URISyntaxException { HttpGet request = new HttpGet(getUri().build()); - return sendRequest(request); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - - return null; } } From 7d5a29192d2f273cefce25f8eae8c57cc5a895b6 Mon Sep 17 00:00:00 2001 From: Diego Merjildo Date: Wed, 18 Nov 2020 22:31:28 -0300 Subject: [PATCH 3/6] Adiciona campo Brand e headers para retorno de bandeiras --- .../br/com/userede/erede/Authorization.java | 12 ++++++ src/main/java/br/com/userede/erede/Brand.java | 43 +++++++++++++++++++ src/main/java/br/com/userede/erede/Store.java | 9 ++++ src/main/java/br/com/userede/erede/eRede.java | 6 +++ .../service/AbstractTransactionService.java | 27 ++++++++---- .../java/br/com/userede/erede/TestErede.java | 38 ++++++++++++++++ 6 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 src/main/java/br/com/userede/erede/Brand.java create mode 100644 src/test/java/br/com/userede/erede/TestErede.java diff --git a/src/main/java/br/com/userede/erede/Authorization.java b/src/main/java/br/com/userede/erede/Authorization.java index 8ac849e..b5b9511 100644 --- a/src/main/java/br/com/userede/erede/Authorization.java +++ b/src/main/java/br/com/userede/erede/Authorization.java @@ -55,6 +55,9 @@ public class Authorization { @SerializedName("tid") private String tid; + @SerializedName("Brand") + private Brand brand; + public String getAffiliation() { return affiliation; } @@ -207,4 +210,13 @@ public Authorization setTid(String tid) { this.tid = tid; return this; } + + public Brand getBrand() { + return brand; + } + + public Authorization setBrand(Brand brand) { + this.brand = brand; + return this; + } } \ No newline at end of file diff --git a/src/main/java/br/com/userede/erede/Brand.java b/src/main/java/br/com/userede/erede/Brand.java new file mode 100644 index 0000000..920ad66 --- /dev/null +++ b/src/main/java/br/com/userede/erede/Brand.java @@ -0,0 +1,43 @@ +package br.com.userede.erede; + +import com.google.gson.annotations.SerializedName; + +public class Brand { + + @SerializedName("name") + private String name; + + @SerializedName("returnCode") + private String returnCode; + + @SerializedName("returnMessage") + private String returnMessage; + + public String getName() { + return name; + } + + public Brand setName(String name) { + this.name = name; + return this; + } + + public String getReturnCode() { + return returnCode; + } + + public Brand setReturnCode(String returnCode) { + this.returnCode = returnCode; + return this; + } + + public String getReturnMessage() { + return returnMessage; + } + + public Brand setReturnMessage(String returnMessage) { + this.returnMessage = returnMessage; + return this; + } + +} diff --git a/src/main/java/br/com/userede/erede/Store.java b/src/main/java/br/com/userede/erede/Store.java index e17d6cb..2ccc692 100644 --- a/src/main/java/br/com/userede/erede/Store.java +++ b/src/main/java/br/com/userede/erede/Store.java @@ -5,6 +5,7 @@ public class Store { private Environment environment; private String filiation; private String token; + private Boolean brandReturnOpen; public Store(String filiation, String token, Environment environment) { this.environment = environment; @@ -42,4 +43,12 @@ public Store setToken(String token) { this.token = token; return this; } + + public Boolean getBrandReturnOpen() { + return brandReturnOpen; + } + + public void setBrandReturnOpen(Boolean brandReturnOpen) { + this.brandReturnOpen = brandReturnOpen; + } } diff --git a/src/main/java/br/com/userede/erede/eRede.java b/src/main/java/br/com/userede/erede/eRede.java index df87595..f603472 100644 --- a/src/main/java/br/com/userede/erede/eRede.java +++ b/src/main/java/br/com/userede/erede/eRede.java @@ -34,6 +34,7 @@ public TransactionResponse authorize(Transaction transaction) throws URISyntaxEx public TransactionResponse create(Transaction transaction) throws URISyntaxException, IOException { CreateTransactionService createTransactionService = new CreateTransactionService(store, transaction, logger); + createTransactionService.setUserAgent(String.format(eRede.USER_AGENT, store.getFiliation())); return createTransactionService.execute(); } @@ -41,6 +42,7 @@ public TransactionResponse create(Transaction transaction) throws URISyntaxExcep public TransactionResponse cancel(Transaction transaction) throws IOException, URISyntaxException { CancelTransactionService cancelTransactionService = new CancelTransactionService(store, transaction, logger); + cancelTransactionService.setUserAgent(String.format(eRede.USER_AGENT, store.getFiliation())); return cancelTransactionService.execute(); } @@ -48,6 +50,7 @@ public TransactionResponse cancel(Transaction transaction) throws IOException, U public TransactionResponse capture(Transaction transaction) throws IOException, URISyntaxException { CaptureTransactionService captureTransactionService = new CaptureTransactionService(store, transaction, logger); + captureTransactionService.setUserAgent(String.format(eRede.USER_AGENT, store.getFiliation())); return captureTransactionService.execute(); } @@ -55,6 +58,7 @@ public TransactionResponse capture(Transaction transaction) throws IOException, public TransactionResponse get(String tid) throws IOException, URISyntaxException { GetTransactionService getTransactionService = new GetTransactionService(store, null, logger); getTransactionService.setTid(tid); + getTransactionService.setUserAgent(String.format(eRede.USER_AGENT, store.getFiliation())); return getTransactionService.execute(); } @@ -62,6 +66,7 @@ public TransactionResponse get(String tid) throws IOException, URISyntaxExceptio public TransactionResponse getByReference(String reference) throws IOException, URISyntaxException { GetTransactionService getTransactionService = new GetTransactionService(store, null, logger); getTransactionService.setReference(reference); + getTransactionService.setUserAgent(String.format(eRede.USER_AGENT, store.getFiliation())); return getTransactionService.execute(); } @@ -70,6 +75,7 @@ public TransactionResponse getRefunds(String tid) throws IOException, URISyntaxE GetTransactionService getTransactionService = new GetTransactionService(store, null, logger); getTransactionService.setTid(tid); getTransactionService.setRefund(true); + getTransactionService.setUserAgent(String.format(eRede.USER_AGENT, store.getFiliation())); return getTransactionService.execute(); } diff --git a/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java b/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java index 4ba6400..45e1f28 100644 --- a/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java +++ b/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java @@ -1,12 +1,5 @@ package br.com.userede.erede.service; -import br.com.userede.erede.Store; -import br.com.userede.erede.Transaction; -import br.com.userede.erede.TransactionResponse; -import br.com.userede.erede.eRede; -import br.com.userede.erede.service.error.RedeError; -import br.com.userede.erede.service.error.RedeException; -import com.google.gson.Gson; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -26,10 +19,19 @@ import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.HttpClientBuilder; +import com.google.gson.Gson; + +import br.com.userede.erede.Store; +import br.com.userede.erede.Transaction; +import br.com.userede.erede.TransactionResponse; +import br.com.userede.erede.service.error.RedeError; +import br.com.userede.erede.service.error.RedeException; + abstract class AbstractTransactionService { final Store store; final Transaction transaction; + private String userAgent; private final Logger logger; @@ -49,11 +51,15 @@ TransactionResponse sendRequest(HttpUriRequest request) throws IOException { String credentials = Base64.getEncoder().encodeToString( String.format("%s:%s", store.getFiliation(), store.getToken()).getBytes(StandardCharsets.US_ASCII)); - request.addHeader(HttpHeaders.USER_AGENT, String.format(eRede.USER_AGENT, store.getFiliation())); + request.addHeader(HttpHeaders.USER_AGENT, this.userAgent); request.addHeader(HttpHeaders.ACCEPT, "application/json"); request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); request.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + credentials); + if (store.getBrandReturnOpen()) { + request.addHeader("Transaction-Response", "brand-return-opened"); + } + System.out.println("line:" + request.getRequestLine().toString()); logger.log(Level.FINE, request.getRequestLine().toString()); HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); @@ -93,4 +99,9 @@ private String parseResponse(HttpResponse response) throws IOException { return responseBuilder.toString(); } + + public void setUserAgent(String userAgent) { + this.userAgent = userAgent; + } + } diff --git a/src/test/java/br/com/userede/erede/TestErede.java b/src/test/java/br/com/userede/erede/TestErede.java new file mode 100644 index 0000000..8fb798f --- /dev/null +++ b/src/test/java/br/com/userede/erede/TestErede.java @@ -0,0 +1,38 @@ +package br.com.userede.erede; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Random; + +import com.google.gson.Gson; + +public class TestErede { + + public static void main(String[] args) throws URISyntaxException, IOException { + // Replace "PV" adn "token" + Store store = new Store("PV", "TOKEN", Environment.sandbox()); + + // Ativa retorno das bandeiras. + store.setBrandReturnOpen(true); + Gson gson = new Gson(); + + // Transação que será autorizada + Integer randnum = new Random().nextInt(10000 - 100) + 100; + Transaction transaction = new Transaction(20.99, "pedido" + randnum.toString()); + transaction.creditCard("5448280000000007", "123", "12", "2020", "Jhon James Rambo"); + + System.out.println("Transação original: \n" + gson.toJson(transaction)); + + // Autoriza a transação + TransactionResponse transactionResp; + transactionResp = (new eRede(store).create(transaction)); + + if (transactionResp.getReturnCode().equals("00")) { + System.out.printf("Transação autorizada com sucesso: %s\n", transactionResp.getTid()); + } + + // Consulta a transação + TransactionResponse transactionResponse = (new eRede(store).get(transactionResp.getTid())); + System.out.println("Consulta:\n" + gson.toJson(transactionResponse)); + } +} From ee0898a8e16a272af221d7453b12f54f3e5907d0 Mon Sep 17 00:00:00 2001 From: Diego Merjildo Date: Mon, 28 Dec 2020 12:50:23 -0300 Subject: [PATCH 4/6] Get authorizationCode from brand field. --- .../br/com/userede/erede/Authorization.java | 9 ++- src/main/java/br/com/userede/erede/Brand.java | 12 +++ .../com/userede/erede/TestAuthorization.java | 74 +++++++++++++++++++ .../java/br/com/userede/erede/TestErede.java | 2 +- 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/test/java/br/com/userede/erede/TestAuthorization.java diff --git a/src/main/java/br/com/userede/erede/Authorization.java b/src/main/java/br/com/userede/erede/Authorization.java index b5b9511..0c34208 100644 --- a/src/main/java/br/com/userede/erede/Authorization.java +++ b/src/main/java/br/com/userede/erede/Authorization.java @@ -55,7 +55,7 @@ public class Authorization { @SerializedName("tid") private String tid; - @SerializedName("Brand") + @SerializedName("brand") private Brand brand; public String getAffiliation() { @@ -77,6 +77,13 @@ public Authorization setAmount(int amount) { } public String getAuthorizationCode() { + if (brand != null) { + authorizationCode = brand.getAuthorizationCode(); + } + + if (authorizationCode != null) { + authorizationCode = authorizationCode.isEmpty() ? null : authorizationCode; + } return authorizationCode; } diff --git a/src/main/java/br/com/userede/erede/Brand.java b/src/main/java/br/com/userede/erede/Brand.java index 920ad66..a58f8b1 100644 --- a/src/main/java/br/com/userede/erede/Brand.java +++ b/src/main/java/br/com/userede/erede/Brand.java @@ -13,6 +13,9 @@ public class Brand { @SerializedName("returnMessage") private String returnMessage; + @SerializedName("authorizationCode") + private String authorizationCode; + public String getName() { return name; } @@ -40,4 +43,13 @@ public Brand setReturnMessage(String returnMessage) { return this; } + public String getAuthorizationCode() { + return authorizationCode; + } + + public Brand setAuthorizationCode(String authorizationCode) { + this.authorizationCode = authorizationCode; + return this; + } + } diff --git a/src/test/java/br/com/userede/erede/TestAuthorization.java b/src/test/java/br/com/userede/erede/TestAuthorization.java new file mode 100644 index 0000000..ce2b143 --- /dev/null +++ b/src/test/java/br/com/userede/erede/TestAuthorization.java @@ -0,0 +1,74 @@ +package br.com.userede.erede; + +import com.google.gson.Gson; + +import junit.framework.TestCase; + +public class TestAuthorization extends TestCase { + + String auth = " { \"dateTime\": \"2020-12-14T19:26:17-03:00\",\n" + + " \"returnCode\": \"00\",\n" + + " \"returnMessage\": \"Success.\",\n" + + " \"affiliation\": 11223344,\n" + + " \"status\": \"Approved\",\n" + + " \"reference\": \"1234\",\n" + + " \"tid\": \"10012012141925580147\",\n" + + " \"nsu\": \"504364149\",\n" + + " \"kind\": \"Credit\",\n" + + " \"amount\": 51,\n" + + " \"installments\": 1,\n" + + " \"cardHolderName\": \"Jhon Dole\",\n" + + " \"cardBin\": \"112233\",\n" + + " \"last4\": \"1234\",\n" + + " \"origin\": 1,\n" + + " \"subscription\": false,\n" + + " \"brand\": {\n" + + " \"name\": \"Mastercard\",\n" + + " \"returnCode\": \"00\",\n" + + " \"returnMessage\": \"Success.\",\n" + + " \"authorizationCode\": \"031605\",\n" + + " \"brandTid\": \"031605\"\n" + + " } }"; + + private Authorization authorization = new Authorization(); + private Gson gson = new Gson(); + + public void testGetAuthorizationCode() { + authorization = gson.fromJson(auth, Authorization.class); + assertEquals("031605", authorization.getAuthorizationCode()); + + auth = " { " + + " \"brand\": {\n" + + " \"name\": \"Mastercard\",\n" + + " \"returnCode\": \"00\",\n" + + " \"returnMessage\": \"Success.\",\n" + + " \"brandTid\": \"031605\"\n" + + " } }"; + authorization = gson.fromJson(auth, Authorization.class); + assertEquals(null, authorization.getAuthorizationCode()); + + auth = " { \"authorizationCode\": \"\"\n } "; + authorization = gson.fromJson(auth, Authorization.class); + assertEquals(null, authorization.getBrand()); + assertEquals(null, authorization.getAuthorizationCode()); + + auth = " { \"authorizationCode\": \"112233\"\n } "; + authorization = gson.fromJson(auth, Authorization.class); + assertEquals(null, authorization.getBrand()); + assertEquals("112233", authorization.getAuthorizationCode()); + + } + +// public void testSetAuthorizationCode() { +// fail("Not yet implemented"); +// } +// +// public void testGetBrand() { +// fail("Not yet implemented"); +// } +// +// public void testSetBrand() { +// fail("Not yet implemented"); +// } + +} diff --git a/src/test/java/br/com/userede/erede/TestErede.java b/src/test/java/br/com/userede/erede/TestErede.java index 8fb798f..e337237 100644 --- a/src/test/java/br/com/userede/erede/TestErede.java +++ b/src/test/java/br/com/userede/erede/TestErede.java @@ -9,7 +9,7 @@ public class TestErede { public static void main(String[] args) throws URISyntaxException, IOException { - // Replace "PV" adn "token" + // Replace "PV" and "token" Store store = new Store("PV", "TOKEN", Environment.sandbox()); // Ativa retorno das bandeiras. From 0cb4f85aabefb42a71ae3445af5f575080fb8b5a Mon Sep 17 00:00:00 2001 From: Diego Merjildo Date: Wed, 3 Feb 2021 23:01:24 -0300 Subject: [PATCH 5/6] Remove RedeException on response message --- .../erede/service/AbstractTransactionService.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java b/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java index 45e1f28..ffd429c 100644 --- a/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java +++ b/src/main/java/br/com/userede/erede/service/AbstractTransactionService.java @@ -63,18 +63,12 @@ TransactionResponse sendRequest(HttpUriRequest request) throws IOException { logger.log(Level.FINE, request.getRequestLine().toString()); HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); - int status = httpResponse.getStatusLine().getStatusCode(); String response = parseResponse(httpResponse); - TransactionResponse transactionResponse = new Gson().fromJson(response, TransactionResponse.class); + logger.log(Level.INFO, "REDE Transaction Response:[{0}]", response); - if (status < 200 || status >= 400) { - RedeError redeError = new RedeError(transactionResponse.getReturnCode(), - transactionResponse.getReturnMessage() + " Original Request:" + request.getRequestLine().toString() + " Response: " - + response); + TransactionResponse transactionResponse = new Gson().fromJson(response, TransactionResponse.class); - throw new RedeException(httpResponse.getStatusLine().toString(), redeError, transactionResponse); - } return transactionResponse; } From 6a151e0e5d35739b1d1381037c501eb592e2a0b6 Mon Sep 17 00:00:00 2001 From: Diego Merjildo Date: Tue, 2 Mar 2021 15:38:23 -0300 Subject: [PATCH 6/6] Fix amount type --- src/main/java/br/com/userede/erede/Transaction.java | 2 +- src/test/java/br/com/userede/erede/TestErede.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/br/com/userede/erede/Transaction.java b/src/main/java/br/com/userede/erede/Transaction.java index 3d08d6c..8e74714 100644 --- a/src/main/java/br/com/userede/erede/Transaction.java +++ b/src/main/java/br/com/userede/erede/Transaction.java @@ -14,7 +14,7 @@ public Transaction(double amount, String reference) { setReference(reference); } - public Transaction(int amount, String reference) { + public Transaction(Integer amount, String reference) { setAmount(amount); setReference(reference); } diff --git a/src/test/java/br/com/userede/erede/TestErede.java b/src/test/java/br/com/userede/erede/TestErede.java index e337237..14a64b3 100644 --- a/src/test/java/br/com/userede/erede/TestErede.java +++ b/src/test/java/br/com/userede/erede/TestErede.java @@ -18,8 +18,8 @@ public static void main(String[] args) throws URISyntaxException, IOException { // Transação que será autorizada Integer randnum = new Random().nextInt(10000 - 100) + 100; - Transaction transaction = new Transaction(20.99, "pedido" + randnum.toString()); - transaction.creditCard("5448280000000007", "123", "12", "2020", "Jhon James Rambo"); + Transaction transaction = new Transaction(new Integer(2092), "pedido" + randnum.toString()); + transaction.creditCard("5448280000000007", "123", "12", "2028", "Jhon James Rambo"); System.out.println("Transação original: \n" + gson.toJson(transaction));