This repository contains a complete implementation of eSewa payment gateway integration in a Next.js (React.js) application. The project includes a frontend payment form, backend APIs for payment initiation and verification, and success/failure pages. Built with TypeScript, Tailwind CSS, and Next.js App Router, this solution is ideal for accepting payments in Nepal-based applications, such as donations or e-commerce platforms.
- Payment Form: A responsive modal form for collecting user details (name, email, amount).
- Backend APIs: Secure APIs to initiate payments, verify transactions, and handle success/failure cases.
- Success/Failure Pages: User-friendly feedback pages with Tailwind CSS and Lucide icons.
- Security: Input validation, HMAC-SHA256 signature generation, and environment variable usage.
- Sandbox Testing**: Supports eSewa sandbox for safe testing.
Before setting up the project, ensure you have:
- Node.js (v16 or higher) and npm or yarn.
- A Next.js project (version 13 or later with App Router).
- An eSewa merchant account with a Merchant ID and Secret Key.
- Basic knowledge of React, Next.js, and TypeScript.
- Git installed for cloning the repository.
-
Clone the Repository:
git clone https://github.com/prashant523580/eSewa-payment-nextjs.git cd esewa-payment-nextjs -
Install Dependencies:
npm install
This installs required packages:
uuid,crypto-js, and@types/uuidfor TypeScript support. -
Set Up Environment Variables: Create a
.env.localfile in the root directory and add the following:ESEWA_MERCHANT_ID=your_merchant_id ESEWA_SECRET_KEY=your_secret_key AUTH_URL=http://localhost:3000 BASE_URL=https://rc-epay.esewa.com.np
- Replace
your_merchant_idandyour_secret_keywith credentials from eSewa. - Update
AUTH_URLto your production URL when deploying.
- Replace
Key files and directories:
components/EsewaPayment.tsx: Frontend payment form component.app/api/payment/initiate/route.ts: API to initiate payment.app/api/payment/success/route.ts: API to verify and handle successful payments.app/api/payment/failure/route.ts: API to handle failed payments.app/success/page.tsx: Success page UI.app/failure/page.tsx: Failure page UI.public/images/esewa_logo.png: eSewa logo for the payment button.
-
Run the Development Server:
npm run dev
Open
http://localhost:3000in your browser. -
Test the Payment Flow:
- Click the "Pay Via eSewa" button to open the payment form.
- Enter your name, email, and amount (in NPR).
- Submit the form to initiate payment via eSewa’s sandbox.
- Complete the payment in the eSewa interface.
- Verify redirection to the success or failure page.
-
Sandbox Testing:
- Use eSewa’s sandbox environment by updating the payment URL in
app/api/payment/initiate/route.tsto:paymentUrl: 'https://dev.esewa.com.np/epay/main' - Obtain sandbox credentials from eSewa’s developer portal.
- Use eSewa’s sandbox environment by updating the payment URL in
- Purpose: Collects user input and initiates payment.
- Key Features:
- Uses
useStatefor form state management. - Submits data to
/api/payment/initiate. - Dynamically creates a hidden form for eSewa redirection.
- Styled with Tailwind CSS, includes a modal and error handling.
- Uses
- Initiate Payment (
app/api/payment/initiate/route.ts):- Validates inputs and calculates 13% VAT.
- Generates a unique transaction ID with
uuid. - Creates an HMAC-SHA256 signature using
crypto-js. - Returns eSewa payment URL and parameters.
- Success Handler (
app/api/payment/success/route.ts):- Verifies transactions with eSewa’s API.
- Validates signatures for security.
- Redirects to the success page.
- Failure Handler (
app/api/payment/failure/route.ts):- Redirects to the failure page for unsuccessful transactions.
- Success Page (
app/success/page.tsx): Displays a confirmation message with a green checkmark (Lucide icon). - Failure Page (
app/failure/page.tsx): Shows an error message with a red X (Lucide icon).
-
Prepare for Production:
- Update
.envwith your productionAUTH_URL(e.g.,https://yourdomain.com). - Change the payment URL in
app/api/payment/initiate/route.tsto:paymentUrl: 'https://epay.esewa.com.np/api/epay/main/v2/form'
- Update
-
Deploy:
- Vercel:
Follow the prompts to deploy.
vercel
- Netlify or other platforms: Push to a Git repository and link to your hosting provider.
- Ensure environment variables are set in the hosting platform’s dashboard.
- Vercel:
-
Test in Production:
- Verify the payment flow with real eSewa credentials.
- Check success and failure redirects.
- Monitor for errors in your hosting platform’s logs.
- Security:
- Store sensitive data (e.g.,
ESEWA_SECRET_KEY) in environment variables. - Use HTTPS in production.
- Validate all inputs to prevent injection attacks.
- Store sensitive data (e.g.,
- Error Handling:
- Display user-friendly error messages.
- Log errors securely for debugging.
- User Experience:
- Ensure the form is mobile-responsive.
- Provide clear feedback during payment processing.
- Testing:
- Test edge cases (e.g., invalid amounts, network issues).
- Use eSewa’s sandbox to avoid real transactions during development.
- Payment Initiation Fails: Check
ESEWA_MERCHANT_IDandESEWA_SECRET_KEYin.env.local. - Redirect Issues: Verify
AUTH_URLmatches your domain. - Signature Errors: Ensure the signature message format matches eSewa’s requirements.
- CORS Errors: Test in a proper development environment, not a file-based browser preview.
- eSewa Developer Documentation
- Next.js Documentation
- Tailwind CSS Documentation
- Lucide Icons
- YouTube Tutorial
For questions or feedback, reach out via:
- GitHub Issues: Open an issue
- Email: [email protected]
- YouTube: Code Paila
Happy coding, and thank you for using this eSewa payment integration project!