UNPKG

palmpaywrappersdk

Version:

PalmPay Wrapper Sdk

89 lines (88 loc) 4.09 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.PayWithWalletService = void 0; require("reflect-metadata"); const axios_1 = __importDefault(require("axios")); const Util_1 = require("../Utils/Util"); const Util_2 = require("../Utils/Util"); const EndpointsConstant_1 = require("../EndpointsConstant"); class PayWithWalletService { constructor(appId, countryCode, privateKey, isTest) { this.appId = appId; this.countryCode = countryCode; this.privateKey = privateKey; this.isTest = isTest; this.appId = appId; this.countryCode = countryCode; this.privateKey = privateKey; this.isTest = isTest; } getCountryCode() { return this.countryCode; } getAppId() { return this.appId; } getPrivateKey() { return this.privateKey; } getIsTest() { return this.isTest; } createOrder(builder) { return __awaiter(this, void 0, void 0, function* () { const createOrderRequest = builder.build(); try { const createOrderEndpoint = this.isTest ? EndpointsConstant_1.API_ENDPOINTS_SANDBOX.CREATE_ORDER : EndpointsConstant_1.API_ENDPOINTS_LIVE.CREATE_ORDER; const signedRequest = (0, Util_1.createSign)(builder, this.privateKey); const header = (0, Util_2.createHeader)(this.countryCode, signedRequest, 'Bearer '.concat(this.appId), 'application/json'); const response = yield axios_1.default.post(createOrderEndpoint, createOrderRequest.toJSON(), { headers: header }); console.log("Order Response Data: ", response.data); return response.data; } catch (error) { if (axios_1.default.isAxiosError(error)) { throw new Error(`Error message: ${error.message}`); } else { console.log("Unexpected error", error); throw new Error; } } }); } fetchOrderStatus(builder) { return __awaiter(this, void 0, void 0, function* () { const orderStatusRequest = builder.build(); console.log(`Order Request: ${orderStatusRequest.toJSON}`); try { const orderStatusEndpoint = this.isTest ? EndpointsConstant_1.API_ENDPOINTS_SANDBOX.ORDER_STATUS : EndpointsConstant_1.API_ENDPOINTS_LIVE.ORDER_STATUS; const signedRequest = (0, Util_1.createSign)(builder, this.privateKey); const header = (0, Util_2.createHeader)(this.countryCode, signedRequest, 'Bearer '.concat(this.appId), 'application/json'); const response = yield axios_1.default.post(orderStatusEndpoint, orderStatusRequest.toJSON(), { headers: header }); return response.data; } catch (error) { if (axios_1.default.isAxiosError(error)) { throw new Error(`Error message: ${error.message}`); } else { throw new Error('Unexpected error'); } } }); } } exports.PayWithWalletService = PayWithWalletService;