Skip to content

Commit

Permalink
Merge branch 'release/2.7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alima Grine committed Feb 3, 2023
2 parents 226f7e3 + 6d9f156 commit 99cc162
Show file tree
Hide file tree
Showing 25 changed files with 667 additions and 316 deletions.
56 changes: 56 additions & 0 deletions Block/Adminhtml/System/Config/Form/Field/PluginDoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Lyra Network.
* This file is part of PayZen plugin for Magento 2. See COPYING.md for license details.
*
* @author Lyra Network (https://www.lyra.com/)
* @copyright Lyra Network
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
namespace Lyranetwork\Payzen\Block\Adminhtml\System\Config\Form\Field;

use Lyranetwork\Payzen\Model\Api\Form\Api as PayzenApi;

/**
* Custom renderer for the module documentation URLs.
*/
class PluginDoc extends Label
{
/**
* @param \Lyranetwork\Payzen\Block\Adminhtml\System\Config\Form\Field\Context $context
* @param array $data
*/
public function __construct(
\Lyranetwork\Payzen\Block\Adminhtml\System\Config\Form\Field\Context $context,
array $data = []
) {
parent::__construct($context, $data);
}

/**
* Unset some non-related element parameters.
*
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
* @return string
*/
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
// Get documentation links.
$languages = array(
'fr' => 'Français',
'en' => 'English',
'es' => 'Español',
'pt' => 'Português'
// Complete when other languages are managed.
);

$docs = "";
foreach (PayzenApi::getOnlineDocUri() as $lang => $docUri) {
$docs .= '<a style="margin-left: 10px; text-decoration: none; text-transform: uppercase;" href="' . $docUri . 'magento2/sitemap.html" target="_blank">' . $languages[$lang] . '</a>';
}

$element->setComment($docs);

return parent::render($element);
}
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.7.0, 2023-02-03:
- [embedded] Added possibility to update order status on each payment attempt.
- [embedded] Improve order and payment processing.
- Added field for online module documentation.
- Minor code fixes

2.6.5, 2023-01-23:
- Bug fix: Fix payment in iframe mode when JavaScript minification is enabled in Magento.
- Update list of supported payment means.
Expand Down
6 changes: 5 additions & 1 deletion Controller/Payment/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ public function execute()

$order = $data['order'];
$response = $data['response'];

$result = $this->responseProcessor->execute($order, $response);

$updateOrder = $this->getRequest()->getParam('payzen_update_order', false);
if ($updateOrder) {
return null;
}

return $this->redirectResponse($order, $result['case'], $result['warn']);
} catch (\Lyranetwork\Payzen\Model\ResponseException $e) {
$this->dataHelper->log($e->getMessage(), \Psr\Log\LogLevel::ERROR);
Expand Down
107 changes: 58 additions & 49 deletions Controller/Payment/Rest/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,62 +112,66 @@ protected function prepareResponse($params)
]
);

$quoteId = (int) $response->getExtInfo('quote_id');
if (! $quoteId || ! $this->quoteRepository->get($quoteId)->getId()) {
$this->dataHelper->log("Quote not found with ID #{$quoteId}.", \Psr\Log\LogLevel::ERROR);
throw new ResponseException($response->getOutputForGateway('order_not_found'));
$orderId = (int) $response->get('order_id');
if (! $orderId) {
$this->dataHelper->log("Received empty Order ID.", \Psr\Log\LogLevel::ERROR);
throw new ResponseException('Order ID is empty.');
}

$quote = $this->quoteRepository->get($quoteId);
$order = $this->orderFactory->create();
$order->loadByIncrementId($orderId);

if (! $order->getId()) {
// Backward compatibility with older versions.
if ($quoteId = (int) $response->getExtInfo('quote_id')) {
if ($this->quoteRepository->get($quoteId)->getId()) {
$this->dataHelper->log("Quote not found with ID #{$quoteId}.", \Psr\Log\LogLevel::ERROR);
throw new ResponseException($response->getOutputForGateway('order_not_found'));
}

$quote = $this->quoteRepository->get($quoteId);
$this->saveOrderForQuote($quote);

// Dispatch save order event.
$result = new DataObject();
$result->setData('success', true);
$result->setData('error', false);

$this->_eventManager->dispatch(
'checkout_controller_onepage_saveOrder',
[
'result' => $result,
'action' => $this
]
);

// Load newly created order.
$order->loadByIncrementId($quote->getReservedOrderId());
if (! $order->getId()) {
$this->dataHelper->log("Order cannot be created. Quote ID: #{$quoteId}, reserved order ID: #{$quote->getReservedOrderId()}.", \Psr\Log\LogLevel::ERROR);
throw new ResponseException($response->getOutputForGateway('ko', 'Error when trying to create order.'));
}
} else {
$this->dataHelper->log("Order not found with ID #{$orderId}.", \Psr\Log\LogLevel::ERROR);
throw new ResponseException("Order not found with ID #{$orderId}.");
}
}

// Disable quote.
$quote = $this->quoteRepository->get($order->getQuoteId());
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()}.");
$this->dataHelper->log("Cleared quote, order ID: #{$orderId}.");
}

// Case of failure or expiration when retries are enabled, do nothing before last attempt.
if (! $response->isAcceptedPayment() && ($answer['orderCycle'] !== 'CLOSED')) {
$this->dataHelper->log("Payment is not accepted but buyer can try to re-order. Do not create order at this time.
Quote ID: #{$quoteId}, reserved order ID: #{$quote->getReservedOrderId()}.");
// Case of failure or expiration when retries are enabled and Update Order option not is not enabled, do nothing before last attempt.
if (! $response->isAcceptedPayment() && ($answer['orderCycle'] !== 'CLOSED') && ! $response->getExtInfo('update_order')) {
$this->dataHelper->log("Payment is not accepted but buyer can try to re-order. Do not process order at this time.
Order ID: #{$orderId}.");
throw new ResponseException($response->getOutputForGateway('payment_ko_bis'));
}

// Token is created before order creation, search order by quote.
$order = $this->orderFactory->create();
$order->loadByIncrementId($quote->getReservedOrderId());
if (! $order->getId()) {
$this->saveOrderForQuote($quote);

// Dispatch save order event.
$result = new DataObject();
$result->setData('success', true);
$result->setData('error', false);

$this->_eventManager->dispatch(
'checkout_controller_onepage_saveOrder',
[
'result' => $result,
'action' => $this
]
);

// Load newly created order.
$order->loadByIncrementId($quote->getReservedOrderId());
if (! $order->getId()) {
$this->dataHelper->log("Order cannot be created. Quote ID: #{$quoteId}, reserved order ID: #{$quote->getReservedOrderId()}.", \Psr\Log\LogLevel::ERROR);
throw new ResponseException($response->getOutputForGateway('ko', 'Error when trying to create order.'));
}

$this->dataHelper->log("Order #{$order->getIncrementId()} has been created for quote #{$quoteId}.");
} else {
$this->dataHelper->log("Found order #{$order->getIncrementId()} for quote #{$quoteId}.");
}

// Get store id from order.
$storeId = $order->getStore()->getId();

Expand All @@ -193,11 +197,16 @@ protected function prepareResponse($params)

protected function saveOrderForQuote($quote)
{
$this->onepage->setQuote($quote);
if ($quote->getCustomerId()) {
$this->onepage->getCustomerSession()->loginById($quote->getCustomerId());
}
try {
$this->onepage->setQuote($quote);
if ($quote->getCustomerId()) {
$this->onepage->getCustomerSession()->loginById($quote->getCustomerId());
}

$this->onepage->saveOrder();
$this->onepage->saveOrder();
} catch (Exception $e) {
$this->dataHelper->log("Order cannot be created. Quote ID: #{$quote->getId()}, reserved order ID: #{$quote->getReservedOrderId()}.", \Psr\Log\LogLevel::ERROR);
throw new ResponseException('Error when trying to create order.');
}
}
}
59 changes: 13 additions & 46 deletions Controller/Payment/Rest/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,53 +102,26 @@ protected function prepareResponse($params)
]
);

$quoteId = (int) $response->getExtInfo('quote_id');
if (! $quoteId || ! $this->quoteRepository->get($quoteId)->getId()) {
throw new ResponseException("Quote #{$quoteId} not found in database.");
$orderId = (int) $response->get('order_id');
if (! $orderId) {
$this->dataHelper->log("Received empty Order ID.", \Psr\Log\LogLevel::ERROR);
throw new ResponseException('Order ID is empty.');
}

$quote = $this->quoteRepository->get($quoteId);
$order = $this->orderFactory->create();
$order->loadByIncrementId($orderId);

if (! $order->getId()) {
$this->dataHelper->log("Order not found with ID #{$orderId}.", \Psr\Log\LogLevel::ERROR);
throw new ResponseException("Order not found with ID #{$orderId}.");
}

// Disable quote.
$quote = $this->quoteRepository->get($order->getQuoteId());
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()}.");
}

// Token is created before order creation, search order by quote.
$order = $this->orderFactory->create();
$order->loadByIncrementId($quote->getReservedOrderId());

if (! $order->getId()) {
// Reload onepage context.
$this->saveOrderForQuote($quote);

// Dispatch save order event.
$result = new DataObject();
$result->setData('success', true);
$result->setData('error', false);

$this->_eventManager->dispatch(
'checkout_controller_onepage_saveOrder',
[
'result' => $result,
'action' => $this
]
);

// Load newly created order.
$order->loadByIncrementId($quote->getReservedOrderId());
if (! $order->getId()) {
throw new ResponseException("Order cannot be created for quote #{$quoteId}.");
}

$this->dataHelper->log("Order #{$order->getIncrementId()} has been created for quote #{$quoteId}.");
} else {
$this->dataHelper->log("Found order #{$order->getIncrementId()} for quote #{$quoteId}.");
$this->dataHelper->log("Cleared quote, order ID: #{$orderId}.");
}

$storeId = $order->getStore()->getId();
Expand All @@ -166,10 +139,4 @@ protected function prepareResponse($params)
'order' => $order
];
}

protected function saveOrderForQuote($quote)
{
$this->onepage->setQuote($quote);
$this->onepage->saveOrder();
}
}
Loading

0 comments on commit 99cc162

Please sign in to comment.