payment-url-generator
Version:
A library for generating payment URLs Edfapay
144 lines (132 loc) • 4.78 kB
JavaScript
const axios = require('axios');
class PaymentUrlGenerator {
/**
* @param EDFA_PAY_MERCHANT_KEY required Merchant Key
* @param EDFA_PAY_PASSWORD required Merchant Password
* @param EDFA_PAY_RETURN_URL required Return URL for 3DS success/failure
*/
constructor(
EDFA_PAY_MERCHANT_KEY,
EDFA_PAY_PASSWORD,
EDFA_PAY_RETURN_URL,
) {
this.apiUrl = 'https://api.edfapay.com/payment/initiate';
this.action = 'SALE';
this.currency = 'SAR';
this.merchantKey = EDFA_PAY_MERCHANT_KEY;
this.merchantPassword = EDFA_PAY_PASSWORD;
this.termUrl3ds = EDFA_PAY_RETURN_URL;
this.payload = {};
}
generateHash(orderId, orderAmount, orderDescription) {
if (!orderId || !orderAmount || !orderDescription || !this.merchantPassword || !this.currency) {
if (!orderId) throw new Error('Order ID is required.');
if (!orderAmount) throw new Error('Order Amount is required.');
if (!orderDescription) throw new Error('Order Description is required.');
if (!this.merchantPassword) throw new Error('Merchant Password is required.');
if (!this.currency) throw new Error('Currency is required.');
throw new Error('Hash input is not correct or not complete.');
}
const input = [
orderId,
orderAmount,
this.currency,
orderDescription,
this.merchantPassword
].map(item => item.toUpperCase()).join('');
return require('crypto').createHash('sha1')
.update(require('crypto').createHash('md5').update(input).digest('hex'))
.digest('hex');
}
validatePayload() {
for (const [key, value] of Object.entries(this.payload)) {
if (!value) {
throw new Error(`Missing payload input: ${key}`);
}
}
}
preparePayload(orderId,
orderAmount,
orderDescription,
payerFirstName,
payerLastName,
payerEmail,
payerMobile,
payerIpAddress,
payerCity,
payerZip
) {
try {
const payload = {
action: this.action,
edfa_merchant_id: this.merchantKey,
order_id: orderId,
order_amount: orderAmount,
order_currency: this.currency,
order_description: orderDescription,
payer_first_name: payerFirstName,
payer_last_name: payerLastName,
payer_email: payerEmail,
payer_phone: payerMobile,
payer_ip: payerIpAddress,
term_url_3ds: this.termUrl3ds,
hash: this.generateHash(),
req_token: 'N',
recurring_init: 'N',
payer_country: 'SA',
payer_city: payerCity || 'Riyadh',
payer_zip: payerZip || '12221',
};
this.validatePayload();
return payload;
} catch (error) {
throw new Error(`Error preparing payload: ${error.message}`);
}
}
/**
* Generate payment URL
* @param orderId
* @param orderAmount
* @param orderDescription
* @param payerFirstName
* @param payerLastName
* @param payerEmail
* @param payerMobile
* @param payerIpAddress
* @param payerCity default Riyadh
* @param payerZip default 12221
* @return {Promise<string>} Payment URL
**/
async generate(
orderId,
orderAmount,
orderDescription,
payerFirstName,
payerLastName,
payerEmail,
payerMobile,
payerIpAddress,
payerCity = 'Riyadh',
payerZip = '12221',
) {
this.preparePayload(
orderId,
orderAmount,
orderDescription,
payerFirstName,
payerLastName,
payerEmail,
payerMobile,
payerIpAddress,
payerCity,
payerZip,
);
try {
const response = await axios.post(this.apiUrl, this.payload, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}});
return response.data.redirect_url;
} catch (error) {
throw new Error(`Error generating payment URL: ${error.response ? error.response.data : error.message}`);
}
}
}
module.exports = PaymentUrlGenerator;