- ✓An active project attempting eSewa ePay v2 integration
- ✓Access to your signature generation code in PHP, Node.js, or Python
🚀 What You Will Learn & Build
- ▸The exact string concatenation formula expected by eSewa v2
- ▸Why float to string formatting breaks the HMAC hash
- ▸The secret key binary vs string encoding trap in PHP and Node.js
- ▸How to debug your hash using standard CLI tools
The 5 Reasons eSewa Rejects Your Signature
Verified Working Implementation (Node.js & PHP)
const crypto = require("crypto");
function generateEsewaSignature(secretKey, totalAmount, transactionUuid, productCode) {
// 1. Format totalAmount consistently
const formattedAmount = Number(totalAmount).toFixed(2);
// 2. Build exact concatenation string
const dataToSign = `total_amount=${formattedAmount},transaction_uuid=${transactionUuid},product_code=${productCode}`;
// 3. Generate raw binary HMAC-SHA256 and convert to base64
const hmac = crypto.createHmac("sha256", secretKey.trim());
hmac.update(dataToSign);
const signature = hmac.digest("base64");
return { signature, formattedAmount, dataToSign };
}
// Test with eSewa Sandbox values
const { signature, dataToSign } = generateEsewaSignature(
"8gBm/:&EnhH.1/q",
100,
"241018-001",
"EPAYTEST"
);
console.log("Input:", dataToSign);
console.log("Valid Signature:", signature);✨ Expected Output & Verification
Immediate elimination of the 'Invalid Signature' error screen and smooth progression to the eSewa login view.
🧪 How to Test the Implementation
- 1Inspect the generated signature using the Node.js test script above
- 2Ensure form inputs in your Blade or React view exactly match the dataToSign string
- 3Verify .env secret key has no trailing space using trim(env('ESEWA_SECRET_KEY'))
⚠️ Common Errors & How to Fix Them
Root Cause: Default hash_hmac outputs 64 hex characters. eSewa expects base64 encoded 32-byte raw binary.
Fix: Pass true as the 4th argument in hash_hmac('sha256', $data, $key, true).
🛡️ Security Considerations & Best Practices
- !Never generate signatures on the client side in JavaScript. The secret key must remain private to your server.
❓ Frequently Asked Questions (FAQ)
The legacy eSewa v1 API used MD5 which is cryptographically broken and vulnerable to collision attacks. ePay v2 adopted modern HMAC-SHA256 to ensure authenticity and integrity.
📌 Summary of Key Takeaways
The eSewa Invalid Signature error is always caused by data concatenation mismatches, float formatting differences, or missing raw binary flags in HMAC generation.
Official References & Further Reading
Written by Manoj Neupane
AuthorSoftware developer and programming instructor, Nepal
Building software systems and SaaS platforms with Laravel, Next.js, and Node.js. Passionate about helping students and developers in Nepal master practical, real-world development skills.