Midtrans Payment Gateway library for NodeJS
- Async/Await and Promise Support
- Typescript Support
- Built-in method
- Easy to Customize
$ npm install midtrans-payment
This library was created refer to MidTrans technical documentation version 3.48.0.
Please see:
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.
};
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);
});
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);
}
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
var mdt = new MidTrans(config);
mdt.type('api').action('point_inquiry','123',{gross_amount:1000})
.send(function(response) {
console.log(response.body);
});
var mdt = new MidTrans(config);
mdt.type('api').action('bins','455633')
.send(function(response) {
console.log(response.body);
});
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);
});
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);
});
var mdt = new MidTrans(config);
mdt.type('api')
.action('subscriptions','SUB1')
.send(function(response){
console.log(response.body);
});
var mdt = new MidTrans(config);
mdt.type('api')
.do('enable').action('subscriptions','SUB1')
.send(function(response){
console.log(response.body);
});
var mdt = new MidTrans(config);
mdt.type('api')
.do('disable').action('subscriptions','SUB1')
.send(function(response){
console.log(response.body);
});
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);
});
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);
});
We use unirest library for handling call API to MidTrans.
If you want to know all available methods in this MidTrans Payment library
var mdt = new MidTrans(config);
console.log(mdt.showAllMethods(mdt));
type(name)
this is to set SNAP or APIdo(name)
this is to set update|enable|disable for subscriptions onlyaction(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 objectsend(callback)
this is to send request to MidTrans endpoint API (Callback based)sendAsync()
this is to send request to MidTrans endpoint API (Promise based)
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='')
remove(name)
this will delete the key in body request objectclean()
this will cleanup the body request objectencode(data)
this will encode {string|any) to base64 stringdecode(data)
this will decode base64 string to original string
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"
})
If you want to play arround with testing
npm test