zips-react-native-sdk-test
Version:
Lightweight ZIPS Payment Gateway SDK for React Native - Complete payment solution with card payments, wallet payments (AfrMoney & ZApp), netbanking, and native UI design
157 lines (156 loc) • 4.12 kB
JavaScript
/**
* Default currency for the ZIPS payment system
*/
export const DEFAULT_CURRENCY = 'GMD';
/**
* Get default currency
*/
export const getDefaultCurrency = () => {
return DEFAULT_CURRENCY;
};
/**
* Format amount for display
*/
export const formatAmount = (amount, currency = DEFAULT_CURRENCY) => {
return `${currency} ${(amount / 100).toFixed(2)}`;
};
/**
* Format date for display
*/
export const formatDate = (dateString) => {
const date = new Date(dateString);
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
};
/**
* Validate account number
*/
export const validateAccountNumber = (account) => {
const cleanAccount = account.replace(/\s/g, '');
return cleanAccount.length >= 10 && /^\d+$/.test(cleanAccount);
};
/**
* Validate card number using Luhn algorithm
*/
export const validateCardNumber = (cardNumber) => {
const num = cardNumber.replace(/\D/g, '');
if (num.length < 13 || num.length > 19) {
return false;
}
let sum = 0;
let shouldDouble = false;
for (let i = num.length - 1; i >= 0; i--) {
let digit = parseInt(num.charAt(i), 10);
if (shouldDouble) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
shouldDouble = !shouldDouble;
}
return sum % 10 === 0;
};
/**
* Format card number with spaces
*/
export const formatCardNumber = (text) => {
const cleaned = text.replace(/\D/g, '');
const formatted = cleaned.replace(/(\d{4})(?=\d)/g, '$1 ');
return formatted;
};
/**
* Format account number with spaces
*/
export const formatAccountNumber = (text) => {
const cleaned = text.replace(/\D/g, '');
const formatted = cleaned.replace(/(\d{4})(?=\d)/g, '$1 ');
return formatted;
};
/**
* Validate phone number
*/
export const validatePhoneNumber = (phone) => {
const cleanPhone = phone.replace(/\D/g, '');
return cleanPhone.length >= 7 && cleanPhone.length <= 15;
};
/**
* Validate email address
*/
export const validateEmail = (email) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};
/**
* Generate UUID v4
*/
export const generateUUID = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};
/**
* Delay function for retries
*/
export const delay = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
/**
* Retry function with exponential backoff
*/
export const retryWithBackoff = async (fn, maxRetries = 3, baseDelay = 1000) => {
let lastError;
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
}
catch (error) {
lastError = error;
if (i === maxRetries - 1) {
throw lastError;
}
const delayTime = baseDelay * Math.pow(2, i);
await delay(delayTime);
}
}
throw lastError;
};
/**
* Check if error is network related
*/
export const isNetworkError = (error) => {
var _a, _b;
return (error.code === 'NETWORK_ERROR' ||
((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Network Error')) ||
((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('Failed to fetch')));
};
/**
* Sanitize input for security
*/
export const sanitizeInput = (input) => {
return input.trim().replace(/[<>\"']/g, '');
};
/**
* Deep clone object
*/
export const deepClone = (obj) => {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (obj instanceof Date) {
return new Date(obj.getTime());
}
if (obj instanceof Array) {
return obj.map((item) => deepClone(item));
}
if (typeof obj === 'object') {
const cloned = {};
Object.keys(obj).forEach((key) => {
cloned[key] = deepClone(obj[key]);
});
return cloned;
}
return obj;
};