UNPKG

easy-pix

Version:

Pix payments made easy for developers build arround payment gateways

187 lines (186 loc) 8.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsaasProvider = void 0; const http_status_codes_1 = require("http-status-codes"); const HttpClient_1 = __importDefault(require("../../clients/HttpClient")); const consts_1 = require("./consts"); const interfaces_1 = require("../../shared/interfaces"); const errors_1 = require("../../clients/HttpClient/errors"); const AsaasProviderError_1 = require("./errors/AsaasProviderError"); const errors_2 = require("../../shared/errors"); const getFormattedDateNow_1 = require("../../shared/getFormattedDateNow"); class AsaasProvider { constructor({ API_KEY = null, useSandbox = true, httpClient = HttpClient_1.default }) { Object.defineProperty(this, "BASE_URL", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "API_KEY", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "httpClient", { enumerable: true, configurable: true, writable: true, value: void 0 }); if (!API_KEY) { throw new errors_2.MissingApiKey(); } this.API_KEY = API_KEY; this.BASE_URL = useSandbox ? consts_1.ASAAS_SAND_BOX_BASE_URL : consts_1.ASAAS_BASE_URL; this.httpClient = httpClient; } async createCustomer({ name, cpfCnpj }) { try { const { body } = await this.httpClient.post(`${this.BASE_URL}/customers`, { name, cpfCnpj }, { headers: { access_token: this.API_KEY, }, }); return body; } catch (error) { if (error instanceof errors_1.HttpClientError) { throw new AsaasProviderError_1.AsaasProviderError(`Error creating the customer, expected and id and status code 200, received status ${(0, http_status_codes_1.getStatusCode)(error.statusCodeAsString || '')} and body ${JSON.stringify(error.body)}`); } throw new AsaasProviderError_1.AsaasProviderError(`Unexpected Error creating the customer - ${error.message} - ${error.stack}`); } } async createPayment({ value, dueDate, customer, description, billingType, postalService, externalReference, }) { try { const { body } = await this.httpClient.post(`${this.BASE_URL}/payments`, { value, dueDate, customer, description, billingType, postalService, externalReference, }, { headers: { access_token: this.API_KEY, }, }); return body; } catch (error) { if (error instanceof errors_1.HttpClientError) { throw new AsaasProviderError_1.AsaasProviderError(`Error creating the pix, expected and id and status code 200, received status ${(0, http_status_codes_1.getStatusCode)(error.statusCodeAsString || '')} and body ${JSON.stringify(error.body || '')}`); } throw new AsaasProviderError_1.AsaasProviderError(`Unexpected Error creating the pix payment - ${error.message} - ${error.stack}`); } } async getPixPaymentQrCodeByPaymentId(paymentId) { try { const { body } = await this.httpClient.get(`${this.BASE_URL}/payments/${paymentId}/pixQrCode`, { headers: { access_token: this.API_KEY, }, }); return body; } catch (error) { if (error instanceof errors_1.HttpClientError) { throw new AsaasProviderError_1.AsaasProviderError(`Error looking for the qr code, expected status code 200, received status ${(0, http_status_codes_1.getStatusCode)(error.statusCodeAsString || '')} and body ${JSON.stringify(error.body || '')}`); } throw new AsaasProviderError_1.AsaasProviderError(`Unexpected Error looking for the qr code - ${error.message} - ${error.stack}`); } } async createPixPayment({ id, name, taxId, value, description }) { const createdCustomer = await this.createCustomer({ name, cpfCnpj: taxId }); const dueDate = (0, getFormattedDateNow_1.getFormattedDateNow)(); const pixPayment = await this.createPayment({ value, dueDate, description, postalService: false, externalReference: id, customer: createdCustomer.id, billingType: interfaces_1.BILLING_TYPE.PIX, }); const qrCode = await this.getPixPaymentQrCodeByPaymentId(pixPayment.id); return { encodedImage: qrCode.encodedImage, payload: qrCode.payload, expirationDate: qrCode.expirationDate, originalId: pixPayment.id, value: pixPayment.value, netValue: pixPayment.netValue, }; } async getPixPaymentStatusByPaymentId(paymentId) { try { const { body: { status: paymentStatus } } = await this.httpClient.get(`${this.BASE_URL}/payments/${paymentId}/status`, { headers: { access_token: this.API_KEY, }, }); const pixStatusMap = { "RECEIVED": interfaces_1.PIX_STATUS.CONFIRMED, CONFIRMED: interfaces_1.PIX_STATUS.CONFIRMED, OVERDUE: interfaces_1.PIX_STATUS.OVERDUE, PENDING: interfaces_1.PIX_STATUS.PENDING, }; return pixStatusMap[paymentStatus] || interfaces_1.PIX_STATUS.PENDING; } catch (error) { if (error instanceof errors_1.HttpClientError) { throw new AsaasProviderError_1.AsaasProviderError(`Error getting the payment status, expected status code 200, received status ${(0, http_status_codes_1.getStatusCode)(error.statusCodeAsString || '')} and body ${JSON.stringify(error.body || '')}`); } throw new AsaasProviderError_1.AsaasProviderError(`Unexpected Error getting the payment status - ${error.message} - ${error.stack}`); } } async deletePixChargeByPaymentId(paymentId) { try { const { body: { deleted: wasDeletedSuccessfuly } } = await this.httpClient.delete(`${this.BASE_URL}/payments/${paymentId}`, { headers: { access_token: this.API_KEY, }, }); return wasDeletedSuccessfuly; } catch (error) { if (error instanceof errors_1.HttpClientError) { throw new AsaasProviderError_1.AsaasProviderError(`Error deleting, expected status code 200, received status ${(0, http_status_codes_1.getStatusCode)(error.statusCodeAsString || '')} and body ${JSON.stringify(error.body || '')}`); } throw new AsaasProviderError_1.AsaasProviderError(`Unexpected Error pix charge, expected status code 200, received status - ${error.message} - ${error.stack}`); } } async createPixTransfer({ value, description, pixAddressKey, pixAddressKeyType, }) { try { const { body } = await this.httpClient.post(`${this.BASE_URL}/transfers`, { value, description, pixAddressKey, pixAddressKeyType, operationType: interfaces_1.OPERATION_TYPE.PIX, }, { headers: { access_token: this.API_KEY, }, }); return { authorized: body.authorized, transferFee: body.transferFee, netValue: body.netValue, value: body.value, }; } catch (error) { if (error instanceof errors_1.HttpClientError) { throw new AsaasProviderError_1.AsaasProviderError(`Error creating transfer by pix, expected status code 200, received status ${(0, http_status_codes_1.getStatusCode)(error.statusCodeAsString || '')} and body ${JSON.stringify(error.body || '')}`); } throw new AsaasProviderError_1.AsaasProviderError(`Unexpected Error creating transfer by pix - ${error.message} - ${error.stack}`); } } } exports.AsaasProvider = AsaasProvider;