Coder Social home page Coder Social logo

indonesiax / midtrans-nodex Goto Github PK

View Code? Open in Web Editor NEW
9.0 6.0 2.0 10 KB

NodeJS library for Midtrans payment gateway

Home Page: https://www.npmjs.com/package/midtrans-nodex

License: MIT License

JavaScript 100.00%
midtrans npm payment nodejs-modules

midtrans-nodex's Introduction

midtrans-node

Promise based Midtrans integration module for Node JS. Written in ES6 and support multiple payment account.

Why multiple account? Currently midtrans not yet support multiple bank account number, and we need to separate some purchases pool for some reason.

Prerequisites

  • Node 6

Installation

$ npm install midtrans-nodex

Usage

Note: For detail description about payload and responses, please refer to Official Midtrans documentation.

Setup

Instantiate by assigning the module using new keyword into variable. See below for examples.

Single Account

For single account, pass configuration object with properties clientKey, serverKey, and isProduction. This configuration can be seen at Midtrans MAP > Configuration

const Midtrans = require('midtrans-node');

const mds = new Midtrans({
    "clientKey": "Your-Midtrans-Client-Key",
    "serverKey": "Your-Midtrans-Server-Key",
    "isProduction": false
  });
Multiple Account

For multiple account, pass array of object containing configuration like single account plus additional properties name.

const Midtrans = require('midtrans-node');

const mdm = new Midtrans([
  {
    "name": "store-wahid",
    "clientKey": "Your-Midtrans-Client-Key",
    "serverKey": "Your-Midtrans-Server-Key",
    "isProduction": false
  },
  {
    "name": "store-isnan",
    "clientKey": "Your-Midtrans-Client-Key",
    "serverKey": "Your-Midtrans-Server-Key",
    "isProduction": false
  }
]);

API Reference

Core

Get Transaction Status

Send transaction_id / order_id of previous purchase to get the transaction status.

  • Single -- mds.transaction.status(id)
  • Multiple -- mdm.transaction.status(id, name)

Params:

  • id (String) - A string representing the order_id or transaction_id.
  • name (String) - A string representing property name that passed when instantiating multiple account, to determine which account will be used.

Example:

  • Single

    mds.transaction.status('ORDER-101')
    .then((res) => {
      console.log(res.data);
    })
    .catch((err) => {
      console.log(err);
    });
    
  • Multiple

    mdm.transaction.status('ORDER-101', 'store-isnan')
    .then((res) => {
      console.log(res.data);
    })
    .catch((err) => {
      console.log(err);
    });
    

SNAP

Get Snap Token

Send payload information about transactions to get SNAP_TOKEN. The SNAP_TOKEN is required to show pop up midtrans payment.

  • Single -- mds.snap.transactions(payload)
  • Multiple -- mdm.snap.transactions(payload, name)

Params:

  • payload (Object) - An object representing transaction details.

    Minimum payload:

    {
      "transaction_details": {
        "order_id": "ORDER-101",
        "gross_amount": 10000
      }
    }
    

    All possible payload and details can be seen here.

  • name (String) - A string representing property name that passed when instantiating multiple account, to determine which account will be used.

Example:

  • Single

    mds.snap.transactions({
      "transaction_details": {
        "order_id": "ORDER-101",
        "gross_amount": 10000
      }
    })
    .then((token) => {
      console.log(token);
    })
    .catch((err) => {
      console.log(err);
    });
    
  • Multiple

    mdm.snap.transactions({
      "transaction_details": {
        "order_id": "ORDER-101",
        "gross_amount": 10000
      }
    }, 'store-wahid')
    .then((token) => {
      console.log(token);
    })
    .catch((err) => {
      console.log(err);
    });
    

Notification

Verify Payload Notification

Midtrans will post payload data to our specified url, this API making sure the payload data is truly sent by midtrans, by comparing the received data signature key with the signature key data response from status API.

  • Single -- mds.notification.verify(orderId, signatureKey)
  • Double -- mdm.notification.verify(orderId, signatureKey, name)

Params:

  • orderId (String) - A string representing the order_id or transaction_id.
  • signatureKey (String) - A string representing signature_key received from midtrans notification.
  • name (String) - A string representing property name that passed when instantiating multiple account, to determine which account will be used.

Example:

  • Single

    mds.notification.verify('ORDER-101', '833c8afeb91f9d913a38304ef1849ed7dbbad5d78ff2892af6f6e472d5f3b61e803ba92c198cb587f3222a746821ad71eda22a594f5b21445a0822d807fc4097')
    .then((res) => {
      console.log(res);
      // If genuine
      // {
      //   status: true,
      //   data: {
      //     // response object like check status transaction
      //   }
      // }
    })
    .catch((err) => {
      console.log(err);
    })
    
  • Multiple

    mdm.notification.verify('ORDER-101', '833c8afeb91f9d913a38304ef1849ed7dbbad5d78ff2892af6f6e472d5f3b61e803ba92c198cb587f3222a746821ad71eda22a594f5b21445a0822d807fc4097', 'store-wahid')
    .then((res) => {
      console.log(res);
      // If not genuine
      // {
      //   status: false,
      //   data: {
      //     // response object like check status transaction
      //   }
      // }
    })
    .catch((err) => {
      console.log(err);
    });
    

LICENSE

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.