1313use Magento \Framework \Controller \ResultFactory ;
1414use Magento \Framework \Controller \ResultInterface ;
1515use Magento \Sales \Model \OrderRepository ;
16+ use Magento \Framework \Encryption \EncryptorInterface ;
1617
1718/**
1819 * @SuppressWarnings(PHPMD.TooManyFields)
@@ -34,6 +35,7 @@ class BPRedirect
3435 protected Client $ client ;
3536 protected OrderRepository $ orderRepository ;
3637 protected BitpayInvoiceRepository $ bitpayInvoiceRepository ;
38+ protected EncryptorInterface $ encryptor ;
3739
3840 /**
3941 * @param Session $checkoutSession
@@ -49,6 +51,7 @@ class BPRedirect
4951 * @param Client $client
5052 * @param OrderRepository $orderRepository
5153 * @param BitpayInvoiceRepository $bitpayInvoiceRepository
54+ * @param EncryptorInterface $encryptor
5255 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
5356 */
5457 public function __construct (
@@ -64,7 +67,8 @@ public function __construct(
6467 ResultFactory $ resultFactory ,
6568 Client $ client ,
6669 OrderRepository $ orderRepository ,
67- BitpayInvoiceRepository $ bitpayInvoiceRepository
70+ BitpayInvoiceRepository $ bitpayInvoiceRepository ,
71+ EncryptorInterface $ encryptor ,
6872 ) {
6973 $ this ->checkoutSession = $ checkoutSession ;
7074 $ this ->orderInterface = $ orderInterface ;
@@ -79,16 +83,19 @@ public function __construct(
7983 $ this ->client = $ client ;
8084 $ this ->orderRepository = $ orderRepository ;
8185 $ this ->bitpayInvoiceRepository = $ bitpayInvoiceRepository ;
86+ $ this ->encryptor = $ encryptor ;
8287 }
8388
8489 /**
8590 * Create bitpay invoice after order creation during redirect to success page
8691 *
92+ * @param ResultInterface $defaultResult
93+ * @param string|null $returnId
8794 * @return ResultInterface
8895 * @throws LocalizedException
8996 * @throws NoSuchEntityException|\Exception
9097 */
91- public function execute (): ResultInterface
98+ public function execute (ResultInterface $ defaultResult , string $ returnId = null ): ResultInterface
9299 {
93100 $ orderId = $ this ->checkoutSession ->getData ('last_order_id ' );
94101 if (!$ orderId ) {
@@ -102,17 +109,36 @@ public function execute(): ResultInterface
102109 return $ this ->resultFactory ->create (\Magento \Framework \Controller \ResultFactory::TYPE_REDIRECT )
103110 ->setUrl ($ this ->url ->getUrl ('checkout/cart ' ));
104111 }
105- $ baseUrl = $ this ->config ->getBaseUrl ();
106112 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 ;
110127 }
111128
112129 try {
130+ $ baseUrl = $ this ->config ->getBaseUrl ();
113131 $ order = $ this ->setToPendingAndOverrideMagentoStatus ($ order );
114132 $ 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+ }
116142 $ params = $ this ->getParams ($ order , $ incrementId , $ modal , $ redirectUrl , $ baseUrl );
117143 $ billingAddressData = $ order ->getBillingAddress ()->getData ();
118144 $ this ->setSessionCustomerData ($ billingAddressData , $ order ->getCustomerEmail (), $ incrementId );
@@ -134,12 +160,20 @@ public function execute(): ResultInterface
134160 #set some info for guest checkout
135161 $ this ->setSessionCustomerData ($ billingAddressData , $ order ->getCustomerEmail (), $ incrementId );
136162
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 ]);
138172
139173 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 );
143177 case false :
144178 default :
145179 return $ this ->resultFactory ->create (
@@ -234,16 +268,16 @@ private function getParams(
234268 */
235269 private function deleteOrderAndRedirectToCart ($ exception , OrderInterface $ order ): ResultInterface
236270 {
271+ $ this ->checkoutSession ->clearHelperData ();
237272 $ this ->logger ->error ($ exception ->getMessage ());
238273 $ this ->registry ->register ('isSecureArea ' , 'true ' );
239274 $ order ->delete ();
240275 $ this ->registry ->unregister ('isSecureArea ' );
241276 $ this ->messageManager ->addErrorMessage ('We are unable to place your Order at this time ' );
242277
243278 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 ' ));
248282 }
249283}
0 commit comments