@muhammedaksam/sipay-node
Version:
Node.js TypeScript SDK for Sipay payment gateway
178 lines • 5.67 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateHashKey = generateHashKey;
exports.validateCreditCard = validateCreditCard;
exports.formatAmount = formatAmount;
exports.generateInvoiceId = generateInvoiceId;
exports.validatePaymentData = validatePaymentData;
exports.maskCreditCard = maskCreditCard;
exports.parseSipayError = parseSipayError;
const crypto = __importStar(require("crypto"));
/**
* Utility functions for Sipay SDK
*/
/**
* Generate hash key for payment requests
* Based on Sipay documentation requirements
*/
function generateHashKey(merchantKey, invoiceId, amount, secretKey) {
const hashString = `${merchantKey}${invoiceId}${amount}${secretKey}`;
return crypto.createHash('sha256').update(hashString).digest('hex');
}
/**
* Validate credit card number using Luhn algorithm
*/
function validateCreditCard(cardNumber) {
const num = cardNumber.replace(/\D/g, '');
// Check if empty or less than 13 digits
if (num.length < 13 || num.length > 19) {
return false;
}
let sum = 0;
let isEven = false;
for (let i = num.length - 1; i >= 0; i--) {
let digit = parseInt(num.charAt(i), 10);
if (isEven) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
isEven = !isEven;
}
return sum % 10 === 0;
}
/**
* Format currency amount for Sipay API
*/
function formatAmount(amount) {
return amount.toFixed(2);
}
/**
* Generate a unique invoice ID
*/
function generateInvoiceId(prefix = 'INV') {
const timestamp = Date.now();
const random = Math.floor(Math.random() * 10000);
return `${prefix}${timestamp}${random}`;
}
/**
* Validate required payment fields
*/
function validatePaymentData(data) {
const errors = [];
const requiredFields = [
'cc_holder_name',
'cc_no',
'expiry_month',
'expiry_year',
'currency_code',
'invoice_id',
'total',
'name',
'surname',
'items',
];
for (const field of requiredFields) {
if (!data[field]) {
errors.push(`Missing required field: ${field}`);
}
}
// Validate credit card
if (data.cc_no && !validateCreditCard(data.cc_no)) {
errors.push('Invalid credit card number');
}
// Validate amount
if (data.total && (isNaN(data.total) || data.total <= 0)) {
errors.push('Invalid amount');
}
// Validate currency
const validCurrencies = ['TRY', 'USD', 'EUR'];
if (data.currency_code && !validCurrencies.includes(data.currency_code)) {
errors.push('Invalid currency code');
}
// Validate expiry
if (data.expiry_month) {
const month = parseInt(data.expiry_month, 10);
if (isNaN(month) || month < 1 || month > 12) {
errors.push('Invalid expiry month');
}
}
if (data.expiry_year) {
const year = parseInt(data.expiry_year, 10);
const currentYear = new Date().getFullYear();
if (isNaN(year) || year < currentYear || year > currentYear + 20) {
errors.push('Invalid expiry year');
}
}
return errors;
}
/**
* Mask credit card number for logging
*/
function maskCreditCard(cardNumber) {
const num = cardNumber.replace(/\D/g, '');
if (num.length <= 3) {
return '*'.repeat(num.length);
}
if (num.length <= 7) {
const firstThree = num.substring(0, 3);
const middle = '*'.repeat(num.length - 4);
const lastOne = num.substring(num.length - 1);
return `${firstThree}${middle}${lastOne}`;
}
const firstFour = num.substring(0, 4);
const lastFour = num.substring(num.length - 4);
const middle = '*'.repeat(Math.max(0, num.length - 8));
return `${firstFour}${middle}${lastFour}`;
}
/**
* Parse Sipay error response
*/
function parseSipayError(error) {
if (error.response?.data) {
return {
code: error.response.data.status_code || 0,
message: error.response.data.status_description || error.response.data.message || 'Unknown error',
};
}
return {
code: 0,
message: error.message || 'Network error',
};
}
//# sourceMappingURL=index.js.map