Last active
November 4, 2024 15:18
-
-
Save ksaitor/6c832c106a8b9aee4f76a9145eb5d764 to your computer and use it in GitHub Desktop.
How to add Ethereum payments to your site with MetaMask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<div> | |
<button class="pay-button">Pay</button> | |
<div id="status"></div> | |
</div> | |
<script type="text/javascript"> | |
window.addEventListener('load', async () => { | |
if (window.ethereum) { | |
window.web3 = new Web3(ethereum); | |
try { | |
await ethereum.enable(); | |
initPayButton() | |
} catch (err) { | |
$('#status').html('User denied account access', err) | |
} | |
} else if (window.web3) { | |
window.web3 = new Web3(web3.currentProvider) | |
initPayButton() | |
} else { | |
$('#status').html('No Metamask (or other Web3 Provider) installed') | |
} | |
}) | |
const initPayButton = () => { | |
$('.pay-button').click(() => { | |
// paymentAddress is where funds will be send to | |
const paymentAddress = '0x192c96bfee59158441f26101b2db1af3b07feb40' | |
const amountEth = 1 | |
web3.eth.sendTransaction({ | |
to: paymentAddress, | |
value: web3.toWei(amountEth, 'ether') | |
}, (err, transactionId) => { | |
if (err) { | |
console.log('Payment failed', err) | |
$('#status').html('Payment failed') | |
} else { | |
console.log('Payment successful', transactionId) | |
$('#status').html('Payment successful') | |
} | |
}) | |
}) | |
} | |
</script> | |
</body> | |
</html> |
thank you
I will update this code with wallet model so it will work on safari or mobile as well. right now this code is only for chrome/firefox with metamask ext only
You can also choose to use CryptoPay, our all in one crypto payments solution for your WordPress sites.
Review the CryptoPay: https://beycanpress.com/cryptopay/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you