Skip to content

Commit

Permalink
Release 2.1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrowanwallee committed Jan 14, 2025
1 parent f8da071 commit 626e562
Show file tree
Hide file tree
Showing 29 changed files with 223 additions and 70 deletions.
30 changes: 10 additions & 20 deletions Controller/Checkout/RestoreCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
namespace PostFinanceCheckout\Payment\Controller\Checkout;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\DataObject;
use Magento\Framework\App\Action\Context;

Expand All @@ -21,27 +20,18 @@
class RestoreCart extends \PostFinanceCheckout\Payment\Controller\Checkout
{

/**
*
* @var CheckoutSession
*/
private $checkoutSession;

/**
*
* @param Context $context
* @param CheckoutSession $checkoutSession
*/
public function __construct(Context $context, CheckoutSession $checkoutSession)
{
parent::__construct($context);
$this->checkoutSession = $checkoutSession;
}

public function execute()
{
$this->checkoutSession->restoreQuote();

try {
// Triggers event to validate and restore quote.
$this->_eventManager->dispatch('postfinancecheckout_validate_and_restore_quote');
} catch (\Exception $e) {
// If an error occurs, we display a generic message and redirect to the cart.
$this->messageManager->addErrorMessage(__('An error occurred while restoring your cart.'));
return $this->_redirect('checkout/cart');
}

// Redirects to the cart or to the path determined by the redirection.
return $this->_redirect($this->getFailureRedirectionPath());
}

Expand Down
8 changes: 8 additions & 0 deletions Controller/Transaction/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public function execute()
'However, the payment was successful. Please contact us.'));
return $this->_redirect('checkout/cart');
}

// Deactivate quote after successful payment.
$quote = $this->checkoutSession->getQuote();
if ($quote && $quote->getId()) {
$quote->setIsActive(false);
$quote->removeAllItems();
$quote->save();
}

$this->checkoutSession->setLastOrderId($order->getId())
->setLastRealOrderId($order->getIncrementId())
Expand Down
8 changes: 6 additions & 2 deletions Model/Config/Source/IntegrationMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IntegrationMethod implements \Magento\Framework\Option\ArrayInterface

const IFRAME = 'iframe';
const LIGHTBOX = 'lightbox';
const PAYMENT_PAGE = 'paymentpage';
const PAYMENT_PAGE = 'payment_page';

public function toOptionArray()
{
Expand All @@ -31,7 +31,11 @@ public function toOptionArray()
[
'value' => self::LIGHTBOX,
'label' => \__('Lightbox')
]
],
[
'value' => self::PAYMENT_PAGE,
'label' => \__('Payment Page')
]
];
}
}
16 changes: 16 additions & 0 deletions Model/Resolver/UpdateTransactionUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ private function setTransactionUrls($cartIdMasked, $successUrl, $failureUrl)
// Get the quote using the actual ID
/** @var Quote $quote */
$quote = $this->cartRepository->get($quoteId);
$spaceId = $quote->getPostfinancecheckoutSpaceId();
$transactionId = $quote->getPostfinancecheckoutTransactionId();

//At this step, if the transaction ID and space ID are empty,
//it could be because the enableAvailablePaymentMethodsCheck option is active,
//and the quote no longer has these values.
if (empty($spaceId) || empty($transactionId)) {
//Fetching the JavaScript URL here allows updating the quote
//with the current transaction and saving it in the session.
$this->transactionQuoteService->getJavaScriptUrl($quote);

//values from the session
$quote = $this->checkoutSession->getQuote();
$spaceId = $quote->getPostfinancecheckoutSpaceId();
$transactionId = $quote->getPostfinancecheckoutTransactionId();
}

//$quoteSession = $this->checkoutSession->getQuote();
/** @var \PostFinanceCheckout\Payment\Model\ResourceModel\TransactionInfo $transactionInfo */
Expand Down
53 changes: 37 additions & 16 deletions Model/Webhook/Listener/AbstractOrderRelatedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Lock\LockManagerInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order as OrderResourceModel;
Expand Down Expand Up @@ -63,6 +64,12 @@ abstract class AbstractOrderRelatedListener implements ListenerInterface
*/
private $transactionInfoRepository;

/**
*
* @var lockManager
*/
protected $lockManager;

/**
*
* @param ResourceConnection $resource
Expand All @@ -71,17 +78,24 @@ abstract class AbstractOrderRelatedListener implements ListenerInterface
* @param OrderResourceModel $orderResourceModel
* @param CommandPoolInterface $commandPool
* @param TransactionInfoRepositoryInterface $transactionInfoRepository
* @param LockManagerInterface $lockManager
*/
public function __construct(ResourceConnection $resource, LoggerInterface $logger, OrderFactory $orderFactory,
OrderResourceModel $orderResourceModel, CommandPoolInterface $commandPool,
TransactionInfoRepositoryInterface $transactionInfoRepository)
{
public function __construct(
ResourceConnection $resource,
LoggerInterface $logger,
OrderFactory $orderFactory,
OrderResourceModel $orderResourceModel,
CommandPoolInterface $commandPool,
TransactionInfoRepositoryInterface $transactionInfoRepository,
LockManagerInterface $lockManager
) {
$this->resource = $resource;
$this->logger = $logger;
$this->orderFactory = $orderFactory;
$this->orderResourceModel = $orderResourceModel;
$this->commandPool = $commandPool;
$this->transactionInfoRepository = $transactionInfoRepository;
$this->lockManager = $lockManager;
}

public function execute(Request $request)
Expand All @@ -90,6 +104,7 @@ public function execute(Request $request)
$connection = $this->beginTransaction();
try {
$order = $this->loadOrder($this->getOrderId($entity));
$transactionId = $order->getPostfinancecheckoutTransactionId();
if ($order instanceof Order) {
if ($order->getPostfinancecheckoutTransactionId() != $this->getTransactionId($entity)) {
$this->logger->warning(
Expand All @@ -98,13 +113,15 @@ public function execute(Request $request)
$connection->commit();
return;
}
$this->lock($order);
$this->lock($transactionId);
$this->process($this->loadEntity($request), $this->loadOrder($order->getId()));
}
$this->unlock($transactionId);
$connection->commit();
} catch (\Exception $e) {
$this->unlock($transactionId);
$connection->rollback();
$this->logger->critical($e);
$connection->rollBack();
throw $e;
}
}
Expand Down Expand Up @@ -171,17 +188,21 @@ private function getOrderId($entity)
/**
* Creates a lock to prevent concurrency.
*
* @param Order $order
* @return void
* @return bool
*/
protected function lock(string $lockId): bool
{
return $this->lockManager->lock($lockId, 1);
}

/**
* Releases our lock.
*
* @return bool
*/
private function lock(Order $order)
protected function unlock(string $lockId): bool
{
$this->resource->getConnection()->update($this->resource->getTableName('sales_order'),
[
'postfinancecheckout_lock' => \date('Y-m-d H:i:s')
], [
'entity_id = ?' => $order->getId()
]);
return $this->lockManager->unlock($lockId);
}

/**
Expand Down Expand Up @@ -216,4 +237,4 @@ abstract protected function loadEntity(Request $request);
* @return int
*/
abstract protected function getTransactionId($entity);
}
}
9 changes: 6 additions & 3 deletions Model/Webhook/Listener/DeliveryIndicationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PostFinanceCheckout\Payment\Model\Webhook\Listener;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Lock\LockManagerInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order as OrderResourceModel;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -41,13 +42,15 @@ class DeliveryIndicationListener extends AbstractOrderRelatedListener
* @param CommandPoolInterface $commandPool
* @param TransactionInfoRepositoryInterface $transactionInfoRepository
* @param ApiClient $apiClient
* @param LockManagerInterface $lockManager
*/
public function __construct(ResourceConnection $resource, LoggerInterface $logger, OrderFactory $orderFactory,
OrderResourceModel $orderResourceModel, CommandPoolInterface $commandPool,
TransactionInfoRepositoryInterface $transactionInfoRepository, ApiClient $apiClient)
TransactionInfoRepositoryInterface $transactionInfoRepository, ApiClient $apiClient,
LockManagerInterface $lockManager)
{
parent::__construct($resource, $logger, $orderFactory, $orderResourceModel, $commandPool,
$transactionInfoRepository);
$transactionInfoRepository, $lockManager);
$this->apiClient = $apiClient;
}

Expand All @@ -73,4 +76,4 @@ protected function getTransactionId($entity)
{
return $entity->getLinkedTransaction();
}
}
}
9 changes: 6 additions & 3 deletions Model/Webhook/Listener/RefundListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PostFinanceCheckout\Payment\Model\Webhook\Listener;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Lock\LockManagerInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order as OrderResourceModel;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -41,13 +42,15 @@ class RefundListener extends AbstractOrderRelatedListener
* @param CommandPoolInterface $commandPool
* @param TransactionInfoRepositoryInterface $transactionInfoRepository
* @param ApiClient $apiClient
* @param LockManagerInterface $lockManager
*/
public function __construct(ResourceConnection $resource, LoggerInterface $logger, OrderFactory $orderFactory,
OrderResourceModel $orderResourceModel, CommandPoolInterface $commandPool,
TransactionInfoRepositoryInterface $transactionInfoRepository, ApiClient $apiClient)
TransactionInfoRepositoryInterface $transactionInfoRepository, ApiClient $apiClient,
LockManagerInterface $lockManager)
{
parent::__construct($resource, $logger, $orderFactory, $orderResourceModel, $commandPool,
$transactionInfoRepository);
$transactionInfoRepository, $lockManager);
$this->apiClient = $apiClient;
}

Expand All @@ -72,4 +75,4 @@ protected function getTransactionId($entity)
{
return $entity->getTransaction()->getId();
}
}
}
9 changes: 6 additions & 3 deletions Model/Webhook/Listener/TransactionCompletionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PostFinanceCheckout\Payment\Model\Webhook\Listener;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Lock\LockManagerInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order as OrderResourceModel;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -41,13 +42,15 @@ class TransactionCompletionListener extends AbstractOrderRelatedListener
* @param CommandPoolInterface $commandPool
* @param TransactionInfoRepositoryInterface $transactionInfoRepository
* @param ApiClient $apiClient
* @param LockManagerInterface $lockManager
*/
public function __construct(ResourceConnection $resource, LoggerInterface $logger, OrderFactory $orderFactory,
OrderResourceModel $orderResourceModel, CommandPoolInterface $commandPool,
TransactionInfoRepositoryInterface $transactionInfoRepository, ApiClient $apiClient)
TransactionInfoRepositoryInterface $transactionInfoRepository, ApiClient $apiClient,
LockManagerInterface $lockManager)
{
parent::__construct($resource, $logger, $orderFactory, $orderResourceModel, $commandPool,
$transactionInfoRepository);
$transactionInfoRepository, $lockManager);
$this->apiClient = $apiClient;
}

Expand Down Expand Up @@ -75,4 +78,4 @@ protected function getTransactionId($entity)
->getTransaction()
->getId();
}
}
}
9 changes: 6 additions & 3 deletions Model/Webhook/Listener/TransactionInvoiceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PostFinanceCheckout\Payment\Model\Webhook\Listener;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Lock\LockManagerInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order as OrderResourceModel;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -41,13 +42,15 @@ class TransactionInvoiceListener extends AbstractOrderRelatedListener
* @param CommandPoolInterface $commandPool
* @param TransactionInfoRepositoryInterface $transactionInfoRepository
* @param ApiClient $apiClient
* @param LockManagerInterface $lockManager
*/
public function __construct(ResourceConnection $resource, LoggerInterface $logger, OrderFactory $orderFactory,
OrderResourceModel $orderResourceModel, CommandPoolInterface $commandPool,
TransactionInfoRepositoryInterface $transactionInfoRepository, ApiClient $apiClient)
TransactionInfoRepositoryInterface $transactionInfoRepository, ApiClient $apiClient,
LockManagerInterface $lockManager)
{
parent::__construct($resource, $logger, $orderFactory, $orderResourceModel, $commandPool,
$transactionInfoRepository);
$transactionInfoRepository, $lockManager);
$this->apiClient = $apiClient;
}

Expand All @@ -73,4 +76,4 @@ protected function getTransactionId($entity)
{
return $entity->getLinkedTransaction();
}
}
}
19 changes: 13 additions & 6 deletions Model/Webhook/Listener/TransactionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PostFinanceCheckout\Payment\Model\Webhook\Listener;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Lock\LockManagerInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order as OrderResourceModel;
Expand Down Expand Up @@ -62,14 +63,20 @@ class TransactionListener extends AbstractOrderRelatedListener
* @param TransactionInfoManagementInterface $transactionInfoManagement
* @param ApiClient $apiClient
* @param LoggerInterface $logger
* @param LockManagerInterface $lockManager
*/
public function __construct(ResourceConnection $resource, LoggerInterface $logger, OrderFactory $orderFactory,
OrderResourceModel $orderResourceModel, CommandPoolInterface $commandPool,
public function __construct(
ResourceConnection $resource,
LoggerInterface $logger, OrderFactory $orderFactory,
OrderResourceModel $orderResourceModel,
CommandPoolInterface $commandPool,
TransactionInfoRepositoryInterface $transactionInfoRepository,
TransactionInfoManagementInterface $transactionInfoManagement, ApiClient $apiClient)
{
TransactionInfoManagementInterface $transactionInfoManagement,
ApiClient $apiClient,
LockManagerInterface $lockManager
) {
parent::__construct($resource, $logger, $orderFactory, $orderResourceModel, $commandPool,
$transactionInfoRepository);
$transactionInfoRepository, $lockManager);
$this->transactionInfoRepository = $transactionInfoRepository;
$this->transactionInfoManagement = $transactionInfoManagement;
$this->apiClient = $apiClient;
Expand Down Expand Up @@ -115,4 +122,4 @@ protected function getTransactionId($entity)
{
return $entity->getId();
}
}
}
Loading

0 comments on commit 626e562

Please sign in to comment.