midtrans-payment
Midtrans Payment Gateway library for NodeJS
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 = ; 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 = config;mdttype'snap'action'transactions' //optional //optional //optional //optional //optional ;
API
Example to create API Charge Bank Transfer with Bank Permata
var mdt = config;mdttype'api'action'charge' //optional //optional //optional //optional //optional ;
Example Get Credit Card Token
var mdt = config; var payload = gross_amount:10000 card_number:'4811 1111 1111 1114' card_exp_month:12 card_exp_year:2019 card_cvv:123; mdttype'api'action'token'payload ;
Example to create API Charge Credit Card
var mdt = config;mdttype'api'action'charge' //optional //optional //optional //optional //optional ;
Example to get Transaction Status
var mdt = config;mdttype'api' //you can set type with snap or api action'status''INV001' ;
Example to get Transaction Status B2B
var mdt = config;mdttype'api' //you can set type with snap or api action'status/b2b''INV001' ;
Example to get Transaction Status B2B with pagination
var mdt = config;mdttype'api' //you can set type with snap or api action'status/b2b''INV001'page:0per_page:10 ;
Example to APPROVE Transaction
var mdt = config;mdttype'api' //you can set type with snap or api action'approve''INV001' ;
Example to DENY Transaction
var mdt = config;mdttype'api' //you can set type with snap or api action'deny''INV001' ;
Example to CANCEL Transaction
var mdt = config;mdttype'api' //you can set type with snap or api action'cancel''INV001' ;
Example to EXPIRE Transaction
var mdt = config;mdttype'api' //you can set type with snap or api action'expire''INV001' ;
Example to REFUND Transaction
var mdt = config;mdttype'api' //you can set type with snap or api action'refund''INV001' ;
Example to REFUND DIRECT Transaction
var mdt = config;mdttype'api' //you can set type with snap or api action'refund/online/direct''INV001' ;
Example to Capture Transactions
var mdt = config;mdttype'api'action'capture' ;
Example to Card Register
var mdt = config; var payload = card_number:'4811222233331114' card_exp_month:12 card_exp_year:2019 card_cvv:123; mdttype'api' //you can set type with snap or api action'card/register'payload ;
Example to Point Inquiry
var mdt = config;mdttype'api'action'point_inquiry''123'gross_amount:1000 ;
Example to BIN API
var mdt = config;mdttype'api'action'bins''455633' ;
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 = config;mdttype'api'action'charge' ;
RECURRING API
Example to Create Subscriptions
var mdt = config; mdttype'api' action'subscriptions' ;
Example to Find Subscriptions
var mdt = config; mdttype'api' action'subscriptions''SUB1' ;
Example to Enable Subscriptions
var mdt = config; mdttype'api' action'subscriptions''SUB1' ;
Example to Disable Subscriptions
var mdt = config; mdttype'api' action'subscriptions''SUB1' ;
Example to Update Subscriptions
var mdt = config; mdttype'api' action'subscriptions''SUB1' ;
Example create body request for subscriptions manually
var mdt = config; mdttype'api' action'subscriptions' ;
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 = config;console;
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
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:
// if you want to add enabled_payments in snap transaction // if you want to add expiry in snap transactions // if you want to add custom_expiry in API charge
Unit Test
If you want to play arround with testing
npm test