paiementfactory
Version:
Unified SDK for multiple payment gateways (Easebuzz, Razorpay, etc.)
151 lines (147 loc) • 4.71 kB
JavaScript
;
const crypto = require('crypto');
const Razorpay = require('razorpay');
const PaymentGateway = require('../interfaces/PaymentGateway');
const {
GATEWAYS,
GATEWAY_STATUS
} = require('../utils/constants');
class RazorpayGateway extends PaymentGateway {
constructor(config) {
super();
this.initialize(config);
}
initialize(config) {
this.config = config;
}
async initiatePayment(data) {
const {
RAZORPAY_KEY,
RAZORPAY_SECRET
} = this.config;
const razorpay = new Razorpay({
key_id: RAZORPAY_KEY,
key_secret: RAZORPAY_SECRET
});
try {
const {
amount,
user,
description,
callback_url,
notes = {}
} = data;
const payload = {
amount: Number(amount) * 100,
currency: 'INR',
accept_partial: false,
description: description || 'Payment for your order',
customer: {
name: user === null || user === void 0 ? void 0 : user.name,
email: user === null || user === void 0 ? void 0 : user.email,
contact: user === null || user === void 0 ? void 0 : user.phone_number
},
notify: {
sms: true,
email: true
},
reminder_enable: true,
notes: notes
};
if (callback_url) {
payload.callback_url = callback_url;
payload.callback_method = 'get'; // safer default
}
const response = await razorpay.paymentLink.create(payload);
if (response && response.status === 'created') {
return {
success: true,
gateway: GATEWAYS.RAZORYPAY,
code: GATEWAY_STATUS.PAYMENT_INITIATED,
data: {
instrumentResponse: {
redirectInfo: {
url: response.short_url
},
...response
}
},
err_msg: null
};
} else {
var _response$error, _response$error2;
return {
success: false,
gateway: GATEWAYS.RAZORYPAY,
code: GATEWAY_STATUS.PAYMENT_INITIATE_FAILED,
error_desc: (response === null || response === void 0 || (_response$error = response.error) === null || _response$error === void 0 ? void 0 : _response$error.description) || 'Unknown error',
error_msg: (response === null || response === void 0 || (_response$error2 = response.error) === null || _response$error2 === void 0 ? void 0 : _response$error2.code) || 'UNKNOWN',
data: null
};
}
} catch (error) {
var _error$error, _error$error2;
return {
success: false,
gateway: GATEWAYS.RAZORYPAY,
code: GATEWAY_STATUS.PAYMENT_INITIATE_FAILED,
error_desc: (error === null || error === void 0 || (_error$error = error.error) === null || _error$error === void 0 ? void 0 : _error$error.description) || error.message || 'Unhandled exception',
error_msg: (error === null || error === void 0 || (_error$error2 = error.error) === null || _error$error2 === void 0 ? void 0 : _error$error2.code) || 'EXCEPTION',
data: null
};
}
}
async checkPaymentStatus(data) {
const {
RAZORPAY_KEY,
RAZORPAY_SECRET
} = this.config;
const razorpay = new Razorpay({
key_id: RAZORPAY_KEY,
key_secret: RAZORPAY_SECRET
});
try {
const {
paymentLinkId
} = data;
const response = await razorpay.paymentLink.fetch(paymentLinkId);
let checkPaymentResponse = {
success: true,
gateway: GATEWAYS.RAZORYPAY,
code: GATEWAY_STATUS.STATUS_FETCHED,
data: {
...response
}
};
return checkPaymentResponse;
} catch (error) {
return {
success: false,
gateway: GATEWAYS.RAZORYPAY,
code: GATEWAY_STATUS.STATUS_FETCH_FAILED,
error_desc: (error === null || error === void 0 ? void 0 : error.statusCode) == 404 ? 'Transaction not found' : (error === null || error === void 0 ? void 0 : error.message) || error,
error_msg: 'Error',
data: null
};
}
}
// * Function to generate hmac-sha256 for succesful payment
generateHMAC(text, secret) {
let hmac = crypto.createHmac('sha256', secret);
//passing the data to be hashed
data = hmac.update(text);
//Creating the hmac in the required format
gen_hmac = data.digest('hex');
return gen_hmac;
}
validateSignature(data) {
const {
text,
secret,
signature
} = data;
const generated_signature = this.generateHMAC(text, secret);
return generated_signature == signature;
}
}
module.exports = RazorpayGateway;