在我的网站中添加使用 web3 的代币支付

问题描述 投票:0回答:0

谢谢帮忙。 我有一个网站www.lapapaya.org

我创建了一个名为 papayos 的 BSC 代币,这里有智能合约:https://bscscan.com/token/0x5cF26934921C3537DB91d0499568F040C5691240

我在我的网站上添加了一个支付按钮来接受加密支付。 它正在使用加密货币,但我想接受代币付款,BSC 内的 ppy。

这是我用来接受我网站付款的代码,你们中的任何一个都知道如何接受代币付款:

     <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 = '0xe790B4BEa7D812Ee9555944f8510dca7A51C31d2'
        const amountEth = "<?php echo $papayos ?>"

        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>
blockchain payment web3
© www.soinside.com 2019 - 2024. All rights reserved.