2017-10-10 3 views
0

2factorからotpサービスを購入し、サンプルAPIを取得しました。 以下のサンプルAPI。 このotpは、顧客登録処理中に生成されます。私は助けを得ることを望む。事前に感謝します。この目標を達成するためにobserverを使用することができます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

。あなたの場合は、オブザーバー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:あなたがthisようにMagentoのスタイルでCURLを使用することができます。

注2:お客様の電話番号が住所に保存されているため、お客様の電話番号を取得する場合は、hereを参照してください。

+0

ありがとうございます。私は今それを試してみます。 –

関連する問題