Skip to content

Commit b667b69

Browse files
authored
Merge pull request #100 from swlodarski-sumoheavy/9.2.x
Update BPRedirect to always return ResultInterface
2 parents 31f348d + 456c180 commit b667b69

File tree

4 files changed

+72
-129
lines changed

4 files changed

+72
-129
lines changed

Diff for: Model/BPRedirect.php

+46-55
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,15 @@
33

44
use Bitpay\BPCheckout\Logger\Logger;
55
use Magento\Checkout\Model\Session;
6-
use Magento\Framework\App\Action\Action;
7-
use Magento\Framework\App\ActionFlag;
8-
use Magento\Framework\App\Response\RedirectInterface;
9-
use Magento\Framework\App\ResponseInterface;
106
use Magento\Framework\DataObject;
11-
use Magento\Framework\Event\Observer;
12-
use Magento\Framework\Event\ObserverInterface;
137
use Magento\Framework\Exception\LocalizedException;
148
use Magento\Framework\Exception\NoSuchEntityException;
159
use Magento\Framework\Message\Manager;
1610
use Magento\Framework\Registry;
17-
use Magento\Framework\Serialize\Serializer\Json;
1811
use Magento\Framework\UrlInterface;
19-
use Magento\Framework\View\Result\Page;
2012
use Magento\Sales\Api\Data\OrderInterface;
21-
use Magento\Framework\App\ResponseFactory;
22-
use Magento\Framework\View\Result\PageFactory;
23-
use Magento\Sales\Model\Order;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\Framework\Controller\ResultInterface;
2415
use Magento\Sales\Model\OrderRepository;
2516

2617
/**
@@ -31,72 +22,60 @@
3122
class BPRedirect
3223
{
3324
protected Session $checkoutSession;
34-
protected RedirectInterface $redirect;
35-
protected ResponseInterface $response;
3625
protected OrderInterface $orderInterface;
3726
protected TransactionRepository $transactionRepository;
3827
protected Config $config;
39-
protected ResponseFactory $responseFactory;
4028
protected Invoice $invoice;
4129
protected Manager $messageManager;
4230
protected Registry $registry;
4331
protected UrlInterface $url;
4432
protected Logger $logger;
45-
protected PageFactory $resultPageFactory;
33+
protected ResultFactory $resultFactory;
4634
protected Client $client;
4735
protected OrderRepository $orderRepository;
4836
protected BitpayInvoiceRepository $bitpayInvoiceRepository;
4937

5038
/**
5139
* @param Session $checkoutSession
52-
* @param RedirectInterface $redirect
53-
* @param ResponseInterface $response
5440
* @param OrderInterface $orderInterface
5541
* @param \Bitpay\BPCheckout\Model\Config $config
5642
* @param \Bitpay\BPCheckout\Model\TransactionRepository $transactionRepository
57-
* @param ResponseFactory $responseFactory
5843
* @param \Bitpay\BPCheckout\Model\Invoice $invoice
5944
* @param Manager $messageManager
6045
* @param Registry $registry
6146
* @param UrlInterface $url
6247
* @param Logger $logger
63-
* @param PageFactory $resultPageFactory
48+
* @param ResultFactory $resultFactory
6449
* @param Client $client
6550
* @param OrderRepository $orderRepository
6651
* @param BitpayInvoiceRepository $bitpayInvoiceRepository
6752
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6853
*/
6954
public function __construct(
7055
Session $checkoutSession,
71-
RedirectInterface $redirect,
72-
ResponseInterface $response,
7356
OrderInterface $orderInterface,
7457
Config $config,
7558
TransactionRepository $transactionRepository,
76-
ResponseFactory $responseFactory,
7759
Invoice $invoice,
7860
Manager $messageManager,
7961
Registry $registry,
8062
UrlInterface $url,
8163
Logger $logger,
82-
PageFactory $resultPageFactory,
64+
ResultFactory $resultFactory,
8365
Client $client,
8466
OrderRepository $orderRepository,
8567
BitpayInvoiceRepository $bitpayInvoiceRepository
8668
) {
8769
$this->checkoutSession = $checkoutSession;
88-
$this->redirect = $redirect;
89-
$this->response = $response;
9070
$this->orderInterface = $orderInterface;
9171
$this->config = $config;
9272
$this->transactionRepository = $transactionRepository;
93-
$this->responseFactory = $responseFactory;
9473
$this->invoice = $invoice;
9574
$this->messageManager = $messageManager;
9675
$this->registry = $registry;
9776
$this->url = $url;
9877
$this->logger = $logger;
99-
$this->resultPageFactory = $resultPageFactory;
78+
$this->resultFactory = $resultFactory;
10079
$this->client = $client;
10180
$this->orderRepository = $orderRepository;
10281
$this->bitpayInvoiceRepository = $bitpayInvoiceRepository;
@@ -105,23 +84,29 @@ public function __construct(
10584
/**
10685
* Create bitpay invoice after order creation during redirect to success page
10786
*
108-
* @return Page|void
87+
* @return ResultInterface
10988
* @throws LocalizedException
11089
* @throws NoSuchEntityException|\Exception
11190
*/
112-
public function execute()
91+
public function execute(): ResultInterface
11392
{
11493
$orderId = $this->checkoutSession->getData('last_order_id');
11594
if (!$orderId) {
116-
$this->response->setRedirect($this->url->getUrl('checkout/cart'))->sendResponse();
117-
return;
95+
return $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
96+
->setUrl($this->url->getUrl('checkout/cart'));
11897
}
11998

12099
$order = $this->orderInterface->load($orderId);
121100
$incrementId = $order->getIncrementId();
101+
if (!$incrementId) {
102+
return $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
103+
->setUrl($this->url->getUrl('checkout/cart'));
104+
}
122105
$baseUrl = $this->config->getBaseUrl();
123106
if ($order->getPayment()->getMethodInstance()->getCode() !== Config::BITPAY_PAYMENT_METHOD_NAME) {
124-
return $this->resultPageFactory->create();
107+
return $this->resultFactory->create(
108+
\Magento\Framework\Controller\ResultFactory::TYPE_PAGE
109+
);
125110
}
126111

127112
try {
@@ -142,29 +127,30 @@ public function execute()
142127
$invoice->getAcceptanceWindow()
143128
);
144129
$this->transactionRepository->add($incrementId, $invoiceID, 'new');
145-
} catch (\Exception $exception) {
146-
$this->deleteOrderAndRedirectToCart($exception, $order);
147130

148-
return;
149-
} catch (\Error $error) {
150-
$this->deleteOrderAndRedirectToCart($error, $order);
131+
switch ($modal) {
132+
case true:
133+
case 1:
134+
#set some info for guest checkout
135+
$this->setSessionCustomerData($billingAddressData, $order->getCustomerEmail(), $incrementId);
151136

152-
return;
153-
}
137+
$redirectUrl = $this->url->getUrl('bitpay-invoice', ['_query' => ['invoiceID' => $invoiceID, 'order_id' => $incrementId, 'm' => 1]]);
154138

155-
switch ($modal) {
156-
case true:
157-
case 1:
158-
#set some info for guest checkout
159-
$this->setSessionCustomerData($billingAddressData, $order->getCustomerEmail(), $incrementId);
160-
$RedirectUrl = $baseUrl . 'bitpay-invoice/?invoiceID=' . $invoiceID . '&order_id='
161-
. $incrementId . '&m=1';
162-
$this->responseFactory->create()->setRedirect($RedirectUrl)->sendResponse();
163-
break;
164-
case false:
165-
default:
166-
$this->redirect->redirect($this->response, $invoice->getUrl());
167-
break;
139+
return $this->resultFactory->create(
140+
\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT
141+
)
142+
->setUrl($redirectUrl);
143+
case false:
144+
default:
145+
return $this->resultFactory->create(
146+
\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT
147+
)
148+
->setUrl($invoice->getUrl());
149+
}
150+
} catch (\Exception $exception) {
151+
return $this->deleteOrderAndRedirectToCart($exception, $order);
152+
} catch (\Error $error) {
153+
return $this->deleteOrderAndRedirectToCart($error, $order);
168154
}
169155
}
170156

@@ -194,7 +180,7 @@ private function setSessionCustomerData(array $billingAddressData, string $email
194180
* @return void
195181
* @throws \Exception
196182
*/
197-
private function setToPendingAndOverrideMagentoStatus(OrderInterface $order): Order
183+
private function setToPendingAndOverrideMagentoStatus(OrderInterface $order): OrderInterface
198184
{
199185
$order->setState('new', true);
200186
$order_status = $this->config->getBPCheckoutOrderStatus();
@@ -246,13 +232,18 @@ private function getParams(
246232
* @return void
247233
* @throws \Exception
248234
*/
249-
private function deleteOrderAndRedirectToCart($exception, OrderInterface $order): void
235+
private function deleteOrderAndRedirectToCart($exception, OrderInterface $order): ResultInterface
250236
{
251237
$this->logger->error($exception->getMessage());
252238
$this->registry->register('isSecureArea', 'true');
253239
$order->delete();
254240
$this->registry->unregister('isSecureArea');
255241
$this->messageManager->addErrorMessage('We are unable to place your Order at this time');
256-
$this->responseFactory->create()->setRedirect($this->url->getUrl('checkout/cart'))->sendResponse();
242+
243+
return $this->resultFactory->create(
244+
\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT
245+
)
246+
->setUrl($this->url->getUrl('checkout/cart'));
247+
257248
}
258249
}

Diff for: Test/Integration/Model/BPRedirectTest.php

+5-29
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
use Bitpay\BPCheckout\Model\TransactionRepository;
1919
use Magento\Framework\App\Action\Action;
2020
use Magento\Framework\App\ActionFlag;
21-
use Magento\Framework\App\Response\RedirectInterface;
22-
use Magento\Framework\App\ResponseInterface;
2321
use Magento\Framework\DataObject;
2422
use Magento\Framework\Event\Observer;
2523
use Magento\Framework\Event\ObserverInterface;
@@ -29,8 +27,7 @@
2927
use Magento\Framework\Serialize\Serializer\Json;
3028
use Magento\Framework\UrlInterface;
3129
use Magento\Sales\Api\Data\OrderInterface;
32-
use Magento\Framework\App\ResponseFactory;
33-
use Magento\Framework\View\Result\PageFactory;
30+
use Magento\Framework\Controller\ResultFactory;
3431
use Magento\Sales\Model\OrderRepository;
3532
use Magento\TestFramework\Helper\Bootstrap;
3633
use PHPUnit\Framework\TestCase;
@@ -56,16 +53,6 @@ class BPRedirectTest extends TestCase
5653
*/
5754
private $checkoutSession;
5855

59-
/**
60-
* @var RedirectInterface $redirect
61-
*/
62-
private $redirect;
63-
64-
/**
65-
* @var ResponseInterface $response
66-
*/
67-
private $response;
68-
6956
/**
7057
* @var OrderInterface $orderInterface
7158
*/
@@ -81,11 +68,6 @@ class BPRedirectTest extends TestCase
8168
*/
8269
private $transactionRepository;
8370

84-
/**
85-
* @var ResponseFactory $responseFactory
86-
*/
87-
private $responseFactory;
88-
8971
/**
9072
* @var Invoice|MockObject $invoice
9173
*/
@@ -112,9 +94,9 @@ class BPRedirectTest extends TestCase
11294
private $logger;
11395

11496
/**
115-
* @var PageFactory $resultPageFactory
97+
* @var ResultFactory $resultFactory
11698
*/
117-
private $resultPageFactory;
99+
private $resultFactory;
118100
/**
119101
* @var Client $client
120102
*/
@@ -134,36 +116,30 @@ public function setUp(): void
134116
{
135117
$this->objectManager = Bootstrap::getObjectManager();
136118
$this->checkoutSession = $this->objectManager->get(Session::class);
137-
$this->redirect = $this->objectManager->get(RedirectInterface::class);
138-
$this->response = $this->objectManager->get(ResponseInterface::class);
139119
$this->orderInterface = $this->objectManager->get(OrderInterface::class);
140120
$this->config = $this->objectManager->get(Config::class);
141121
$this->transactionRepository = $this->objectManager->get(TransactionRepository::class);
142-
$this->responseFactory = $this->objectManager->get(ResponseFactory::class);
143122
$this->invoice = $this->getMockBuilder(Invoice::class)->disableOriginalConstructor()->getMock();
144123
$this->messageManager = $this->objectManager->get(Manager::class);
145124
$this->registry = $this->objectManager->get(Registry::class);
146125
$this->url = $this->objectManager->get(UrlInterface::class);
147126
$this->logger = $this->objectManager->get(Logger::class);
148-
$this->resultPageFactory = $this->objectManager->get(PageFactory::class);
127+
$this->resultFactory = $this->objectManager->get(ResultFactory::class);
149128
$this->client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock();
150129
$this->orderRepository = $this->objectManager->get(OrderRepository::class);
151130
$this->bitpayInvoiceRepository = $this->objectManager->get(BitpayInvoiceRepository::class);
152131

153132
$this->bpRedirect = new BPRedirect(
154133
$this->checkoutSession,
155-
$this->redirect,
156-
$this->response,
157134
$this->orderInterface,
158135
$this->config,
159136
$this->transactionRepository,
160-
$this->responseFactory,
161137
$this->invoice,
162138
$this->messageManager,
163139
$this->registry,
164140
$this->url,
165141
$this->logger,
166-
$this->resultPageFactory,
142+
$this->resultFactory,
167143
$this->client,
168144
$this->orderRepository,
169145
$this->bitpayInvoiceRepository

0 commit comments

Comments
 (0)