UNPKG

@ballin-team/paysync-nubank

Version:

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

48 lines (47 loc) 1.69 kB
import axios from 'axios'; import { HttpStatus, NubankApiError } from './helpers'; import nacl from 'tweetnacl'; export class NubankApiRequest { config; api; publicKey; constructor(input) { this.config = input; this.api = axios.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 === HttpStatus.OK) }); } setHost(testEnv) { if (testEnv) { return 'https://sandbox-api.spinpay.com.br/v1'; } return 'https://api.spinpay.com.br/v1'; } async getSigningKey() { const path = '/security/request-signing-keys'; try { const { data } = await this.api.get(path); return data.publicKey; } catch (e) { throw new NubankApiError({ path, data: e }); } } async isAuthenticMessage(response) { if (!this.publicKey) { this.publicKey = await this.getSigningKey(); } const signature = response.config?.headers.get('x-spin-signature'); const message = response.config.method + new URL(response.config?.url || '').pathname + response.config?.headers.get('x-spin-timestamp') + JSON.stringify(response.data); return nacl.sign.detached.verify(Buffer.from(message), Buffer.from(signature), Buffer.from(this.publicKey, 'base64')); } get env() { return this.config.testEnv ? 'SANDBOX' : 'PRODUCTION'; } }