Forked from ksaitor/ethereum-payment-metamask.html
Last active
February 5, 2022 23:09
-
-
Save El-JojA/2cca7b2e136aca4f28094d29707cc1c3 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> | |
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script> | |
</head> | |
<body> | |
<div> | |
<input id="paymentAddress" type="text" value="0x6cbddf616a1ad5ae13628b342cd667c6bc919ba8" /> | |
<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 = $("#paymentAddress").val(); | |
const amountEth = "0.1" | |
web3.eth.sendTransaction({ | |
to: paymentAddress, | |
value: web3.utils.toWei(amountEth, 'ether'), | |
from: '0x6cBdDF616a1ad5aE13628B342CD667C6Bc919Ba8' | |
}, (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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it says ! No Metamask (or other Web3 Provider) installed have u updated your code to this ? https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3