Skip to content

Building the backend

Getting the BizApp server side SDK

The easiest way to get the BizApp server side SDK is via npm. Note that we only support Node.js with express.js framework at the moment.

Run the following line in terminal to get bizapp package:

npm install bizapp --save

Boilerplate

Use the boilerplate below to quick start

const express = require("express");
const bodyParser = require("body-parser");
const BizApp = require("bizapp");

const app = express();
const biz = BizApp({
  appId: "YOUR_APP_ID_HERE"
});

// Allow cross origin
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

// Parse body
app.use(bodyParser.urlencoded({ extended: true }));

// Listen
app.listen(3000, () => console.log("Server listening on port 3000"));

User authentication

In order to achieve user authentication, you have to specify a POST endpoint in Express.JS, then use biz.Express to handle that endpoint.

It accepts 2 parameters, the first one is the options, and the second one is the callback when login success or failed. You can read more about it in the References.

response is the object represents the HTTP response that an Express app sends when it gets an HTTP request. Official documentation can be found here

var loginHandler = biz.Express({}, (result, response) => {
  if (result.err) {
    console.log("User failed to log in, error: " + result.err);
    return response.send({});
  }

  var userData = {
    id: 10,
    name: "John Doe"
  };
  console.log("User logged in! Address:" + result.address);
  response.send(userData);
});

app.post("/api/login", loginHandler);

Handling payment

Payments are verified using the biz.verify function.

It accepts 2 parameters, the first one is the options, and the second one is the callback when payment success or failed. You can read more about it in the References.

You will receive the transaction ID when the user paid on Client side SDK. You should send the transaction ID along with the information to be verified on server side against the Trivechain insight explorer.

app.post("/api/verify", (req, res) => {
  var options = {
    trxId: "de224c99af5e1b7ec368f126c1c255344d58477c6f366ff0e60502b439807b69",
    toAddress: "TWpWX8BozztxYMc8AEPtXfLtADGUzf7xiG",
    amount: 900000276,
    minConfirmations: 6
  };

  biz.verify(options, (error, result) => {
    if (!error && result) {
      console.log("Valid payment");
    } else {
      console.log("Invalid payment");
    }
  });

  res.send("OK");
}