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

add controllers and api for sms verification

This commit is contained in:
Sergey Chazov 2020-09-14 17:25:18 +03:00
parent 22172bec36
commit 39ced72ada
7 changed files with 297 additions and 22 deletions

View file

@ -247,27 +247,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,7 +33,6 @@ use Intaro\RetailCrm\Model\Api\Response\OperationResponse;
*/
trait CustomersCorporateTrait
{
use BaseClientTrait;
/**
* Create customers corporate

View file

@ -0,0 +1,103 @@
<?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 Intaro\RetailCrm\Service\UserVerificationService;
class AdminPanel extends Controller
{
const DEFAULT_CODE_LENGHT = 4;
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']),
],
],
];
}
/**
* 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
) {
$service = new UserVerificationService();
return $service->sendSms($actionType, $orderId, $verificationLength);
}
/**
* @param string $checkId
* @return \Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationStatusResponse|null
*/
public function getSmsStatusAction(string $checkId)
{
$service = new UserVerificationService();
return $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)
{
$service = new UserVerificationService();
return $service->confirmVerification($code, $checkId);
}
/**
* @param int $userId
* @return bool
*/
public function checkPlRegistrationStatusAction(int $userId)
{
$service = new UserVerificationService();
return $service->checkPlRegistrationStatus($userId);
}
}

View file

@ -0,0 +1,40 @@
<?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
{
/**
* @param $userId
* @return \Intaro\RetailCrm\Model\Bitrix\User|null
*/
public function get($userId): ?User
{
return UserRepository::getById($userId);
}
/**
* @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 $where
* @param $select
* @return mixed|null
*/
public static function getFirstByParams($where, $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,125 @@
<?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::creacteClientAdapter();
}
/**
* 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();
$where = [['ID', '=', $userId]];
$select = ['PERSONAL_PHONE'];
/** @var \Intaro\RetailCrm\Model\Bitrix\User $user */
$user = UserRepository::getFirstByParams($where, $select);
$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);
}
}
}