项目作者: aalfiann

项目描述 :
Midtrans Payment Gateway library for NodeJS
高级语言: JavaScript
项目地址: git://github.com/aalfiann/midtrans-payment.git
创建时间: 2019-08-01T07:37:44Z
项目社区:https://github.com/aalfiann/midtrans-payment

开源协议:MIT License

下载


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

  1. $ npm install midtrans-payment

Usage

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

Set Config

  1. var MidTrans = require('midtrans-payment');
  2. var config = {
  3. client_key: "YOUR_CLIENT_KEY",
  4. server_key: "YOUR_SERVER_KEY",
  5. mode: "" // you can set to sandbox or production. Default is sandbox if empty.
  6. };

SNAP

Example to create Transactions

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. mdt.sendAsync().then(res => {
  2. console.log(res.body);
  3. }).catch(err => {
  4. console.log(err);
  5. });

Async Await

  1. const res = await mdt.sendAsync();
  2. if(res) {
  3. console.log(res.body);
  4. } else {
  5. console.log(res);
  6. }

Example Get Credit Card Token

  1. var mdt = new MidTrans(config);
  2. var payload = {
  3. gross_amount: 10000,
  4. card_number: '4811 1111 1111 1114',
  5. card_exp_month: 12,
  6. card_exp_year: 2019,
  7. card_cvv: 123
  8. };
  9. mdt.type('api').action('token',payload)
  10. .send(function(response) {
  11. console.log(response.body);
  12. });

Example to create API Charge Credit Card

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. var payload = {
  3. card_number: '4811222233331114',
  4. card_exp_month: 12,
  5. card_exp_year: 2019,
  6. card_cvv: 123
  7. };
  8. mdt.type('api') //you can set type with snap or api
  9. .action('card/register',payload)
  10. .send(function(response) {
  11. console.log(response.body);
  12. });

Example to Point Inquiry

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

Example to BIN API

  1. var mdt = new MidTrans(config);
  2. mdt.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.

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

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

Example to Enable Subscriptions

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

Example to Disable Subscriptions

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

Example to Update Subscriptions

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. mdt.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

  1. var mdt = new MidTrans(config);
  2. 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:

  1. // if you want to add enabled_payments in snap transaction
  2. .add('enabled_payments',[ "credit_card", "permata_va", "bca_va", "bni_va"])
  3. // if you want to add expiry in snap transactions
  4. .add('expiry',{
  5. start_time: "2018-12-13 18:11:08 +0700",
  6. unit: "minutes",
  7. duration: 1
  8. })
  9. // if you want to add custom_expiry in API charge
  10. .add('custom_expiry',{
  11. order_time: "2017-04-13 18:11:08 +0700",
  12. expiry_duration: 180,
  13. unit: "minute"
  14. })

Unit Test

If you want to play arround with testing

  1. npm test