diff --git a/Controller/Tpay/Create.php b/Controller/Tpay/Create.php index 66b7a2e..a01e128 100644 --- a/Controller/Tpay/Create.php +++ b/Controller/Tpay/Create.php @@ -10,6 +10,7 @@ use Tpay\Magento2\Model\ApiFacade\Transaction\TransactionApiFacade; use Tpay\Magento2\Model\ApiFacade\Transaction\TransactionOriginApi; use Tpay\Magento2\Service\RedirectHandler; +use Tpay\Magento2\Service\TpayAliasServiceInterface; use Tpay\Magento2\Service\TpayService; use Tpay\Magento2\Validator\AdditionalPaymentInfoValidator; use Tpay\OriginApi\Utilities\Util; @@ -37,6 +38,12 @@ class Create implements ActionInterface /** @var AdditionalPaymentInfoValidator */ private $additionalPaymentInfoValidator; + /** @var \Magento\Customer\Model\Session */ + private $customerSession; + + /** @var TpayAliasServiceInterface */ + private $aliasService; + public function __construct( TpayInterface $tpayModel, TpayConfigInterface $tpayConfig, @@ -44,7 +51,9 @@ public function __construct( Session $checkoutSession, TransactionApiFacade $transactionApiFacade, RedirectHandler $redirectFactory, - AdditionalPaymentInfoValidator $additionalPaymentInfoValidator + AdditionalPaymentInfoValidator $additionalPaymentInfoValidator, + \Magento\Customer\Model\Session $customerSession, + TpayAliasServiceInterface $aliasService ) { $this->tpay = $tpayModel; $this->tpayConfig = $tpayConfig; @@ -53,6 +62,8 @@ public function __construct( $this->transaction = $transactionApiFacade; $this->redirectFactory = $redirectFactory; $this->additionalPaymentInfoValidator = $additionalPaymentInfoValidator; + $this->customerSession = $customerSession; + $this->aliasService = $aliasService; Util::$loggingEnabled = false; } @@ -124,15 +135,10 @@ public function execute(): ResultInterface * * @param string $blikTransactionId * @param string $blikCode - * @param mixed $blikAlias */ - protected function blikPay($blikTransactionId, $blikCode, $blikAlias): bool + protected function blikPay($blikTransactionId, $blikCode): bool { - if ($blikAlias) { - $apiResult = $this->transaction->blikAlias($blikTransactionId, $blikAlias); - } else { - $apiResult = $this->transaction->blik($blikTransactionId, $blikCode); - } + $apiResult = $this->transaction->blik($blikTransactionId, $blikCode); return isset($apiResult['result']) && 1 === $apiResult['result']; } @@ -144,7 +150,7 @@ private function prepareTransaction($orderId, array $additionalPaymentInformatio if ($this->additionalPaymentInfoValidator->validateBlikIfPresent($additionalPaymentInformation)) { $data['group'] = TransactionOriginApi::BLIK_CHANNEL; $data['channel'] = null; - $this->handleBlikData($data, $additionalPaymentInformation['blik_code'] ?? '', $additionalPaymentInformation['blik_alias'] ?? ''); + $this->handleBlikData($data, $additionalPaymentInformation['blik_code'] ?? '', $additionalPaymentInformation['blik_alias'] ?? false); } else { $data['group'] = (int) ($additionalPaymentInformation['group'] ?? null); $data['channel'] = (int) ($additionalPaymentInformation['channel'] ?? null); @@ -164,22 +170,28 @@ private function prepareTransaction($orderId, array $additionalPaymentInformatio return $this->transaction->create($data); } - private function handleBlikData(array &$data, string $blikCode, string $blikAlias) + private function handleBlikData(array &$data, string $blikCode, bool $blikAlias) { if ($this->transaction->isOpenApiUse() && $this->tpay->checkBlikLevel0Settings()) { if ($blikCode) { - $data['blikPaymentData'] = [ - 'blikToken' => $blikCode, - ]; + $data['blikPaymentData']['blikToken'] = $blikCode; } - if ($blikAlias) { + $customerId = $this->customerSession->getCustomerId(); + + if ($customerId) { + $alias = $blikAlias + ? $this->aliasService->getCustomerAlias($customerId) + : $this->buildBlikAlias($customerId); + $data['blikPaymentData']['aliases'] = [ 'type' => 'UID', - 'value' => $blikAlias, + 'value' => $alias, + 'label' => 'tpay-magento2' ]; } } + if (!$this->transaction->isOpenApiUse()) { unset($data['channel']); unset($data['currency']); @@ -193,4 +205,9 @@ private function handleOpenApiTrId(array &$paymentData, array $transaction) $paymentData['additional_information']['transaction_id'] = $transaction['transactionId']; } } + + private function buildBlikAlias(string $customerId): string + { + return sprintf("32322223242222323241231232233333%s-%s", base64_encode('tpay-magento'), $customerId); + } } diff --git a/Controller/Tpay/Notification.php b/Controller/Tpay/Notification.php index 73bea83..8b00cd5 100644 --- a/Controller/Tpay/Notification.php +++ b/Controller/Tpay/Notification.php @@ -31,9 +31,7 @@ public function __construct( public function execute(): ?Response { try { - $orderId = isset($_POST['order_id']) ? base64_decode($_POST['order_id']) : base64_decode($_POST['tr_crc']); - - $this->notificationProcessor->process($orderId); + $this->notificationProcessor->process(); } catch (Throwable $e) { return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent($e->getMessage()); } diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index a8d9dd4..7bbb54d 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -35,7 +35,7 @@ public function __construct(TpayConfigInterface $tpay, CacheInterface $cache, ?i public function create(array $data): array { - if (!empty($data['blikPaymentData']) && empty($data['blikPaymentData']['aliases'])) { + if (!empty($data['blikPaymentData'])) { return $this->createBlikZero($data); } @@ -60,6 +60,7 @@ public function createTransaction(array $data): array public function createWithInstantRedirect(array $data): array { $transactionData = $this->handleDataStructure($data); + $transaction = $this->tpayApi->transactions()->createTransactionWithInstantRedirection($transactionData); return $this->updateRedirectUrl($transaction); @@ -75,14 +76,14 @@ public function createBlikZeroTransaction(array $data): array return $this->updateRedirectUrl($transaction); } - public function blik(string $transactionId, string $blikCode): array + public function blik(string $transactionId, array $blikPaymentData): array { $additional_payment_data = [ 'channelId' => 64, - 'method' => 'pay_by_link', 'blikPaymentData' => [ 'type' => 0, - 'blikToken' => $blikCode, + 'blikToken' => $blikPaymentData['blikToken'], + 'aliases' => $blikPaymentData['aliases'], ], ]; @@ -91,17 +92,14 @@ public function blik(string $transactionId, string $blikCode): array return $this->updateRedirectUrl($this->waitForBlikAccept($result)); } - public function blikAlias(string $transactionId, string $blikAlias): array + public function blikAlias(string $transactionId, array $aliases): array { $additional_payment_data = [ 'channelId' => 64, 'method' => 'pay_by_link', 'blikPaymentData' => [ 'type' => 0, - 'aliases' => [ - 'value' => $blikAlias, - 'type' => 'UID', - ], + 'aliases' => $aliases, ], ]; @@ -114,7 +112,11 @@ public function createBlikZero(array $data): array { $transaction = $this->createBlikZeroTransaction($data); - return $this->blik($transaction['transactionId'], $data['blikPaymentData']['blikToken']); + if (!empty($data['blikPaymentData']['aliases']) && empty($data['blikPaymentData']['blikToken'])) { + return $this->blikAlias($transaction['transactionId'], $data['blikPaymentData']['aliases']); + } + + return $this->blik($transaction['transactionId'], $data['blikPaymentData']); } public function makeRefund(InfoInterface $payment, float $amount): array diff --git a/Model/ApiFacade/Transaction/TransactionApiFacade.php b/Model/ApiFacade/Transaction/TransactionApiFacade.php index a0e131e..2f4a70b 100755 --- a/Model/ApiFacade/Transaction/TransactionApiFacade.php +++ b/Model/ApiFacade/Transaction/TransactionApiFacade.php @@ -64,9 +64,9 @@ public function createWithInstantRedirection(array $config): array return $this->openApi->createWithInstantRedirect($config); } - public function blik($blikTransactionId, $blikCode): array + public function blik($blikTransactionId, $blikCode, $blikAlias): array { - return $this->getCurrentApi()->blik($blikTransactionId, $blikCode); + return $this->getCurrentApi()->blik($blikTransactionId, ['blikToken' => $blikCode, 'aliases' => ['type' => 'UID', 'value' => $blikAlias, 'label' => 'tpay-magento2']]); } public function blikAlias($blikAliasTransactionId, $blikAlias): array diff --git a/Model/TpayConfigProvider.php b/Model/TpayConfigProvider.php index 52cdcb4..c60de0b 100644 --- a/Model/TpayConfigProvider.php +++ b/Model/TpayConfigProvider.php @@ -62,7 +62,7 @@ public function getConfig(): array $alias = $this->aliasService->getCustomerAlias((int) $this->customerSession->getCustomerId()); if ($alias) { - $config['blik_alias'] = $alias['alias']; + $config['blik_alias'] = true; } } diff --git a/Notification/NotificationProcessor.php b/Notification/NotificationProcessor.php index 4fad778..8aaaf2f 100644 --- a/Notification/NotificationProcessor.php +++ b/Notification/NotificationProcessor.php @@ -2,6 +2,7 @@ namespace Tpay\Magento2\Notification; +use Tpay\Magento2\Notification\Strategy\BlikAliasNotificationProcessor; use Tpay\Magento2\Notification\Strategy\Factory\NotificationProcessorFactoryInterface; use Tpay\Magento2\Service\TpayService; @@ -19,11 +20,18 @@ public function __construct(NotificationProcessorFactoryInterface $factory, Tpay $this->tpayService = $tpayService; } - public function process(string $orderId) + public function process() { $strategy = $this->factory->create($_POST); - $strategy->process($this->getOrderStore($orderId)); + if ($strategy instanceof BlikAliasNotificationProcessor) { + $storeId = null; + } else { + $orderId = isset($_POST['order_id']) ? base64_decode($_POST['order_id']) : base64_decode($_POST['tr_crc']); + $storeId = $this->getOrderStore($orderId); + } + + $strategy->process($storeId); } private function getOrderStore(string $orderId): ?int diff --git a/Notification/Strategy/BlikAliasNotificationProcessor.php b/Notification/Strategy/BlikAliasNotificationProcessor.php index f0bb9b0..7c37bae 100644 --- a/Notification/Strategy/BlikAliasNotificationProcessor.php +++ b/Notification/Strategy/BlikAliasNotificationProcessor.php @@ -16,17 +16,17 @@ public function __construct(TpayAliasServiceInterface $aliasService) $this->aliasService = $aliasService; } - public function process(?int $storeId) + public function process(?int $storeId = null) { $response = $_POST; - $userId = (int) explode('_', $response['value'])[1]; + $userId = (int) explode('-', $response['msg_value']['value'])[1]; if ('ALIAS_REGISTER' === $response['event']) { - $this->aliasService->saveCustomerAlias($userId, $response['value']); + $this->aliasService->saveCustomerAlias($userId, $response['msg_value']['value']); } if ('ALIAS_UNREGISTER' === $response['event']) { - $this->aliasService->removeCustomerAlias($userId, $response['value']); + $this->aliasService->removeCustomerAlias($userId, $response['msg_value']['value']); } } } diff --git a/Service/TpayAliasService.php b/Service/TpayAliasService.php index 5ad8e0c..d6d2adb 100644 --- a/Service/TpayAliasService.php +++ b/Service/TpayAliasService.php @@ -39,7 +39,7 @@ public function getCustomerAlias(int $customerId) ->from($tableName) ->where('cli_id = ?', $customerId); - return $connection->fetchRow($select); + return $connection->fetchRow($select)['alias']; } public function saveCustomerAlias(int $customerId, string $alias): void diff --git a/view/base/web/js/open_render_channels.js b/view/base/web/js/open_render_channels.js index 1c3e7ad..db71d7b 100644 --- a/view/base/web/js/open_render_channels.js +++ b/view/base/web/js/open_render_channels.js @@ -149,7 +149,6 @@ require(['jquery', 'mage/translate'], function ($, $t) { $('#blik_alias').prop('checked', false); } - $("#blik_alias_value").val(window.checkoutConfig.blik_alias); payButton.removeClass('disabled'); }); } diff --git a/view/base/web/js/render_channels.js b/view/base/web/js/render_channels.js index 13a891d..b8b6b5c 100644 --- a/view/base/web/js/render_channels.js +++ b/view/base/web/js/render_channels.js @@ -149,5 +149,7 @@ require(['jquery', 'mage/translate'], function ($, $t) { var submitBtn = $("#tpaycom_magento2generic_submit", parent); submitBtn.addClass('disabled'); }); + + $(".blik-alias").hide(); } ); diff --git a/view/frontend/web/js/view/payment/method-renderer/tpay-method.js b/view/frontend/web/js/view/payment/method-renderer/tpay-method.js index 9e87094..89df705 100644 --- a/view/frontend/web/js/view/payment/method-renderer/tpay-method.js +++ b/view/frontend/web/js/view/payment/method-renderer/tpay-method.js @@ -66,7 +66,10 @@ define( paymentData['blik_code'] = $('#blik_code').val(); paymentData['accept_tos'] = true; paymentData['channel'] = ""; - paymentData['blik_alias'] = $('#blik_alias_value').val(); + + if ($('#blik_alias').prop('checked')) { + paymentData['blik_alias'] = $('#blik_alias').val(); + } return $.extend(true, parent, {'additional_data': paymentData}); }, diff --git a/view/frontend/web/template/payment/tpay-form.html b/view/frontend/web/template/payment/tpay-form.html index 6dd2d84..b470e3e 100644 --- a/view/frontend/web/template/payment/tpay-form.html +++ b/view/frontend/web/template/payment/tpay-form.html @@ -43,7 +43,6 @@
-