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

add controller for LP register

This commit is contained in:
Sergey Chazov 2020-10-15 15:55:30 +03:00
parent a1abded059
commit 7a4fac7f8e
27 changed files with 214 additions and 26 deletions

View file

@ -34,6 +34,7 @@ class ApiClient
const VERSION = 'v5';
protected $client;
protected $unversionedClient;
/**
* Site code
@ -55,10 +56,13 @@ class ApiClient
$url .= '/';
}
$url = $url . 'api/' . self::VERSION;
$this->client = new Client($url, array('apiKey' => $apiKey));
$versionedUrl = $url . 'api/' . self::VERSION;
$unversionedUrl = $url . 'api';
$this->client = new Client($versionedUrl, array('apiKey' => $apiKey));
$this->siteCode = $site;
$this->unversionedClient = new Client($unversionedUrl, ['apiKey' => $apiKey]);
}
/**
@ -2979,7 +2983,7 @@ class ApiClient
public function loyaltyOrderApply(array $request): ApiResponse
{
return $this->client->makeRequest(
"/api/v5/orders/loyalty/apply",
"/orders/loyalty/apply",
Client::METHOD_POST,
$request
);
@ -2990,9 +2994,34 @@ class ApiClient
*/
public function getCredentials(): ApiResponse
{
return $this->client->makeRequest(
"/api/credentials",
return $this->unversionedClient->makeRequest(
"/credentials",
Client::METHOD_GET
);
}
/**
* @param array $request
* @return \RetailCrm\Response\ApiResponse
*/
public function createLoyaltyAccount(array $request): ApiResponse
{
return $this->client->makeRequest(
"/loyalty/account/create",
Client::METHOD_POST,
$request
);
}
/**
* @param int $loyaltyId
* @return \RetailCrm\Response\ApiResponse
*/
public function activateLoyaltyAccount(int $loyaltyId): ApiResponse
{
return $this->client->makeRequest(
"/api/v5/loyalty/account/".$loyaltyId."/activate",
Client::METHOD_POST
);
}
}

View file

@ -1,4 +1,22 @@
function addTelNumber(userId) {
$('#confirmationCode').show();
console.log(userId);
function addTelNumber(customerId) {
const phone = $('#loyaltyRegPhone').val();
const card = $('#loyaltyRegCard').val();
console.log(phone);
console.log(card);
BX.ajax.runAction('intaro:retailcrm.api.loyalty.register.accountCreate',
{
data: {
sessid: BX.bitrix_sessid(),
loyaltyAccount: {
phone: phone,
card: card,
customerId: customerId
}
}
}
).then(
function(data) {
});
}

View file

@ -96,13 +96,15 @@ if (
&& (int)$arUser['UF_AGREE_PL_INTARO'] === 1
&& (int)$arUser['UF_PD_PROC_PL_INTARO'] === 1
) { ?>
<b>Для завершения регистрации в программе лояльности введите номер телефона</b><br>
<b>Мы отправим на него код подтверждения</b><br>
<input type="tel" id="regNumber" placeholder="+7 (900) 000-00-00">
<input type="button" onclick="addTelNumber(<?=$USER->GetID()?>)" value="Отправить код подтверждения"/>
<br>
<div id="confirmationCode" style="display: none;">
<input type="text" placeholder="Код подтверждения">
<div id="regbody">
<b>Для завершения регистрации в программе лояльности введите номер телефона и номер карты</b><br>
<input type="tel" id="loyaltyRegPhone" placeholder="+7 (900) 000-00-00">
<br>
<input type="text" id="loyaltyRegCard" placeholder="Номер карты">
<input type="button" onclick="addTelNumber(<?=$USER->GetID()?>)" value="Отправить"/>
<br>
<div id="confirmationCode" style="display: none;">
</div>
</div>
<?php } ?>
<?else:?>

View file

@ -138,7 +138,7 @@ class ClientAdapter
public function getCredentials(): CredentialsResponse
{
$response = $this->client->getCredentials();
return Deserializer::deserializeArray($response->getResponseBody(), CredentialsResponse::class);
}
}

View file

@ -13,9 +13,13 @@ namespace Intaro\RetailCrm\Component\ApiClient\Traits;
use Intaro\RetailCrm\Component\Json\Deserializer;
use Intaro\RetailCrm\Component\Json\Serializer;
use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountActivateRequest;
use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountCreateRequest;
use Intaro\RetailCrm\Model\Api\Request\Loyalty\LoyaltyCalculateRequest;
use Intaro\RetailCrm\Model\Api\Request\Order\Loyalty\OrderLoyaltyApplyRequest;
use Intaro\RetailCrm\Model\Api\Request\SmsVerification\SmsVerificationConfirmRequest;
use Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountActivateResponse;
use Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountCreateResponse;
use Intaro\RetailCrm\Model\Api\Response\Loyalty\LoyaltyCalculateResponse;
use Intaro\RetailCrm\Model\Api\Response\Order\Loyalty\OrderLoyaltyApplyResponse;
use Intaro\RetailCrm\Model\Api\Response\SmsVerification\SmsVerificationConfirmResponse;
@ -75,4 +79,28 @@ trait LoyaltyTrait
return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyCalculateResponse::class);
}
/**
* @param \Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountCreateRequest $request
* @return \Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountCreateResponse|null
*/
public function createLoyaltyAccount(LoyaltyAccountCreateRequest $request): ?LoyaltyAccountCreateResponse
{
$serialized = Serializer::serializeArray($request);
$response = $this->client->createLoyaltyAccount($serialized);
return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyAccountCreateResponse::class);
}
/**
* @param \Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountActivateRequest $request
* @return \Intaro\RetailCrm\Model\Api\Response\Loyalty\Account\LoyaltyAccountActivateResponse|null
*/
public function activateLoyaltyAccount(LoyaltyAccountActivateRequest $request): ?LoyaltyAccountActivateResponse
{
$serialized = Serializer::serializeArray($request);
$response = $this->client->activateLoyaltyAccount($serialized['loyaltyId']);
return Deserializer::deserializeArray($response->getResponseBody(), LoyaltyAccountActivateResponse::class);
}
}

View file

@ -17,6 +17,11 @@ class AdminPanel extends Controller
Authentication::class,
],
],
'createTemplate' => [
'-prefilters' => [
Authentication::class,
],
],
];
}

View file

@ -0,0 +1,66 @@
<?php
namespace Intaro\RetailCrm\Controller\Loyalty;
use Bitrix\Main\Engine\ActionFilter\Authentication;
use Bitrix\Main\Engine\Controller;
use Intaro\RetailCrm\Component\ConfigProvider;
use Intaro\RetailCrm\Component\Constants;
use Intaro\RetailCrm\Component\Factory\ClientFactory;
use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountActivateRequest;
use Intaro\RetailCrm\Model\Api\Request\Loyalty\Account\LoyaltyAccountCreateRequest;
use Intaro\RetailCrm\Model\Api\SerializedCreateLoyaltyAccount;
class Register extends Controller
{
public function configureActions(): array
{
return [
'accountCreate' => [
'-prefilters' => [
Authentication::class,
],
],
];
}
/**
* @return string[]
*/
public function accountCreateAction($loyaltyAccount): array
{
/** @var \Intaro\RetailCrm\Component\ApiClient\ClientAdapter $client */
$client = ClientFactory::createClientAdapter();
$credentials = $client->getCredentials();
$createRequest = new LoyaltyAccountCreateRequest();
$createRequest->site = $credentials->sitesAvailable[0];
$createRequest->loyaltyAccount = new SerializedCreateLoyaltyAccount();
$createRequest->loyaltyAccount->phoneNumber = $loyaltyAccount->phone;
$createRequest->loyaltyAccount->cardNumber = $loyaltyAccount->card;
$createRequest->loyaltyAccount->customerId = $loyaltyAccount->customerId;
$createRequest->loyaltyAccount->customFields = $loyaltyAccount->customFields;
$createResponse = $client->createLoyaltyAccount($createRequest);
if ($createResponse !== null) {
//если участник ПЛ создан и активирован
if ($createResponse->loyaltyAccount->active) {
return ['status' => 'activate'];
}
$activateRequest = new LoyaltyAccountActivateRequest();
$activateRequest->loyaltyId = $createResponse->loyaltyAccount->id;
$activateResponse = $client->activateLoyaltyAccount($activateRequest);
if (isset($activateResponse->verification)) {
return ['status' => 'smsVerification'];
}
}
return ['newStatus' => $newStatus];
}
}

View file

@ -19,10 +19,10 @@ use Intaro\RetailCrm\Component\ServiceLocator;
use Intaro\RetailCrm\Service\UserVerificationService;
/**
* Class AdminPanel
* Class SmsVerification
* @package Intaro\RetailCrm\Controller\Loyalty
*/
class AdminPanel extends Controller
class SmsVerification extends Controller
{
/** @var int */
const DEFAULT_CODE_LENGHT = 4;

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class AbstractLoyaltyEvent
*

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class CalculateMaximum
*

View file

@ -13,7 +13,6 @@ namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class Contragent
*

View file

@ -29,4 +29,14 @@ class LoyaltyAccountCreateRequest extends AbstractApiModel
* @Mapping\SerializedName("loyalty_account")
*/
public $loyaltyAccount;
/**
* Символьный код магазина
*
* @var string $site
*
* @Mapping\Type("string")
* @Mapping\SerializedName("site")
*/
public $site;
}

View file

@ -13,7 +13,6 @@ namespace Intaro\RetailCrm\Model\Api\Request\Loyalty;
use Intaro\RetailCrm\Component\Json\Mapping;
use Intaro\RetailCrm\Model\Api\AbstractApiModel;
use Intaro\RetailCrm\Model\Api\SerializedOrder;
/**
* Class LoyaltyCalculateRequest

View file

@ -13,7 +13,6 @@ namespace Intaro\RetailCrm\Model\Api\Request\Order\Loyalty;
use Intaro\RetailCrm\Component\Json\Mapping;
use Intaro\RetailCrm\Model\Api\AbstractApiModel;
use Intaro\RetailCrm\Model\Api\SerializedOrderReference;
/**
* Class OrderLoyaltyApplyRequest

View file

@ -14,7 +14,6 @@ namespace Intaro\RetailCrm\Model\Api\Response;
use Intaro\RetailCrm\Model\Api\AbstractApiModel;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class AbstractApiResponseModel
*

View file

@ -12,6 +12,7 @@
namespace Intaro\RetailCrm\Model\Api\Response\Loyalty\Account;
use Intaro\RetailCrm\Model\Api\Response\AbstractApiResponseModel;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class LoyaltyAccountCreateResponse

View file

@ -12,6 +12,7 @@
namespace Intaro\RetailCrm\Model\Api\Response\Loyalty;
use Intaro\RetailCrm\Model\Api\Response\AbstractApiResponseModel;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class LoyaltyCalculateResponse

View file

@ -13,6 +13,7 @@
namespace Intaro\RetailCrm\Model\Api\Response\Settings;
use Intaro\RetailCrm\Model\Api\Response\AbstractApiResponseModel;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class CredentialsResponse
@ -24,9 +25,9 @@ class CredentialsResponse extends AbstractApiResponseModel
/**
* Результат запроса (успешный/неуспешный)
*
* @var boolean $success
* @var \bool $success
*
* @Mapping\Type("boolean")
* @Mapping\Type("bool")
* @Mapping\SerializedName("success")
*/
public $success;

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class SerializedCreateLoyaltyAccount
*
@ -57,4 +59,14 @@ class SerializedCreateLoyaltyAccount
* @Mapping\SerializedName("customerId")
*/
public $customerId;
/**
* Ассоциативный массив пользовательских полей
*
* @var array $customFields
*
* @Mapping\Type("array")
* @Mapping\SerializedName("customFields")
*/
public $customFields;
}

View file

@ -1,4 +1,5 @@
<?php
/**
* PHP version 7.1
*
@ -11,6 +12,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Метод применения бонусов по программе лояльности
* Class SerializedLoyaltyOrder

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class SerializedOrder
*

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class SerializedOrderDelivery
*

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class SerializedOrderProduct
*

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class SerializedOrderProductOffer
*

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class LoyaltyCalculateRequest
*

View file

@ -11,6 +11,8 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class SerializedRelationCustomer
*

View file

@ -11,7 +11,7 @@
*/
namespace Intaro\RetailCrm\Model\Api;
use DateTime;
use Intaro\RetailCrm\Component\Json\Mapping;
/**
* Class SmsVerification