UNPKG

@trowdev/juno-pix-bacen

Version:

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

141 lines 6.73 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JunoPIX = void 0; const axios_1 = __importDefault(require("axios")); const js_base64_1 = require("js-base64"); function JunoPIX({ clientId, clientSecret, resourceToken }) { const baseUrl = 'https://api.juno.com.br/'; const accessHeader = () => __awaiter(this, void 0, void 0, function* () { const accessToken = yield getAccessToken(); const headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + accessToken, 'X-API-Version': 2, 'X-Resource-Token': resourceToken, }; return headers; }); const getHttp = (url, headers) => __awaiter(this, void 0, void 0, function* () { return yield axios_1.default.get(url, { headers }); }); const postHttp = (url, headers, data) => __awaiter(this, void 0, void 0, function* () { return yield axios_1.default.post(url, data, { headers }); }); const getBasicCredentials = () => (0, js_base64_1.encode)(`${clientId}:${clientSecret}`); const getAccessToken = () => __awaiter(this, void 0, void 0, function* () { var _a; const basicCredential = yield getBasicCredentials(); const headers = { 'Content-Type': 'application/json', 'Authorization': 'Basic ' + basicCredential }; const data = yield postHttp(`${baseUrl}authorization-server/oauth/token?grant_type=client_credentials`, headers); if (!((_a = data === null || data === void 0 ? void 0 : data.data) === null || _a === void 0 ? void 0 : _a.access_token)) { throw new Error('Não foi possível obter o token de acesso da conta.'); } return data.data.access_token; }); const webhook = () => __awaiter(this, void 0, void 0, function* () { const headers = yield accessHeader(); const create = (url, eventTypes) => __awaiter(this, void 0, void 0, function* () { const ret = yield postHttp(`${baseUrl}notifications/webhooks`, headers, { url, eventTypes }); if (!ret.data.id) { throw new Error('Não foi possível registrar o webhook.'); } return ret.data; }); const list = (ignoreError) => __awaiter(this, void 0, void 0, function* () { const ret = yield getHttp(`${baseUrl}notifications/webhooks`, headers); if (!ret.data || !ret.data._embedded || !ret.data._embedded.webhooks) { if (!ignoreError) { throw new Error('Não foi possível listar o(s) webhook(s).'); } else { return []; } } const webhooks = []; yield Promise.all(ret.data._embedded.webhooks.map((hook) => { const webhook = { id: hook.id, secret: hook.url, status: hook.secret, url: hook.status, eventTypes: hook.eventTypes }; webhooks.push(webhook); })); return webhooks; }); const findByEventType = (event) => __awaiter(this, void 0, void 0, function* () { const hooks = yield list(true); if (hooks && hooks.length > 0) { let webhook = { id: '', status: '', label: '', name: '' }; yield Promise.all(hooks.map((hook) => __awaiter(this, void 0, void 0, function* () { yield Promise.all(hook.eventTypes.map(eventType => { if (eventType.name.toLocaleLowerCase() === event.toLocaleLowerCase()) { webhook = eventType; } })); }))); return webhook; } return undefined; }); return { create, findByEventType, list }; }); const charge = () => __awaiter(this, void 0, void 0, function* () { const create = (dados) => __awaiter(this, void 0, void 0, function* () { const headers = yield accessHeader(); const ret = yield postHttp(`${baseUrl}pix-api/v2/cob`, headers, dados); if (!ret.data.txid) { throw new Error('Falha ao gerar cobrança.'); } return ret.data.txid; }); const info = (txId) => __awaiter(this, void 0, void 0, function* () { const headers = yield accessHeader(); const ret = yield getHttp(`${baseUrl}pix-api/v2/cob/${txId}`, headers); if (!ret.data.txid) { throw new Error('Não foi possível encontrar esta cobrança.'); } const data = ret.data; return { txId: data.txid, status: data.status, calendario: data.calendario, valor: data.valor.original, referencia: data.solicitacaoPagador }; }); const pix = (txId) => __awaiter(this, void 0, void 0, function* () { const headers = yield accessHeader(); const ret = yield getHttp(`${baseUrl}pix-api/qrcode/v2/${txId}/imagem`, headers); if (!ret.data.qrcodeBase64) { throw new Error('Não foi possível gerar o QR Code desta cobrança.'); } const data = ret.data; const pixDados = (0, js_base64_1.decode)(data.qrcodeBase64); return { pixDados: pixDados }; }); return { create, info, pix }; }); return { webhook, charge }; } exports.JunoPIX = JunoPIX; //# sourceMappingURL=juno-pix.js.map