7

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.

7

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;
        }
    }

}
  • Rob, thank you for your answer! It may be a stupid question but as you correctly noticed I'm new ) Should I create new contract for each order? – Andrei Jan 30 '17 at 18:51
  • No problem. Remember, it's just a simplistic example to give you some ideas and hopefully you can sort of see how it flows. Before you put anything into production, suggest you look for a carefully considered solution that meets your requirements; not something a guy put together in a few minutes and never tested. ;-) – Rob Hitchens - B9lab Jan 30 '17 at 18:54
  • If it's helpful, you can vote up ... up arrow. ;-) – Rob Hitchens - B9lab Jan 30 '17 at 18:55
  • For sure I'll test it ) But I'm not sure how can I use it. If I will create new smart contract for new order payment it is the same if I create personal account? What is the difference in my case. – Andrei Jan 30 '17 at 19:53
  • It's a just a little toy. A javascript function would have everyone send Eth to a single contract address with an argument representing the order identifier. The actually receives Eth, hence the withdraw() function so the owner can get the funds. The contract records the funds received for each order. Meant to address your initial question about matching funds received to a single address to who actually sent, and how much. – Rob Hitchens - B9lab Jan 30 '17 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.