PrePay API Tutorial (in-store)

This tutorial gives a stepwise approach to call the Pay Transaction API.

POCKYT ADD API sandbox example

// Import crypto for MD5 hash calculation

var crypto = require('crypto');

// Assign all Sanbox parameters

var URL = "<https://mapi.yuansferdev.com/app-instore/v3/prepay"> ;  
var merchantNo = "200043" ;  
var storeNo = "303660" ;  
var MyToken = "359c05eb811c7c8576f4a8a277dc6f6b" ;

// construct POS fields and assign to object  
var myPOSParamObject = {  
    merchantNo: merchantNo,  
    storeNo: storeNo,  
    transactionNo: "316129873376769782",  
    paymentBarcode: "286521182446652715",  
    vendor: "alipay"  
}

// Calculate the VeriSign signature:

var MySignature = CalculateSignature(MyToken, myPOSParamObject)

// Add the signature to the POS parametes so we  
// can use it in the body of the API call

var MyPocketParamObject = {  
        merchantNo: myPOSParamObject.merchantNo,  
        storeNo: myPOSParamObject.storeNo,  
        verifySign: MySignature,  
        transactionNo: myPOSParamObject.transactionNo,  
        paymentBarcode: myPOSParamObject.paymentBarcode,  
        vendor: myPOSParamObject.vendor  
}

// function that calculates the signature according to Pockyt rules

function CalculateSignature(token,parameters)  
{  
    // calculate the hash value of the token

var ApiTokenHashvalue = crypto.createHash('md5').update(token).digest("hex")

// order parameters alphabetically  
var SortedParams = sortObj(parameters);

// Concatenate: add '&' between key and value pair and replace:

for =  
    var MyString = '' ;  
    for (const [key, value] of Object.entries(SortedParams)) {  
        MyString += (`${key}=${value}&`);}

//  add hash value of token at the and of the string

MyString += ApiTokenHashvalue ;

// create the verifySign

 const MySignature = crypto.createHash('md5').update(MyString).digest("hex");

return MySignature;

// algabetical sort helper function

function sortObj(obj) {  
    return Object.keys(obj).sort().reduce(function (result, key) {  
      result[key] = obj[key];  
      return result;  
    }, {});  
  }

}

// Prepare JSON that will be used in the body of the API call

var MyPocketParamJason = JSON.stringify(MyPocketParamObject);  
console.log(MyPocketParamJason);

fetch(URL, {  
  method: "POST",  
  headers: {  
    "Content-Type": "application/json"  
  },  
  body: MyPocketParamJason  
})  
.then(response => response.json())  
.then(data => console.log(data))  
.catch(error => console.error(error));