pawapay-nodejs-sdk
Version:
Unofficial Node.js SDK for the PawaPay Merchant API v2. Simplify Mobile Money integrations (Deposits, Payouts, Refunds, status checks, provider prediction) with this type-safe TypeScript client.
204 lines (203 loc) • 8.77 kB
JavaScript
"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',
},
});
}
// --- Deposits ---
/**
* Initiates a deposit (collecting a payment from a customer).
* @param payload - The deposit details. depositId is optional; the SDK generates a UUIDv4 if omitted.
*/
requestDeposit(payload) {
return __awaiter(this, void 0, void 0, function* () {
const depositData = Object.assign(Object.assign({}, payload), { depositId: payload.depositId || (0, uuid_1.v4)() });
try {
const response = yield this.axiosInstance.post('/v2/deposits', depositData);
return response.data;
}
catch (error) {
this.handleApiError(error, 'Deposit Request Failed');
}
});
}
/** Checks the current status of a previously initiated deposit. */
checkDepositStatus(depositId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.axiosInstance.get(`/v2/deposits/${depositId}`);
return response.data;
}
catch (error) {
this.handleApiError(error, 'Deposit Status Check Failed');
}
});
}
// --- Payouts ---
/**
* Initiates a payout (sending money to a customer).
* @param payload - The payout details. payoutId is optional; the SDK generates a UUIDv4 if omitted.
*/
requestPayout(payload) {
return __awaiter(this, void 0, void 0, function* () {
const payoutData = Object.assign(Object.assign({}, payload), { payoutId: payload.payoutId || (0, uuid_1.v4)() });
try {
const response = yield this.axiosInstance.post('/v2/payouts', payoutData);
return response.data;
}
catch (error) {
this.handleApiError(error, 'Payout Request Failed');
}
});
}
/** Checks the current status of a previously initiated payout. */
checkPayoutStatus(payoutId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.axiosInstance.get(`/v2/payouts/${payoutId}`);
return response.data;
}
catch (error) {
this.handleApiError(error, 'Payout Status Check Failed');
}
});
}
/** Cancels a payout that is still in the ENQUEUED state. */
cancelEnqueuedPayout(payoutId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.axiosInstance.post(`/v2/payouts/fail-enqueued/${payoutId}`);
return response.data;
}
catch (error) {
this.handleApiError(error, 'Cancel Enqueued Payout Failed');
}
});
}
// --- Refunds ---
/**
* Initiates a refund for a previous deposit.
* @param payload - The refund details. refundId is optional; the SDK generates a UUIDv4 if omitted.
*/
requestRefund(payload) {
return __awaiter(this, void 0, void 0, function* () {
const refundData = Object.assign(Object.assign({}, payload), { refundId: payload.refundId || (0, uuid_1.v4)() });
try {
const response = yield this.axiosInstance.post('/v2/refunds', refundData);
return response.data;
}
catch (error) {
this.handleApiError(error, 'Refund Request Failed');
}
});
}
/** Checks the current status of a previously initiated refund. */
checkRefundStatus(refundId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.axiosInstance.get(`/v2/refunds/${refundId}`);
return response.data;
}
catch (error) {
this.handleApiError(error, 'Refund Status Check Failed');
}
});
}
/** Cancels a refund that is still in the ENQUEUED state. */
cancelEnqueuedRefund(refundId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.axiosInstance.post(`/v2/refunds/fail-enqueued/${refundId}`);
return response.data;
}
catch (error) {
this.handleApiError(error, 'Cancel Enqueued Refund Failed');
}
});
}
// --- Toolkit ---
/** Retrieves the countries, providers, and currencies active on your account. */
getActiveConfiguration(query) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.axiosInstance.get('/v2/active-conf', {
params: query,
});
return response.data;
}
catch (error) {
this.handleApiError(error, 'Active Configuration Request Failed');
}
});
}
/** Predicts the mobile money provider for a given phone number (including country code). */
predictProvider(phoneNumber) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.axiosInstance.post('/v2/predict-provider', {
phoneNumber,
});
return response.data;
}
catch (error) {
this.handleApiError(error, 'Predict Provider Request Failed');
}
});
}
/** Retrieves your account's wallet balances, optionally filtered by country. */
getWalletBalances(country) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.axiosInstance.get('/v2/wallet-balances', {
params: country ? { country } : undefined,
});
return response.data;
}
catch (error) {
this.handleApiError(error, 'Wallet Balances Request Failed');
}
});
}
handleApiError(error, context) {
var _a, _b, _c, _d;
if (axios_1.default.isAxiosError(error)) {
const axiosError = error;
const failureReason = (_b = (_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.failureReason;
const errorMessage = failureReason
? `PawaPay rejected (${failureReason.code}): ${failureReason.message}`
: `PawaPay API request failed with status ${(_c = axiosError.response) === null || _c === void 0 ? void 0 : _c.status}: ${JSON.stringify((_d = axiosError.response) === null || _d === void 0 ? void 0 : _d.data)}`;
throw new Error(errorMessage);
}
throw new Error(`An unknown error occurred during the PawaPay request: ${error instanceof Error ? error.message : String(error)}`);
}
}
exports.PawaPayClient = PawaPayClient;
// Export the client class as the main export
exports.default = PawaPayClient;