-
Notifications
You must be signed in to change notification settings - Fork 4
/
BoletosManager.java
166 lines (138 loc) · 7.58 KB
/
BoletosManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package br.com.pjbank.sdk.recebimento;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import br.com.pjbank.sdk.api.PJBankClient;
import br.com.pjbank.sdk.auth.PJBankAuthenticatedService;
import br.com.pjbank.sdk.enums.StatusPagamentoBoleto;
import br.com.pjbank.sdk.exceptions.PJBankException;
import br.com.pjbank.sdk.models.recebimento.BoletoRecebimento;
import br.com.pjbank.sdk.models.recebimento.ExtratoBoleto;
/**
* @author Vinícius Silva <vinicius.silva@superlogica.com>
* @version 1.0
* @since 1.0
*/
public class BoletosManager extends PJBankAuthenticatedService {
/**
* EndPoint a ser requisitado na API
*/
private String endPoint = "recebimentos/{{credencial}}";
public BoletosManager(String credencial, String chave) {
super(credencial, chave);
this.endPoint = this.endPoint.replace("{{credencial}}", credencial);
}
/**
* Realiza a emissão do boleto bancário para o cliente informado
* @param boletoRecebimento: boleto à ser emitido
* @return BoletoRecebimento
*/
public BoletoRecebimento create(BoletoRecebimento boletoRecebimento) throws IOException, PJBankException {
PJBankClient client = new PJBankClient(this.endPoint.concat("/transacoes"));
HttpPost httpPost = client.getHttpPostClient();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
JSONObject params = new JSONObject();
params.put("vencimento", dateFormat.format(boletoRecebimento.getVencimento()));
params.put("valor", boletoRecebimento.getValor());
params.put("juros", boletoRecebimento.getJuros());
params.put("multa", boletoRecebimento.getMulta());
params.put("desconto", boletoRecebimento.getDesconto());
params.put("nome_cliente", boletoRecebimento.getCliente().getNome());
params.put("cpf_cliente", boletoRecebimento.getCliente().getCpfCnpj());
params.put("endereco_cliente", boletoRecebimento.getCliente().getEndereco().getLogradouro());
params.put("numero_cliente", boletoRecebimento.getCliente().getEndereco().getNumero());
params.put("complemento_cliente", boletoRecebimento.getCliente().getEndereco().getComplemento());
params.put("bairro_cliente", boletoRecebimento.getCliente().getEndereco().getBairro());
params.put("cidade_cliente", boletoRecebimento.getCliente().getEndereco().getCidade());
params.put("estado_cliente", boletoRecebimento.getCliente().getEndereco().getEstado());
params.put("cep_cliente", boletoRecebimento.getCliente().getEndereco().getCep());
params.put("logo_url", boletoRecebimento.getLogoUrl());
params.put("texto", boletoRecebimento.getTexto());
params.put("grupo", boletoRecebimento.getGrupo());
params.put("pedido_numero", boletoRecebimento.getPedidoNumero());
httpPost.setEntity(new StringEntity(params.toString(), StandardCharsets.UTF_8));
String response = EntityUtils.toString(client.doRequest(httpPost).getEntity());
JSONObject responseObject = new JSONObject(response);
boletoRecebimento.setIdUnico(responseObject.getString("nossonumero"));
boletoRecebimento.setLinkBoleto(responseObject.getString("linkBoleto"));
boletoRecebimento.setLinkGrupo(responseObject.getString("linkGrupo"));
boletoRecebimento.setLinhaDigitavel(responseObject.getString("linhaDigitavel"));
return boletoRecebimento;
}
/**
* Retorna a lista de boletos emitidos por códigos de pedidos
* @param pedidos: Lista de códigos de pedidos os quais deseja retornar os boletos
* @return String: Link contendo os boletos relacionados aos códigos de pedidos enviados
*/
public String getByIds(Set<String> pedidos) throws IOException, PJBankException {
PJBankClient client = new PJBankClient(this.endPoint.concat("/transacoes/lotes"));
HttpPost httpPost = client.getHttpPostClient();
httpPost.addHeader("x-chave", this.getChave());
JSONArray pedidosArray = new JSONArray(pedidos);
JSONObject params = new JSONObject();
params.put("pedido_numero", pedidosArray);
httpPost.setEntity(new StringEntity(params.toString(), StandardCharsets.UTF_8));
String response = EntityUtils.toString(client.doRequest(httpPost).getEntity());
JSONObject responseObject = new JSONObject(response);
return responseObject.getString("linkBoleto");
}
public List<ExtratoBoleto> get(Date inicio, Date fim, StatusPagamentoBoleto pago) throws URISyntaxException, IOException, PJBankException, java.text.ParseException {
PJBankClient client = new PJBankClient(this.endPoint.concat("/transacoes"));
HttpGet httpGet = client.getHttpGetClient();
httpGet.addHeader("x-chave", this.getChave());
this.adicionarFiltros(httpGet, inicio, fim, pago);
String response = EntityUtils.toString(client.doRequest(httpGet).getEntity());
JSONArray extratoObject = new JSONArray(response);
int totalItensExtrato = extratoObject.length();
List<ExtratoBoleto> extratos = new ArrayList<>();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
for (int i = 0; i < totalItensExtrato; i++) {
JSONObject itemExtrato = extratoObject.getJSONObject(i);
ExtratoBoleto extrato = new ExtratoBoleto(
itemExtrato.getDouble("valor"),
itemExtrato.optDouble("valor_pago", 0.0),
itemExtrato.getDouble("valor_liquido"),
itemExtrato.optDouble("valor_tarifa", 0.0),
itemExtrato.getString("nosso_numero"),
itemExtrato.getString("nosso_numero_original"),
itemExtrato.getString("banco_numero"),
itemExtrato.getString("token_facilitador")
);
String dataVencimento = itemExtrato.getString("data_vencimento");
if (!StringUtils.isBlank(dataVencimento))
extrato.setDataVencimento(dateFormat.parse(dataVencimento));
String dataPagamento = itemExtrato.getString("data_pagamento");
if (!StringUtils.isBlank(dataPagamento))
extrato.setDataPagamento(dateFormat.parse(dataPagamento));
String dataCredito = itemExtrato.getString("data_credito");
if (!StringUtils.isBlank(dataCredito))
extrato.setDataCredito(dateFormat.parse(dataCredito));
extratos.add(extrato);
}
return extratos;
}
private void adicionarFiltros(HttpRequestBase httpRequestClient, Date inicio, Date fim, StatusPagamentoBoleto pago)
throws URISyntaxException {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
URIBuilder uriBuilder = new URIBuilder(httpRequestClient.getURI());
uriBuilder.addParameter("data_inicio", formatter.format(inicio));
uriBuilder.addParameter("data_fim", formatter.format(fim));
uriBuilder.addParameter("pago", pago.getName());
httpRequestClient.setURI(uriBuilder.build());
}
}