Creating an endpoint to post payment requests

The /payment endpoint will accept the payment requests from the bank portal frontend. It uses multer to accept the transaction object with the transaction details and upload the compliance and AML documents.

Before we write the code for the service, we define the cpUpload object that will be used to configure multer, to upload the documents.

The cpUpload object provides multer the configuration for the files to be uploaded. The files at the invfile (invoice file), boefile (BOE/BOL file), and docfile (other document) fields will be uploaded, as follows:

var cpUpload = upload.fields([{ name: 'invfile', maxCount: 1 }, { name: 'boefile', maxCount: 1 }, { name: 'docfile', maxCount: 1 }])

The upload object is also configured with the location to which the files will be uploaded and with the naming format of the files, as follows:

var upload = multer({ storage: storage })

var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/home/ishan/CorpRemApp/CorpRemApp/public/uploads/')
},
filename: function (req, file, cb) {
cb(null, 'BankA' + CurrTxID+'-'+file.fieldname+'.txt' )
}
})

In my case, the files are uploaded to the public/uploads folder of the bank portal frontend React app. The naming convention for the uploaded file is BankA <CurrTxID value> - <Fieldname(invfile/boefile/docfile)>.txt

Let's start writing our service, by running the following code:

app.post('/payment', cpUpload, function (request, response, next) {

When a request is posted to the /payment endpoint, the request body is first sent to multer for parsing.

The multer module will upload the files whose information is available under the invfile, boefile, and docfile request body fields, and stores them to the local storage at the location defined by us, with the nomenclature defined by us.

It then passes the details of the files and the other input parameters in the request body to the next layer, which is the business logic written in the /payment endpoint. 

We store the location of the documents in the local storage to the invpath, boepath, and docpath variables, as follows:

var invpath = request.files.invfile[0].destination + request.files.invfile[0].filename;
var boepath = request.files.boefile[0].destination + request.files.boefile[0].filename;
var docpath = request.files.docfile[0].destination + request.files.docfile[0].filename;

Next, we call the iwrite() method to publish the uploaded files to the IPFS network. The iwrite() method adds a file to the IPFS network between the bank and returns to us the hash value of the uploaded file. We add the hash value of the three files to the array object, hasharray, like this:

var hasharray = [];
iwrite(invpath).then(function(res,err){

if(res) {
hasharray[0] = res[0].hash ;
iwrite(boepath).then(function(res,err){

if(res)
{
hasharray[1] = res[0].hash ;
iwrite(docpath).then(function(res,err){

if(res)
{
hasharray[2] = res[0].hash ;

Next, we generate the transaction ID for the outgoing transaction, like this:

var txID = 'BankA' + CurrTxID;
console.log("Transaction ID",txID);

The transaction ID follows the format BankA<CurrTxID>. Thus, when the value of CurrTxID is 1, the transaction ID is BankA1. After every successful transaction submitted to the blockchain, the CurrTxID value is incremented by one, to get the next transaction ID.

Next, we call the submitTrans() method to submit the transaction to the blockchain network. The txID (transaction ID), request.body (request body with details of the transaction to be submitted), and hasharray (array object with the hash signatures of the three files) variables are sent as input parameters to the method, as follows:

submitTrans(txID, request.body, hasharray).then(function(err,res){

On the response from the submitTrans() method, we check for error responses. In the case of no responses, we call the updateBal() method to update the balance of the customer in the Bank A database.

We set the parameter object with the transaction amount and the sending customer's account number, and call updateBal(), as follows:

var paramsBal = {amount : request.body.amount,
account : request.body.saccount};

updateBal(paramsBal,function(res){
console.log("Updated result",res);

On a successful response from the updateBal() method, we call the insertTrans() method to insert the transaction into the transactions table of the Bank A database.

We send the transaction ID (txID), the transaction details in the request body, and hasharray as input parameters to the insertTrans method, like this:

if(res)
{

var paramsTx = {txID : txID,
det : request.body,
hasharray: hasharray};

insertTrans(paramsTx,function(res){

On a successful response from the insertTrans() method, we send the response object back to the requestor from the frontend with the success message, as shown in the following code block:

if(res)
{
response.json({
result: res,
});

CurrTxID++;
console.log(CurrTxID);
response.end();

}

We also increment the CurrTxID variable by 1 to get the next transaction ID. That wraps up our payment endpoint.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.4.206