Doku
This page is used to test creating order for Direct Transfer transaction without using backend API. This page is only available on Sandbox and couldn’t be accessed on Jokul BO.
midtrans-payment
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
1var MidTrans = require('midtrans-payment');
2
3var config = {
4 client_key: "YOUR_CLIENT_KEY",
5 server_key: "YOUR_SERVER_KEY",
6 mode: "" // you can set to sandbox or production. Default is sandbox if empty.
7};
SNAP
Example to create Transactions
1var mdt = new MidTrans(config);
2mdt.type('snap').action('transactions')
3 .transaction_details('INV001',2000)
4 .item_details('Midtrans Bear',1000,1,'Kid Toys') //optional
5 .item_details('Midtrans Cat',1000,1,'Kid Toys') //optional
6 .customer_details('John','Doe','john.doe@gmail.com','+62856') //optional
7 .billing_address('John','Doe','john.doe@gmail.com','+62856') //optional
8 .shipping_address('John','Doe','john.doe@gmail.com','+62856') //optional
9 .send(function(response) {
10 console.log(response.body);
11 });
API
Example to create API Charge Bank Transfer with Bank Permata
1var mdt = new MidTrans(config);
2mdt.type('api').action('charge')
3 .transaction_details('INV002',2000)
4 .item_details('Midtrans Bear',1000,1,'Kid Toys') //optional
5 .item_details('Midtrans Cat',1000,1,'Kid Toys') //optional
6 .customer_details('John','Doe','john.doe@gmail.com','+62856') //optional
7 .billing_address('John','Doe','john.doe@gmail.com','+62856') //optional
8 .shipping_address('John','Doe','john.doe@gmail.com','+62856') //optional
9 .add('payment_type','bank_transfer')
10 .add('bank_transfer',{
11 bank: "permata",
12 va_number: "1234567890",
13 permata: {
14 recipient_name: "SUDARSONO"
15 }
16 })
17 .send(function(response) {
18 console.log(response.body);
19 });
Promise
1mdt.sendAsync().then(res => {
2 console.log(res.body);
3}).catch(err => {
4 console.log(err);
5});
Async Await
1const res = await mdt.sendAsync();
2if(res) {
3 console.log(res.body);
4} else {
5 console.log(res);
6}
Example Get Credit Card Token
1var mdt = new MidTrans(config);
2
3var payload = {
4 gross_amount: 10000,
5 card_number: '4811 1111 1111 1114',
6 card_exp_month: 12,
7 card_exp_year: 2019,
8 card_cvv: 123
9};
10
11mdt.type('api').action('token',payload)
12 .send(function(response) {
13 console.log(response.body);
14 });
Example to create API Charge Credit Card
1var mdt = new MidTrans(config);
2mdt.type('api').action('charge')
3 .transaction_details('INV003',2000)
4 .item_details('Midtrans Bear',1000,1,'Kid Toys') //optional
5 .item_details('Midtrans Cat',1000,1,'Kid Toys') //optional
6 .customer_details('John','Doe','john.doe@gmail.com','+62856') //optional
7 .billing_address('John','Doe','john.doe@gmail.com','+62856') //optional
8 .shipping_address('John','Doe','john.doe@gmail.com','+62856') //optional
9 .add('payment_type','credit_card')
10 .add('credit_card',{
11 token_id: "<you must call API to get credit card token first>"
12 })
13 .send(function(response) {
14 console.log(response.body);
15 });
Example to get Transaction Status
1var mdt = new MidTrans(config);
2mdt.type('api') //you can set type with snap or api
3 .action('status','INV001')
4 .send(function(response) {
5 console.log(response.body);
6 });
Example to get Transaction Status B2B
1var mdt = new MidTrans(config);
2mdt.type('api') //you can set type with snap or api
3 .action('status/b2b','INV001')
4 .send(function(response) {
5 console.log(response.body);
6 });
Example to get Transaction Status B2B with pagination
1var mdt = new MidTrans(config);
2mdt.type('api') //you can set type with snap or api
3 .action('status/b2b','INV001',{page:0,per_page:10})
4 .send(function(response) {
5 console.log(response.body);
6 });
Example to APPROVE Transaction
1var mdt = new MidTrans(config);
2mdt.type('api') //you can set type with snap or api
3 .action('approve','INV001')
4 .send(function(response) {
5 console.log(response.body);
6 });
Example to DENY Transaction
1var mdt = new MidTrans(config);
2mdt.type('api') //you can set type with snap or api
3 .action('deny','INV001')
4 .send(function(response) {
5 console.log(response.body);
6 });
Example to CANCEL Transaction
1var mdt = new MidTrans(config);
2mdt.type('api') //you can set type with snap or api
3 .action('cancel','INV001')
4 .send(function(response) {
5 console.log(response.body);
6 });
Example to EXPIRE Transaction
1var mdt = new MidTrans(config);
2mdt.type('api') //you can set type with snap or api
3 .action('expire','INV001')
4 .send(function(response) {
5 console.log(response.body);
6 });
Example to REFUND Transaction
1var mdt = new MidTrans(config);
2mdt.type('api') //you can set type with snap or api
3 .action('refund','INV001')
4 .send(function(response) {
5 console.log(response.body);
6 });
Example to REFUND DIRECT Transaction
1var mdt = new MidTrans(config);
2mdt.type('api') //you can set type with snap or api
3 .action('refund/online/direct','INV001')
4 .send(function(response) {
5 console.log(response.body);
6 });
Example to Capture Transactions
1var mdt = new MidTrans(config);
2mdt.type('api').action('capture')
3 .add('transaction_id','be4f3e44-d6ee-4355-8c64-c1d1dc7f4590')
4 .add('gross_amount',145000)
5 .send(function(response) {
6 console.log(response.body);
7 });
Example to Card Register
1var mdt = new MidTrans(config);
2
3var payload = {
4 card_number: '4811222233331114',
5 card_exp_month: 12,
6 card_exp_year: 2019,
7 card_cvv: 123
8};
9
10mdt.type('api') //you can set type with snap or api
11 .action('card/register',payload)
12 .send(function(response) {
13 console.log(response.body);
14 });
Example to Point Inquiry
1var mdt = new MidTrans(config);
2mdt.type('api').action('point_inquiry','123',{gross_amount:1000})
3 .send(function(response) {
4 console.log(response.body);
5 });
Example to BIN API
1var mdt = new MidTrans(config);
2mdt.type('api').action('bins','455633')
3 .send(function(response) {
4 console.log(response.body);
5 });
Example create body request manually
If our methods doesn't fit in your situation. You're able to build your custom body request.
1var mdt = new MidTrans(config);
2mdt.type('api').action('charge')
3 .add('payment_type','bank_transfer')
4 .add('transaction_details',{
5 gross_amount: 44000,
6 order_id: "order-101c"
7 })
8 .add('customer_details',{
9 email: "noreply@example.com",
10 first_name: "budi",
11 last_name: "utomo",
12 phone: "+6281 1234 1234"
13 })
14 .add('item_details',[{
15 id: "item01",
16 price: 21000,
17 quantity: 1,
18 name: "Ayam Zozozo"
19 },
20 {
21 id: "item02",
22 price: 23000,
23 quantity: 1,
24 name: "Ayam Xoxoxo"
25 }
26 ])
27 .add('bank_transfer',{
28 bank: "bca",
29 va_number: "12345678901",
30 free_text: {
31 inquiry: [{
32 id: "Your Custom Text in ID language",
33 en: "Your Custom Text in EN language"
34 }],
35 payment: [{
36 id: "Your Custom Text in ID language",
37 en: "Your Custom Text in EN language"
38 }]
39 }
40 })
41 .send(function(response) {
42 console.log(response.body);
43 });
RECURRING API
Example to Create Subscriptions
1var mdt = new MidTrans(config);
2mdt.type('api')
3 .action('subscriptions')
4 .subscriptions('SUB1',1000,'IDR','credit_card','yourtoken',1)
5 .send(function(response){
6 console.log(response.body);
7 });
Example to Find Subscriptions
1var mdt = new MidTrans(config);
2mdt.type('api')
3 .action('subscriptions','SUB1')
4 .send(function(response){
5 console.log(response.body);
6 });
Example to Enable Subscriptions
1var mdt = new MidTrans(config);
2mdt.type('api')
3 .do('enable').action('subscriptions','SUB1')
4 .send(function(response){
5 console.log(response.body);
6 });
Example to Disable Subscriptions
1var mdt = new MidTrans(config);
2mdt.type('api')
3 .do('disable').action('subscriptions','SUB1')
4 .send(function(response){
5 console.log(response.body);
6 });
Example to Update Subscriptions
1var mdt = new MidTrans(config);
2mdt.type('api')
3 .do('update').action('subscriptions','SUB1')
4 .subscriptions('SUB1',2000,'IDR','credit_card','yourtoken',1)
5 .send(function(response){
6 console.log(response.body);
7 });
Example create body request for subscriptions manually
1var mdt = new MidTrans(config);
2mdt.type('api')
3 .action('subscriptions')
4 .add('name','SUB1')
5 .add('amount','2000')
6 .add('currency','IDR')
7 .add('payment_type','credit_card')
8 .add('token','yourtoken')
9 .add('interval',1)
10 .send(function(response){
11 console.log(response.body);
12 });
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
1var mdt = new MidTrans(config);
2console.log(mdt.showAllMethods(mdt));
Main methods
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)
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 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
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:
1// if you want to add enabled_payments in snap transaction
2.add('enabled_payments',[ "credit_card", "permata_va", "bca_va", "bni_va"])
3
4// if you want to add expiry in snap transactions
5.add('expiry',{
6 start_time: "2018-12-13 18:11:08 +0700",
7 unit: "minutes",
8 duration: 1
9})
10
11// if you want to add custom_expiry in API charge
12.add('custom_expiry',{
13 order_time: "2017-04-13 18:11:08 +0700",
14 expiry_duration: 180,
15 unit: "minute"
16})
Unit Test
If you want to play arround with testing
1npm test
Install now
npm install midtrans-payment
Create your own plugin
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.