neppayments
Version:
A simple and easy-to-use package for integrating Nepali payment gateways (Khalti and eSewa) into your applications
106 lines (105 loc) • 4.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateAmount = validateAmount;
exports.validateUrl = validateUrl;
exports.validateRequiredField = validateRequiredField;
exports.validateCustomerInfo = validateCustomerInfo;
exports.validateAmountBreakdown = validateAmountBreakdown;
exports.validateProductDetails = validateProductDetails;
const PaymentError_1 = require("../errors/PaymentError");
/**
* Validate payment amount
* @param amount Amount in paisa
* @param gateway Payment gateway name
*/
function validateAmount(amount, gateway) {
if (typeof amount !== 'number' || isNaN(amount)) {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.INVALID_AMOUNT, 'Payment amount must be a valid number', gateway);
}
if (amount < 1000) {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.INVALID_AMOUNT, 'Amount should be greater than Rs. 10 (1000 paisa)', gateway);
}
}
/**
* Validate URL
* @param url URL to validate
* @param fieldName Name of the field for error message
* @param gateway Payment gateway name
*/
function validateUrl(url, fieldName, gateway) {
try {
new URL(url);
}
catch (_a) {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.INVALID_URL, `Invalid ${fieldName} URL: ${url}. Must be a valid absolute URL`, gateway);
}
}
/**
* Validate required field
* @param value Field value
* @param fieldName Name of the field
* @param gateway Payment gateway name
*/
function validateRequiredField(value, fieldName, gateway) {
if (value === undefined || value === null || value === '') {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, `Missing required field: ${fieldName}`, gateway, { field: fieldName });
}
}
/**
* Validate customer info
* @param customerInfo Customer information
* @param gateway Payment gateway name
*/
function validateCustomerInfo(customerInfo, gateway) {
if (customerInfo.name && typeof customerInfo.name !== 'string') {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Customer name must be a string', gateway);
}
if (customerInfo.email && typeof customerInfo.email !== 'string') {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Customer email must be a string', gateway);
}
if (customerInfo.phone && typeof customerInfo.phone !== 'string') {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Customer phone must be a string', gateway);
}
}
/**
* Validate amount breakdown
* @param amountBreakdown Amount breakdown array
* @param totalAmount Total amount
* @param gateway Payment gateway name
*/
function validateAmountBreakdown(amountBreakdown, totalAmount, gateway) {
if (!Array.isArray(amountBreakdown)) {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Amount breakdown must be an array', gateway);
}
const sum = amountBreakdown.reduce((acc, item) => acc + item.amount, 0);
if (sum !== totalAmount) {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.AMOUNT_MISMATCH, 'Sum of amount breakdown must equal total amount', gateway);
}
}
/**
* Validate product details
* @param productDetails Product details array
* @param gateway Payment gateway name
*/
function validateProductDetails(productDetails, gateway) {
if (!Array.isArray(productDetails)) {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Product details must be an array', gateway);
}
for (const product of productDetails) {
if (typeof product.identity !== 'string') {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Product identity must be a string', gateway);
}
if (typeof product.name !== 'string') {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Product name must be a string', gateway);
}
if (typeof product.total_price !== 'number' || isNaN(product.total_price)) {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Product total price must be a number', gateway);
}
if (typeof product.quantity !== 'number' || isNaN(product.quantity)) {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Product quantity must be a number', gateway);
}
if (typeof product.unit_price !== 'number' || isNaN(product.unit_price)) {
throw new PaymentError_1.PaymentError(PaymentError_1.PaymentErrorCode.VALIDATION_ERROR, 'Product unit price must be a number', gateway);
}
}
}