2017-10-10 2 views
0

J'ai acheté le service otp de 2factor et j'ai obtenu un exemple d'API. Exemple d'API ci-dessous. Cette otp doit être générée lors de l'enregistrement du client. J'espère que j'aurai de l'aide. Merci d'avanceJ'ai besoin d'intégrer le service otp avec Magento 2

<?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); 
?> 

Répondre

0

Vous pouvez utiliser observer pour atteindre cet objectif. Dans votre cas, vous devez utiliser l'observateur customer_register_success. Alors maintenant:

  1. Créez un nouveau module, disons Vendor_Module. Je suppose que vous savez comment créer un module. Sinon, se reporter au here.
  2. Créer un fichier app\code\Vendor\Module\etc\frontend\events.xml avec le contenu suivant:

    <?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. Créer un fichier app\code\Vendor\Module\Observer\RegisterCustomer avec le contenu suivant:

    <?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: Vous pouvez utiliser CURL dans le style Magento comme this .

Remarque 2: Comme le numéro de téléphone du client est stocké dans l'adresse, si vous souhaitez obtenir le numéro de téléphone du client, voir here.

+0

Merci beaucoup. Je vais l'essayer maintenant. –