easy-pix
Version:
Pix payments made easy for developers build arround payment gateways
120 lines (119 loc) • 5.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MercadoPagoProvider = void 0;
const http_status_codes_1 = require("http-status-codes");
const HttpClient_1 = __importDefault(require("../../clients/HttpClient"));
const errors_1 = require("../../clients/HttpClient/errors");
const errors_2 = require("../../shared/errors");
const interfaces_1 = require("../../shared/interfaces");
const consts_1 = require("./consts");
const errors_3 = require("./errors");
const getFormattedDateNow_1 = require("../../shared/getFormattedDateNow");
class MercadoPagoProvider {
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(interfaces_1.PROVIDERS.MERCADO_PAGO);
}
this.API_KEY = API_KEY;
this.BASE_URL = consts_1.MERCADO_PAGO_BASE_URL;
this.httpClient = httpClient;
}
async createPixPayment({ id, name, taxId, value, description }) {
try {
const { body: payment } = await this.httpClient.post(`${this.BASE_URL}/payments`, {
description,
payer: {
email: taxId,
first_name: name.split(" ")[0] || " ",
},
external_reference: id,
payment_method_id: "Pix",
transaction_amount: value,
}, {
headers: {
Authorization: `Bearer ${this.API_KEY}`,
"Content-Type": "application/json",
},
});
return {
value,
originalId: String(payment.id),
netValue: value - (payment.taxes_amount || 0),
expirationDate: new Date(payment.date_of_expiration || (0, getFormattedDateNow_1.getFormattedDateNow)()),
payload: payment.point_of_interaction.transaction_data.qr_code,
encodedImage: payment.point_of_interaction.transaction_data.qr_code_base64,
};
}
catch (error) {
if (error instanceof errors_1.HttpClientError) {
throw new errors_3.MercadoPagoProviderError(`Error creating the pix, expected status code 200 or 201, received status ${(0, http_status_codes_1.getStatusCode)(error.statusCodeAsString || '')} and body ${JSON.stringify(error.body || '')}`);
}
throw new errors_3.MercadoPagoProviderError(`Unexpected Error creating the pix payment - ${error.message} - ${error.stack}`);
}
}
async getPixPaymentStatusByPaymentId(paymentId) {
try {
const { body: { status: paymentStatus } } = await this.httpClient.get(`${this.BASE_URL}/payments/${paymentId}`, {
headers: {
Authorization: `Bearer ${this.API_KEY}`,
"Content-Type": "application/json",
},
});
const pixStatusMap = {
approved: interfaces_1.PIX_STATUS.CONFIRMED,
authorized: interfaces_1.PIX_STATUS.CONFIRMED,
cancelled: interfaces_1.PIX_STATUS.OVERDUE,
rejected: interfaces_1.PIX_STATUS.OVERDUE,
};
return pixStatusMap[paymentStatus] || interfaces_1.PIX_STATUS.PENDING;
}
catch (error) {
if (error instanceof errors_1.HttpClientError) {
throw new errors_3.MercadoPagoProviderError(`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 errors_3.MercadoPagoProviderError(`Unexpected Error getting the payment status - ${error.message} - ${error.stack}`);
}
}
async deletePixChargeByPaymentId(paymentId) {
try {
await this.httpClient.put(`${this.BASE_URL}/payments/${paymentId}`, { status: "cancelled" }, {
headers: {
Authorization: `Bearer ${this.API_KEY}`,
"Content-Type": "application/json",
},
});
return true;
}
catch (error) {
if (error instanceof errors_1.HttpClientError) {
throw new errors_3.MercadoPagoProviderError(`Error deleting, expected status code 200, received status ${(0, http_status_codes_1.getStatusCode)(error.statusCodeAsString || '')} and body ${JSON.stringify(error.body || '')}`);
}
throw new errors_3.MercadoPagoProviderError(`Unexpected Error pix charge, expected status code 200, received status - ${error.message} - ${error.stack}`);
}
}
createPixTransfer() {
throw new errors_2.MethodNotImplemented(interfaces_1.PROVIDERS.MERCADO_PAGO);
}
}
exports.MercadoPagoProvider = MercadoPagoProvider;