UNPKG

pawapay-nodejs-sdk

Version:

Unofficial Node.js SDK for the PawaPay Merchant API. Simplify Mobile Money integrations (Deposits, Payouts, Refunds) with this type-safe TypeScript client.

129 lines (128 loc) 6.84 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.PawaPayClient = void 0; const axios_1 = __importDefault(require("axios")); const uuid_1 = require("uuid"); const utils_1 = require("./utils"); class PawaPayClient { constructor(config) { if (!config || !config.apiToken || !config.baseUrl) { throw new Error('PawaPay configuration (apiToken, baseUrl) is required.'); } this.apiToken = config.apiToken; const resolvedBaseUrl = (0, utils_1.getBaseUrl)(config.baseUrl); this.axiosInstance = axios_1.default.create({ baseURL: resolvedBaseUrl, headers: { 'Authorization': `Bearer ${this.apiToken}`, 'Content-Type': 'application/json', }, }); } /** * Requests a deposit payment. * @param payload - The deposit details. depositId and customerTimestamp are optional. * @returns Promise resolving with the PawaPay deposit response. */ requestDeposit(payload) { return __awaiter(this, void 0, void 0, function* () { const depositData = Object.assign(Object.assign({}, payload), { depositId: payload.depositId || (0, uuid_1.v4)(), customerTimestamp: payload.customerTimestamp || new Date().toISOString() }); try { const response = yield this.axiosInstance.post('/deposits', depositData); return response.data; } catch (error) { this.handleApiError(error, 'Deposit Request Failed'); // Note: handleApiError throws, so this line technically won't be reached, // but needed for type checking unless handleApiError has return type 'never' throw error; } }); } // --- Placeholder for future methods --- // async requestPayout(payload: PayoutPayload): Promise<PayoutResponse> { // // Implementation later // } // async requestRefund(payload: RefundPayload): Promise<RefundResponse> { // // Implementation later // } // --- End Placeholder --- handleApiError(error, context) { var _a, _b, _c, _d, _e, _f; if (axios_1.default.isAxiosError(error)) { const axiosError = error; // Use 'any' or define a PawaPayErrorResponse type console.error(`PawaPay API Error (${context}): Status ${(_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.status}`, ((_b = axiosError.response) === null || _b === void 0 ? void 0 : _b.data) || axiosError.message); // Extract specific PawaPay rejection reason if available const rejectionReason = (_d = (_c = axiosError.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.rejectionReason; const errorMessage = rejectionReason ? `PawaPay rejected (${rejectionReason.code}): ${rejectionReason.message}` : `PawaPay API request failed with status ${(_e = axiosError.response) === null || _e === void 0 ? void 0 : _e.status}: ${JSON.stringify((_f = axiosError.response) === null || _f === void 0 ? void 0 : _f.data)}`; throw new Error(errorMessage); } else { console.error(`Unknown Error (${context}):`, error); throw new Error(`An unknown error occurred during the PawaPay request: ${error instanceof Error ? error.message : String(error)}`); } } /** * Requests a payout transfer. (Not Yet Implemented) * @param payload - The payout details. payoutId and customerTimestamp are optional. * @returns Promise resolving with the PawaPay payout response. */ requestPayout(payload) { return __awaiter(this, void 0, void 0, function* () { // TODO: Implement actual API call logic console.warn("requestPayout is not fully implemented yet."); const payoutData = Object.assign(Object.assign({}, payload), { payoutId: payload.payoutId || (0, uuid_1.v4)(), customerTimestamp: payload.customerTimestamp || new Date().toISOString() }); try { // Placeholder for the actual call structure const response = yield this.axiosInstance.post('/payouts', payoutData); return response.data; // Return mock data or actual response if implemented } catch (error) { this.handleApiError(error, 'Payout Request Failed'); throw error; // Re-throw after handling } // Temporary throw until implemented // throw new Error("requestPayout method not implemented."); }); } /** * Requests a refund for a previous deposit. (Not Yet Implemented) * @param payload - The refund details. refundId is optional. Amount is optional for full refund. * @returns Promise resolving with the PawaPay refund response. */ requestRefund(payload) { return __awaiter(this, void 0, void 0, function* () { // TODO: Implement actual API call logic console.warn("requestRefund is not fully implemented yet."); const refundData = Object.assign(Object.assign({}, payload), { refundId: payload.refundId || (0, uuid_1.v4)() }); try { // Placeholder for the actual call structure const response = yield this.axiosInstance.post('/refunds', refundData); return response.data; // Return mock data or actual response if implemented } catch (error) { this.handleApiError(error, 'Refund Request Failed'); throw error; // Re-throw after handling } // Temporary throw until implemented // throw new Error("requestRefund method not implemented."); }); } } exports.PawaPayClient = PawaPayClient; // Export the client class as the main export exports.default = PawaPayClient;