UNPKG

@nkhind/vivawallet-sdk

Version:
142 lines (141 loc) 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.VivaAuthISV = exports.VivaAuth = void 0; const axiosInstance_ts_1 = require("../utils/axiosInstance.ts"); const VivaSkull_class_1 = __importDefault(require("./VivaSkull.class")); class VivaAuth extends VivaSkull_class_1.default { constructor(datas) { super(datas); this.init(); } /** Credentials verification, `throw` error if credentials is not gived ***(required for API calls)*** */ async init() { console.log('Init viva'); if (!this.apikey || !this.merchantId || !this.clientId || !this.clientSecret) throw new Error('Credentials not provided'); } /** * Return the VivaWallet API Auth2.0 code from Credentials (needed for API Bearer calls) * or `null` on request failed * * @param basic If `true`, apikey and merchantId will be used instead of clientId and clientSecret */ async getVivaToken(basic = false) { if (!this.clientId || !this.clientSecret) { return { success: false, message: 'Init not called', code: 'initerror', data: null, }; } try { const res = await axiosInstance_ts_1.useAxios.post(this.endpoints.auth.url, { grant_type: 'client_credentials', }, { headers: { 'Content-Type': 'application/x-www-form-urlencoded', Authorization: 'Basic ' + Buffer.from(basic ? this.merchantId + ':' + this.apikey : this.clientId + ':' + this.clientSecret).toString('base64'), }, }); if (!res.data || !res.data.access_token) return { success: false, message: 'Failed to get Viva token', code: 'tokenerror', data: null, }; return { success: true, message: 'Viva token fetched', data: res.data.access_token, }; } catch (e) { console.error('Viva Auth Error', e); return { success: false, message: 'Failed to get Viva token', code: 'tokenerror', data: null, }; } } getVivaBasicToken() { return Buffer.from(this.merchantId + ':' + this.apikey).toString('base64'); } /** Return the code needed for Viva webhooks returns or `null` on request failed */ async getVivaWebhookCode() { if (!this.merchantId || !this.apikey) { return { success: false, message: 'Init not called', code: 'initerror', data: null, }; } try { const r = await axiosInstance_ts_1.useAxios.get(this.endpoints.webhookAuth.url, { headers: { Authorization: 'Basic ' + this.getVivaBasicToken(), }, }); if (!r.data || !r.data.Key) { return { success: false, message: 'Failed to get Viva webhook code', code: 'webhookerror', data: null, }; } return { success: true, message: 'Viva webhook code fetched', data: r.data.Key, }; } catch (e) { console.error('Viva Webhook Code Error', e); return { success: false, message: 'Failed to get Viva webhook code', code: 'webhookerror', data: null, }; } } } exports.VivaAuth = VivaAuth; // ------------------------------------------------------------ class VivaAuthISV extends VivaSkull_class_1.default { getVivaToken; getVivaWebhookCode; constructor(datas) { const adaptedDatas = { ...datas, merchantId: 'null', apikey: 'null', }; super(adaptedDatas); this.init(); const auth = new VivaAuth(adaptedDatas); this.getVivaToken = auth.getVivaToken.bind(auth); this.getVivaWebhookCode = auth.getVivaWebhookCode.bind(auth); } /** Credentials verification, `throw` error if credentials is not gived ***(required for API calls)*** */ async init() { console.log('Init viva'); if (!this.clientId || !this.clientSecret) throw new Error('Credentials not provided'); } } exports.VivaAuthISV = VivaAuthISV;