From 54c88e1c9ffe1fa54bf795b8223159e05aec922d Mon Sep 17 00:00:00 2001 From: kGablo Date: Fri, 15 Mar 2024 14:00:15 +0100 Subject: [PATCH] AdditionalInfo fix --- .../CardTransaction/CardApiFacade.php | 9 +++++-- Model/ApiFacade/CardTransaction/CardOpen.php | 12 +++++----- .../ApiFacade/CardTransaction/CardOrigin.php | 24 +++++++------------ 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Model/ApiFacade/CardTransaction/CardApiFacade.php b/Model/ApiFacade/CardTransaction/CardApiFacade.php index 5c1947c..daec085 100755 --- a/Model/ApiFacade/CardTransaction/CardApiFacade.php +++ b/Model/ApiFacade/CardTransaction/CardApiFacade.php @@ -30,9 +30,14 @@ public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig $this->createOpenApiInstance($tpay, $tpayConfig, $tokensService, $tpayService); } - public function makeCardTransaction(string $orderId): string + public function makeCardTransaction(string $orderId, ?array $customerToken = null): string { - return $this->getCurrent()->makeFullCardTransactionProcess($orderId); + return $this->getCurrent()->makeFullCardTransactionProcess($orderId, $customerToken); + } + + public function payTransaction(string $orderId, array $additionalPaymentInformation, ?string $transactionId = null, ?array $customerToken = null): string + { + return $this->useOpenCard ? $this->cardOpen->payTransaction($orderId, $additionalPaymentInformation, $transactionId, $customerToken) : 'error'; } private function getCurrent() diff --git a/Model/ApiFacade/CardTransaction/CardOpen.php b/Model/ApiFacade/CardTransaction/CardOpen.php index 3df16a6..66000d6 100755 --- a/Model/ApiFacade/CardTransaction/CardOpen.php +++ b/Model/ApiFacade/CardTransaction/CardOpen.php @@ -38,12 +38,12 @@ public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig $this->tpayApi->authorization()->setClientName($tpayConfig->buildMagentoInfo()); } - public function makeFullCardTransactionProcess(string $orderId): string + public function makeFullCardTransactionProcess(string $orderId, ?array $customerToken = null): string { $additionalPaymentInformation = $this->getAdditionalData($orderId); $transaction = $this->makeCardTransaction($orderId); - return $this->payTransaction($orderId, $additionalPaymentInformation, $transaction['transactionId'] ?? null); + return $this->payTransaction($orderId, $additionalPaymentInformation, $transaction['transactionId'] ?? null, $customerToken); } public function getAdditionalData(string $orderId): array @@ -74,24 +74,24 @@ public function createTransaction(): array return $transaction; } - public function payTransaction(string $orderId, array $additionalPaymentInformation, ?string $transactionId = null): string + public function payTransaction(string $orderId, array $additionalPaymentInformation, ?string $transactionId = null, ?array $customerToken = null): string { if (isset($additionalPaymentInformation['card_id']) && false !== $additionalPaymentInformation['card_id'] && $this->tpayConfig->getCardSaveEnabled()) { $cardId = (int) $additionalPaymentInformation['card_id']; - return $this->processSavedCardPayment($orderId, $cardId, $transactionId); + return $this->processSavedCardPayment($orderId, $cardId, $transactionId, $customerToken); } return $this->processNewCardPayment($orderId, $additionalPaymentInformation, $transactionId); } - private function processSavedCardPayment(string $orderId, int $cardId, ?string $transactionId = null): string + private function processSavedCardPayment(string $orderId, int $cardId, ?string $transactionId = null, ?array $customerToken = null): string { if (!$transactionId) { return 'magento2basic/tpay'; } - $customerToken = $this->tokensService->getTokenById($cardId, $this->tpay->getCustomerId($orderId)); + $customerToken ??= $this->tokensService->getTokenById($cardId, $this->tpay->getCustomerId($orderId)); if ($customerToken) { try { diff --git a/Model/ApiFacade/CardTransaction/CardOrigin.php b/Model/ApiFacade/CardTransaction/CardOrigin.php index bc8b191..693ade1 100755 --- a/Model/ApiFacade/CardTransaction/CardOrigin.php +++ b/Model/ApiFacade/CardTransaction/CardOrigin.php @@ -39,7 +39,7 @@ public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig parent::__construct(); } - public function makeFullCardTransactionProcess(string $orderId): string + public function makeFullCardTransactionProcess(string $orderId, ?array $customerToken = null): string { $payment = $this->tpayService->getPayment($orderId); $paymentData = $payment->getData(); @@ -61,25 +61,18 @@ public function makeFullCardTransactionProcess(string $orderId): string if (isset($additionalPaymentInformation['card_id']) && false !== $additionalPaymentInformation['card_id'] && $this->tpayConfig->getCardSaveEnabled()) { $cardId = (int) $additionalPaymentInformation['card_id']; - return $this->processSavedCardPayment($orderId, $cardId); + return $this->processSavedCardPayment($orderId, $cardId, $customerToken); } return $this->processNewCardPayment($orderId, $additionalPaymentInformation); } - private function processSavedCardPayment(string $orderId, string $cardId): string + private function processSavedCardPayment(string $orderId, int $cardId, ?array $customerToken = null): string { - $customerTokens = $this->tokensService->getCustomerTokens($this->tpay->getCustomerId($orderId)); - - $isValid = false; - $token = ''; - foreach ($customerTokens as $key => $value) { - if ((int) $value['tokenId'] === $cardId) { - $isValid = true; - $token = $value['token']; - } - } - if ($isValid) { + $customerToken ??= $this->tokensService->getTokenById($cardId, $this->tpay->getCustomerId($orderId)); + + if ($customerToken) { + $token = $customerToken['cli_auth']; try { $paymentResult = $this->presale($this->tpayPaymentConfig['description'], $token); @@ -108,8 +101,7 @@ private function processSavedCardPayment(string $orderId, string $cardId): strin } catch (Exception $e) { return $this->trySaleAgain($orderId); } - } - if (!$isValid) { + } else { $this->tpayService->addCommentToHistory($orderId, 'Attempt of payment by not owned card has been blocked!'); }