npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

midtrans-payment

v1.4.2

Published

Midtrans Payment Gateway library for NodeJS

Downloads

309

Readme

midtrans-payment

Types npm version CircleCI Known Vulnerabilities NPM download/month NPM download total
Midtrans Payment Gateway library for NodeJS

Features

  • Async/Await and Promise Support
  • Typescript Support
  • Built-in method
  • Easy to Customize

Install using NPM

$ npm install midtrans-payment

Usage

This library was created refer to MidTrans technical documentation version 3.48.0.
Please see:

Set Config

var MidTrans = require('midtrans-payment');

var config = {
  client_key: "YOUR_CLIENT_KEY",
  server_key: "YOUR_SERVER_KEY",
  mode: ""    // you can set to sandbox or production. Default is sandbox if empty.
};

SNAP

Example to create Transactions

var mdt = new MidTrans(config);
mdt.type('snap').action('transactions')
  .transaction_details('INV001',2000)
  .item_details('Midtrans Bear',1000,1,'Kid Toys')                //optional
  .item_details('Midtrans Cat',1000,1,'Kid Toys')                 //optional
  .customer_details('John','Doe','[email protected]','+62856')   //optional
  .billing_address('John','Doe','[email protected]','+62856')    //optional
  .shipping_address('John','Doe','[email protected]','+62856')   //optional
  .send(function(response) {
    console.log(response.body);
  });

API

Example to create API Charge Bank Transfer with Bank Permata

var mdt = new MidTrans(config);
mdt.type('api').action('charge')
  .transaction_details('INV002',2000)
  .item_details('Midtrans Bear',1000,1,'Kid Toys')                //optional
  .item_details('Midtrans Cat',1000,1,'Kid Toys')                 //optional
  .customer_details('John','Doe','[email protected]','+62856')   //optional
  .billing_address('John','Doe','[email protected]','+62856')    //optional
  .shipping_address('John','Doe','[email protected]','+62856')   //optional
  .add('payment_type','bank_transfer')
  .add('bank_transfer',{
    bank: "permata",
    va_number: "1234567890",
    permata: {
      recipient_name: "SUDARSONO"
    }
  })
  .send(function(response) {
    console.log(response.body);
  });

Promise

mdt.sendAsync().then(res => {
  console.log(res.body);
}).catch(err => {
  console.log(err);
});

Async Await

const res = await mdt.sendAsync();
if(res) {
  console.log(res.body);
} else {
  console.log(res);
}

Example Get Credit Card Token

var mdt = new MidTrans(config);

var payload = {
  gross_amount: 10000,
  card_number: '4811 1111 1111 1114',
  card_exp_month: 12,
  card_exp_year: 2019,
  card_cvv: 123
};

mdt.type('api').action('token',payload)
  .send(function(response) {
    console.log(response.body);
  });

Example to create API Charge Credit Card

var mdt = new MidTrans(config);
mdt.type('api').action('charge')
  .transaction_details('INV003',2000)
  .item_details('Midtrans Bear',1000,1,'Kid Toys')                //optional
  .item_details('Midtrans Cat',1000,1,'Kid Toys')                 //optional
  .customer_details('John','Doe','[email protected]','+62856')   //optional
  .billing_address('John','Doe','[email protected]','+62856')    //optional
  .shipping_address('John','Doe','[email protected]','+62856')   //optional
  .add('payment_type','credit_card')
  .add('credit_card',{
    token_id: "<you must call API to get credit card token first>"
  })
  .send(function(response) {
    console.log(response.body);
  });

Example to get Transaction Status

var mdt = new MidTrans(config);
mdt.type('api')    //you can set type with snap or api
  .action('status','INV001')
  .send(function(response) {
    console.log(response.body);
  });

Example to get Transaction Status B2B

var mdt = new MidTrans(config);
mdt.type('api')    //you can set type with snap or api
  .action('status/b2b','INV001')
  .send(function(response) {
    console.log(response.body);
  });

Example to get Transaction Status B2B with pagination

var mdt = new MidTrans(config);
mdt.type('api')    //you can set type with snap or api
  .action('status/b2b','INV001',{page:0,per_page:10})
  .send(function(response) {
    console.log(response.body);
  });

Example to APPROVE Transaction

var mdt = new MidTrans(config);
mdt.type('api')    //you can set type with snap or api
  .action('approve','INV001')
  .send(function(response) {
    console.log(response.body);
  });

Example to DENY Transaction

var mdt = new MidTrans(config);
mdt.type('api')    //you can set type with snap or api
  .action('deny','INV001')
  .send(function(response) {
    console.log(response.body);
  });

Example to CANCEL Transaction

var mdt = new MidTrans(config);
mdt.type('api')    //you can set type with snap or api
  .action('cancel','INV001')
  .send(function(response) {
    console.log(response.body);
  });

Example to EXPIRE Transaction

var mdt = new MidTrans(config);
mdt.type('api')    //you can set type with snap or api
  .action('expire','INV001')
  .send(function(response) {
    console.log(response.body);
  });

Example to REFUND Transaction

var mdt = new MidTrans(config);
mdt.type('api')    //you can set type with snap or api
  .action('refund','INV001')
  .send(function(response) {
    console.log(response.body);
  });

Example to REFUND DIRECT Transaction

var mdt = new MidTrans(config);
mdt.type('api')    //you can set type with snap or api
  .action('refund/online/direct','INV001')
  .send(function(response) {
    console.log(response.body);
  });

Example to Capture Transactions

var mdt = new MidTrans(config);
mdt.type('api').action('capture')
  .add('transaction_id','be4f3e44-d6ee-4355-8c64-c1d1dc7f4590')
  .add('gross_amount',145000)
  .send(function(response) {
    console.log(response.body);
  });

Example to Card Register

var mdt = new MidTrans(config);

var payload = {
  card_number: '4811222233331114',
  card_exp_month: 12,
  card_exp_year: 2019,
  card_cvv: 123
};

mdt.type('api')    //you can set type with snap or api
  .action('card/register',payload)
  .send(function(response) {
    console.log(response.body);
  });

Example to Point Inquiry

var mdt = new MidTrans(config);
mdt.type('api').action('point_inquiry','123',{gross_amount:1000})
  .send(function(response) {
    console.log(response.body);
  });

Example to BIN API

var mdt = new MidTrans(config);
mdt.type('api').action('bins','455633')
  .send(function(response) {
    console.log(response.body);
  });

Example create body request manually

If our methods doesn't fit in your situation. You're able to build your custom body request.

var mdt = new MidTrans(config);
mdt.type('api').action('charge')
  .add('payment_type','bank_transfer')
  .add('transaction_details',{
    gross_amount: 44000,
    order_id: "order-101c"
  })
  .add('customer_details',{
    email: "[email protected]",
    first_name: "budi",
    last_name: "utomo",
    phone: "+6281 1234 1234"
  })
  .add('item_details',[{
      id: "item01",
      price: 21000,
      quantity: 1,
      name: "Ayam Zozozo"
    },
    {
      id: "item02",
      price: 23000,    
      quantity: 1,
      name: "Ayam Xoxoxo"
    }
  ])
  .add('bank_transfer',{
    bank: "bca",
    va_number: "12345678901",
    free_text: {
      inquiry: [{
        id: "Your Custom Text in ID language",
        en: "Your Custom Text in EN language"
      }],
      payment: [{
        id: "Your Custom Text in ID language",
        en: "Your Custom Text in EN language"
      }]
    }
  })
  .send(function(response) {
    console.log(response.body);
  });

RECURRING API

Example to Create Subscriptions

var mdt = new MidTrans(config);
mdt.type('api')
  .action('subscriptions')
  .subscriptions('SUB1',1000,'IDR','credit_card','yourtoken',1)
  .send(function(response){
    console.log(response.body);
  });

Example to Find Subscriptions

var mdt = new MidTrans(config);
mdt.type('api')
  .action('subscriptions','SUB1')
  .send(function(response){
    console.log(response.body);
  });

Example to Enable Subscriptions

var mdt = new MidTrans(config);
mdt.type('api')
  .do('enable').action('subscriptions','SUB1')
  .send(function(response){
    console.log(response.body);
  });

Example to Disable Subscriptions

var mdt = new MidTrans(config);
mdt.type('api')
  .do('disable').action('subscriptions','SUB1')
  .send(function(response){
    console.log(response.body);
  });

Example to Update Subscriptions

var mdt = new MidTrans(config);
mdt.type('api')
  .do('update').action('subscriptions','SUB1')
  .subscriptions('SUB1',2000,'IDR','credit_card','yourtoken',1)
  .send(function(response){
    console.log(response.body);
  });

Example create body request for subscriptions manually

var mdt = new MidTrans(config);
mdt.type('api')
  .action('subscriptions')
  .add('name','SUB1')
  .add('amount','2000')
  .add('currency','IDR')
  .add('payment_type','credit_card')
  .add('token','yourtoken')
  .add('interval',1)
  .send(function(response){
    console.log(response.body);
  });

Response

We use unirest library for handling call API to MidTrans.

Available methods

If you want to know all available methods in this MidTrans Payment library

var mdt = new MidTrans(config);
console.log(mdt.showAllMethods(mdt));

Main methods

  • type(name) this is to set SNAP or API
  • do(name) this is to set update|enable|disable for subscriptions only
  • action(name,data='',additional_payload='') this to set action API feature. Ex: charge|approve|deny|cancel|expiry|point_inquiry|bins|subscriptions|status|status/b2b|refund|refund/online/direct|card/register
  • add(name,data) this is to add new key for body request object
  • send(callback) this is to send request to MidTrans endpoint API (Callback based)
  • sendAsync() this is to send request to MidTrans endpoint API (Promise based)

Shortcut methods

We provide a shortcut methods for you to make easier create common body request

  • subscriptions(name,amount,currency,payment_type,token,interval)
  • transaction_details(order_id,amount)
  • item_details(name,price,quantity,brand='',category='',merchant_name='',tenor='',code_plan='',mid='')
  • customer_details(first_name='',last_name='',email='',phone='')
  • billing_address(first_name='',last_name='',email='',phone='',address='',city='',postal_code='',country_code='')
  • shipping_address(first_name='',last_name='',email='',phone='',address='',city='',postal_code='',country_code='')

Helper methods

  • remove(name) this will delete the key in body request object
  • clean() this will cleanup the body request object
  • encode(data) this will encode {string|any) to base64 string
  • decode(data) this will decode base64 string to original string

Additional Feature

For all additional feature like create custom_field, custom_expiry, enabled_payments, etc.
We don't create that because we want this library always lightweight and stable when MidTrans add another new feature again.

But you can still use additional feature with this way:


// if you want to add enabled_payments in snap transaction
.add('enabled_payments',[ "credit_card", "permata_va", "bca_va", "bni_va"])

// if you want to add expiry in snap transactions
.add('expiry',{
  start_time: "2018-12-13 18:11:08 +0700",
  unit: "minutes",
  duration: 1
})

// if you want to add custom_expiry in API charge
.add('custom_expiry',{
  order_time: "2017-04-13 18:11:08 +0700",
  expiry_duration: 180,
  unit: "minute"
})

Unit Test

If you want to play arround with testing

npm test