Histórico de mensagens sobre java em pix

EXIBINDO CONVERSAS RECENTES:

Texto: java
Data: 16/10/2024
Canal: pix
Avatar discord do usuario emiliocalvet

emiliocalvet

Ver Respostas

System.setProperty("javax.net.ssl.keyStore", credentials.getCertificate());

O certificado que coloquei foi de forma global, pra ir em todas as requisições independente de qual seja. Não sei se era conflito ou coisa do tipo mas essa implementação que mandei parou de dar erro quando removi o SDK. Pelo menos até agr.

Avatar discord do usuario emiliocalvet

emiliocalvet

java
@Getter
public class Credentials {
private final String clientId;
private final String clientSecret;
private final String certificate;
private final boolean sandbox;
private final boolean debug;

public Credentials() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream credentialsFile = classLoader.getResourceAsStream("credentials.json");
assert credentialsFile != null;
JSONTokener tokenizer = new JSONTokener(credentialsFile);
JSONObject credentials = new JSONObject(tokenizer);
try {
credentialsFile.close();
} catch (IOException e) {
System.out.println("Impossible to close file credentials.json");
}

this.clientId = credentials.getString("client_id");
this.clientSecret = credentials.getString("client_secret");
this.certificate = credentials.getString("certificate");
this.sandbox = credentials.getBoolean("sandbox");
this.debug = credentials.getBoolean("debug");
}

public HashMap getOptionsMap() {
HashMap options = new HashMap<>();
options.put("client_id", this.getClientId());
options.put("client_secret", this.getClientSecret());
options.put("certificate", this.getCertificate());
options.put("sandbox", this.isSandbox());
return options;
}

public JSONObject getOptionsJson() {
return new JSONObject(this.getOptionsMap());
}
}

Avatar discord do usuario emiliocalvet

emiliocalvet

Ver Respostas

java
public PixChargeResponse pixCreateImmediateCharge(PixChargeRequest pixChargeRequest) {
if (!isValidValue(pixChargeRequest.valor())) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Valor inválido de criação do pix");
}

var options = new Credentials().getOptionsJson();

JSONObject body = new JSONObject();
body.put("calendario", new JSONObject().put("expiracao", pixDurationSeconds));
body.put("valor", new JSONObject().put("original", pixChargeRequest.valor()));
body.put("chave", token);

JSONArray infoAdicionais = new JSONArray();
infoAdicionais.put(new JSONObject().put("nome", "Descrição do serviço").put("valor", pixChargeRequest.serviceDescription()));
body.put("infoAdicionais", infoAdicionais);

try {
EfiPay efi = new EfiPay(options);
JSONObject response = efi.call("pixCreateImmediateCharge", new HashMap<>(), body);
String copyAndPaste = response.getString("pixCopiaECola");
byte[] qrCode = PixGenerateQRCode.generateQRCode(copyAndPaste, 300, 300);

pixWebhookService.configPixWebhook(token);

return new PixChargeResponse(
response.get("txid").toString(),
qrCode,
copyAndPaste,
pixChargeRequest.valor(),
pixChargeRequest.serviceDescription()
);
} catch (EfiPayException e) {
logger.error("Falha ao criar cobrança pix!", e);
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Falha ao criar cobrança Pix!"
);
} catch (Exception e) {
logger.error("Erro desconhecido ao criar cobrança pix!", e);
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR,
"Erro ao criar cobrança Pix!"
);
}
}