Skip to content

Commit

Permalink
EfiSettings.ts: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jul 12, 2024
1 parent 18ead81 commit 00c46a6
Showing 1 changed file with 101 additions and 77 deletions.
178 changes: 101 additions & 77 deletions backend/src/services/PaymentGatewayServices/EfiServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,44 @@ import EfiPay, { EfiCredentials } from "sdk-typescript-apis-efi";
import path from "path";
import GetSuperSettingService from "../SettingServices/GetSuperSettingService";
import { logger } from "../../utils/logger";
import { getIO } from "../../libs/socket";
import Invoices from "../../models/Invoices";
import Company from "../../models/Company";
import AppError from "../../errors/AppError";
import { processInvoiceExpired, processInvoicePaid } from "./PaymentGatewayServices";
import {
processInvoiceExpired,
processInvoicePaid
} from "./PaymentGatewayServices";

const webhookUrl = `${process.env.BACKEND_URL}/subscription/ticketz/webhook`;

async function getEfiOptions(): Promise<EfiCredentials> {
const cert = path.join(
__dirname,
`../../../private/${await GetSuperSettingService({ key: "_efiCertFile"})}`
`../../../private/${await GetSuperSettingService({ key: "_efiCertFile" })}`
);

return {
sandbox: false,
client_id: await GetSuperSettingService({ key: "_efiClientId" }),
client_secret: await GetSuperSettingService({ key: "_efiClientSecret" }),
pix_cert: cert,
validateMtls: false,
}
validateMtls: false
};
}


const newEfiPayInstance = async () => {
const efiOptions = await getEfiOptions();
return new EfiPay(efiOptions);
}
};

const createWebHook = async (efiPay: EfiPay) => {
const params = {
chave: await GetSuperSettingService({ key: "_efiPixKey" }),
chave: await GetSuperSettingService({ key: "_efiPixKey" })
};

const body = {
webhookUrl
}
};

return efiPay.pixConfigWebhook(params, body).then(
(ok: unknown) => {
Expand All @@ -49,45 +50,53 @@ const createWebHook = async (efiPay: EfiPay) => {
logger.error({ result: error }, "pixConfigWebhook error:");
}
);
}
};

export const efiInitialize = async () => {
const paymentGateway = await GetSuperSettingService({ key: "_paymentGateway" });
const paymentGateway = await GetSuperSettingService({
key: "_paymentGateway"
});

if (!webhookUrl.startsWith("https://")) {
logger.debug("efiInitialize: only SSL webhooks are supported");
return;
};
}

try {
if (paymentGateway === "efi") {
const efiOptions = await getEfiOptions();
const efiPay = new EfiPay(efiOptions);
const params = {
chave: await GetSuperSettingService({ key: "_efiPixKey" }),
chave: await GetSuperSettingService({ key: "_efiPixKey" })
};

efiPay.pixDetailWebhook(params).then(
(hooks: { webhookUrl: string; }) => {
(hooks: { webhookUrl: string }) => {
if (hooks?.webhookUrl !== webhookUrl) {
createWebHook(efiPay);
} else {
logger.debug({ result: hooks }, "efiInitialize: webhook correto já instalado");
logger.debug(
{ result: hooks },
"efiInitialize: webhook correto já instalado"
);
}
},
(error: { nome: string; }) => {
(error: { nome: string }) => {
if (error?.nome === "webhook_nao_encontrado") {
createWebHook(efiPay);
} else {
logger.error(error, "efiInitialize: fail to verify current webhook");
logger.error(
error,
"efiInitialize: fail to verify current webhook"
);
}
}
);
}
} catch (error) {
logger.error(error, "efiInitialize: ");
}
}
};

export const efiWebhook = async (
req: Request,
Expand All @@ -98,44 +107,48 @@ export const efiWebhook = async (
return res.json({ ok: true });
}
if (req.body.pix) {
req.body.pix.forEach( async (pix: { status: string; txid: string; valor: number; }) => {
logger.debug( pix, "Processando pagamento");
req.body.pix.forEach(
async (pix: { status: string; txid: string; valor: number }) => {
logger.debug(pix, "Processando pagamento");

const invoice = await Invoices.findOne({
where: {
txId: pix.txid,
status: "open",
},
include: { model: Company, as: "company" }
});

if (!invoice) {
logger.debug( "efiWebhook: Invoice not found or already paid" );
return true;
}

if (pix.valor < invoice.value) {
logger.debug( "Recebido valor menor" );
const invoice = await Invoices.findOne({
where: {
txId: pix.txid,
status: "open"
},
include: { model: Company, as: "company" }
});

if (!invoice) {
logger.debug("efiWebhook: Invoice not found or already paid");
return true;
}

if (pix.valor < invoice.value) {
logger.debug("Recebido valor menor");
return true;
}

await processInvoicePaid(invoice);
return true;
}

await processInvoicePaid(invoice);
return true;
});
);
}

return res.json({ ok: true });
};


export const efiCheckStatus = async ( invoice: Invoices, efiPay: EfiPay = null ): Promise<boolean> => {
export const efiCheckStatus = async (
invoice: Invoices,
efiPay: EfiPay = null
): Promise<boolean> => {
try {
if (!efiPay) {
efiPay = await newEfiPayInstance();
}
const txDetail = await efiPay.pixDetailCharge( { txid: invoice.txId });

const txDetail = await efiPay.pixDetailCharge({ txid: invoice.txId });

if (txDetail.status === "ATIVA" || txDetail.status !== "CONCLUIDA") {
return false;
}
Expand All @@ -145,60 +158,68 @@ export const efiCheckStatus = async ( invoice: Invoices, efiPay: EfiPay = null )
await processInvoicePaid(invoice);
return true;
}

return false;
} catch (error) {
logger.error(error, "Error getting detail of txid");
}

return false;
}
};

const efiPollCheckStatus = async ( efiPay: EfiPay, invoice: Invoices, retries = 10, interval = 30000 ) => {
let attempts = 0;
const efiPollCheckStatus = async (
efiPay: EfiPay,
invoice: Invoices,
retries = 10,
interval = 30000
) => {
let attempts = 0;

async function pollStatus(): Promise<void> {
await invoice.reload();

if (invoice.status === "paid") {
logger.debug(`efiPollCheckStatus: Invoice ${invoice.id} already paid, finishing polling`);
return;
}

const successful = await efiCheckStatus(invoice, efiPay);
if (successful) {
return;
}
async function pollStatus(): Promise<void> {
await invoice.reload();

if (invoice.status === "paid") {
logger.debug(
`efiPollCheckStatus: Invoice ${invoice.id} already paid, finishing polling`
);
return;
}

attempts += 1;
const successful = await efiCheckStatus(invoice, efiPay);
if (successful) {
return;
}

if (attempts >= retries) {
processInvoiceExpired(invoice);
return;
}
attempts += 1;

await new Promise(resolve => {setTimeout(resolve, interval)});
await pollStatus();
if (attempts >= retries) {
processInvoiceExpired(invoice);
return;
}

return pollStatus();
}
await new Promise(resolve => {
setTimeout(resolve, interval);
});
await pollStatus();
}

return pollStatus();
};

export const efiCreateSubscription = async (
req: Request,
res: Response
): Promise<Response> => {
const {
price,
invoiceId
} = req.body;
const { price, invoiceId } = req.body;

const body = {
calendario: {
expiracao: 300
},
valor: {
original: price.toLocaleString("pt-br", { minimumFractionDigits: 2 }).replace(",", ".")
original: price
.toLocaleString("pt-br", { minimumFractionDigits: 2 })
.replace(",", ".")
},
chave: await GetSuperSettingService({ key: "_efiPixKey" }),
solicitacaoPagador: `#Fatura:${invoiceId}`
Expand All @@ -209,9 +230,9 @@ export const efiCreateSubscription = async (
if (!invoice) {
throw new Error("Invoice not found");
}

await efiInitialize();

const efiPay = new EfiPay(efiOptions);
const pix = await efiPay.pixCreateImmediateCharge([], body);
await invoice.update({
Expand All @@ -230,6 +251,9 @@ export const efiCreateSubscription = async (
});
} catch (error) {
logger.error({ efiOptions, error }, "efiCreateSubscription error");
throw new AppError("Problema encontrado, entre em contato com o suporte!", 400);
throw new AppError(
"Problema encontrado, entre em contato com o suporte!",
400
);
}
};

0 comments on commit 00c46a6

Please sign in to comment.