13
13
use Magento \Framework \Controller \ResultFactory ;
14
14
use Magento \Framework \Controller \ResultInterface ;
15
15
use Magento \Sales \Model \OrderRepository ;
16
+ use Magento \Framework \Encryption \EncryptorInterface ;
16
17
17
18
/**
18
19
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -34,6 +35,7 @@ class BPRedirect
34
35
protected Client $ client ;
35
36
protected OrderRepository $ orderRepository ;
36
37
protected BitpayInvoiceRepository $ bitpayInvoiceRepository ;
38
+ protected EncryptorInterface $ encryptor ;
37
39
38
40
/**
39
41
* @param Session $checkoutSession
@@ -49,6 +51,7 @@ class BPRedirect
49
51
* @param Client $client
50
52
* @param OrderRepository $orderRepository
51
53
* @param BitpayInvoiceRepository $bitpayInvoiceRepository
54
+ * @param EncryptorInterface $encryptor
52
55
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
53
56
*/
54
57
public function __construct (
@@ -64,7 +67,8 @@ public function __construct(
64
67
ResultFactory $ resultFactory ,
65
68
Client $ client ,
66
69
OrderRepository $ orderRepository ,
67
- BitpayInvoiceRepository $ bitpayInvoiceRepository
70
+ BitpayInvoiceRepository $ bitpayInvoiceRepository ,
71
+ EncryptorInterface $ encryptor ,
68
72
) {
69
73
$ this ->checkoutSession = $ checkoutSession ;
70
74
$ this ->orderInterface = $ orderInterface ;
@@ -79,16 +83,19 @@ public function __construct(
79
83
$ this ->client = $ client ;
80
84
$ this ->orderRepository = $ orderRepository ;
81
85
$ this ->bitpayInvoiceRepository = $ bitpayInvoiceRepository ;
86
+ $ this ->encryptor = $ encryptor ;
82
87
}
83
88
84
89
/**
85
90
* Create bitpay invoice after order creation during redirect to success page
86
91
*
92
+ * @param ResultInterface $defaultResult
93
+ * @param string|null $returnId
87
94
* @return ResultInterface
88
95
* @throws LocalizedException
89
96
* @throws NoSuchEntityException|\Exception
90
97
*/
91
- public function execute (): ResultInterface
98
+ public function execute (ResultInterface $ defaultResult , string $ returnId = null ): ResultInterface
92
99
{
93
100
$ orderId = $ this ->checkoutSession ->getData ('last_order_id ' );
94
101
if (!$ orderId ) {
@@ -102,17 +109,36 @@ public function execute(): ResultInterface
102
109
return $ this ->resultFactory ->create (\Magento \Framework \Controller \ResultFactory::TYPE_REDIRECT )
103
110
->setUrl ($ this ->url ->getUrl ('checkout/cart ' ));
104
111
}
105
- $ baseUrl = $ this ->config ->getBaseUrl ();
106
112
if ($ order ->getPayment ()->getMethodInstance ()->getCode () !== Config::BITPAY_PAYMENT_METHOD_NAME ) {
107
- return $ this ->resultFactory ->create (
108
- \Magento \Framework \Controller \ResultFactory::TYPE_PAGE
109
- );
113
+ return $ defaultResult ;
114
+ }
115
+
116
+ $ isStandardCheckoutSuccess = $ this ->config ->getBitpayCheckoutSuccess () === 'standard ' ;
117
+ $ returnHash = $ this ->encryptor ->hash ("$ incrementId: {$ order ->getCustomerEmail ()}: {$ order ->getProtectCode ()}" );
118
+ if ($ isStandardCheckoutSuccess && $ returnId ) {
119
+ if ($ returnId !== $ returnHash ) {
120
+ $ this ->checkoutSession ->clearHelperData ();
121
+
122
+ return $ this ->resultFactory ->create (\Magento \Framework \Controller \ResultFactory::TYPE_REDIRECT )
123
+ ->setUrl ($ this ->url ->getUrl ('checkout/cart ' ));
124
+ }
125
+
126
+ return $ defaultResult ;
110
127
}
111
128
112
129
try {
130
+ $ baseUrl = $ this ->config ->getBaseUrl ();
113
131
$ order = $ this ->setToPendingAndOverrideMagentoStatus ($ order );
114
132
$ modal = $ this ->config ->getBitpayUx () === 'modal ' ;
115
- $ redirectUrl = $ baseUrl .'bitpay-invoice/?order_id= ' . $ incrementId ;
133
+ $ redirectUrl = $ this ->url ->getUrl ('bitpay-invoice ' , ['_query ' => ['order_id ' => $ incrementId ]]);
134
+ if ($ isStandardCheckoutSuccess ) {
135
+ $ this ->checkoutSession ->setLastSuccessQuoteId ($ order ->getQuoteId ());
136
+ if (!$ modal ) {
137
+ $ redirectUrl = $ this ->url ->getUrl ('checkout/onepage/success ' , [
138
+ '_query ' => ['return_id ' => $ returnHash ]
139
+ ]);
140
+ }
141
+ }
116
142
$ params = $ this ->getParams ($ order , $ incrementId , $ modal , $ redirectUrl , $ baseUrl );
117
143
$ billingAddressData = $ order ->getBillingAddress ()->getData ();
118
144
$ this ->setSessionCustomerData ($ billingAddressData , $ order ->getCustomerEmail (), $ incrementId );
@@ -134,12 +160,20 @@ public function execute(): ResultInterface
134
160
#set some info for guest checkout
135
161
$ this ->setSessionCustomerData ($ billingAddressData , $ order ->getCustomerEmail (), $ incrementId );
136
162
137
- $ redirectUrl = $ this ->url ->getUrl ('bitpay-invoice ' , ['_query ' => ['invoiceID ' => $ invoiceID , 'order_id ' => $ incrementId , 'm ' => 1 ]]);
163
+ $ redirectParams = [
164
+ 'invoiceID ' => $ invoiceID ,
165
+ 'order_id ' => $ incrementId ,
166
+ 'm ' => 1 ,
167
+ ];
168
+ if ($ isStandardCheckoutSuccess ) {
169
+ $ redirectParams ['return_id ' ] = $ returnHash ;
170
+ }
171
+ $ redirectUrl = $ this ->url ->getUrl ('bitpay-invoice ' , ['_query ' => $ redirectParams ]);
138
172
139
173
return $ this ->resultFactory ->create (
140
- \Magento \Framework \Controller \ResultFactory::TYPE_REDIRECT
141
- )
142
- ->setUrl ($ redirectUrl );
174
+ \Magento \Framework \Controller \ResultFactory::TYPE_REDIRECT
175
+ )
176
+ ->setUrl ($ redirectUrl );
143
177
case false :
144
178
default :
145
179
return $ this ->resultFactory ->create (
@@ -234,16 +268,16 @@ private function getParams(
234
268
*/
235
269
private function deleteOrderAndRedirectToCart ($ exception , OrderInterface $ order ): ResultInterface
236
270
{
271
+ $ this ->checkoutSession ->clearHelperData ();
237
272
$ this ->logger ->error ($ exception ->getMessage ());
238
273
$ this ->registry ->register ('isSecureArea ' , 'true ' );
239
274
$ order ->delete ();
240
275
$ this ->registry ->unregister ('isSecureArea ' );
241
276
$ this ->messageManager ->addErrorMessage ('We are unable to place your Order at this time ' );
242
277
243
278
return $ this ->resultFactory ->create (
244
- \Magento \Framework \Controller \ResultFactory::TYPE_REDIRECT
245
- )
246
- ->setUrl ($ this ->url ->getUrl ('checkout/cart ' ));
247
-
279
+ \Magento \Framework \Controller \ResultFactory::TYPE_REDIRECT
280
+ )
281
+ ->setUrl ($ this ->url ->getUrl ('checkout/cart ' ));
248
282
}
249
283
}
0 commit comments