Build a Cryptocurrency Payment Checkout Function for a Flask App Using Python, Infura, Metamask, and Web3

The cryptocurrency payment method facilitates merchants to receive funds from buyers to avoid strict regulations from traditional bank payments. In a way, it also can save costs in long run and facilitate business expansion by using in-house built Cryptocurrency payment methods.

In this article, I would walk through briefly how to use Python, Infura, and Web3 modules to develop a Cryptocurrency payment checkout on the website. By the end of this tutorial, you can add this on your web and let your buyers check out and send you funds through the Crypto network.

The cryptocurrency payment method facilitates merchants to receive funds from buyers to avoid strict regulations from traditional bank payments. In a way, it also can save costs in long run and facilitate business expansion by using in-house built Cryptocurrency payment methods.

In this article, I would walk through briefly how to use Python, Infura, and Web3 modules to develop a Cryptocurrency payment checkout on the website. By the end of this tutorial, you can add this on your web and let your buyers check out and send you funds through the Crypto network.

Table of Contents: Add a Cryptocurrency Payment Method to a Flask App Using Python, Infura, Metamask, and Web3 

Arguments and value required to create a transaction

Basically, there are some must-have arguments and values in a transaction and the data format is in the dictionary. Here is the fundamental formula as follows:

transaction = {"gasPrice": gasPrice, "chainId": int(

       chain_id), "from": address, "to": receiverAddress, "value": valuetoSend, "nonce": nonce}

   gas = w3.eth.estimate_gas(transaction)

   transaction['gas'] = gas

  • Sender wallet address: Buyers add her/his address by signing in to the merchant Website when checking out the item
  • Receiver wallet address (It’s the merchant account by default added in Flask .env)
  • Amount to pay
  • Gas price: Processing price per transaction
  • Chaine ID: Blockchain node ID you are connecting with
  • Nonce: Transaction Count from sender pubic wallet ID
  • The total cost of gas

Some argument value can be gained as follows:

  • Gas price: w3.eth.gas_price
  • Chain ID: https://chainlist.org/
  • Nonce: w3.eth.getTransactionCount(buyer wallet address)
  • Gas price: w3.eth.gas_price
  • Total Cost of Gas:

gas = w3.eth.estimate_gas(transaction)

   transaction2['gas'] = gas

Metamask and Infura API node to Connect Ethereum Blockchain

I’ve shared in another article before regarding Infura API connection and Metamask usage instructions, please check out this article for more details.

https://www.easy2digital.com/web3/use-infura-and-metamask-in-python-scripts-to-deploy-web3-on-ethereum/

In the Cryptocurrency payment method added to the Flask app, this section can be added at the global level of a script that can be shared to be used in each local function. Sample as follows:

# -------------- Connect ETH network using a node: Infura -------------- ##

w3 = Web3(Web3.HTTPProvider(

   "Your API endpoint"))

gasPrice = w3.eth.gas_price

chain_id = your chain ID

private_key = os.environ.get('PRIVATE_KEY2')

def accountBalance(address):

   accountBalance = w3.eth.get_balance(address)

   return accountBalance

Sign and send the transaction

Now network connection and transaction module are ready. We can start testing the checkout from buyers.

  signed_txn = w3.eth.account.sign_transaction(

       transaction2, private_key='buyer Metamask sign-in authentication')

   tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

   tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

Flask route

Last but not least, we need to connect this function with the Flask main app route. The code sample is as follows:

@app.route('/', methods=['GET', 'POST'])

def web3Transaction123():

   if request.args.get('SenderAddress') and request.args.get('sendingValue'):

       prompt = request.args.get('SenderAddress')

       prompt3 = request.args.get('sendingValue')

       if request.args.get('user_signed'):

           response2 = web3Transaction(

               prompt, prompt3, request.args.get('user_signed'))

           flash("Done! Thank you for your payment")

           return render_template('test.html', response22=response2)

   return render_template('test.html')

For the user authentication using Metamas signing-in, I will share in the next article.

Full Python Script of a Cryptocurrency payment method creation using Infura, Metamask, and Web3

If you are interested in Web3.0 tutorial 8 – Cryptocurrency payment method creation using Infura, Metamask, and Web3, please subscribe to our newsletter by adding the message “Web3.0 tutorial 8”. We would send you the script immediately to your mailbox.

I hope you enjoy reading Web3.0 tutorial 8 – Cryptocurrency payment method creation using Infura, Metamask, and Web3. If you did, please support us by doing one of the things listed below, because it always helps out our channel.