Connecting to Taxamo from Node.js

The easiest way to access Taxamo RESTful API from Node.js is to generate client bindings on the fly with swagger-js for Node.js.

First, let’s add swagger dependency to our package.json:

{
  //...
  "dependencies": {
    //...
    "swagger-client": "2.0.x"
  }
}

Next, we can reference the Taxamo API in our application’s JavaScript file (for example app.js or server.js):

var client = require('swagger-client');

//use private token here, since we're talking server-server
client.authorizations.add("apiKey", new client.ApiKeyAuthorization("private_token", "***************", "query"));

//...

var taxamo = new client.SwaggerApi({
  url: 'https://api.taxamo.com/swagger',
  success: function() {
    if(taxamo.ready === true) {
        //app initializations - e.g. server startup can go here
    }
  }
});

Invoking the API

As our Node.js example uses the taxamo.js library to pre-calculate VAT and gather payment data from Stripe in JavaScript, all we have to do is:

  1. Invoke the Taxamo API to get stored transaction details – so we can validate the transaction data – amounts, currencies, etc.
  2. Invoke the Taxamo API to capture payment.

The example express.js route that implements the features listed above, looks as following:

app.get('/stripe', function index(req, res){
    taxamo.apis.transactions.getTransaction({key: req.query.transaction_key}, 
        function(data) {
            var transaction = JSON.parse(data.data).transaction;
            //merchants can check if transaction amounts/products make sense here
            taxamo.apis.payments.capturePayment({key: req.query.transaction_key}, 
            function(data) {
                res.send("Payment accepted.");
            }, function (error) {
                var errorResponse = JSON.parse(error.data);
                res.send(errorResponse);
        });
    });
});

Naturally, if using different PSP, such as Braintree, the /stripe url should be changed to a something more universal.