UNPKG

neppayments

Version:

A simple and easy-to-use package for integrating Nepali payment gateways (Khalti and eSewa) into your applications

109 lines (108 loc) 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PaymentError = exports.PaymentErrorCode = void 0; /** * Payment error codes */ var PaymentErrorCode; (function (PaymentErrorCode) { // Configuration errors PaymentErrorCode["INVALID_CONFIG"] = "INVALID_CONFIG"; PaymentErrorCode["GATEWAY_NOT_CONFIGURED"] = "GATEWAY_NOT_CONFIGURED"; // Validation errors PaymentErrorCode["VALIDATION_ERROR"] = "VALIDATION_ERROR"; PaymentErrorCode["INVALID_AMOUNT"] = "INVALID_AMOUNT"; PaymentErrorCode["INVALID_URL"] = "INVALID_URL"; // Authentication errors PaymentErrorCode["AUTHENTICATION_ERROR"] = "AUTHENTICATION_ERROR"; PaymentErrorCode["INVALID_CREDENTIALS"] = "INVALID_CREDENTIALS"; PaymentErrorCode["TOKEN_EXPIRED"] = "TOKEN_EXPIRED"; // Payment errors PaymentErrorCode["PAYMENT_FAILED"] = "PAYMENT_FAILED"; PaymentErrorCode["PAYMENT_EXPIRED"] = "PAYMENT_EXPIRED"; PaymentErrorCode["PAYMENT_CANCELED"] = "PAYMENT_CANCELED"; PaymentErrorCode["PAYMENT_ALREADY_COMPLETED"] = "PAYMENT_ALREADY_COMPLETED"; // Verification errors PaymentErrorCode["VERIFICATION_FAILED"] = "VERIFICATION_FAILED"; PaymentErrorCode["INVALID_TRANSACTION"] = "INVALID_TRANSACTION"; PaymentErrorCode["AMOUNT_MISMATCH"] = "AMOUNT_MISMATCH"; // Gateway errors PaymentErrorCode["GATEWAY_ERROR"] = "GATEWAY_ERROR"; PaymentErrorCode["GATEWAY_TIMEOUT"] = "GATEWAY_TIMEOUT"; PaymentErrorCode["GATEWAY_UNAVAILABLE"] = "GATEWAY_UNAVAILABLE"; })(PaymentErrorCode || (exports.PaymentErrorCode = PaymentErrorCode = {})); /** * Standard payment error class */ class PaymentError extends Error { constructor(code, message, gateway, details) { super(message); this.name = 'PaymentError'; this.code = code; this.gateway = gateway; this.details = details; // Maintain proper stack trace if (Error.captureStackTrace) { Error.captureStackTrace(this, PaymentError); } } /** * User-friendly error message */ get friendlyMessage() { switch (this.code) { case PaymentErrorCode.INVALID_CONFIG: return 'Invalid payment configuration. Please check your setup.'; case PaymentErrorCode.GATEWAY_NOT_CONFIGURED: return `Payment gateway ${this.gateway} is not configured.`; case PaymentErrorCode.VALIDATION_ERROR: return 'Validation error: ' + this.message; case PaymentErrorCode.INVALID_AMOUNT: return 'Amount should be greater than Rs. 10 (1000 paisa).'; case PaymentErrorCode.INVALID_URL: return 'Invalid URL provided. Please provide valid return_url and website_url.'; case PaymentErrorCode.AUTHENTICATION_ERROR: return 'Authentication failed. Please check your credentials.'; case PaymentErrorCode.INVALID_CREDENTIALS: return 'Invalid credentials provided.'; case PaymentErrorCode.TOKEN_EXPIRED: return 'Authentication token has expired.'; case PaymentErrorCode.PAYMENT_FAILED: return 'Payment failed. Please try again.'; case PaymentErrorCode.PAYMENT_EXPIRED: return 'Payment link has expired. Please try again.'; case PaymentErrorCode.PAYMENT_CANCELED: return 'Payment was cancelled by the user.'; case PaymentErrorCode.PAYMENT_ALREADY_COMPLETED: return 'Payment has already been completed.'; case PaymentErrorCode.VERIFICATION_FAILED: return 'Payment verification failed.'; case PaymentErrorCode.INVALID_TRANSACTION: return 'Invalid transaction.'; case PaymentErrorCode.AMOUNT_MISMATCH: return 'Payment amount does not match.'; case PaymentErrorCode.GATEWAY_ERROR: return `Payment gateway error: ${this.message}`; case PaymentErrorCode.GATEWAY_TIMEOUT: return 'Payment gateway is not responding. Please try again later.'; case PaymentErrorCode.GATEWAY_UNAVAILABLE: return 'Payment gateway is currently unavailable.'; default: return this.message || 'An unknown error occurred.'; } } /** * Convert error to JSON for logging/response */ toJSON() { return { name: this.name, code: this.code, message: this.message, friendlyMessage: this.friendlyMessage, gateway: this.gateway, details: this.details }; } } exports.PaymentError = PaymentError;