Skip to content

Commit

Permalink
Blik alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Wysocki committed Dec 2, 2024
1 parent 4561d93 commit b90fdfc
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 41 deletions.
47 changes: 32 additions & 15 deletions Controller/Tpay/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,14 +38,22 @@ 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,
TpayService $tpayService,
Session $checkoutSession,
TransactionApiFacade $transactionApiFacade,
RedirectHandler $redirectFactory,
AdditionalPaymentInfoValidator $additionalPaymentInfoValidator
AdditionalPaymentInfoValidator $additionalPaymentInfoValidator,
\Magento\Customer\Model\Session $customerSession,
TpayAliasServiceInterface $aliasService
) {
$this->tpay = $tpayModel;
$this->tpayConfig = $tpayConfig;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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'];
}
Expand All @@ -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);
Expand All @@ -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']);
Expand All @@ -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);
}
}
4 changes: 1 addition & 3 deletions Controller/Tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
22 changes: 12 additions & 10 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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'],
],
];

Expand All @@ -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,
],
];

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Model/TpayConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
12 changes: 10 additions & 2 deletions Notification/NotificationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Notification/Strategy/BlikAliasNotificationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
}
2 changes: 1 addition & 1 deletion Service/TpayAliasService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion view/base/web/js/open_render_channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
Expand Down
2 changes: 2 additions & 0 deletions view/base/web/js/render_channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,7 @@ require(['jquery', 'mage/translate'], function ($, $t) {
var submitBtn = $("#tpaycom_magento2generic_submit", parent);
submitBtn.addClass('disabled');
});

$(".blik-alias").hide();
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -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});
},
Expand Down
1 change: 0 additions & 1 deletion view/frontend/web/template/payment/tpay-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
<input id="blik_alias" type="radio">
<!-- ko i18n: 'I pay with BLIK from saved account.'--><!--/ko-->
</p>
<input style="display: none;" id="blik_alias_value"/>
</div>
</div>
</div>
Expand Down

0 comments on commit b90fdfc

Please sign in to comment.