2017-10-10 43 views
0

我從2factor購買了otp服務並獲得了示例API。 下面的示例API。 此otp將在客戶註冊過程中生成。我希望我能得到幫助。在此先感謝我需要與Magento 2集成otp服務

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

回答

0

您可以使用observer來實現此目標。在你的情況下,你應該使用觀察者customer_register_success。所以現在:

  1. 創建一個新的模塊,比方說Vendor_Module。我假設你知道如何創建一個模塊。如果不是,請參閱here
  2. 與以下內容創建文件app\code\Vendor\Module\etc\frontend\events.xml

    <?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. 與以下內容創建文件app\code\Vendor\Module\Observer\RegisterCustomer

    <?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 
    
        } 
    } 
    

注1:你可以使用捲曲在Magento風格像this 。注2:由於客戶電話號碼存儲在地址中,如果您想獲取客戶電話號碼,請參見here

+0

非常感謝。我現在會嘗試。 –