Skip to content

Instantly share code, notes, and snippets.

@ayinot
Last active February 15, 2018 00:05
Show Gist options
  • Save ayinot/c45d6b21185789d811c99c6a4c674392 to your computer and use it in GitHub Desktop.
Save ayinot/c45d6b21185789d811c99c6a4c674392 to your computer and use it in GitHub Desktop.
E-commerce Payment
pragma solidity ^0.4.2;
contract retail {
address public admin;
uint public count=0;
// a constructor to set the admin i.e the person who deploys the contract
function retail() {
admin= msg.sender;
}
// A struct of a Item
struct itemstruct {
string itemname;
address itemowner;
uint itemprice;
uint itemid;
bool isSold;
}
mapping(uint => itemstruct) public itemStructs; // mapping the itemId with itemStruct
uint[] public itemList; //list of Items
// A function to add a Item in the store
function addItem(string _itemname,uint _itemprice)public returns (string){
if(msg.sender!=admin) {throw;}
count = count+1;
itemStructs[count].itemname =_itemname;
itemStructs[count].itemowner =msg.sender;
itemStructs[count].itemprice =_itemprice;
itemStructs[count].itemid =count;
itemStructs[count].isSold =false;
itemList.push(count);
return ("successfully added item");
}
// A function to get a particular item by Id
function getItem(uint itemId) public constant returns(string,address,uint,bool){
return (itemStructs[itemId].itemname,itemStructs[itemId].itemowner,itemStructs[itemId].itemprice,itemStructs[itemId].isSold);
}
// A function to get the count of items in the list
function getItemCount() public constant returns(uint itemCount){
return itemList.length;
}
// A function to update items in the list
function updateItem(uint itemId,string _itemname,uint _itemprice) public returns(string) {
if(msg.sender!=admin) throw;
itemStructs[itemId].itemname = _itemname;
itemStructs[itemId].itemprice = _itemprice;
return ("updated successfully");
}
// A function to update the itemprice alone
//this method will override updateItem
function updateItem(uint itemId,uint _itemprice) public returns(string) {
if(msg.sender!=admin) throw;
itemStructs[itemId].itemprice = _itemprice;
return ("updated successfully");
}
// A function to update the item name alone
//this method will override updateItem
function updateItem(uint itemId,string _itemname) public returns(string) {
if(msg.sender!=admin) throw;
itemStructs[itemId].itemname = _itemname;
return ("updated successfully");
}
// a function to buy an Item
function buyItem(uint itemId) payable public returns (string) {
if(itemStructs[itemId].isSold==true) throw; else {
if(msg.value==itemStructs[itemId].itemprice) {
require(itemId >= 0 && itemId <= count);
itemStructs[itemId].itemowner = msg.sender;
itemStructs[itemId].isSold = true;
return ("Item purchased"); }
else {
msg.sender.transfer(msg.value);
throw;
}
}
}
// A function to check whether a product is sold
function isSold(uint itemId) public constant returns (bool){
return itemStructs[itemId].isSold;
}
// a function to get the item list
function getItemList() public constant returns (uint[]){
return itemList;
}
// A function to check the sender
function sender() public constant returns(address){
return msg.sender;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment