A Node.js app to integrate Payhere.lk payment gateway using Netlify Functions & Fauna DB.
- Clone the repo & run
npm install
to install the dependencies. - Setup the Fauna DB connection & create the collection & index.
- Add Environment Variables to
.env
file.
To ensure the seamless functioning of the entire funnel, follow these steps:
- Execute both
netlify dev
and set upngrok
to examine the complete process. This is necessary as Payhere requires a public-facing URL for thenotify_url
. - If the repository is already linked to a Netlify site, simply run
netlify dev --live
without the need for ngrok. You can usenetlify link
to connect the repository and the Netlify site efficiently.
- Create a Database in US Region Group.
- Go to the Database > Security > Create a secret key with the server role.
- Add the key to the
.env
file asFAUNA_SECRET_KEY
- Run below to create the collection
payhere-demo-orders
& indexorders_by_order_id
node faunadb/faunaSetup.js
- Generate
.env
file from.env.example
and add your env variables.cp .env.example .env
- If new keys are added to the
.env
file, run below command to update the example file with new keys.sed 's/=.*/=/' .env > .env.example
1. Why Netlify Functions?
- 🔐 Security: The Payhere Merchant Secret is required for generating the hash. Performing this operation on the front-end poses security risks. Netlify Functions act as a secure backend for handling the hash generation process.
- 📡 Server Callback: Netlify Functions listen to payment notifications from PayHere, facilitating the passing of relevant information to the front-end.
2. Why Fauna DB?
- 🏦 Payment Status Handling: Since PayHere doesn't provide payment status parameters to the
return_url
during customer redirection, updating the database upon fetching payment status via yournotify_url
script is crucial. - 🏢 Modern Database Solution: Fauna DB is a cloud-native database with a flexible document data model. It seamlessly integrates semi-structured data with powerful relational features, including foreign keys, views, and joins.
- 🔁 Efficient Data Retrieval: Integration with Fauna DB allows you to efficiently retrieve payment status & display to customers on the
return_url
page.