如何将 OTP 服务与 Magento 2 集成
How to integrate an OTP service with Magento 2
我从 2factor 购买了 OTP 服务并获得了样本 API。此 OTP 将在客户注册过程中生成。
这是一个示例 API 调用
<?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);
?>
您可以使用 observer 来实现这个目标。在你的情况下,你应该使用观察者 customer_register_success
。所以现在:
- 创建一个新模块,比方说Vendor_Module。我假设您知道如何创建模块。如果不是,参考here.
创建包含以下内容的文件 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>
创建包含以下内容的文件 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 样式中使用 CURL,例如 this。
注2:由于客户phone号存储在地址中,如果要获取客户phone号,请参见here。
我从 2factor 购买了 OTP 服务并获得了样本 API。此 OTP 将在客户注册过程中生成。
这是一个示例 API 调用
<?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);
?>
您可以使用 observer 来实现这个目标。在你的情况下,你应该使用观察者 customer_register_success
。所以现在:
- 创建一个新模块,比方说Vendor_Module。我假设您知道如何创建模块。如果不是,参考here.
创建包含以下内容的文件
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>
创建包含以下内容的文件
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 样式中使用 CURL,例如 this。
注2:由于客户phone号存储在地址中,如果要获取客户phone号,请参见here。