This repository demonstrates how to integrate eSewa payment gateway into your Node.js application using Express. It includes functionality for generating a secure signature for payment transactions and submitting a payment form to the eSewa API.
- Generate secure HMAC-SHA256 signatures for eSewa transactions.
- Create and submit dynamic payment forms to the eSewa gateway.
- Handle payment success and failure callbacks.
- Clone the repository:
git clone https://github.com/Sushank-ghimire/esewa-payment-integration.git
- Navigate to the project directory:
cd esewa-payment-integration
- Install dependencies:
npm install
- Start the server:
npm start
The application will run on http://localhost:3000
by default.
Generates a random 25-character string to be used as the transaction_uuid
for unique transaction identification.
function generateRandomString() {
const strings =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let code = "";
let length = 25;
for (let i = 0; i < length; i++) {
code += strings[Math.floor(Math.random() * strings.length)];
}
return code;
}
Generates an HMAC-SHA256 signature for secure transaction data.
function generateSignature(message, secret) {
const hash = crypto.HmacSHA256(message, secret);
const hashInBase64 = crypto.enc.Base64.stringify(hash);
return hashInBase64;
}
Renders a dynamic payment form with pre-filled values and the generated signature.
app.get("/pay-with-esewa", (req, res, next) => {
let order_price = req.query.price;
let tax_amount = 0;
let amount = order_price;
let transaction_uuid = generateRandomString();
let product_code = "EPAYTEST";
let product_service_charge = 0;
let product_delivery_charge = 0;
let secretKey = "8gBm/:&EnhH.1/q";
let signature = generateSignature(
`total_amount=${amount},transaction_uuid=${transaction_uuid},product_code=${product_code}`,
secretKey
);
res.send(`
<body>
<form style="display: flex; flex-direction: column; gap: 15px;" action="https://rc-epay.esewa.com.np/api/epay/main/v2/form" method="POST">
<input type="text" id="amount" name="amount" value="${amount}" required>
<input type="text" id="tax_amount" name="tax_amount" value ="${tax_amount}" required>
<input type="text" id="total_amount" name="total_amount" value="${amount}" required>
<input type="text" id="transaction_uuid" name="transaction_uuid" value="${transaction_uuid}" required>
<input type="text" id="product_code" name="product_code" value ="EPAYTEST" required>
<input type="text" id="product_service_charge" name="product_service_charge" value="${product_service_charge}" required>
<input type="text" id="product_delivery_charge" name="product_delivery_charge" value="${product_delivery_charge}" required>
<input type="text" id="success_url" name="success_url" value="http://localhost:3000/success" required>
<input type="text" id="failure_url" name="failure_url" value="http://localhost:3000/failure" required>
<input type="text" id="signed_field_names" name="signed_field_names" value="total_amount,transaction_uuid,product_code" required>
<input type="text" id="signature" name="signature" value="${signature}" required>
<input value="Submit" type="submit">
</form>
</body>
`);
});
Handles successful payment responses.
app.get("/success", (req, res) => {
return res
.status(201)
.json({ message: "Payment successful.", success: true });
});
Handles failed payment responses.
app.get("/failure", (req, res) => {
return res.status(400).json({ message: "Payment failed.", success: false });
});
- Navigate to
http://localhost:3000/pay-with-esewa?price=<amount>
. - Replace
<amount>
with the total price for the transaction. - The app dynamically generates a payment form with a secure signature.
- Submit the form to proceed with the payment via eSewa.
- Replace
success_url
andfailure_url
in the payment form with your production URLs for proper payment callbacks. - Use a secure, secret key (
secretKey
) in your environment for generating signatures. - Ensure that the
product_code
aligns with your eSewa account configuration.
This project is open-source and available under the MIT License.