Skip to content

Commit

Permalink
Delete extension of deprecated class by controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly committed Feb 27, 2024
1 parent 73605cb commit 4891363
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 97 deletions.
49 changes: 21 additions & 28 deletions Controller/tpay/CardPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,38 @@
namespace tpaycom\magento2basic\Controller\tpay;

use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Model\Context as ModelContext;
use Magento\Framework\Registry;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\Controller\Result\RedirectFactory;
use Tpay\OriginApi\Utilities\Util;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\CardTransaction\CardApiFacade;
use tpaycom\magento2basic\Model\Tpay;
use tpaycom\magento2basic\Service\TpayService;
use tpaycom\magento2basic\Service\TpayTokensService;

class CardPayment extends Action
class CardPayment implements ActionInterface
{
/** @var TpayService */
protected $tpayService;

/** @var Session */
protected $checkoutSession;

/** @var TpayInterface */
private $tpay;
/** @var CardApiFacade */
private $cardApiFacade;

/** @var TpayTokensService */
private $tokensService;
/** @var RedirectFactory */
private $redirectFactory;

/** @var StoreManagerInterface */
private $storeManager;

public function __construct(Context $context, TpayInterface $tpayModel, TpayService $tpayService, Session $checkoutSession, ModelContext $modelContext, Registry $registry, ResourceConnection $resourceConnection, StoreManagerInterface $storeManager)
{
$this->tpay = $tpayModel;
public function __construct(
TpayService $tpayService,
Session $checkoutSession,
CardApiFacade $cardApiFacade,
RedirectFactory $redirectFactory
) {
$this->tpayService = $tpayService;
$this->checkoutSession = $checkoutSession;
$this->tokensService = new TpayTokensService($modelContext, $registry, $resourceConnection);
$this->storeManager = $storeManager;
$this->cardApiFacade = $cardApiFacade;
$this->redirectFactory = $redirectFactory;
Util::$loggingEnabled = false;
parent::__construct($context);
}

public function execute()
Expand All @@ -53,17 +46,17 @@ public function execute()
$payment = $this->tpayService->getPayment($orderId);
$additionalPaymentInformation = $payment->getData()['additional_information'];

if (!$additionalPaymentInformation[Tpay::TERMS_ACCEPT]) {
return $this->_redirect('magento2basic/tpay/error');
if (!$additionalPaymentInformation[TpayInterface::TERMS_ACCEPT]) {
return $this->redirectFactory->create()->setPath('magento2basic/tpay/error');
}

$cardTransaction = new CardApiFacade($this->tpay, $this->tokensService, $this->tpayService, $this->storeManager);
$redirectUrl = $cardTransaction->makeCardTransaction($orderId);
$redirectUrl = $this->cardApiFacade->makeCardTransaction($orderId);

return $this->_redirect($redirectUrl);
return $this->redirectFactory->create()->setPath($redirectUrl);
}

$this->checkoutSession->unsQuoteId();

return $this->_redirect('magento2basic/tpay/error');
return $this->redirectFactory->create()->setPath('magento2basic/tpay/error');
}
}
41 changes: 19 additions & 22 deletions Controller/tpay/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

namespace tpaycom\magento2basic\Controller\tpay;

use Magento\Backend\Model\View\Result\RedirectFactory;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\Controller\ResultInterface;
use Tpay\OriginApi\Utilities\Util;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionOriginApi;
use tpaycom\magento2basic\Model\Tpay;
use tpaycom\magento2basic\Service\TpayService;

class Create extends Action
class Create implements ActionInterface
{
/** @var TpayService */
protected $tpayService;
Expand All @@ -28,43 +27,41 @@ class Create extends Action
/** @var TransactionApiFacade */
private $transaction;

/** @var CacheInterface */
private $cache;
/** @var RedirectFactory */
private $redirectFactory;

public function __construct(
Context $context,
TpayInterface $tpayModel,
TpayService $tpayService,
Session $checkoutSession,
CacheInterface $cache
TransactionApiFacade $transactionApiFacade,
RedirectFactory $redirectFactory
) {
$this->tpay = $tpayModel;
$this->tpayService = $tpayService;
$this->checkoutSession = $checkoutSession;
$this->cache = $cache;
$this->transaction = $transactionApiFacade;
$this->redirectFactory = $redirectFactory;
Util::$loggingEnabled = false;

parent::__construct($context);
}

public function execute(): ResponseInterface
public function execute(): ResultInterface
{
$orderId = $this->checkoutSession->getLastRealOrderId();

if ($orderId) {
$payment = $this->tpayService->getPayment($orderId);
$paymentData = $payment->getData();
$this->transaction = new TransactionApiFacade($this->tpay, $this->cache);
$additionalPaymentInformation = $paymentData['additional_information'];

if (!$additionalPaymentInformation[Tpay::TERMS_ACCEPT]) {
return $this->_redirect('magento2basic/tpay/error');
return $this->redirectFactory->create()->setPath('magento2basic/tpay/error');
}

$transaction = $this->prepareTransaction($orderId, $additionalPaymentInformation);

if (!isset($transaction['title'], $transaction['url'])) {
return $this->_redirect('magento2basic/tpay/error');
return $this->redirectFactory->create()->setPath('magento2basic/tpay/error');
}

$this->handleOpenApiTrId($paymentData, $transaction);
Expand All @@ -83,10 +80,10 @@ public function execute(): ResponseInterface
if (6 === strlen($additionalPaymentInformation['blik_code'] ?? '') && $this->tpay->checkBlikLevel0Settings()) {
if (true === $this->transaction->isOpenApiUse()) {
if (isset($transaction['payments']['errors']) && count($transaction['payments']['errors']) > 0) {
return $this->_redirect('magento2basic/tpay/error');
return $this->redirectFactory->create()->setPath('magento2basic/tpay/error');
}

return $this->_redirect('magento2basic/tpay/success');
return $this->redirectFactory->create()->setPath('magento2basic/tpay/success');
}
$result = $this->blikPay($transaction['title'], $additionalPaymentInformation['blik_code']);
$this->checkoutSession->unsQuoteId();
Expand All @@ -97,16 +94,16 @@ public function execute(): ResponseInterface
'User has typed wrong blik code and has been redirected to transaction panel in order to finish payment'
);

return $this->_redirect('magento2basic/tpay/error');
return $this->redirectFactory->create()->setPath('magento2basic/tpay/error');
}

return $this->_redirect('magento2basic/tpay/success');
return $this->redirectFactory->create()->setPath('magento2basic/tpay/success');
}

return $this->_redirect($transactionUrl);
return $this->redirectFactory->create()->setPath($transactionUrl);
}

return $this->_redirect('magento2basic/tpay/error');
return $this->redirectFactory->create()->setPath('magento2basic/tpay/error');
}

/**
Expand Down
23 changes: 19 additions & 4 deletions Controller/tpay/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@

namespace tpaycom\magento2basic\Controller\tpay;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Message\ManagerInterface;

class Error extends Action
class Error implements ActionInterface
{
public function execute()
/** @var ManagerInterface */
private $messageManager;

/** @var RedirectFactory */
private $redirectFactory;

public function __construct(ManagerInterface $messageManager, RedirectFactory $redirectFactory)
{
$this->messageManager = $messageManager;
$this->redirectFactory = $redirectFactory;
}

public function execute(): ResultInterface
{
$this->messageManager->addWarningMessage(__('There was an error during your payment.'));

return $this->_redirect('checkout/onepage/failure');
return $this->redirectFactory->create()->setPath('checkout/onepage/failure');
}
}
60 changes: 35 additions & 25 deletions Controller/tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
namespace tpaycom\magento2basic\Controller\tpay;

use Exception;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Laminas\Http\Response;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\Http;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Framework\Controller\ResultInterface;
use Magento\Sales\Model\Order;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
Expand All @@ -21,38 +19,34 @@
use tpaycom\magento2basic\Service\TpayTokensService;
use tpaySDK\Webhook\JWSVerifiedPaymentNotification;

class Notification extends Action implements CsrfAwareActionInterface
class Notification implements CsrfAwareActionInterface
{
/** @var TpayInterface */
protected $tpay;

/** @var RemoteAddress */
protected $remoteAddress;

/** @var TpayService */
protected $tpayService;

protected $request;

/** @var TpayTokensService */
private $tokensService;

/** @var StoreManagerInterface */
private $storeManager;

public function __construct(Context $context, RemoteAddress $remoteAddress, TpayInterface $tpayModel, TpayService $tpayService, TpayTokensService $tokensService, StoreManagerInterface $storeManager)
{
public function __construct(
TpayInterface $tpayModel,
TpayService $tpayService,
TpayTokensService $tokensService,
StoreManagerInterface $storeManager
) {
$this->tpay = $tpayModel;
$this->remoteAddress = $remoteAddress;
$this->tpayService = $tpayService;
$this->tokensService = $tokensService;
$this->storeManager = $storeManager;
Util::$loggingEnabled = false;

parent::__construct($context);
}

public function execute()
public function execute(): ResultInterface
{
return $this->getNotification();
}
Expand All @@ -78,9 +72,9 @@ public function validateForCsrf(RequestInterface $request): ?bool
/**
* Check if the order has been canceled and get response to Tpay server.
*
* @return string response for Tpay server
* @throws Exception
*
* @return string response for Tpay server
*/
protected function getPaidTransactionResponse(string $orderId): string
{
Expand All @@ -102,7 +96,10 @@ private function saveCard(array $notification, string $orderId)
$order = $this->tpayService->getOrderById($orderId);

if (isset($notification['card_token']) && !$this->tpay->isCustomerGuest($orderId)) {
$token = $this->tokensService->getWithoutAuthCustomerTokens((int) $order->getCustomerId(), $notification['tr_crc']);
$token = $this->tokensService->getWithoutAuthCustomerTokens(
(int) $order->getCustomerId(),
$notification['tr_crc']
);
if (!empty($token)) {
$this->tokensService->updateTokenById((int) $token['tokenId'], $notification['card_token']);
}
Expand All @@ -124,29 +121,42 @@ private function getNotification()

private function extractNotification(StoreInterface $store): array
{
$storeId = (int) $store->getStoreId();
$storeId = $store->getStoreId();

try {
$notification = (new JWSVerifiedPaymentNotification($this->tpay->getSecurityCode($storeId), !$this->tpay->useSandboxMode($storeId)))->getNotification();
$notification = (new JWSVerifiedPaymentNotification(
$this->tpay->getSecurityCode($storeId),
!$this->tpay->useSandboxMode($storeId)
))->getNotification();
$notification = $notification->getNotificationAssociative();
$orderId = base64_decode($notification['tr_crc']);

if ('PAID' === $notification['tr_status']) {
$response = $this->getPaidTransactionResponse($orderId);

$returnData = $this->getResponse()->setStatusCode(Http::STATUS_CODE_200)->setContent($response);
$returnData = (new Response())->setStatusCode(Response::STATUS_CODE_200)->setContent($response);

return [$returnData, true];
}

$this->saveCard($notification, $orderId);
$this->tpayService->SetOrderStatus($orderId, $notification, $this->tpay);

$returnData = $this->getResponse()->setStatusCode(Http::STATUS_CODE_200)->setContent('TRUE');
$returnData = (new Response())->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE');
$isPassed = true;
} catch (Exception $e) {
Util::log('Notification exception', "{$e->getMessage()} in file {$e->getFile()} line: {$e->getLine()} \n\n {$e->getTraceAsString()}");

$returnData = $this->getResponse()->setStatusCode(Http::STATUS_CODE_500)->setContent('FALSE');
Util::log(
'Notification exception',
sprintf(
'%s in file %s line: %d \n\n %s',
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
)
);

$returnData = (new Response())->setStatusCode(Response::STATUS_CODE_200)->setContent('FALSE');
$isPassed = false;
}

Expand Down
Loading

0 comments on commit 4891363

Please sign in to comment.