Veritrans integration module for Node.js platform.
Note: This module is written in ES6 standards, so in order to make it works you have to run your app using Node.js v4.x.x or use --harmony
flag to enable ES6 features. Also you can use babel to transpile ES6 codes to ES5 codes if you are working with old version of Node.js.
This module follows Veritrans official API documentation. Please see this for API reference.
npm install veritrans
Examples bellow are using express. Description for transaction / payment payloads can be found on Veritrans official API documentation.
require('veritrans');
returns Veritrans
constructor function that should be instantiated using new
keyword. It only requires one argument that define its configuration.
Config:
- serverKey (String) - Used to pass basic auth that is required by Veritrans API.
- clientKey (String) - Used to pass basic auth that is required by Veritrans API.
- url (String) - Base url for API call. There are two types of url that can be used for sandbox mode and production mode.
The values for those fields can be obtained through Veritrans MAP.
const config = {
serverKey: 'Your-Server-Key',
clientKey: 'Your-Client-Key',
url: 'https://api.sandbox.veritrans.co.id/v2'
};
const Veritrans = require('veritrans');
const vt = new Veritrans(config);
vt.transaction.charge(transaction, callback)
- All payment methods that are provided by Veritrans are supported by this module. Example below is using credit_card
payment type.
Params:
transaction
(Object) - An object representing transaction payload.payment_type
,transaction_details
fields + one that is named afterpayment_type
value are required.callback(err, response)
(Function) - Callback function that will be fired once transaction is finished.
route.post('/pay', (req, res, next) => {
const transaction = {
payment_type: 'credit_card',
transaction_details: {
order_id: 'sample-order-1',
gross_amount: '1000000',
},
credit_card: {
token_id: 'your-credit-card-token',
bank: 'bni',
},
};
vt.transaction.charge(transaction, (err, result) => {
if (err) {
console.error(err);
return res.redirect('/pay/error');
}
return res.redirect('/pay/success')
});
});
vt.transaction.status(id, callback)
- Check the status of transaction / order that is identified by id
.
Params:
id
(String) - Identifier of a transaction / order.callback(err, response)
(Function) - Callback function that will be called once the status is checked.
route.post('/status/:id', (req, res, next) => {
const id = req.params.id;
vt.transaction.status(id, (err, result) => {
if (err) {
console.error(err);
return res.redirect('/transaction/' + req.params.id);
}
return res.redirect('/transaction/' + req.params.id);
});
});
vt.transaction.capture(payload, callback)
- Charge a pre-authorized transaction in which transaction_status
is authorize
.
Params:
payload
(Object) - Consists oftransaction_id
andgross_amount
fields.transaction_id
is required.callback(err, response)
(Function) - Callback function that will be called once transaction is captured.
route.get('/capture/:id', (req, res, next) => {
const payload = {
transaction_id: req.params.id,
gross_amount: 100000,
};
vt.transaction.capture(payload, (err, result) => {
if (err) {
console.error(err);
return res.redirect('/transaction/' + req.params.id);
}
return res.redirect('/transaction/' + req.params.id);
});
});
vt.transaction.approve(id, callback)
- Accept card payment transaction in which the fraud_status
is challenge
.
Params:
id
(String) - Identifier of a transaction / order.callback(err, response)
(Function) - Callback function that will be called once the transaction is approved.
route.get('/approve/:id', (req, res, next) => {
const id = req.params.id;
vt.transaction.approve(id, (err, result) => {
if (err) {
console.error(err);
return res.redirect('/transaction/' + req.params.id);
}
return res.redirect('/transaction/' + req.params.id);
});
});
vt.transaction.cancel(id, callback)
- Cancel card payment transaction if the transaction has not been settled yet.
Params:
id
(String) - Identifier of a transaction / order.callback(err, response)
(Function) - Callback function that will be called once the transaction is canceled.
route.get('/cancel/:id', (req, res, next) => {
const id = req.params.id;
vt.transaction.cancel(id, (err, result) => {
if (err) {
console.error(err);
return res.redirect('/transaction/' + req.params.id);
}
return res.redirect('/transaction/' + req.params.id);
});
});
vt.transaction.expire(id, callback)
- Expire a transaction which payment has not yet completed by the customer.
Params:
id
(String) - Identifier of a transaction / order.callback(err, response)
(Function) - Callback function that will be called once the transaction is expired.
route.get('/expire/:id', (req, res, next) => {
const id = req.params.id;
vt.transaction.expire(id, (err, result) => {
if (err) {
console.error(err);
return res.redirect('/transaction/' + req.params.id);
}
return res.redirect('/transaction/' + req.params.id);
});
});
vt.transaction.refund(id, callback)
- Cancel the completed / settlement payment.
Params:
id
(String) - Identifier of a transaction / order.callback(err, response)
(Function) - Callback function that will be called once the refund process is completed.
route.get('/refund/:id', (req, res, next) => {
const id = req.params.id;
vt.transaction.refund(id, (err, result) => {
if (err) {
console.error(err);
return res.redirect('/transaction/' + req.params.id);
}
return res.redirect('/transaction/' + req.params.id);
});
});
MIT License