Nowadays, Laravel is probably the most secure and dependable platform for e-commerce web application and it’s excessive in demand. However, for a purchase order to complete it’s typically necessary to have an OTP or Security Code verification process to validate a customer purchase or email/number verification. Laravel doesn’t have a inbuilt OTP or Security Code based mostly verifier process, there are a number of OTP verifier present to serve the aim, however doesn’t comes with combo of retry or resend mechanism and configurable digit or timeouts. Today I’ll focus on on one of many package for Laravel, which is able to allow you to set the OTP verification easy .

So, Let’s start and follow below three easy steps.

  1. You possibly can generate OTP using any unique identification (Prefer Mobile number or Email),
  2. Send to end user using any notification provider
  3. Finally, confirm using the same unique identification.

Installation and Setup process


You’ll be able to install this package via composer by running this command:

composer require techzpd/otp

You can publish and run the migrations with:

php artisan vendor:publish --provider="techzpd\Otp\OtpServiceProvider" --tag="migrations"
php artisan migrate

To publish the config file for laravel you can run

php artisan vendor:publish --provider="techzpd\Otp\OtpServiceProvider" --tag="config"

Once you published you can configure all otp-generator.php config values based on your requirements.

Generate an OTP and Usage

use techzpd\Otp\Otp;
$otp =  Otp::generate($identifier);
$verify = Otp::validate($identifier, $otp->token);
// response
  "status": true
  "message": "OTP is valid"

You have control to update the setting at otp-generator.php config file

Other Generate and Advance Usage

use techzpd\Otp\Otp;
$otp =  Otp::setValidity(30)  // otp validity time in mins
      ->setLength(4)  // Lenght of the generated otp
      ->setMaximumOtpsAllowed(10) // Number of times allowed to regenerate otps
      ->setOnlyDigits(false)  // generated otp contains mixed characters ex:ab2312
      ->setUseSameToken(true) // if you re-generate OTP, you will get same token
$verify = Otp::setAllowedAttempts(10) // number of times they can allow to attempt with wrong token
    ->validate($identifier, $otp->token);

TIP: OTP is mostly used for user verification. So the easiest method of determining the unique secret is the user’s e mail or phone number. Or possibly even the User ID. You can even get creative about the unique secret. You can use md5($email) the md5 of the user’s email or phone number.

Also Read : How To Convert English Number To Nepali Number Using PHP, Python And JavaScript

Leave Your Comment