|
9 | 9 |
|
10 | 10 | namespace WooCommerce\PayPalCommerce\Button;
|
11 | 11 |
|
| 12 | +use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; |
| 13 | +use WooCommerce\PayPalCommerce\ApiClient\Factory\ReturnUrlFactory; |
12 | 14 | use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveSubscriptionEndpoint;
|
13 | 15 | use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint;
|
14 | 16 | use WooCommerce\PayPalCommerce\Button\Endpoint\SaveCheckoutFormEndpoint;
|
|
22 | 24 | use WooCommerce\PayPalCommerce\Button\Endpoint\GetOrderEndpoint;
|
23 | 25 | use WooCommerce\PayPalCommerce\Button\Endpoint\StartPayPalVaultingEndpoint;
|
24 | 26 | use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
|
| 27 | +use WooCommerce\PayPalCommerce\Button\Helper\WooCommerceOrderCreator; |
| 28 | +use WooCommerce\PayPalCommerce\Button\Session\CartDataTransientStorage; |
25 | 29 | use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule;
|
26 | 30 | use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule;
|
27 | 31 | use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
|
@@ -98,6 +102,8 @@ static function ( $value ) use ( $c ) {
|
98 | 102 |
|
99 | 103 | $this->register_ajax_endpoints( $c );
|
100 | 104 |
|
| 105 | + $this->register_appswitch_crossbrowser_handler( $c ); |
| 106 | + |
101 | 107 | return true;
|
102 | 108 | }
|
103 | 109 |
|
@@ -228,4 +234,75 @@ static function () use ( $container ) {
|
228 | 234 | }
|
229 | 235 | );
|
230 | 236 | }
|
| 237 | + |
| 238 | + private function register_appswitch_crossbrowser_handler( ContainerInterface $container ): void { |
| 239 | + if ( ! $container->get( 'wcgateway.appswitch-enabled' ) ) { |
| 240 | + return; |
| 241 | + } |
| 242 | + |
| 243 | + // After returning from cross-browser AppSwitch (started in non-default browser, then redirected to the default one) |
| 244 | + // we need to retrieve the saved cart and PayPal order, create a WC order and redirect to Pay for order. |
| 245 | + add_action( |
| 246 | + 'wp', |
| 247 | + static function () use ( $container ) { |
| 248 | + // phpcs:ignore WordPress.Security.NonceVerification |
| 249 | + if ( ! isset( $_GET[ ReturnUrlFactory::PCP_QUERY_ARG ] ) ) { |
| 250 | + return; |
| 251 | + } |
| 252 | + |
| 253 | + if ( is_checkout_pay_page() ) { |
| 254 | + return; |
| 255 | + } |
| 256 | + |
| 257 | + // phpcs:ignore WordPress.Security.NonceVerification |
| 258 | + if ( ! isset( $_GET[ CreateOrderEndpoint::RETURN_URL_CART_QUERY_ARG ] ) ) { |
| 259 | + return; |
| 260 | + } |
| 261 | + |
| 262 | + // phpcs:ignore WordPress.Security.NonceVerification |
| 263 | + $cart_key = wc_clean( wp_unslash( $_GET[ CreateOrderEndpoint::RETURN_URL_CART_QUERY_ARG ] ) ); |
| 264 | + if ( ! is_string( $cart_key ) ) { |
| 265 | + return; |
| 266 | + } |
| 267 | + |
| 268 | + $card_data_storage = $container->get( 'button.session.storage.card-data.transient' ); |
| 269 | + assert( $card_data_storage instanceof CartDataTransientStorage ); |
| 270 | + |
| 271 | + $cart_data = $card_data_storage->get( $cart_key ); |
| 272 | + if ( ! $cart_data ) { |
| 273 | + return; |
| 274 | + } |
| 275 | + |
| 276 | + // Delete the data to avoid accidentally triggering it again, duplicating orders etc. |
| 277 | + $card_data_storage->remove( $cart_data ); |
| 278 | + |
| 279 | + if ( ! WC()->cart ) { |
| 280 | + return; |
| 281 | + } |
| 282 | + // The current cart is the same, so we don't need to do anything (probably not cross-browser). |
| 283 | + if ( WC()->cart->get_cart_hash() === $cart_data->cart_hash() ) { |
| 284 | + return; |
| 285 | + } |
| 286 | + |
| 287 | + $paypal_order_id = $cart_data->paypal_order_id(); |
| 288 | + if ( empty( $paypal_order_id ) ) { |
| 289 | + return; |
| 290 | + } |
| 291 | + |
| 292 | + $order_endpoint = $container->get( 'api.endpoint.order' ); |
| 293 | + assert( $order_endpoint instanceof OrderEndpoint ); |
| 294 | + |
| 295 | + $paypal_order = $order_endpoint->order( $paypal_order_id ); |
| 296 | + |
| 297 | + $wc_order_creator = $container->get( 'button.helper.wc-order-creator' ); |
| 298 | + assert( $wc_order_creator instanceof WooCommerceOrderCreator ); |
| 299 | + |
| 300 | + $wc_order = $wc_order_creator->create_from_paypal_order( $paypal_order, $cart_data ); |
| 301 | + |
| 302 | + // Redirect via JS because we need to keep the # parameters which are not accessible on the server side. |
| 303 | + // phpcs:ignore WordPress.Security.EscapeOutput |
| 304 | + echo "<script>location.href = '" . $wc_order->get_checkout_payment_url() . "' + location.hash;</script>"; |
| 305 | + } |
| 306 | + ); |
| 307 | + } |
231 | 308 | }
|
0 commit comments