我需要将otp服务与Magento 2集成

时间:2017-10-10 09:18:19

标签: magento magento2 magento-2.0 magento2.0.2 magento2.1

我从2factor购买了otp服务并获得了示例API。 下面的示例API。 此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);
?>

1 个答案:

答案 0 :(得分:1)

您可以使用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
    
        }
    }
    
  4. 注1:您可以使用Magento样式的CURL,如this

    注2:由于客户电话号码存储在地址中,如果您想获取客户电话号码,请参阅here