1

I would like to accept my own ERC20 token as payment on my website. The current way I'm doing it requires 2 transactions and a 3rd party plugin that requires a fee. For security's sake, I'd rather have no 3rd party plugin. This will also likely prove cheaper for my customers and overall peace of mind for me.

Conceptually, I'd think it would be as simple as just sending the tokens to my address with a memo.

But would like it to be as easy as possible for anyone. I don't mind using metamask as I think that is an easy route for most people to follow. The goal is to make this so simple, someone who doesn't know much about crypto could use it. Thanks!

2 Answers 2

2

Asking the users to send tokens to your address could be a simple solution. You could also include code in your website to allow them to pay by just clicking a button. In the last case, you can use web3 on your webpage. This is an intro to how to do it:

Check if Web3 has been injected (i.e, metamask):

if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
    } else {
    // set the provider you want from Web3.providers
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

Then create an instance to your contract:

// instantiate your contract
web3.eth.defaultAccount = web3.eth.accounts[0];
var ABI = [put here your ABI]
var contractInstance = web3.eth.contract(ABI);
var MyContract = contractInstance.at('contractaddress')

Now you can access your contract functions. For instance, you can use transfer function of the ERC20 standard to let people send tokens to the address you decide or directly to your contract (this last option implies that you have a way to transfer from your contract to your personal account).

Assume then that you have a button on the website to execute the payment, then onClick you can call a function (i.e, payforproducts) , I will assume for simplicity that you have a variable called amount that indicate how many tokens the user will pay.

function payforproduct(amount){
    MyContract.transfer(web3.eth.accounts[0], 'the contract Address', amount, {from: web3.eth.accounts[0]}, function(err, res){})
}    

Here you need to wait for the transaction to be confirmed to be sure that the tokens are submitted to your contract (or any other address you define)

This will show metamask window in the user side asking to approve the transfer of the tokens, they will see the amount and the recipient account, so that they can check that everything is ok, that you are not charging more than expected or that the address is the correct one.

That will be all.

Hope this helps

1
  • Jaime you're awesome!
    – shyheim103
    Commented May 28, 2018 at 18:14
1

You just have to post a wallet address on your website and anybody will be able to send ERC20 tokens to this address. No JavaScript needed.

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