Skip to content

Instantly share code, notes, and snippets.

@yupnano
Last active May 19, 2018 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yupnano/376de5f697c89fda3dddc5bd84433d75 to your computer and use it in GitHub Desktop.
Save yupnano/376de5f697c89fda3dddc5bd84433d75 to your computer and use it in GitHub Desktop.
<script src="nebPay.js"></script>
<script >
var NebPay = require("nebpay");
var nebPay = new NebPay();
var serialNumber; //transaction serial number
var intervalQuery; //periodically query tx results
//initiate the transaction with a button click, here is an example of calling a smart contract
function onButtonClick() {
var to = dappAddress; //the smart contract address of your Dapp
var value = "0";
var callFunction = "" //the function name to be called
var callArgs = "" //the parameter, it's format JSON string of parameter arrays, such as'["arg"]','["arg1","arg2]'
var options = {
goods: { //commodity description
name: "example"
}
}
//Send transaction (here is smart contract call)
serialNumber = nebPay.call(to, value, callFunction, callArgs, options);
//Set a periodically query
intervalQuery = setInterval(function() {
funcIntervalQuery();
}, 10000); //it's recommended that the query frequency is between 10-15s.
}
//Query the result of the transaction. queryPayInfo returns a Promise object.
function funcIntervalQuery() {
nebPay.queryPayInfo(serialNumber) //search transaction result from server (result upload to server by app)
.then(function (resp) {
console.log("tx result: " + resp) //resp is a JSON string
var respObject = JSON.parse(resp)
if(respObject.code === 0){
//The transaction is successful
clearInterval(intervalQuery) //stop the periodically query
}
})
.catch(function (err) {
console.log(err);
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment