mpesajs
Version:
A Node.js SDK for seamless integration with M-Pesa payment gateway, providing easy-to-use methods for handling transactions, payments, and API interactions
46 lines • 2.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StkPushErrorHandler = exports.StkPushError = void 0;
const ErrorHandlers_1 = require("./ErrorHandlers");
class StkPushError extends ErrorHandlers_1.MpesaError {
constructor(message, responseCode, merchantRequestId, checkoutRequestId) {
super(message);
this.responseCode = responseCode;
this.merchantRequestId = merchantRequestId;
this.checkoutRequestId = checkoutRequestId;
this.name = 'StkPushError';
}
}
exports.StkPushError = StkPushError;
class StkPushErrorHandler {
static handle(error) {
var _a, _b;
// Handle STK Push initiation errors
if (error === null || error === void 0 ? void 0 : error.ResponseCode) {
throw new StkPushError(error.ResponseDescription || error.CustomerMessage || 'STK Push error occurred', error.ResponseCode, error.MerchantRequestID, error.CheckoutRequestID);
}
// Handle STK Push callback errors
if ((_b = (_a = error === null || error === void 0 ? void 0 : error.Envelope) === null || _a === void 0 ? void 0 : _a.Body) === null || _b === void 0 ? void 0 : _b.stkCallback) {
const callback = error.Envelope.Body.stkCallback;
let message = '';
switch (callback.ResultCode) {
case 'TP40087':
message = 'User entered wrong M-PESA PIN. Please try again with correct PIN.';
break;
case '17':
message = 'M-PESA system internal error. Please try again after a few minutes.';
break;
default:
message = callback.ResultDesc;
}
throw new StkPushError(message, callback.ResultCode, callback.MerchantRequestID, callback.CheckoutRequestID);
}
// Handle network errors
if (error === null || error === void 0 ? void 0 : error.request) {
throw new ErrorHandlers_1.NetworkError('No response received from the API. Please check your network connection.');
}
throw new StkPushError('Unknown STK Push error occurred');
}
}
exports.StkPushErrorHandler = StkPushErrorHandler;
//# sourceMappingURL=StkPushError.js.map