Skip to content

Instantly share code, notes, and snippets.

@ayinot
Created February 15, 2018 12:14
Show Gist options
  • Save ayinot/1d030becd5107092a19dcaad4ced7ab6 to your computer and use it in GitHub Desktop.
Save ayinot/1d030becd5107092a19dcaad4ced7ab6 to your computer and use it in GitHub Desktop.
E-commerce Payment
const express=require('express'); // importing express js framework
const Web3=require('web3'); //initiating web3
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); // connect to blockchain using web3
const bodyParser=require('body-parser'); // pasrsing the encoded format
const app=express(); //loading the express object
//loading the abi for the contract
var retailContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"itemId","type":"uint256"}],"name":"isSold","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getItemList","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"count","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"itemId","type":"uint256"}],"name":"getItem","outputs":[{"name":"","type":"uint256"},{"name":"","type":"string"},{"name":"","type":"address"},{"name":"","type":"uint256"},{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"itemList","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"itemId","type":"uint256"},{"name":"_itemname","type":"string"},{"name":"_itemprice","type":"uint256"}],"name":"updateItem","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_itemname","type":"string"},{"name":"_itemprice","type":"uint256"}],"name":"addItem","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getItemCount","outputs":[{"name":"itemCount","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"itemStructs","outputs":[{"name":"itemname","type":"string"},{"name":"itemowner","type":"address"},{"name":"itemprice","type":"uint256"},{"name":"itemid","type":"uint256"},{"name":"isSold","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"itemId","type":"uint256"}],"name":"buyItem","outputs":[{"name":"","type":"string"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"admin","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
app.use(bodyParser.urlencoded({extended:true})); //pasrsing into URL encoded form
app.use(bodyParser.json()); // pasrsing into JSON
// Deploying the contract
app.post('/deployContract',function(req,res){
var test = retailContract.new(
{
from: web3.eth.accounts[0],
data: '0x60606040526000600155341561001457600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550610eaf806100636000396000f3006060604052600436106100af576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630110a176146100b4578063063b2e5b146100ef57806306661abd146101595780633129e773146101825780633d4d36001461026a57806353b200e5146102a157806362d6bbdf146103895780637749cf2314610468578063c10250a014610491578063e7fb74c714610596578063f851a44014610627575b600080fd5b34156100bf57600080fd5b6100d5600480803590602001909190505061067c565b604051808215151515815260200191505060405180910390f35b34156100fa57600080fd5b6101026106a9565b6040518080602001828103825283818151815260200191508051906020019060200280838360005b8381101561014557808201518184015260208101905061012a565b505050509050019250505060405180910390f35b341561016457600080fd5b61016c610707565b6040518082815260200191505060405180910390f35b341561018d57600080fd5b6101a3600480803590602001909190505061070d565b60405180868152602001806020018573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200184815260200183151515158152602001828103825286818151815260200191508051906020019080838360005b8381101561022b578082015181840152602081019050610210565b50505050905090810190601f1680156102585780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390f35b341561027557600080fd5b61028b600480803590602001909190505061086a565b6040518082815260200191505060405180910390f35b34156102ac57600080fd5b61030e600480803590602001909190803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190803590602001909190505061088e565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561034e578082015181840152602081019050610333565b50505050905090810190601f16801561037b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561039457600080fd5b6103ed600480803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091908035906020019091905050610976565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561042d578082015181840152602081019050610412565b50505050905090810190601f16801561045a5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561047357600080fd5b61047b610b3e565b6040518082815260200191505060405180910390f35b341561049c57600080fd5b6104b26004808035906020019091905050610b4b565b60405180806020018673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001858152602001848152602001831515151581526020018281038252878181546001816001161561010002031660029004815260200191508054600181600116156101000203166002900480156105835780601f1061055857610100808354040283529160200191610583565b820191906000526020600020905b81548152906001019060200180831161056657829003601f168201915b5050965050505050505060405180910390f35b6105ac6004808035906020019091905050610bad565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156105ec5780820151818401526020810190506105d1565b50505050905090810190601f1680156106195780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561063257600080fd5b61063a610d65565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b60006002600083815260200190815260200160002060040160009054906101000a900460ff169050919050565b6106b1610d8a565b60038054806020026020016040519081016040528092919081815260200182805480156106fd57602002820191906000526020600020905b8154815260200190600101908083116106e9575b5050505050905090565b60015481565b6000610717610d9e565b60008060006002600087815260200190815260200160002060030154600260008881526020019081526020016000206000016002600089815260200190815260200160002060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600260008a815260200190815260200160002060020154600260008b815260200190815260200160002060040160009054906101000a900460ff16838054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156108505780601f1061082557610100808354040283529160200191610850565b820191906000526020600020905b81548152906001019060200180831161083357829003601f168201915b505050505093509450945094509450945091939590929450565b60038181548110151561087957fe5b90600052602060002090016000915090505481565b610896610d9e565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156108f157600080fd5b8260026000868152602001908152602001600020600001908051906020019061091b929190610db2565b508160026000868152602001908152602001600020600201819055506040805190810160405280601481526020017f75706461746564207375636365737366756c6c7900000000000000000000000081525090509392505050565b61097e610d9e565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156109d957600080fd5b6001805401600181905550826002600060015481526020019081526020016000206000019080519060200190610a10929190610db2565b503360026000600154815260200190815260200160002060010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550816002600060015481526020019081526020016000206002018190555060015460026000600154815260200190815260200160002060030181905550600060026000600154815260200190815260200160002060040160006101000a81548160ff02191690831515021790555060038054806001018281610ae99190610e32565b91600052602060002090016000600154909190915055506040805190810160405280601781526020017f7375636365737366756c6c79206164646564206974656d000000000000000000815250905092915050565b6000600380549050905090565b600260205280600052604060002060009150905080600001908060010160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060020154908060030154908060040160009054906101000a900460ff16905085565b610bb5610d9e565b600115156002600084815260200190815260200160002060040160009054906101000a900460ff1615151415610bea57600080fd5b6002600083815260200190815260200160002060020154341415610ce75760008210158015610c1b57506001548211155b1515610c2657600080fd5b336002600084815260200190815260200160002060010160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060016002600084815260200190815260200160002060040160006101000a81548160ff0219169083151502179055506040805190810160405280600e81526020017f4974656d207075726368617365640000000000000000000000000000000000008152509050610d60565b3373ffffffffffffffffffffffffffffffffffffffff166108fc349081150290604051600060405180830381858888f193505050501515610d2757600080fd5b6040805190810160405280601281526020017f496e73756666656369656e742066756e6473000000000000000000000000000081525090505b919050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b602060405190810160405280600081525090565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610df357805160ff1916838001178555610e21565b82800160010185558215610e21579182015b82811115610e20578251825591602001919060010190610e05565b5b509050610e2e9190610e5e565b5090565b815481835581811511610e5957818360005260206000209182019101610e589190610e5e565b5b505050565b610e8091905b80821115610e7c576000816000905550600101610e64565b5090565b905600a165627a7a7230582090458b5872b13934410424d1d2df653505afc3189a6346946111442839863d190029',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
res.send({'addresss':contract.address});
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
});
// Add an Item to the list
app.post('/addItem',function(req,res){
let contractaddress = req.body.contractaddress;
let anyAccount = req.body.anyAccountaddress;
let itemname = req.body.name;
let itemprice = parseInt(req.body.price);
let retailInstance=retailContract.at(contractaddress);
retailInstance.addItem(itemname,itemprice,{from:anyAccount,gas: '4700000'},function(err,data){
if(err) {
console.log(err);
res.status(500).send({Error:'Only Admin is authorized to add Item'})
} else {res.send({'Result':data,'message':'Item Added successfully'})}
})
})
// Update an Item
app.post('/updateItem',function(req,res){
let contractaddress = req.body.contractaddress;
let anyAccount = req.body.anyAccountaddress;
let itemname = req.body.name;
let itemprice = parseInt(req.body.price);
let retailInstance=retailContract.at(contractaddress);
retailInstance.updateItem(itemname,itemprice,{from:anyAccount,gas: '4700000'},function(err,data){
if(err) {
console.log(err);
res.status(500).send({Error:'Only Admin is authorized to update the Item details'})
} else {res.send({'Result':data,'message':'Item updated successfully'})}
})
})
//Buy an Item in the list
app.post('/buyItem',function(req,res){
let contractaddress = req.body.contractaddress;
let buyeraddress = req.body.buyerAddress;
let itemId = parseInt(req.body.itemId);
let amount = req.body.amount;
let accountBalance = web3.eth.getBalance(buyeraddress).toNumber();
console.log(accountBalance);
let retailInstance = retailContract.at(contractaddress);
retailInstance.buyItem(itemId,{from:buyeraddress,gas:'4700000',value:amount},function(err,data){
if(err) {
console.log(err);
res.send({Error:'Item has already sold'})
} else {res.send({'Result':data.message})}
})
})
// get all the item details
app.get('/items',function(req,res){
let contractaddress = req.query.contractaddress;
let retailInstance=retailContract.at(contractaddress);
let itemlength = parseInt(retailInstance.getItemCount.call());
console.log(itemlength);
let items=[];
for(var i= 1; i <= itemlength;i++){
items[i] = retailInstance.getItem(i);
}
res.send({'Item List':items})
})
// Get Item data by itemId
app.get('/getItem',function(req,res){
let contractaddress=req.query.contractaddress;
let itemId=req.query.itemId;
console.log(contractaddress);
let retailInstance=retailContract.at(contractaddress);
retailInstance.getItem(itemId,function(err,data){
if(err) {
console.log(err);
res.send({Error:'Error occured in retreiving'})
} else {res.send({'Result':data})}
})
});
// Get Item count to the List
app.get('/getItemCount',function(req,res){
let contractaddress=req.query.contractaddress;
console.log(contractaddress);
let retailInstance=retailContract.at(contractaddress);
let itemlength = retailInstance.getItemCount.call();
res.send(itemlength);
});
app.listen(3000,'127.0.0.1',function(){
console.log('webservice started');
}); // starting the webservice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment