UNPKG

@ballin-team/paysync-nubank

Version:

<h1 align="center"> <img src="https://docs.nupaybusiness.com.br/images/logotipo.png" alt="NuPay for Business" /> </h1>

66 lines (65 loc) 3.07 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.NubankApiRequest = void 0; const axios_1 = __importDefault(require("axios")); const helpers_1 = require("./helpers"); const tweetnacl_1 = __importDefault(require("tweetnacl")); class NubankApiRequest { constructor(input) { this.config = input; this.api = axios_1.default.create({ baseURL: this.setHost(input.testEnv), timeout: input.timeout || 30000, headers: { 'X-Merchant-Key': input.credentials.merchantKey, 'X-Merchant-Token': input.credentials.merchantToken, }, validateStatus: ((status) => status === helpers_1.HttpStatus.OK) }); } setHost(testEnv) { if (testEnv) { return 'https://sandbox-api.spinpay.com.br/v1'; } return 'https://api.spinpay.com.br/v1'; } getSigningKey() { return __awaiter(this, void 0, void 0, function* () { const path = '/security/request-signing-keys'; try { const { data } = yield this.api.get(path); return data.publicKey; } catch (e) { throw new helpers_1.NubankApiError({ path, data: e }); } }); } isAuthenticMessage(response) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { if (!this.publicKey) { this.publicKey = yield this.getSigningKey(); } const signature = (_a = response.config) === null || _a === void 0 ? void 0 : _a.headers.get('x-spin-signature'); const message = response.config.method + new URL(((_b = response.config) === null || _b === void 0 ? void 0 : _b.url) || '').pathname + ((_c = response.config) === null || _c === void 0 ? void 0 : _c.headers.get('x-spin-timestamp')) + JSON.stringify(response.data); return tweetnacl_1.default.sign.detached.verify(Buffer.from(message), Buffer.from(signature), Buffer.from(this.publicKey, 'base64')); }); } get env() { return this.config.testEnv ? 'SANDBOX' : 'PRODUCTION'; } } exports.NubankApiRequest = NubankApiRequest;