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

Prerequisites

Installation and Usage

Clone the repository

git clone https://github.com/raprmdn/express-midtrans.git
bash

cd into the directory

cd express-midtrans
bash

Install dependencies

npm install
bash

copy .env.example to .env

cp .env.example .env
bash

Configure environment variables in .env file

.env
# Access Token & Refresh Token
ACCESS_TOKEN_SECRET_KEY=
REFRESH_TOKEN_SECRET_KEY=
 
# Midtrans
MIDTRANS_MERCHANT_ID=
MIDTRANS_IS_PRODUCTION=
MIDTRANS_SERVER_KEY=
MIDTRANS_CLIENT_KEY=
 
# Mailtrap
SMTP_EMAIL=
SMTP_HOST=
SMTP_PORT=
SMTP_USER=
SMTP_PASS=
dotenv

Login or Register Midtrans website to get your server key and client key.
Login or Register in Mailtrap website to get your SMTP credentials.

Create database

sequelize db:create
bash

Run migration

sequelize db:migrate
bash

Run seed

sequelize db:seed:all
bash

Run the app

npm run dev
bash

For receiving Payment Notification Callback, you have to expose your local server to the internet. For testing purpose, you can use ngrok.

ngrok http 5000
bash

Copy the https url and paste it to Midtrans Dashboard > Settings > Configuration > Payment Notification URL, with /api/orders/notification endpoint.

https://xxxxxxxxxxxx.ngrok.io/api/orders/notification
bash

API Documentation

Authentication

Register

Register a new user.

Request :

  • Method : POST
  • Endpoint : /api/register
  • Headers :
    • Content-Type : application/json
    • Accept : application/json
  • Body :
{
  "name": "string",
  "username": "string",
  "email": "string",
  "password": "string",
  "password_confirmation": "string"
}
json

Login

Login user.

Request :

  • Method : POST
  • Endpoint : /api/login
  • Headers :
    • Content-Type : application/json
    • Accept : application/json
  • Body :
{
  "email": "string",
  "password": "string"
}
json

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 :
{
  "series_id": "integer"
}
json

Orders

Order Checkout

Order Series Checkout.

Request :

  • Method : POST
  • Endpoint : /api/orders
  • Headers :
    • Content-Type : application/json
    • Accept : application/json
  • Authorization : Bearer {accessToken}
  • Body :
{
  "payment_channel": "string"
}
json

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
raprmdn
Rafi Putra Ramadhan
ExpressJS
Midtrans