October 12, 2022
Express Midtrans
Learn how to create a basic e-commerce website API using ExpressJS, integrating Midtrans for secure payment processing with the Core API integration method.
Express Midtrans
Build a simple e-commerce website API with ExpressJS and Midtrans. Integration using Midtrans payment gateway with Core API integration method.
Midtrans is a payment gateway that provides a simple and secure payment solution for online businesses. Midtrans provides a variety of payment methods, including credit card, bank transfer, e-wallet, and more.
Midtrans provides a variety of integration methods, including Core API, Snap, and Mobile SDK. In this project, I use Core API integration method.
Core API is a simple integration method that allows you to build your own payment page. Core API is suitable for developers who want to build a custom payment page.
Live demo : Not available yet.
Source code : GitHub
Features
- Purchase series with Midtrans payment gateway
- Midtrans payment gateway integration using Core API integration method
- Send email notification to customer after payment success
Tech Stack
- ExpressJS
- Midtrans
- nodemailer
- Mailtrap (for testing email notification)
Prerequisites
Installation and Usage
Clone the repository
cd into the directory
Install dependencies
copy .env.example to .env
Configure environment variables in .env
file
Create database
Run migration
Run seed
Run the app
For receiving Payment Notification Callback, you have to expose your local server to the internet. For testing purpose, you can use ngrok.
Copy the https url and paste it to Midtrans Dashboard > Settings > Configuration > Payment Notification URL, with /api/orders/notification
endpoint.
API Documentation
Authentication
Register
Register a new user.
Request :
- Method : POST
- Endpoint :
/api/register
- Headers :
- Content-Type : application/json
- Accept : application/json
- Body :
Login
Login user.
Request :
- Method : POST
- Endpoint :
/api/login
- Headers :
- Content-Type : application/json
- Accept : application/json
- Body :
Authenticated User
Get authenticated user.
Request :
- Method : GET
- Endpoint :
/api/me
- Headers :
- Content-Type : application/json
- Accept : application/json
- Authorization : Bearer
{accessToken}
Logout
Logout user.
Request :
- Method : POST
- Endpoint :
/api/logout
- Headers :
- Content-Type : application/json
- Accept : application/json
- Authorization : Bearer
{accessToken}
Refresh Token
Get new access token using refresh token.
Request :
- Method : GET
- Endpoint :
/api/refresh-token
- Headers :
- Content-Type : application/json
- Accept : application/json
My Library
Get authenticated user purchased series.
Request :
- Method : GET
- Endpoint :
/api/library
- Headers :
- Content-Type : application/json
- Accept : application/json
- Authorization : Bearer
{accessToken}
Series
Get All Series
Get all series.
Request :
- Method : GET
- Endpoint :
/api/series
- Headers :
- Content-Type : application/json
- Accept : application/json
Show Series
Show series.
Request :
- Method : GET
- Endpoint :
/api/series/{slug}
- Headers :
- Content-Type : application/json
- Accept : application/json
- Authorization : Bearer
{accessToken}
(optional) - Params :
- slug : string
- Description : Bearer
{accessToken}
is optional. If user is authenticated, the user can see the information of the series that has been purchased. - Example :
/api/series/express-midtrans
Videos
Watch Series Video
Watch series video.
Request :
- Method : GET
- Endpoint :
/api/series/{slug}/eps/{episode}
- Headers :
- Content-Type : application/json
- Accept : application/json
- Authorization : Bearer
{accessToken}
(optional) - Params :
- slug : string
- episode : integer
- Description : Bearer
{accessToken}
is optional. If user is authenticated, the user can watch the video of the series that has been purchased. - Example :
/api/series/express-midtrans/eps/1
Carts
Get Carts
Get all carts.
Request :
- Method : GET
- Endpoint :
/api/carts
- Headers :
- Content-Type : application/json
- Accept : application/json
- Authorization : Bearer
{accessToken}
Add or Remove Series Cart
Add or remove series cart.
Request :
- Method : POST
- Endpoint :
/api/carts
- Headers :
- Content-Type : application/json
- Accept : application/json
- Authorization : Bearer
{accessToken}
- Body :
Orders
Order Checkout
Order Series Checkout.
Request :
- Method : POST
- Endpoint :
/api/orders
- Headers :
- Content-Type : application/json
- Accept : application/json
- Authorization : Bearer
{accessToken}
- Body :
Invoice
Get invoice.
Request :
- Method : POST
- Endpoint :
/api/orders/invoice/{identifier}
- Headers :
- Content-Type : application/json
- Accept : application/json
- Authorization : Bearer
{accessToken}
- Params :
- identifier : string
- Example :
/api/orders/invoice/7ccc79aa2c521f432183b6d9d5d883e20b77918e