Some checks are pending
Telegram Mini App Shop Builder / Compute version metadata (push) Waiting to run
Telegram Mini App Shop Builder / Run Frontend tests (push) Waiting to run
Telegram Mini App Shop Builder / Run Backend tests (push) Waiting to run
Telegram Mini App Shop Builder / Run PHP_CodeSniffer (push) Waiting to run
Telegram Mini App Shop Builder / Build module. (push) Blocked by required conditions
Telegram Mini App Shop Builder / release (push) Blocked by required conditions
74 lines
2.3 KiB
PHP
Executable File
74 lines
2.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Handlers;
|
|
|
|
use App\Models\TelegramCustomer;
|
|
use Carbon\Carbon;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Acme\ECommerceFramework\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Acme\ECommerceFramework\Support\Arr;
|
|
use Acme\ECommerceFramework\Telegram\Enums\TelegramHeader;
|
|
use Acme\ECommerceFramework\Telegram\TelegramService;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class PrivacyPolicyHandler
|
|
{
|
|
private TelegramService $telegramService;
|
|
private TelegramCustomer $telegramCustomer;
|
|
private LoggerInterface $logger;
|
|
|
|
public function __construct(
|
|
TelegramService $telegramService,
|
|
TelegramCustomer $telegramCustomer,
|
|
LoggerInterface $logger
|
|
) {
|
|
$this->telegramService = $telegramService;
|
|
$this->telegramCustomer = $telegramCustomer;
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
public function checkIsUserPrivacyConsented(Request $request): JsonResponse
|
|
{
|
|
$isPrivacyConsented = false;
|
|
$telegramUserId = $this->telegramService->userId($request->header(TelegramHeader::INIT_DATA));
|
|
|
|
if (! $telegramUserId) {
|
|
return new JsonResponse([
|
|
'data' => [
|
|
'is_privacy_consented' => false,
|
|
],
|
|
]);
|
|
}
|
|
|
|
$customer = $this->telegramCustomer->findByTelegramUserId($telegramUserId);
|
|
|
|
if ($customer) {
|
|
$isPrivacyConsented = Arr::get($customer, 'privacy_consented_at') !== null;
|
|
}
|
|
|
|
return new JsonResponse([
|
|
'data' => [
|
|
'is_privacy_consented' => $isPrivacyConsented,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function userPrivacyConsent(Request $request): JsonResponse
|
|
{
|
|
$telegramUserId = $this->telegramService->userId($request->header(TelegramHeader::INIT_DATA));
|
|
|
|
if ($telegramUserId) {
|
|
$this->telegramCustomer->updateByTelegramUserId($telegramUserId, [
|
|
'privacy_consented_at' => Carbon::now()->toDateTimeString(),
|
|
]);
|
|
} else {
|
|
$this->logger->warning(
|
|
'Could not find customer with telegram user_id: ' . $telegramUserId . ' to give privacy consent.'
|
|
);
|
|
}
|
|
|
|
return new JsonResponse([], Response::HTTP_NO_CONTENT);
|
|
}
|
|
}
|