UNPKG

@xmoney/api-sdk

Version:
76 lines 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.xMoneyApiService = void 0; const tslib_1 = require("tslib"); const qs_1 = tslib_1.__importDefault(require("qs")); const axios_1 = tslib_1.__importDefault(require("axios")); class xMoneyApiService { constructor(commonService) { this.getConfig = () => { return { baseURL: this.commonService.getApiBaseUrl(), headers: { Authorization: `Bearer ${this.commonService.secretKey}`, }, }; }; this.commonService = commonService; } async getCardsByxMoneyCustomerId(xMoneyCustomerId) { let baseQueryParams = `customerId=${xMoneyCustomerId}&hasToken=yes`; const response = await this.get(`card?${baseQueryParams}`); return response.data; } async createOrder(order) { const response = await this.post('order', qs_1.default.stringify(order), { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, }); return response.data; } async getOrderByExternalId(externalId) { const response = await this.get(`order?page=0&perPage=1&externalOrderId=${externalId}`); return response.data; } async getSessionToken() { const response = await this.get(`auth/jwt-token`); return response.data; } async get(url, config) { try { return await axios_1.default.get(url, Object.assign(Object.assign({}, config), this.getConfig())); } catch (error) { this.logErrorIfNeeded(error, url); return error === null || error === void 0 ? void 0 : error.response; } } async post(url, data, config) { try { return await axios_1.default.post(url, data, Object.assign(Object.assign({}, config), this.getConfig())); } catch (error) { this.logErrorIfNeeded(error, url); return error === null || error === void 0 ? void 0 : error.response; } } stringifyError(error) { var _a, _b, _c; if (typeof ((_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data) === 'object') { return JSON.stringify((_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.data); } return (_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.data; } logErrorIfNeeded(error, url) { if (!this.commonService.verbose) { return; } console.error('An error occurred while calling Twispay API Route', { route: url, error: this.stringifyError(error), }); } } exports.xMoneyApiService = xMoneyApiService; //# sourceMappingURL=xmoney-api.service.js.map