UNPKG

@trowdev/juno-pix-bacen

Version:

Lib para gerar pagamentos com a API do BCB ou com a API do Juno

122 lines 4.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.dinamicPix = exports.staticPix = void 0; const crc_1 = __importDefault(require("./crc")); function addLeftZero(value) { return value < 10 ? `0${value}` : value; } function formatText(value) { var str = value.toUpperCase().replace("Ç", "C"); return str.normalize("NFD").replace(/[^A-Z0-9$%*+-\./:]/gi, " "); } function staticPix({ merchantKey = "", merchantName = "", amount, merchantCity, merchantCep, description, identifier, isUnique }) { const merchantNameF = formatText(merchantName); const merchantCityF = formatText(merchantCity || ""); const merchantCepF = formatText(merchantCep || ""); const descriptionF = formatText(description || ""); const identifierF = formatText(identifier || ""); const amountF = amount ? amount.toFixed(2).toString() : "0"; if (merchantKey.length < 1 || merchantNameF.length < 1) { throw "merchantKey and merchantName are mandatory fields"; } if (merchantNameF.length > 25) { throw "merchantName has a max of 25 characters"; } if (merchantCityF.length > 15) { throw "merchantCity has a max of 15 characters"; } if (amountF.length > 13) { throw "Amount has a maximum of 13 characters"; } var brCode = []; brCode.push("000201"); if (isUnique) brCode.push("010212"); var extra = 22; if (descriptionF !== "") { extra += descriptionF.length + 4; } brCode.push(`26${merchantKey.length + extra}`); brCode.push("0014br.gov.bcb.pix"); brCode.push(`01${addLeftZero(merchantKey.length)}${merchantKey}`); if (descriptionF !== "") { brCode.push(`02${addLeftZero(descriptionF.length)}${descriptionF}`); } brCode.push("52040000"); brCode.push("5303986"); if (parseFloat(amountF) >= 0.01) { brCode.push(`54${addLeftZero(amountF.length)}${amountF}`); } brCode.push("5802BR"); brCode.push(`59${addLeftZero(merchantNameF.length)}${merchantNameF}`); if (merchantCityF !== "") { brCode.push(`60${addLeftZero(merchantCityF.length)}${merchantCityF}`); } if (merchantCepF !== "") { brCode.push(`61${addLeftZero(merchantCepF.length)}${merchantCepF}`); } if (identifierF !== "") { brCode.push(`62${identifierF.length + 38}`); brCode.push(`05${addLeftZero(identifierF.length)}${identifierF}`); } brCode.push("6304"); var brCodeString = brCode.join(""); return `${brCodeString}${(0, crc_1.default)(brCodeString)}`; } exports.staticPix = staticPix; function dinamicPix({ merchantName, merchantCity, merchantCep, amount, location, identifier, isUnique }) { const merchantNameF = formatText(merchantName); const merchantCityF = formatText(merchantCity || ""); const merchantCepF = formatText(merchantCep || ""); const amountF = amount ? amount.toFixed(2).toString() : "0"; const locationF = location.replace('https://', ''); const identifierF = formatText(identifier || ""); if (merchantNameF.length < 1 || locationF.length < 1 || amountF.length < 1) { throw "merchantName, amount and location are mandatory fields"; } if (merchantNameF.length > 25) { throw "merchantName has a max of 25 characters"; } if (merchantCityF.length > 15) { throw "merchantCity has a max of 15 characters"; } if (amountF.length > 13) { throw "Amount has a maximum of 13 characters"; } if (parseFloat(amountF) < 0.01) { throw "Amount needs to be at least 0.01"; } var brCode = []; brCode.push("000201"); if (isUnique) brCode.push("010212"); brCode.push(`26${locationF.length + 22}`); brCode.push("0014br.gov.bcb.pix"); brCode.push(`25${addLeftZero(locationF.length)}${locationF}`); brCode.push("52040000"); brCode.push("5303986"); brCode.push(`54${addLeftZero(amountF.length)}${amountF}`); brCode.push("5802BR"); brCode.push(`59${addLeftZero(merchantNameF.length)}${merchantNameF}`); if (merchantCityF !== "") { brCode.push(`60${addLeftZero(merchantCityF.length)}${merchantCityF}`); } if (merchantCepF !== "") { brCode.push(`61${addLeftZero(merchantCepF.length)}${merchantCepF}`); } if (identifierF !== "") { brCode.push(`62${identifierF.length + 38}`); brCode.push(`05${addLeftZero(identifierF.length)}${identifierF}`); } else { brCode.push("62070503***"); } brCode.push("6304"); var brCodeString = brCode.join(""); return `${brCodeString}${(0, crc_1.default)(brCodeString)}`; } exports.dinamicPix = dinamicPix; //# sourceMappingURL=bacen-pix.js.map