1
0
Fork 0
mirror of synced 2025-04-20 01:21:01 +00:00

add controllers and api for sms verification (#138)

This commit is contained in:
Сергей Чазов 2020-09-15 14:00:21 +03:00 committed by Павел
parent 027369a24b
commit 25916870d8
10 changed files with 343 additions and 26 deletions

View file

@ -2,6 +2,7 @@
use Intaro\RetailCrm\Component\ServiceLocator;
use Intaro\RetailCrm\Service\CollectorCookieExtractor;
use Intaro\RetailCrm\Service\UserVerificationService;
use Intaro\RetailCrm\Vendor\Doctrine\Common\Annotations\AnnotationReader;
use Intaro\RetailCrm\Vendor\Doctrine\Common\Annotations\AnnotationRegistry;
@ -27,5 +28,6 @@ ServiceLocator::registerServices([
\Intaro\RetailCrm\Service\Utils::class,
Logger::class,
AnnotationReader::class,
CollectorCookieExtractor::class
CollectorCookieExtractor::class,
UserVerificationService::class
]);

View file

@ -242,27 +242,7 @@ class intaro_retailcrm extends CModule
} catch (ObjectPropertyException | ArgumentException | SystemException $e) {
return false;
}
include($this->INSTALL_PATH . '/../lib/model/bitrix/abstractmodelproxy.php');
include($this->INSTALL_PATH . '/../lib/model/bitrix/orderprops.php');
include($this->INSTALL_PATH . '/../lib/model/bitrix/tomodule.php');
include($this->INSTALL_PATH . '/../lib/repository/abstractrepository.php');
include($this->INSTALL_PATH . '/../lib/repository/orderpropsrepository.php');
include($this->INSTALL_PATH . '/../lib/repository/persontyperepository.php');
include($this->INSTALL_PATH . '/../lib/repository/tomodulerepository.php');
include($this->INSTALL_PATH . '/../lib/model/bitrix/orm/tomodule.php');
$this->CopyFiles();
$this->addBonusPaySystem();
$this->addLPUserFields();
$this->addLPEvents();
try {
$this->addLPOrderProps();
} catch (ObjectPropertyException | ArgumentException | SystemException $e) {
return false;
}
if ($step == 11) {
$arResult['arSites'] = RCrmActions::SitesList();
if (count($arResult['arSites']) < 2) {

View file

@ -11,6 +11,7 @@
*/
namespace Intaro\RetailCrm\Component\ApiClient;
use Intaro\RetailCrm\Component\ApiClient\Traits\BaseClientTrait;
use Intaro\RetailCrm\Component\ApiClient\Traits\CustomersCorporateTrait;
use Intaro\RetailCrm\Component\ApiClient\Traits\CustomersTrait;
use Intaro\RetailCrm\Component\ApiClient\Traits\LoyaltyTrait;
@ -101,6 +102,7 @@ use RetailCrm\Response\ApiResponse;
*/
class ClientAdapter
{
use BaseClientTrait;
use CustomersTrait;
use CustomersCorporateTrait;
use LoyaltyTrait;

View file

@ -33,8 +33,6 @@ use Intaro\RetailCrm\Model\Api\Response\OperationResponse;
*/
trait CustomersCorporateTrait
{
use BaseClientTrait;
/**
* Create customers corporate
*

View file

@ -25,7 +25,7 @@ class ClientFactory
*
* @return \Intaro\RetailCrm\Component\ApiClient\ClientAdapter|null
*/
public static function creacteClientAdapter(): ?ClientAdapter
public static function createClientAdapter(): ?ClientAdapter
{
$apiHost = ConfigProvider::getApiUrl();
$apiKey = ConfigProvider::getApiKey();

View file

@ -0,0 +1,126 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\Controller\Loyalty
* @author retailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\Controller\Loyalty;
use Bitrix\Main\Engine\ActionFilter\Authentication;
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
use Bitrix\Main\Engine\Controller;
use Bitrix\Main\Request;
use Intaro\RetailCrm\Component\ServiceLocator;
use Intaro\RetailCrm\Service\UserVerificationService;
/**
* Class AdminPanel
* @package Intaro\RetailCrm\Controller\Loyalty
*/
class AdminPanel extends Controller
{
/** @var int */
const DEFAULT_CODE_LENGHT = 4;
/** @var UserVerificationService */
private $service;
/**
* AdminPanel constructor.
* @param \Bitrix\Main\Request|null $request
*/
public function __construct(Request $request = null)
{
$this->service = ServiceLocator::get(UserVerificationService::class);
parent::__construct($request);
}
/**
* Send verification sms
*
* @param string $actionType
* @param int|null $orderId
* @param int $verificationLength
* @return \Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationCreateResponse|null
* @throws \Exception
*/
public function sendSmsAction(
string $actionType = 'verify_customer',
int $orderId = null,
int $verificationLength = self::DEFAULT_CODE_LENGHT
) {
return $this->service->sendSms($actionType, $orderId, $verificationLength);
}
/**
* Контроллер получает статус текущего состояния верификации
*
* @param string $checkId
* @return \Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationStatusResponse|null
*/
public function getSmsStatusAction(string $checkId)
{
return $this->service->getSmsStatus($checkId);
}
/**
* Контроллер подтверждает верификацию
*
* @param string $code
* @param string $checkId
* @return \Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationConfirmResponse|null
*/
public function confirmVerificationAction(string $code, string $checkId)
{
return $this->service->confirmVerification($code, $checkId);
}
/**
* Контроллер проверяет, зарегистрирован ли пользователь в программе лояльности
*
* @param int $userId
* @return bool
*/
public function checkPlRegistrationStatusAction(int $userId)
{
return $this->service->checkPlRegistrationStatus($userId);
}
/**
* @return \array[][]
*/
public function configureActions(): array
{
return [
'sendSms' => [
'-prefilters' => [
new Authentication,
new HttpMethod(['GET']),
],
],
'getSmsStatus' => [
'-prefilters' => [
new Authentication,
new HttpMethod(['GET']),
],
],
'confirmVerification' => [
'-prefilters' => [
new Authentication,
new HttpMethod(['GET']),
],
],
'checkPlRegistrationStatus' => [
'-prefilters' => [
new Authentication,
new HttpMethod(['GET']),
],
],
];
}
}

View file

@ -0,0 +1,44 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\DataProvider
* @author retailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\DataProvider;
use Intaro\RetailCrm\Repository\UserRepository;
use Intaro\RetailCrm\Model\Bitrix\User;
/**
* Class CurrentUserProvider
* @package Intaro\RetailCrm\DataProvider
*/
class CurrentUserProvider
{
/**
* Получает текущего пользователя
*
* @return \Intaro\RetailCrm\Model\Bitrix\User|null
*/
public function get(): ?User
{
global $USER;
return UserRepository::getById($USER->GetID());
}
/**
* Получаем ID пользователя
*
* @return int|null
*/
public function getId(): ?int
{
global $USER;
return $USER->GetID();
}
}

View file

@ -11,6 +11,10 @@
*/
namespace Intaro\RetailCrm\Repository;
use Bitrix\Main\ArgumentException;
use Bitrix\Main\ObjectPropertyException;
use Bitrix\Main\SystemException;
use Bitrix\Main\UserTable;
use Intaro\RetailCrm\Component\Json\Deserializer;
use Intaro\RetailCrm\Model\Bitrix\User;
@ -36,4 +40,26 @@ class UserRepository extends AbstractRepository
return Deserializer::deserializeArray($fields, User::class);
}
/**
* @param array $where
* @param array $select
* @return mixed|null
*/
public static function getFirstByParams(array $where, array $select){
try {
$user = UserTable::query()
->setSelect($select)
->where($where)
->fetch();
} catch (ObjectPropertyException | ArgumentException | SystemException $exception) {
return null;
}
if (!$user) {
return null;
}
return Deserializer::deserializeArray($user, User::class);
}
}

View file

@ -0,0 +1,139 @@
<?php
/**
* PHP version 7.1
*
* @category Integration
* @package Intaro\RetailCrm\Service
* @author retailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
namespace Intaro\RetailCrm\Service;
use Exception;
use Intaro\RetailCrm\Component\Factory\ClientFactory;
use Intaro\RetailCrm\Model\Api\Request\SmsVerification\SmsVerificationConfirmRequest;
use Intaro\RetailCrm\Model\Api\Request\SmsVerification\SmsVerificationCreateRequest;
use Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationStatusRequest;
use Intaro\RetailCrm\Model\Api\SmsVerificationConfirm;
use Intaro\RetailCrm\Model\Api\SmsVerificationCreate;
use Intaro\RetailCrm\Repository\UserRepository;
/**
* Class UserVerificationService
*/
class UserVerificationService
{
const NOT_AUTHORIZE = 'Пользователь не авторизован';
const DEFAULT_CODE_LENGHT = 4;
/**
* @var \Intaro\RetailCrm\Component\ApiClient\ClientAdapter
*/
private $client;
private $userId;
/**
* @var \CAllUser|\CUser
*/
private $user;
public function __construct()
{
$this->client = ClientFactory::createClientAdapter();
}
/**
* send verification sms
*
* @param string $actionType [verify_customer|verify_privacy]
* @param int|null $orderId
* @param int $verificationLength
* @return \Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationCreateResponse|null
* @throws \Exception
*/
public function sendSms(
string $actionType = 'verify_customer',
int $orderId = null,
int $verificationLength = self::DEFAULT_CODE_LENGHT
) {
$this->checkAuth();
$userId = $this->user->GetID();
/** @var \Intaro\RetailCrm\Model\Bitrix\User $user */
$user = UserRepository::getFirstByParams(
[
['ID', '=', $userId]
],
['PERSONAL_PHONE']
);
$request = new SmsVerificationCreateRequest();
$request->verification = new SmsVerificationCreate();
$request->verification->setLength($verificationLength);
$request->verification->setPhone($user->getPersonalPhone());
$request->verification->setPhone($this->userId);
$request->verification->setActionType($actionType);
$request->verification->setCustomerId($userId);
$request->verification->setOrderId($orderId);
return $this->client->sendSmsForLpVerification($request);
}
/**
* Получает статус текущего состояния верификации
*
* @param string $checkId Идентификатор проверки кода
* @return \Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationStatusResponse|null
*/
public function getSmsStatus(string $checkId)
{
$request = new SmsVerificationStatusRequest();
$request->checkId = $checkId;
return $this->client->checkStatusPlVerification($request);
}
/**
* Подтверждает верификацию
*
* @param string $code Проверочный код
* @param string $checkId Идентификатор проверки кода
* @return \Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationConfirmResponse|null
*/
public function confirmVerification(string $code, string $checkId)
{
$request = new SmsVerificationConfirmRequest();
$request->verification = new SmsVerificationConfirm();
$request->verification->setCode($code);
$request->verification->setCheckId($checkId);
return $this->client->confirmLpVerificationBySMS($request);
}
/**
* Проверяем статус регистрации пользователя в ПЛ
*
* @param int $userId
* @return bool
*/
public function checkPlRegistrationStatus(int $userId)
{
//TODO когда метод будет реализован в АПИ, нужно будет написать реализацию
return true;
}
/**
* @throws \Exception
*/
private function checkAuth()
{
global $USER;
$this->user = $USER;
if (!$this->user->IsAuthorized()) {
throw new Exception(self::NOT_AUTHORIZE);
}
}
}

View file

@ -11,7 +11,7 @@ class ClientFactoryTest extends TestCase
{
public function testCreacteClientAdapter(): void
{
$client = ClientFactory::creacteClientAdapter();
$client = ClientFactory::createClientAdapter();
if (empty(ConfigProvider::getApiUrl())) {
self::assertEquals(null, $client);