DEV Community

Cover image for Metamask Integration With Laravel
Lathindu Pramduitha
Lathindu Pramduitha

Posted on • Updated on

Metamask Integration With Laravel

Alt Text

What Is MetaMask ?

MetaMask is a cryptocurrency wallet which can be install in several browsers such as Chrome, FireFox and Brave Browser [ Unfortunately you can’t install in Safari 🤪 ]. That mean you can use Metamask as a bridge between ETH blockchain and browser.

And what is the amazing one, You can store ERCi20 token also in Metamask like ETH.
So you can build custom token and use it inside your platform.

So How To Integrate ?

Let’s go with Metamask integration process.

Here I’m use simple Laravel application to show entire integration process.
And I'm using screen shots of my code to make this article much easier.

🪄 Don't Worry End Of the Document I will mention the GitHub repository and also Codepen link. then you can get entire code 😊.

So let’s divide process as a two sections.

  • How To Integrate Metamask.
  • How To Validate Transactions.

In this article I will describe the step 01.

01 How To Integrate Metamask

Here I’m using Chrome as my browser.

Step 01

Let’s create simple input with button for enter the amount.

Screenshot 2021-02-10 at 21.56.22

Screenshot 2021-02-10 at 22.24.02

Step 02

Now we need to create javascript function to submit payment. Here I add function with onClick event of the button.

Screenshot 2021-02-10 at 21.57.59

Step 03

Now we need to create javascript function to initialise, metamask and run payment.

Before that let's simplify the process which we need to do here.
1 Web3 Browser Detection

Screenshot 2021-02-10 at 22.03.20

2 Connect to MetaMask Account

Screenshot 2021-02-10 at 22.10.49

3 Do A Payment

Screenshot 2021-02-10 at 22.12.29

so here I'm adding some major validations to make process more creative. and also we must use javascript await expression because of async process.

1 Detect Web3

Screenshot 2021-02-10 at 22.16.04

2 Connect With Metamask Account

Screenshot 2021-02-10 at 22.16.55

3 Do Payment

Screenshot 2021-02-10 at 22.19.13

here you can see I mentioned a place where you should enter the payments receive address. for testings you can simply create two accounts in metamask and use one account for receive payments and second account for make payments.

after this step we can do a payments with Metamask , without an issue.

Screenshot 2021-02-10 at 22.27.27

Then we need to store transaction in the database.

for that I will use simple Ajax Request for send data to TransactionController.

Screenshot 2021-02-10 at 22.30.06

In TransactionsController
Screenshot 2021-02-10 at 22.33.09

Now we need to show all the transactions to user.

first get transactions from the transactions table.

Screenshot 2021-02-10 at 22.35.53

Now we can show all the transaction in simple bootstrap table.

Screenshot 2021-02-10 at 22.36.51

finally transactions page will looks like this.

Screenshot 2021-02-10 at 22.38.03

Okay Grate 🎉 Now all the steps are completed.

** When you try to make a transaction. In Very first attempt Metamask will ask to connect your one of account. So here you must connect your account with your website [This process need only first attempt]. Then you can send payments. but if you have already connected account you can pay directly**

let's try to make a transaction.

Enter The amount
Screenshot 2021-02-10 at 22.42.57

Click Pay Now Button
Screenshot 2021-02-10 at 22.43.12

Next And Connect With Account
Screenshot 2021-02-10 at 22.43.20

Do A Payment
Screenshot 2021-02-10 at 22.43.27

success Payment
Screenshot 2021-02-10 at 22.43.42

Then Verify Our Payment With Etherscan.

Simply copy transaction id and put in Etherscan transaction id box.

or you can use Link Like this.

https://ropsten.etherscan.io/tx/{Your_Transaction_Id}

Here ropsten is our network.

for the testings we can use ropsten network. and make sure when you go live you must use Mainnet as your network.

Test Transaction Example in Etherscan.

Screenshot 2021-02-10 at 22.53.16

In Next Article I will guid You To Make Validator to validate transactions.

I think if you read carefully you may learn something new.

Here I'm Adding Public GitHub Repository which will store all of my tutorials. you may clone it and see every tutorials what I will publish 🤗.

You may Find my Fiver Gig Here.

https://www.fiverr.com/s2/0c68721323

You may read second Article Here

GitHub Repository


CodePen

Thank You Very much.

Top comments (5)

Collapse
 
reubendickson profile image
ReubenDickson

I find this helpful. Thanks alot

Collapse
 
minematur profile image
mine matur

hi can you help me? i connected metamask and pay button is working but i cant call transactions. Can you send only this pages pls? help me

Collapse
 
prossino3 profile image
Prossino

what should i put to receive BUSD?

Collapse
 
blossampgarc profile image
Blossampgarc

Works like magic!!! love it,Thankyou

Collapse
 
faqnurul profile image
Nurul Islam

cant run on my localserver.
can you help me!