From c4585c7ee369aab52d0777c4e24d84a3ecf632f0 Mon Sep 17 00:00:00 2001 From: Alima Grine Date: Thu, 10 Nov 2022 16:29:11 +0100 Subject: [PATCH] [BOT] Migrate hotfix 2.6.3 to GitHub. --- Block/Payment/Rest/Head.php | 4 +- CHANGELOG.md | 5 ++ Controller/Payment/Response.php | 2 +- Controller/Payment/Rest/Check.php | 3 + Controller/Payment/Rest/Response.php | 3 + Controller/Payment/Rest/Token.php | 2 +- Helper/Data.php | 5 ++ Helper/Payment.php | 6 +- Model/Api/Rest/Api.php | 1 + Model/Method/Standard.php | 33 ++++++++- Model/StandardConfigProvider.php | 6 ++ composer.json | 2 +- etc/config.xml | 2 +- .../method-renderer/payzen-standard.js | 67 ++++++++++++++----- 14 files changed, 114 insertions(+), 27 deletions(-) diff --git a/Block/Payment/Rest/Head.php b/Block/Payment/Rest/Head.php index 4ba44da..49b50be 100644 --- a/Block/Payment/Rest/Head.php +++ b/Block/Payment/Rest/Head.php @@ -58,9 +58,7 @@ public function getStaticUrl() public function getReturnUrl() { - return $this->_urlBuilder->getUrl('payzen/payment_rest/response', [ - '_secure' => true - ]); + return $this->dataHelper->getRestReturnUrl(); } public function getLanguage() diff --git a/CHANGELOG.md b/CHANGELOG.md index 74fb5c6..e317e52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +2.6.3, 2022-11-10: +- [embedded] Do not create payment token if quote data has not changed. +- [embedded] Workarround to avoid the extra retry when the number of attempts is reached. +- Minor code fixes. + 2.6.2, 2022-10-17: - Bug fix: Fix error related to number of times a coupon is used when payment is failed. - Bug fix: Fix error related to CURLOPT_SSL_VERIFYHOST supported values in REST API. diff --git a/Controller/Payment/Response.php b/Controller/Payment/Response.php index b2b67d0..e4c1c51 100644 --- a/Controller/Payment/Response.php +++ b/Controller/Payment/Response.php @@ -157,9 +157,9 @@ protected function redirectResponse($order, $case, $checkUrlWarn = false) $this->messageManager->addWarningMessage(__('Your payment was not accepted. Please, try to re-order.')); } - $this->dataHelper->log("Restore cart for order #{$order->getIncrementId()} to allow re-order quicker."); $quote = $this->quoteRepository->get($order->getQuoteId()); if ($quote->getId()) { + $this->dataHelper->log("Restore cart for order #{$order->getIncrementId()} to allow re-order quicker."); $quote->setIsActive(true)->setReservedOrderId(null); $this->quoteRepository->save($quote); diff --git a/Controller/Payment/Rest/Check.php b/Controller/Payment/Rest/Check.php index 09e9d68..cc48666 100644 --- a/Controller/Payment/Rest/Check.php +++ b/Controller/Payment/Rest/Check.php @@ -122,6 +122,9 @@ protected function prepareResponse($params) // Disable quote. if ($quote->getIsActive()) { + $quote->getPayment()->unsAdditionalInformation(\Lyranetwork\Payzen\Helper\Payment::TOKEN_DATA); + $quote->getPayment()->unsAdditionalInformation(\Lyranetwork\Payzen\Helper\Payment::TOKEN); + $quote->setIsActive(false); $this->quoteRepository->save($quote); $this->dataHelper->log("Cleared quote, reserved order ID: #{$quote->getReservedOrderId()}."); diff --git a/Controller/Payment/Rest/Response.php b/Controller/Payment/Rest/Response.php index 2a4db28..e2005b3 100644 --- a/Controller/Payment/Rest/Response.php +++ b/Controller/Payment/Rest/Response.php @@ -111,6 +111,9 @@ protected function prepareResponse($params) // Disable quote. if ($quote->getIsActive()) { + $quote->getPayment()->unsAdditionalInformation(\Lyranetwork\Payzen\Helper\Payment::TOKEN_DATA); + $quote->getPayment()->unsAdditionalInformation(\Lyranetwork\Payzen\Helper\Payment::TOKEN); + $quote->setIsActive(false); $this->quoteRepository->save($quote); $this->dataHelper->log("Cleared quote, reserved order ID: #{$quote->getReservedOrderId()}."); diff --git a/Controller/Payment/Rest/Token.php b/Controller/Payment/Rest/Token.php index d3249eb..462ea0c 100644 --- a/Controller/Payment/Rest/Token.php +++ b/Controller/Payment/Rest/Token.php @@ -128,7 +128,7 @@ public function execute() $this->dataHelper->log("Updating form token for quote #{$quote->getId()}, reserved order ID: #{$quote->getReservedOrderId()}."); - $token = $this->standardMethod->getRestApiFormToken(); + $token = $this->standardMethod->getRestApiFormToken(true); if (! $token) { return $this->ajaxErrorResponse(); } diff --git a/Helper/Data.php b/Helper/Data.php index acf4889..f29ac93 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -241,6 +241,11 @@ public function getReturnUrl($storeId = null) return $this->_getUrl('payzen/payment/response', $params); } + public function getRestReturnUrl() + { + return $this->_getUrl('payzen/payment_rest/response', ['_secure' => true]); + } + /** * Return true if this is a backend session. * diff --git a/Helper/Payment.php b/Helper/Payment.php index 174026a8f..6114380 100644 --- a/Helper/Payment.php +++ b/Helper/Payment.php @@ -15,7 +15,11 @@ class Payment { // Key to save if payment is by identifier. const IDENTIFIER = 'payzen_identifier'; - const SEPA_IDENTIFIER = 'payzen_identifier'; + const SEPA_IDENTIFIER = 'payzen_sepa_identifier'; // TODO ????? + + const TOKEN_DATA = 'payzen_token_data'; // Key to save payment token data. + const TOKEN = 'payzen_token'; // Key to save payment token. + const TOKEN_EXPIRE = 'payzen_token_expire'; // Key to save payment token expected expiration delay. // Key to save choosen multi option. const MULTI_OPTION = 'payzen_multi_option'; diff --git a/Model/Api/Rest/Api.php b/Model/Api/Rest/Api.php index 0535582..897655f 100644 --- a/Model/Api/Rest/Api.php +++ b/Model/Api/Rest/Api.php @@ -3,6 +3,7 @@ * Copyright © Lyra Network and contributors. * * @author Lyra Network (https://www.lyra.com/) + * @author Simon Sprankel (https://github.com/sprankhub) * @copyright Lyra Network and contributors * @license See COPYING.md for license details. */ diff --git a/Model/Method/Standard.php b/Model/Method/Standard.php index 2023fdc..c6734d5 100644 --- a/Model/Method/Standard.php +++ b/Model/Method/Standard.php @@ -432,7 +432,7 @@ protected function getRestApiFormTokenData($quote) return json_encode($data); } - public function getRestApiFormToken() + public function getRestApiFormToken($renew = false) { $quote = $this->dataHelper->getCheckoutQuote(); @@ -449,6 +449,26 @@ public function getRestApiFormToken() $params = $this->getRestApiFormTokenData($quote); + $tokenDataName = \Lyranetwork\Payzen\Helper\Payment::TOKEN_DATA; + $tokenName = \Lyranetwork\Payzen\Helper\Payment::TOKEN; + $expireName = \Lyranetwork\Payzen\Helper\Payment::TOKEN_EXPIRE; + + $expireTime = $quote->getPayment()->getAdditionalInformation($expireName); + if ($renew || ($expireTime && (time() >= $expireTime))) { + $quote->getPayment()->unsAdditionalInformation($tokenDataName); + $quote->getPayment()->unsAdditionalInformation($tokenName); + } else { + $lastTokenData = $quote->getPayment()->getAdditionalInformation($tokenDataName); + $lastToken = $quote->getPayment()->getAdditionalInformation($tokenName); + + $tokenData = base64_encode(serialize($params)); + if ($lastToken && $lastTokenData && ($lastTokenData === $tokenData)) { + // Cart data does not change from last payment attempt, do not re-create payment token. + $this->dataHelper->log("Cart data did not change since last payment attempt, use last created token for quote #{$quote->getId()}, reserved order ID #{$quote->getReservedOrderId()}."); + return $lastToken; + } + } + $this->dataHelper->log("Creating form token for quote #{$quote->getId()}, reserved order ID: #{$quote->getReservedOrderId()}" . " with parameters: {$params}"); @@ -475,8 +495,17 @@ public function getRestApiFormToken() } else { $this->dataHelper->log("Form token created successfully for quote #{$quote->getId()}, reserved order ID: #{$quote->getReservedOrderId()}."); + $token = $response['answer']['formToken']; + $tokenData = base64_encode(serialize($params)); + + $quote->getPayment()->setAdditionalInformation($tokenDataName, $tokenData); + $quote->getPayment()->setAdditionalInformation($tokenName, $token); + $quote->getPayment()->setAdditionalInformation($expireName, strtotime("+15 minutes", time())); + + $quote->getPayment()->save(); + // Payment form token created successfully. - return $response['answer']['formToken']; + return $token; } } catch (\Exception $e) { $this->dataHelper->log($e->getMessage(), \Psr\Log\LogLevel::ERROR); diff --git a/Model/StandardConfigProvider.php b/Model/StandardConfigProvider.php index 071a69e..a71b5ca 100644 --- a/Model/StandardConfigProvider.php +++ b/Model/StandardConfigProvider.php @@ -56,6 +56,7 @@ public function getConfig() // For payment via REST API. $config['payment'][$this->method->getCode()]['restFormToken'] = $this->getRestFormToken(); $config['payment'][$this->method->getCode()]['language'] = $this->method->getPaymentLanguage(); + $config['payment'][$this->method->getCode()]['restReturnUrl'] = $this->dataHelper->getRestReturnUrl(); return $config; } @@ -76,6 +77,11 @@ private function getIframeLoaderUrl() private function getRestFormToken() { + // Do not create payment token until arriving to checkout page. + if ($this->urlBuilder->getCurrentUrl() != $this->urlBuilder->getUrl('checkout', ['_secure' => true])) { + return false; + } + if (! $this->method->isAvailable()) { return false; } diff --git a/composer.json b/composer.json index feed399..99ad4c4 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "php" : "~7|~8" }, "type" : "magento2-module", - "version" : "2.6.2", + "version" : "2.6.3", "license" : "OSL-3.0", "autoload" : { "files" : [ diff --git a/etc/config.xml b/etc/config.xml index 8707a32..ec32833 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -25,7 +25,7 @@ - 2.6.2 + 2.6.3 V2 Magento_2.x 1 diff --git a/view/frontend/web/js/view/payment/method-renderer/payzen-standard.js b/view/frontend/web/js/view/payment/method-renderer/payzen-standard.js index 7f6f548..423f9c9 100644 --- a/view/frontend/web/js/view/payment/method-renderer/payzen-standard.js +++ b/view/frontend/web/js/view/payment/method-renderer/payzen-standard.js @@ -198,6 +198,10 @@ define( return window.checkoutConfig.payment.payzen_standard.restFormToken || null; }, + getRestReturnUrl: function() { + return window.checkoutConfig.payment[this.item.method].restReturnUrl || null; + }, + getLanguage: function() { return window.checkoutConfig.payment.payzen_standard.language || null; }, @@ -243,32 +247,61 @@ define( language: me.getLanguage() }).then( function(v) { - var KR = v.KR; + KR = v.KR; KR.onFocus(function(e) { $('#payzen_rest_form .kr-form-error').html(''); }); KR.onError(function(e) { - fullScreenLoader.stopLoader(); - me.isPlaceOrderActionAllowed(true); + var answer = e.metadata.answer; - var msg = ''; - if (DFAULT_MESSAGES.indexOf(e.errorCode) > -1) { - msg = e.errorMessage; - var endsWithDot = (msg.lastIndexOf('.') == (msg.length - 1) && msg.lastIndexOf('.') >= 0); + // Force redirection to response page if possibility of retries is exhausted. + if (answer.hasOwnProperty('clientAnswer') && (answer.clientAnswer.orderStatus == "UNPAID") && (answer.clientAnswer.orderCycle == "CLOSED")) { + var data = { + 'kr-answer-type': 'V4/Payment', + 'kr-answer': JSON.stringify(answer.clientAnswer), + 'kr-hash': answer.hash, + 'kr-hash-algorithm': answer.hashAlgorithm + }; - msg += (endsWithDot ? '' : '.'); - } else { - msg = me.translateError(e.errorCode); - } + var form = $('
'); + form.attr("method", "post"); + form.attr("action", me.getRestReturnUrl()); - // Expiration errors, display a link to refresh the page. - if (EXPIRY_ERRORS.indexOf(e.errorCode) >= 0) { - msg += ' ' - + me.translateError('RELOAD_LINK') + ''; - } + $.each(data, function(key, value) { + var field = $(''); - $('#payzen_rest_form .kr-form-error').html('' + msg + ''); + field.attr("type", "hidden"); + field.attr("name", key); + field.attr("value", value); + + form.append(field); + }); + + $(document.body).append(form); + form.submit(); + } else { + fullScreenLoader.stopLoader(); + me.isPlaceOrderActionAllowed(true); + + var msg = ''; + if (DFAULT_MESSAGES.indexOf(e.errorCode) > -1) { + msg = e.errorMessage; + var endsWithDot = (msg.lastIndexOf('.') == (msg.length - 1) && msg.lastIndexOf('.') >= 0); + + msg += (endsWithDot ? '' : '.'); + } else { + msg = me.translateError(e.errorCode); + } + + // Expiration errors, display a link to refresh the page. + if (EXPIRY_ERRORS.indexOf(e.errorCode) >= 0) { + msg += ' ' + + me.translateError('RELOAD_LINK') + ''; + } + + $('#payzen_rest_form .kr-form-error').html('' + msg + ''); + } }); KR.onSubmit(function(e) {