This project provides integration with the PayMob payment gateway in a Node.js application, allowing you to process payments, retrieve transactions, and perform void and refund operations.
- Process payments securely using PayMob payment gateway
- Retrieve transaction details for analysis and reporting
- Perform void and refund operations on transactions
- Easy integration into Node.js applications with clean and readable code
- Clone the repository or download the source code.
- Install the required dependencies by running the following command:
npm install
- Create a
.env
file in the root directory of the project and add the following environment variables:
PAY_API
HMAC_KEY
PASSWORD
USERNAME
MONGO_URI
PORT
- Run the application using the following command:
npm start
This file contains the authenticate
function, which is used to authenticate the application with the PayMob payment gateway. It takes the following parameters:
username
: The username of the PayMob accountpassword
: The password of the PayMob accountapi_key
: The API key of the PayMob account
The function returns a promise that resolves to an object containing the following properties:
token
: The token to be used for authentication akaaccess token
This file contains the checkout
function, which is used to register an order to Accept's database and obtain payment_key token for this order, think of it as a wrapper around the payment flow found in paymob documentation It takes the following parameters:
order_cart
: An array of objects containing the items to be purchasedamount_cents
: The total amount to be paid in centsbilling_data
: An object containing the billing details of the customer
and returns a promise that resolves to an object containing the following properties:
id
: The ID of the order
the function then proceeds to create the payment key token for this order and returns a promise that resolves to an object containing the following properties:
token
: The payment key token
this token is then used in the iframe to redirect the user to the payment page
These functions are used to perform void, get transaction details, and refund operations respectively. all of them need to be authenticated first using the authenticate
function then continue with the operation.
in the rest folder you can find examples of how to use the routes in the project, take a look at them to get a better understanding of how to use the project but in general the flow is as follows:
- create an order using the
checkout
- Do any the following operations:
- get the transaction details using
get_transaction
- get the order status using
orders
- get the transaction details using
- if the payment is not captured yet, you can void it using
void
- if the payment is captured, you can refund it using
refund
This project was inspired by the need to integrate PayMob payment gateway into Node.js applications, making it easier to handle payments and related operations especially in Egypt.
Special thanks to the PayMob team for providing the support for integrating with their payment gateway.
This project is licensed under the MIT License. You are free to use, modify, and distribute the code.