PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 22, 2022

[FIXED] How to integrate an OTP service with Magento 2

 August 22, 2022     magento, magento2     No comments   

Issue

I bought an OTP service from 2factor and got sample API. This OTP is to be generated while customer registration process.

Here's a sample API call

 <?php

$YourAPIKey='<YourAPI>';
$OTP='<OTPValue>';
$SentTo='<User10DigitNumber>';


### DO NOT Change anything below this line
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$url = "https://2factor.in/API/V1/$YourAPIKey/SMS/$SentTo/$OTP"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
echo curl_exec($ch); 
curl_close($ch);
?>

Solution

You can use observer to achieve this goal. In your case, you should use observer customer_register_success. So now:

  1. Create a new module, let's say Vendor_Module. I assume you know how to create a module. If not, refer to here.
  2. Create file app\code\Vendor\Module\etc\frontend\events.xml with the following content:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="customer_register_success">
            <observer name="call_sample_api" instance="Vendor\Module\Observer\RegisterCustomer"/>
        </event>
    </config>
    
  3. Create file app\code\Vendor\Module\Observer\RegisterCustomer with the following content:

    <?php
    namespace Vendor\Module\Observer;
    
    use \Magento\Framework\Event\ObserverInterface;
    use \Magento\Framework\HTTP\Client\Curl;
    use \Magento\Customer\Api\AddressRepositoryInterface;
    
    class RegisterCustomer implements ObserverInterface {
        //Your API details
        protected $YourAPIKey='<YourAPI>';
        protected $OTP='<OTPValue>';
    
        /**
         * @var \Magento\Framework\HTTP\Client\Curl
         */
        protected $curl;
    
        /**
         * @var \Magento\Customer\Api\AddressRepositoryInterface
         */
        protected $address;
    
        /**
         * @param Curl $curl
         * @param AddressRepositoryInterface $address
         */
        public function __construct(
            Curl $curl,
            AddressRepositoryInterface $address
        ) {
            $this->curl = $curl;
            $this->address = $address;
        }
    
        public function execute(Observer $observer) {
            //I assume you use get method
            $YourAPIKey = $this->YourAPIKey;
            $OTP= $this->OTP;
            //I assume SentTo Should be get from customer registration details, refer to Note 2
            $customer = $observer->getEvent()->getCustomer();
            $billingAddressId = $customer->getDefaultBilling();
            $billingAddress = $this->addressRepo->getById($billingAddressId);
            $SentTo= $billingAddress->getTelephone();
            //Compose URL
            $url = "https://2factor.in/API/V1/$YourAPIKey/SMS/$SentTo/$OTP";
            //See Note 1, I completely rewrite the CURL part
            $this->curl->get($url);
            $response = $this->curl->getBody();
            //Do rest of your works if applicable
    
        }
    }
    

Note 1: You can use CURL in Magento style like this.

Note 2: As customer phone number is stored in address, if you want to get customer phone number, see here.



Answered By - PY Yick
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing