Integrate USDT BEP20 Automatic Payments In a Laravel Application

May 04, 2024

Table Of Contents

Introduction

This guide will direct you on how to integrate USDT BEP20 Automatic Payments in your Laravel website, allowing you to start accepting USDT Automatic Payments over the Binance Smart Chain (BSC) directly to your wallet without having to rely on any third part payment gateway.

Hierarchical Deterministic Wallets (HD) Wallets

This PHP script leverages the power of Hierarchical Deterministic Wallets (HD) Wallets to generate addresses that serve the purpose of receiving the payments within your application.

With HD Wallets, all the payment addresses are generated from on single mnemonic phrase and that is all you need to start accepting USDT BEP20 payments.

Prerequisites:

This guide assumes you are actively engaged in developing a Laravel application with user authentication and are keen to integrate USDT BEP payments, enabling seamless acceptance of USDT payments from your user base.

Additionally, it is assumed that you possess a Crypto wallet of your choice that gives you access to your wallet's Mnemonic Phrase. The wallet address should always hold enough BNB to cater for transfer fees and in this case each transaction consumes 0.0004 BNB

Familiarity with the Laravel PHP framework is also presumed for a smoother understanding of the integration process.

It is also important to note that this scripts is developed in Laravel 11 and in case of any compatibility issue with your project Laravel version, feel free to contact me via telegram @lakescripts or via email lakescripts@gmail.com

You should make sure to have the php-bcmath extension installed on your server

With that said, let's dive in and get started with integrating USDT BEP20 Automatic Payments in your Laravel project so that you can start accepting USDT payments over the Binance Smart Chain (BSC) Network.

Step 1: Install required Packages

To begin, you'll need to install the required PHP packages with Composer as shown below.

        
          composer require bitwasp/bitcoin
        
      

        
          composer require kornrunner/keccak
        
      

        
          composer require simplito/elliptic-php
        
      

        
         composer require sop/asn1
        
      

        
         composer require sop/crypto-encoding
        
      

        
         composer require sop/crypto-types
        
      

        
         composer require web3p/ethereum-tx
        
      

Step 2: Configuration

As you already have your Laravel project in place, it's time to manage the configuration by adding the necessary models, controllers, and other essential files to the project.

Begin by downloading and extracting the USDT BEP20 Automatic Payments With HD Wallets for Laravel zipped file. The file structure is outlined below:

file structure

Now, proceed to copy the files into your Laravel application, following the designated file structure.

For certain files (routes/console.php, app/Models/User.php, routes/web.php), simply copy the specified lines of code into the corresponding files in your Laravel application.

Copy files

To begin, copy the scheduler commands to your Laravel project.

        
          app/Console/Commands/CheckUsdtPayments.php
          app/Console/Commands/MoveFunds.php
        
      

Copy the controller into your project.

        
          app/Http/Controllers/UsdtPaymentController.php
        
      

Copy the Utils folder and along with the contained files

        
          app/Http/Utils/Formatter.php
          app/Http/Utils/Util.php
        
      

Copy the models to your project.

        
          app/Models/BepAddress.php,
          app/Models/UsdtPayment.php,
          app/Models/Bsc.php
        
      

Copy the migration file into your migrations folder.

        
          database/migrations/2024_04_28_172151_create_bep_addresses_table.php,
          database/migrations/2024_04_28_172207_create_usdt_payments_table.php
        
      

Copy the js folder along with its contained javascript files.

        
          public/js/easy.qrcode.min.js,
          public/js/jquery.min.js,
          public/js/pay.js,
        
      

Also copy the amount.blade.php and pay.blade.php files along with their directory to your views folder

        
          usdt_payments/amount.blade.php,
          usd_payments/pay.blade.php
        
      

Copy and paste code

routes/console.php

Copy the following code into your console.php file.

        
          
use Illuminate\Support\Facades\Schedule;

Schedule::command('app:check-usdt-payments')->everyMinute()->withoutOverlapping();
Schedule::command('app:move-funds')->everyThreeMinutes()->withoutOverlapping();

        
      

The cron job

To facilitate payment processing and maintain constant interaction with the BSC blockchain, the system employs a cron job/task scheduling to execute the scheduled commands mentioned above. For comprehensive guidance on managing Laravel task scheduling and cron jobs, refer to the Laravel documentation at Laravel Scheduling

For testing purposes, initiate the schedules by executing the following Artisan command.

        
          php artisan schedule:run
        
      

Alternatively, you can directly execute the commands using the Artisan commands below for the 'check-usdt-payments', and 'move-funds' command.

        
          Php artisan app:check-usdt-payments
          Php artisan app:move-funds
        
      

app/Models/User.php

Copy the following code into your User Model.

        
          public function usdtPayments(){
        return $this->hasMany(UsdtPayment::class);
    }
    public function getTotalUsdtPayments(){
        return $this->usdtPayments()->where('status', '=', 'success')->sum('amount');
    }
        
      

routes/web.php

Copy the following code to your routes/web.php file

        
          Route::middleware([
    'auth:sanctum',
    config('jetstream.auth_session'),
    'verified',
])->group(function () {
    Route::get('/usdt-payment', [
        \App\Http\Controllers\UsdtPaymentController::class,
        'showPayment',
    ]);
    Route::post('/usdt-payment', [
        \App\Http\Controllers\UsdtPaymentController::class,
        'initiatePayment',
    ]);
    Route::get('/pay-usdt', [
        \App\Http\Controllers\UsdtPaymentController::class,
        'pay',
    ]);
    Route::get('/check_status', [
        \App\Http\Controllers\UsdtPaymentController::class,
        'checkStatus',
    ]);
});

        
      

Run the migrations.

Now, proceed to execute the migrations to update and generate the necessary tables in the database.

During this process, new tables will be generated, specifically the 'bep_addresses' and 'usdt_payments' table."

Execute the migrations by running the following artisan command below.

        
          php artisan migrate
        
      

.env

Add the MNEMONIC_PHRASE, BEP_NODE_URI and the BEP_NETWORK in your .env file. The mnemonic phrase is a 12 word phrase the for your wallet and from it, all the payment addresses will be generated including the central wallet which will be the address at index zero.

You will also need to set the BEP_USER and BEP_PASSWORD in case you are using a BSC node with authentication.

        
         MNEMONIC_PHRASE=""
         BEP_NODE_URI="https://bsc-dataseed1.defibit.io/"
         BEP_NETWORK="mainnet"
         BEP_USER=
         BEP_PASSWORD=
        
      

Step 3: Testing and Running

With everything set up, it's time to proceed to test the payments.

Now, log in as a user and visit /usdt-payment. You should encounter a straightforward page prompting you to enter the desired payment amount before proceeding.

Feel free to customize the user interface and flow to align with the specific requirements of your application.

        
          usdt_payments/amount.blade.php
        
      

amount

Next, you will be redirected to a page displaying the payment address and QR code.

qrcode

After making the payment to the displayed wallet address, execute the following Artisan commands to process the payment by interacting with the BSC blockchain in the development environment

        
          Php artisan app:check-usdt-payments
          Php artisan app:move-funds
        
      

After sending the funds to the specified address, You should receive a successful alert indicating that the payment was processed successfully.

With all configurations in place, the funds should be seamlessly transferred to your central wallet.

With that said, you should now be equipped to proceed and make the final adjustments in accordance with your specific application requirements.

Thanks and Best Wishes