Giter Club home page Giter Club logo

veritrans-node's Introduction

veritrans-node

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.

Veritrans API Documentation

This module follows Veritrans official API documentation. Please see this for API reference.

Install

npm install veritrans

Usage

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);

1. Charge transaction

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 after payment_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')
    });
});

2. Check Transaction / Order Status

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);
    });
});

3. Capture Transaction

vt.transaction.capture(payload, callback) - Charge a pre-authorized transaction in which transaction_status is authorize.

Params:

  • payload (Object) - Consists of transaction_id and gross_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);
    });
});

4. Approve Transaction

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);
    });
});

5. Cancel Transaction

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);
    });
});

6. Expire Transaction

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);
    });
});

License

MIT License

veritrans-node's People

Contributors

alwint3r avatar sgpagek avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.