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!"
);
}
}