Skip to content

Commit

Permalink
Merge pull request #770 from buckaroo-it/BP-2132-clean-refactor-push-…
Browse files Browse the repository at this point in the history
…redirect

BP- 2132 BP-2813 clean refactor push & redirect
  • Loading branch information
LucianTuriacArnia committed Aug 16, 2023
2 parents 5b52c7e + f73ec9e commit b4d9c97
Show file tree
Hide file tree
Showing 35 changed files with 4,547 additions and 2,810 deletions.
12 changes: 12 additions & 0 deletions Api/PushProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Buckaroo\Magento2\Api;

interface PushProcessorInterface
{
/**
* @param PushRequestInterface $pushRequest
* @return bool
*/
public function processPush(PushRequestInterface $pushRequest): bool;
}
94 changes: 26 additions & 68 deletions Controller/Payconiq/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,31 @@
* @copyright Copyright (c) Buckaroo B.V.
* @license https://tldrlegal.com/license/mit-license
*/
declare(strict_types=1);

namespace Buckaroo\Magento2\Controller\Payconiq;

use Buckaroo\Magento2\Exception;
use Buckaroo\Magento2\Helper\Data;
use Buckaroo\Magento2\Logging\Log;
use Buckaroo\Magento2\Model\ConfigProvider\Factory;
use Buckaroo\Magento2\Model\BuckarooStatusCode;
use Buckaroo\Magento2\Model\ConfigProvider\Account as AccountConfig;
use Buckaroo\Magento2\Model\OrderStatusFactory;
use Buckaroo\Magento2\Model\RequestPush\RequestPushFactory;
use Buckaroo\Magento2\Model\Service\Order as OrderService;
use Buckaroo\Magento2\Service\Push\OrderRequestService;
use Buckaroo\Magento2\Service\Sales\Quote\Recreate;
use Magento\Checkout\Model\Cart;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\ResourceModel\CustomerFactory;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Customer\Model\SessionFactory;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Model\Quote;
use Magento\Sales\Api\Data\TransactionInterface;
use Magento\Sales\Api\Data\TransactionSearchResultInterface;
use Magento\Sales\Api\TransactionRepositoryInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Magento\Sales\Model\Order\Payment\Transaction;

/**
Expand All @@ -57,91 +52,57 @@ class Process extends \Buckaroo\Magento2\Controller\Redirect\Process
/**
* @var null|Transaction
*/
protected $transaction = null;
protected ?Transaction $transaction = null;

/**
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
protected SearchCriteriaBuilder $searchCriteriaBuilder;

/**
* @var TransactionRepositoryInterface
*/
protected $transactionRepository;
protected TransactionRepositoryInterface $transactionRepository;

/**
* @param Context $context
* @param Data $helper
* @param Cart $cart
* @param Order $order
* @param Quote $quote
* @param TransactionInterface $transaction
* @param Log $logger
* @param Factory $configProviderFactory
* @param OrderSender $orderSender
* @param Quote $quote
* @param AccountConfig $accountConfig
* @param OrderRequestService $orderRequestService
* @param OrderStatusFactory $orderStatusFactory
* @param CheckoutSession $checkoutSession
* @param CustomerSession $customerSession
* @param CustomerRepositoryInterface $customerRepository
* @param SessionFactory $sessionFactory
* @param Customer $customerModel
* @param CustomerFactory $customerFactory
* @param OrderService $orderService
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param TransactionRepositoryInterface $transactionRepository
* @param ManagerInterface $eventManager
* @param Recreate $quoteRecreate
* @param RequestPushFactory $requestPushFactory
* @throws Exception
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param TransactionRepositoryInterface $transactionRepository
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Context $context,
Data $helper,
Cart $cart,
Order $order,
Quote $quote,
TransactionInterface $transaction,
Log $logger,
Factory $configProviderFactory,
OrderSender $orderSender,
Quote $quote,
AccountConfig $accountConfig,
OrderRequestService $orderRequestService,
OrderStatusFactory $orderStatusFactory,
CheckoutSession $checkoutSession,
CustomerSession $customerSession,
CustomerRepositoryInterface $customerRepository,
SessionFactory $sessionFactory,
Customer $customerModel,
CustomerFactory $customerFactory,
OrderService $orderService,
SearchCriteriaBuilder $searchCriteriaBuilder,
TransactionRepositoryInterface $transactionRepository,
ManagerInterface $eventManager,
Recreate $quoteRecreate,
RequestPushFactory $requestPushFactory
RequestPushFactory $requestPushFactory,
SearchCriteriaBuilder $searchCriteriaBuilder,
TransactionRepositoryInterface $transactionRepository
) {
parent::__construct(
$context,
$helper,
$cart,
$order,
$quote,
$transaction,
$logger,
$configProviderFactory,
$orderSender,
$orderStatusFactory,
$checkoutSession,
$customerSession,
$customerRepository,
$sessionFactory,
$customerModel,
$customerFactory,
$orderService,
$eventManager,
$quoteRecreate,
$requestPushFactory
);
parent::__construct($context, $logger, $quote, $accountConfig, $orderRequestService,
$orderStatusFactory, $checkoutSession, $customerSession, $customerRepository,
$orderService, $eventManager, $quoteRecreate, $requestPushFactory);

$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->transactionRepository = $transactionRepository;
Expand All @@ -151,7 +112,6 @@ public function __construct(
* Redirect Process Payconiq
*
* @return ResponseInterface|void
* @throws LocalizedException
* @throws Exception
*/
public function execute()
Expand All @@ -162,7 +122,7 @@ public function execute()
}

$transaction = $this->getTransaction();
$this->order = $transaction->getOrder();
$this->order = $transaction->getOrder()->getOrder();

if ($this->customerSession->getCustomerId() == $this->order->getCustomerId()) {
$this->logger->addError('Customer is different then the customer that start payconiq process request.');
Expand All @@ -173,17 +133,15 @@ public function execute()
'checkout',
[
'_fragment' => 'payment',
'_query' => ['bk_e' => 1]
'_query' => ['bk_e' => 1]
]
);
}
$this->quote->load($this->order->getQuoteId());

// @codingStandardsIgnoreStart
try {
$this->handleFailed(
$this->helper->getStatusCode('BUCKAROO_MAGENTO2_STATUSCODE_CANCELLED_BY_USER')
);
$this->handleFailed(BuckarooStatusCode::CANCELLED_BY_USER);
} catch (\Exception $exception) {
// handle failed exception
}
Expand Down Expand Up @@ -212,7 +170,7 @@ protected function getTransactionKey()
/**
* Get transaction object
*
* @return TransactionInterface|Transaction
* @return TransactionInterface|Transaction|null
* @throws Exception
*/
protected function getTransaction()
Expand All @@ -239,7 +197,7 @@ protected function getTransaction()
* @return TransactionSearchResultInterface
* @throws Exception
*/
protected function getList()
protected function getList(): TransactionSearchResultInterface
{
$transactionKey = $this->getTransactionKey();

Expand Down
168 changes: 168 additions & 0 deletions Controller/Redirect/IdinProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License
* It is available through the world-wide-web at this URL:
* https://tldrlegal.com/license/mit-license
* If you are unable to obtain it through the world-wide-web, please email
* to [email protected], so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please contact [email protected] for more information.
*
* @copyright Copyright (c) Buckaroo B.V.
* @license https://tldrlegal.com/license/mit-license
*/
declare(strict_types=1);

namespace Buckaroo\Magento2\Controller\Redirect;

use Buckaroo\Magento2\Logging\Log;
use Buckaroo\Magento2\Model\ConfigProvider\Account as AccountConfig;
use Buckaroo\Magento2\Model\OrderStatusFactory;
use Buckaroo\Magento2\Model\RequestPush\RequestPushFactory;
use Buckaroo\Magento2\Model\Service\Order as OrderService;
use Buckaroo\Magento2\Service\Push\OrderRequestService;
use Buckaroo\Magento2\Service\Sales\Quote\Recreate;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\ResourceModel\CustomerFactory;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Quote\Model\Quote;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class IdinProcess extends Process
{
/**
* @var CustomerFactory
*/
private CustomerFactory $customerResourceFactory;

/**
* @param Context $context
* @param Log $logger
* @param Quote $quote
* @param AccountConfig $accountConfig
* @param OrderRequestService $orderRequestService
* @param OrderStatusFactory $orderStatusFactory
* @param CheckoutSession $checkoutSession
* @param CustomerSession $customerSession
* @param CustomerRepositoryInterface $customerRepository
* @param OrderService $orderService
* @param ManagerInterface $eventManager
* @param Recreate $quoteRecreate
* @param RequestPushFactory $requestPushFactory
* @param CustomerFactory $customerFactory
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Context $context,
Log $logger,
Quote $quote,
AccountConfig $accountConfig,
OrderRequestService $orderRequestService,
OrderStatusFactory $orderStatusFactory,
CheckoutSession $checkoutSession,
CustomerSession $customerSession,
CustomerRepositoryInterface $customerRepository,
OrderService $orderService,
ManagerInterface $eventManager,
Recreate $quoteRecreate,
RequestPushFactory $requestPushFactory,
CustomerFactory $customerFactory
) {
parent::__construct($context, $logger, $quote, $accountConfig, $orderRequestService,
$orderStatusFactory, $checkoutSession, $customerSession, $customerRepository,
$orderService, $eventManager, $quoteRecreate, $requestPushFactory);

$this->customerResourceFactory = $customerFactory;
}

/**
* @return ResponseInterface|void
* @throws \Buckaroo\Magento2\Exception
*/
public function execute(): ResponseInterface
{
// Initialize the order, quote, payment
if ($this->redirectRequest->hasPostData('primary_service', 'IDIN')) {

Check failure on line 99 in Controller/Redirect/IdinProcess.php

View workflow job for this annotation

GitHub Actions / M2 PHPStan

Method Buckaroo\Magento2\Controller\Redirect\IdinProcess::execute() should return Magento\Framework\App\ResponseInterface but return statement is missing.
if ($this->setCustomerIDIN()) {
$this->addSuccessMessage(__('Your iDIN verified succesfully!'));
} else {
$this->addErrorMessage(
__(
'Unfortunately iDIN not verified!'
)
);
}

return $this->redirectToCheckout();
}
}

/**
* Set consumer bin IDIN on customer
*
* @return bool
*/
private function setCustomerIDIN(): bool
{
if (!empty($this->redirectRequest->getServiceIdinConsumerbin())
&& !empty($this->redirectRequest->getServiceIdinIseighteenorolder())
&& $this->redirectRequest->getServiceIdinIseighteenorolder() == 'True'
) {
$this->checkoutSession->setCustomerIDIN($this->redirectRequest->getServiceIdinConsumerbin());
$this->checkoutSession->setCustomerIDINIsEighteenOrOlder(true);
$idinCid = $this->redirectRequest->getAdditionalInformation('idin_cid');
if (!empty($idinCid)) {
try {
/** @var Customer $customerNew */
$customerNew = $this->customerRepository->getById((int)$idinCid);
} catch (\Exception $e) {
$this->addErrorMessage(__('Unfortunately customer was not find by IDIN id: "%1"!', $idinCid));
$this->logger->addError(__METHOD__ . ' | ' . $e->getMessage());
return false;
}
$customerData = $customerNew->getDataModel();
$customerData->setCustomAttribute('buckaroo_idin', $this->redirectRequest->getServiceIdinConsumerbin());
$customerData->setCustomAttribute('buckaroo_idin_iseighteenorolder', 1);
$customerNew->updateData($customerData);

$customerResource = $this->customerResourceFactory->create();
$customerResource->saveAttribute($customerNew, 'buckaroo_idin');
$customerResource->saveAttribute($customerNew, 'buckaroo_idin_iseighteenorolder');
}
return true;
}
return false;
}

/**
* Create redirect response
*
* @return ResponseInterface
*/
protected function redirectToCheckout(): ResponseInterface
{
$this->logger->addDebug('start redirectToCheckout');
try {
$this->checkoutSession->restoreQuote();

} catch (\Exception $e) {
$this->logger->addError('Could not restore the quote.');
}

return $this->handleProcessedResponse('checkout', ['_query' => ['bk_e' => 1]]);
}
}
Loading

0 comments on commit b4d9c97

Please sign in to comment.