Skip to content

Commit

Permalink
[BOT] Migrate hotfix 2.6.3 to GitHub.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alima Grine committed Nov 10, 2022
1 parent 594f212 commit c4585c7
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 27 deletions.
4 changes: 1 addition & 3 deletions Block/Payment/Rest/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Controller/Payment/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions Controller/Payment/Rest/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}.");
Expand Down
3 changes: 3 additions & 0 deletions Controller/Payment/Rest/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}.");
Expand Down
2 changes: 1 addition & 1 deletion Controller/Payment/Rest/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 5 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 5 additions & 1 deletion Helper/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions Model/Api/Rest/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
33 changes: 31 additions & 2 deletions Model/Method/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ protected function getRestApiFormTokenData($quote)
return json_encode($data);
}

public function getRestApiFormToken()
public function getRestApiFormToken($renew = false)
{
$quote = $this->dataHelper->getCheckoutQuote();

Expand All @@ -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}");

Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions Model/StandardConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"php" : "~7|~8"
},
"type" : "magento2-module",
"version" : "2.6.2",
"version" : "2.6.3",
"license" : "OSL-3.0",
"autoload" : {
"files" : [
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<payzen>
<general>
<support_email><![CDATA[[email protected]]]></support_email>
<plugin_version>2.6.2</plugin_version>
<plugin_version>2.6.3</plugin_version>
<gateway_version>V2</gateway_version>
<cms_identifier>Magento_2.x</cms_identifier>
<enable_logs>1</enable_logs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -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></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 += ' <a href="#" onclick="window.location.reload(); return false;">'
+ me.translateError('RELOAD_LINK') + '</a>';
}
$.each(data, function(key, value) {
var field = $('<input></input>');

$('#payzen_rest_form .kr-form-error').html('<span style="color: red;"><span>' + msg + '</span></span>');
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 += ' <a href="#" onclick="window.location.reload(); return false;">'
+ me.translateError('RELOAD_LINK') + '</a>';
}

$('#payzen_rest_form .kr-form-error').html('<span style="color: red;"><span>' + msg + '</span></span>');
}
});

KR.onSubmit(function(e) {
Expand Down

0 comments on commit c4585c7

Please sign in to comment.