ProjectExpressJS

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.

Stack

ExpressJS
Midtrans
Year
2022

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

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

cd into the directory

Terminal
cd express-midtrans

Install dependencies

Terminal
npm install

copy .env.example to .env

Terminal
cp .env.example .env

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=

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

Terminal
sequelize db:create

Run migration

Terminal
sequelize db:migrate

Run seed

Terminal
sequelize db:seed:all

Run the app

Terminal
npm run dev

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

Terminal
ngrok http 5000

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

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

API Documentation

Authentication

Register

Register a new user.

Request :

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

Login

Login user.

Request :

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

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

Orders

Order Checkout

Order Series Checkout.

Request :

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

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