3
3
4
4
use Bitpay \BPCheckout \Logger \Logger ;
5
5
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 ;
10
6
use Magento \Framework \DataObject ;
11
- use Magento \Framework \Event \Observer ;
12
- use Magento \Framework \Event \ObserverInterface ;
13
7
use Magento \Framework \Exception \LocalizedException ;
14
8
use Magento \Framework \Exception \NoSuchEntityException ;
15
9
use Magento \Framework \Message \Manager ;
16
10
use Magento \Framework \Registry ;
17
- use Magento \Framework \Serialize \Serializer \Json ;
18
11
use Magento \Framework \UrlInterface ;
19
- use Magento \Framework \View \Result \Page ;
20
12
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 ;
24
15
use Magento \Sales \Model \OrderRepository ;
25
16
26
17
/**
31
22
class BPRedirect
32
23
{
33
24
protected Session $ checkoutSession ;
34
- protected RedirectInterface $ redirect ;
35
- protected ResponseInterface $ response ;
36
25
protected OrderInterface $ orderInterface ;
37
26
protected TransactionRepository $ transactionRepository ;
38
27
protected Config $ config ;
39
- protected ResponseFactory $ responseFactory ;
40
28
protected Invoice $ invoice ;
41
29
protected Manager $ messageManager ;
42
30
protected Registry $ registry ;
43
31
protected UrlInterface $ url ;
44
32
protected Logger $ logger ;
45
- protected PageFactory $ resultPageFactory ;
33
+ protected ResultFactory $ resultFactory ;
46
34
protected Client $ client ;
47
35
protected OrderRepository $ orderRepository ;
48
36
protected BitpayInvoiceRepository $ bitpayInvoiceRepository ;
49
37
50
38
/**
51
39
* @param Session $checkoutSession
52
- * @param RedirectInterface $redirect
53
- * @param ResponseInterface $response
54
40
* @param OrderInterface $orderInterface
55
41
* @param \Bitpay\BPCheckout\Model\Config $config
56
42
* @param \Bitpay\BPCheckout\Model\TransactionRepository $transactionRepository
57
- * @param ResponseFactory $responseFactory
58
43
* @param \Bitpay\BPCheckout\Model\Invoice $invoice
59
44
* @param Manager $messageManager
60
45
* @param Registry $registry
61
46
* @param UrlInterface $url
62
47
* @param Logger $logger
63
- * @param PageFactory $resultPageFactory
48
+ * @param ResultFactory $resultFactory
64
49
* @param Client $client
65
50
* @param OrderRepository $orderRepository
66
51
* @param BitpayInvoiceRepository $bitpayInvoiceRepository
67
52
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
68
53
*/
69
54
public function __construct (
70
55
Session $ checkoutSession ,
71
- RedirectInterface $ redirect ,
72
- ResponseInterface $ response ,
73
56
OrderInterface $ orderInterface ,
74
57
Config $ config ,
75
58
TransactionRepository $ transactionRepository ,
76
- ResponseFactory $ responseFactory ,
77
59
Invoice $ invoice ,
78
60
Manager $ messageManager ,
79
61
Registry $ registry ,
80
62
UrlInterface $ url ,
81
63
Logger $ logger ,
82
- PageFactory $ resultPageFactory ,
64
+ ResultFactory $ resultFactory ,
83
65
Client $ client ,
84
66
OrderRepository $ orderRepository ,
85
67
BitpayInvoiceRepository $ bitpayInvoiceRepository
86
68
) {
87
69
$ this ->checkoutSession = $ checkoutSession ;
88
- $ this ->redirect = $ redirect ;
89
- $ this ->response = $ response ;
90
70
$ this ->orderInterface = $ orderInterface ;
91
71
$ this ->config = $ config ;
92
72
$ this ->transactionRepository = $ transactionRepository ;
93
- $ this ->responseFactory = $ responseFactory ;
94
73
$ this ->invoice = $ invoice ;
95
74
$ this ->messageManager = $ messageManager ;
96
75
$ this ->registry = $ registry ;
97
76
$ this ->url = $ url ;
98
77
$ this ->logger = $ logger ;
99
- $ this ->resultPageFactory = $ resultPageFactory ;
78
+ $ this ->resultFactory = $ resultFactory ;
100
79
$ this ->client = $ client ;
101
80
$ this ->orderRepository = $ orderRepository ;
102
81
$ this ->bitpayInvoiceRepository = $ bitpayInvoiceRepository ;
@@ -105,23 +84,29 @@ public function __construct(
105
84
/**
106
85
* Create bitpay invoice after order creation during redirect to success page
107
86
*
108
- * @return Page|void
87
+ * @return ResultInterface
109
88
* @throws LocalizedException
110
89
* @throws NoSuchEntityException|\Exception
111
90
*/
112
- public function execute ()
91
+ public function execute (): ResultInterface
113
92
{
114
93
$ orderId = $ this ->checkoutSession ->getData ('last_order_id ' );
115
94
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 ' )) ;
118
97
}
119
98
120
99
$ order = $ this ->orderInterface ->load ($ orderId );
121
100
$ 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
+ }
122
105
$ baseUrl = $ this ->config ->getBaseUrl ();
123
106
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
+ );
125
110
}
126
111
127
112
try {
@@ -142,29 +127,30 @@ public function execute()
142
127
$ invoice ->getAcceptanceWindow ()
143
128
);
144
129
$ this ->transactionRepository ->add ($ incrementId , $ invoiceID , 'new ' );
145
- } catch (\Exception $ exception ) {
146
- $ this ->deleteOrderAndRedirectToCart ($ exception , $ order );
147
130
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 );
151
136
152
- return ;
153
- }
137
+ $ redirectUrl = $ this ->url ->getUrl ('bitpay-invoice ' , ['_query ' => ['invoiceID ' => $ invoiceID , 'order_id ' => $ incrementId , 'm ' => 1 ]]);
154
138
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 );
168
154
}
169
155
}
170
156
@@ -194,7 +180,7 @@ private function setSessionCustomerData(array $billingAddressData, string $email
194
180
* @return void
195
181
* @throws \Exception
196
182
*/
197
- private function setToPendingAndOverrideMagentoStatus (OrderInterface $ order ): Order
183
+ private function setToPendingAndOverrideMagentoStatus (OrderInterface $ order ): OrderInterface
198
184
{
199
185
$ order ->setState ('new ' , true );
200
186
$ order_status = $ this ->config ->getBPCheckoutOrderStatus ();
@@ -246,13 +232,18 @@ private function getParams(
246
232
* @return void
247
233
* @throws \Exception
248
234
*/
249
- private function deleteOrderAndRedirectToCart ($ exception , OrderInterface $ order ): void
235
+ private function deleteOrderAndRedirectToCart ($ exception , OrderInterface $ order ): ResultInterface
250
236
{
251
237
$ this ->logger ->error ($ exception ->getMessage ());
252
238
$ this ->registry ->register ('isSecureArea ' , 'true ' );
253
239
$ order ->delete ();
254
240
$ this ->registry ->unregister ('isSecureArea ' );
255
241
$ 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
+
257
248
}
258
249
}
0 commit comments