Ethereum Transactions – How to Accept on a Website

transactions

I would like to accept Ethereum on my website but can not find information how to do this. I already accept Bitcoin. With Bitcoin it is quite easy to link unique Bitcoin address with the order number. When I see the incoming transaction I know the order number customer trying to pay.
But the difference between Bitcoin and Ethereum that it is not recommended to use new account for each new transaction. Am I right?

If I have one account for all incoming payments how I can know what order customer is paying? There is no payment memo or other information coming with the transfer.

Could anyone write a short algorithm or give some ideas?

I already have VPS (Ubintu), geth running with HTTP JSON-RPC support.

Best Answer

Here's a really simplistic contract to give you some ideas. I would suggest playing with it in Browser Solidity.

It records an order using orderNumber to identify them. orderNumber is presumed to come from order processing outside of the contract... it's a reference you can use to see how much Eth was sent and from whom.

The withdraw function let's the contract owner withdraw funds.

Since you're new, I'll point out that you must send Eth to the payable recordOrder() function, but you must not send Eth to the other non-payable functions. You'll probably find yourself missing that step and getting "Invalid Jump" errors. Order numbers are in hex format, e.g. "0x1". You'll do those conversions on the javascript side.

Hastily contrived example matching payments to order numbers. No warranty. :-)

pragma solidity ^0.4.6;

contract Order {

    address public owner;

    struct OrderStruct {
        address sender;
        uint amountReceived;
    }

    mapping(bytes32 => OrderStruct) orderStructs;

    event LogWithdrawal(uint amount);
    event LogOrder(address sender, bytes32 orderNumber, uint amount);

    modifier onlyOwner {
        if(msg.sender != owner) throw;
        _;
    }    

    function recordOrder(bytes32 orderNumber) payable returns(bool success) {
        if(msg.value==0) throw;
        orderStructs[orderNumber].sender = msg.sender;
        orderStructs[orderNumber].amountReceived = msg.value;
        LogOrder(msg.sender, orderNumber, msg.value);
        return true;
    }

    function getOrder(bytes32 orderNumber) constant returns(address sender, uint amount) {
        return(orderStructs[orderNumber].sender, orderStructs[orderNumber].amountReceived);
    }

    function withdrawFromContract(uint amount) onlyOwner returns(bool success) {
        if(amount > this.balance) throw; // not enough money
        if(msg.sender.send(amount)) {
            LogWithdrawal(amount);
            return true;
        } else {
            throw;
        }
    }

}

enter image description here

Hope it helps.

Or, blindly accept payments and just keep track of who sent Eth.

pragma solidity ^0.4.6;

contract Payments {

    address public owner;

    mapping(address => uint) amountReceived;

    event LogWithdrawal(uint amount);
    event LogPayment(address indexed sender, uint amount); // indexed means javascript can filter by user and get a history

    modifier onlyOwner {
        if(msg.sender != owner) throw;
        _;
    } 

    function Payments() {
        owner = msg.sender; // whomever deploys this contract is the privileged "owner" address
    }

    function pay() payable returns(bool success) {
        if(msg.value==0) throw; // can't pay "nothing"
        amountReceived[msg.sender] += msg.value; // just a running total of receipts
        LogPayment(msg.sender, msg.value); // transaction log of all receipts
        return true;
    }

    function getReceived(address user) constant returns(uint received) {
        return amountReceived[msg.sender]; // total sum of all-time receipts from this user
    }

    function withdrawFromContract(uint amount) onlyOwner returns(bool success) {
        if(amount > this.balance) throw; // not enough money
        if(msg.sender.send(amount)) {
            LogWithdrawal(amount);
            return true;
        } else {
            throw;
        }
    }

}
Related Topic