From 370db2de55c6cdff2800f930eecb2c3e5638e086 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Mon, 30 Nov 2026 15:37:38 +0100 Subject: [PATCH] Keep the subdirectory once in return URLs --- .../src/Factory/ExperienceContextBuilder.php | 3 +- modules/ppcp-axo/src/Gateway/AxoGateway.php | 3 +- .../src/FreeTrialVaultRedirect.php | 3 +- .../src/Endpoint/CaptureCardPayment.php | 3 +- .../Endpoint/PayerActionReturnForwarder.php | 3 +- .../Factory/ExperienceContextBuilderTest.php | 35 ++++++++++ .../FreeTrialVaultRedirectTest.php | 2 +- tests/PHPUnit/bootstrap.php | 1 + tests/stubs/WC_Ajax.php | 12 +++- tests/stubs/WP_Http.php | 70 +++++++++++++++++++ 10 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 tests/stubs/WP_Http.php diff --git a/modules/ppcp-api-client/src/Factory/ExperienceContextBuilder.php b/modules/ppcp-api-client/src/Factory/ExperienceContextBuilder.php index d58d47a36a..cdb5e4cd1c 100644 --- a/modules/ppcp-api-client/src/Factory/ExperienceContextBuilder.php +++ b/modules/ppcp-api-client/src/Factory/ExperienceContextBuilder.php @@ -10,6 +10,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory; use WC_AJAX; +use WP_Http; use WC_Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\CallbackConfig; use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; @@ -95,7 +96,7 @@ public function with_default_paypal_config( public function with_endpoint_return_urls(): ExperienceContextBuilder { $builder = clone $this; - $return_url = home_url( WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ); + $return_url = WP_Http::make_absolute_url( WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ), home_url() ); // The return URL is public and PayPal keeps its query string, so a single-use // secret in the URL is the proof that ReturnUrlEndpoint compares. It needs no diff --git a/modules/ppcp-axo/src/Gateway/AxoGateway.php b/modules/ppcp-axo/src/Gateway/AxoGateway.php index 0f07b01de1..b1527189fc 100644 --- a/modules/ppcp-axo/src/Gateway/AxoGateway.php +++ b/modules/ppcp-axo/src/Gateway/AxoGateway.php @@ -12,6 +12,7 @@ use Psr\Log\LoggerInterface; use Exception; use WC_AJAX; +use WP_Http; use WC_Order; use WC_Payment_Gateway; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; @@ -299,7 +300,7 @@ public function process_payment( $order_id ) { $return_url = add_query_arg( 'token', $order->id(), - home_url( WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ) + WP_Http::make_absolute_url( WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ), home_url() ) ); // ReturnUrlEndpoint refuses a return with no proof. build_order_data() diff --git a/modules/ppcp-save-payment-methods/src/FreeTrialVaultRedirect.php b/modules/ppcp-save-payment-methods/src/FreeTrialVaultRedirect.php index 2be57455ed..bc6a0dd2c7 100644 --- a/modules/ppcp-save-payment-methods/src/FreeTrialVaultRedirect.php +++ b/modules/ppcp-save-payment-methods/src/FreeTrialVaultRedirect.php @@ -17,6 +17,7 @@ use Exception; use Psr\Log\LoggerInterface; use WC_AJAX; +use WP_Http; use WC_Order; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentMethodTokensEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource; @@ -52,7 +53,7 @@ public function create_redirect_url( WC_Order $wc_order ): string { 'ppcp_vault_wc_order' => $wc_order->get_id(), 'ppcp_vault_nonce' => $nonce, ), - home_url( WC_AJAX::get_endpoint( FreeTrialVaultReturnEndpoint::ENDPOINT ) ) + WP_Http::make_absolute_url( WC_AJAX::get_endpoint( FreeTrialVaultReturnEndpoint::ENDPOINT ), home_url() ) ); $customer_id = is_user_logged_in() diff --git a/modules/ppcp-wc-gateway/src/Endpoint/CaptureCardPayment.php b/modules/ppcp-wc-gateway/src/Endpoint/CaptureCardPayment.php index 6952a0a814..7f8b3e5ca4 100644 --- a/modules/ppcp-wc-gateway/src/Endpoint/CaptureCardPayment.php +++ b/modules/ppcp-wc-gateway/src/Endpoint/CaptureCardPayment.php @@ -7,6 +7,7 @@ use Psr\Log\LoggerInterface; use RuntimeException; use WC_AJAX; +use WP_Http; use WC_Order; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait; @@ -101,7 +102,7 @@ public function create_order( string $vault_id, WC_Order $wc_order, string $resu 'ppcp_resume_wc_order' => $wc_order->get_id(), 'ppcp_resume_nonce' => $resume_nonce, ), - home_url( WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ) + WP_Http::make_absolute_url( WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ), home_url() ) ); $card = array( diff --git a/modules/ppcp-wc-gateway/src/Endpoint/PayerActionReturnForwarder.php b/modules/ppcp-wc-gateway/src/Endpoint/PayerActionReturnForwarder.php index cce044e78a..7f44a39136 100644 --- a/modules/ppcp-wc-gateway/src/Endpoint/PayerActionReturnForwarder.php +++ b/modules/ppcp-wc-gateway/src/Endpoint/PayerActionReturnForwarder.php @@ -10,6 +10,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Endpoint; use WC_AJAX; +use WP_Http; use WooCommerce\PayPalCommerce\Http\RedirectorInterface; use WooCommerce\PayPalCommerce\Session\SessionHandler; @@ -55,7 +56,7 @@ public function maybe_forward(): void { 'token' => $token, 'PayerID' => $payer_id, ), - home_url( WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ) + WP_Http::make_absolute_url( WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ), home_url() ) ) ); } diff --git a/tests/PHPUnit/ApiClient/Factory/ExperienceContextBuilderTest.php b/tests/PHPUnit/ApiClient/Factory/ExperienceContextBuilderTest.php index a4823d4765..21371443a0 100644 --- a/tests/PHPUnit/ApiClient/Factory/ExperienceContextBuilderTest.php +++ b/tests/PHPUnit/ApiClient/Factory/ExperienceContextBuilderTest.php @@ -9,6 +9,7 @@ use WooCommerce\PayPalCommerce\TestCase; use WooCommerce\PayPalCommerce\WcGateway\Shipping\ShippingCallbackUrlFactory; use function Brain\Monkey\Functions\expect; +use function Brain\Monkey\Functions\when; use Mockery; class ExperienceContextBuilderTest extends TestCase @@ -29,6 +30,12 @@ public function setUp(): void $this->sut = new ExperienceContextBuilder($this->settings, $this->shipping_callback_url_factory); } + public function tearDown(): void + { + \WC_Ajax::$home_path = ''; + parent::tearDown(); + } + public function testOrderReturnUrls() { $wcOrder = Mockery::mock(\WC_Order::class); @@ -371,4 +378,32 @@ public function test_create_order_endpoint_chain_retracts_the_pending_endpoint_n // Then self::assertSame( 'https://example.com/cart', $result->to_array()['return_url'] ?? null ); } + + /** + * GIVEN a site installed in a subdirectory, where WC's AJAX endpoint already includes "/shop" + * WHEN with_endpoint_return_urls() builds the return URL sent to PayPal + * THEN the subdirectory appears exactly once in the return URL + */ + public function test_endpoint_return_url_does_not_repeat_the_subdirectory_of_the_site(): void + { + \WC_Ajax::$home_path = '/shop'; + + $return_url_secret = Mockery::mock( \WooCommerce\PayPalCommerce\ApiClient\Helper\ReturnUrlSecret::class ); + $return_url_secret->allows( 'issue_pending' )->andReturn( 'pending-secret' ); + + when( 'home_url' )->justReturn( 'https://example.com/shop' ); + when( 'wc_get_checkout_url' )->justReturn( 'https://example.com/shop/checkout' ); + when( 'add_query_arg' )->alias( function ( $key, $value, $url ) { + $separator = strpos( $url, '?' ) === false ? '?' : '&'; + return $url . $separator . $key . '=' . $value; + } ); + + $result = $this->create_sut_with_return_url_secret( $return_url_secret ) + ->with_endpoint_return_urls() + ->build(); + + $return_url = $result->to_array()['return_url'] ?? ''; + self::assertStringStartsWith( 'https://example.com/shop/?wc-ajax=ppc-return-url', $return_url ); + self::assertSame( 1, substr_count( $return_url, '/shop/?wc-ajax=' ) ); + } } diff --git a/tests/PHPUnit/SavePaymentMethods/FreeTrialVaultRedirectTest.php b/tests/PHPUnit/SavePaymentMethods/FreeTrialVaultRedirectTest.php index 7d05528fd0..b989fa2e05 100644 --- a/tests/PHPUnit/SavePaymentMethods/FreeTrialVaultRedirectTest.php +++ b/tests/PHPUnit/SavePaymentMethods/FreeTrialVaultRedirectTest.php @@ -40,7 +40,7 @@ public function setUp(): void { when( 'is_user_logged_in' )->justReturn( false ); when( 'get_user_meta' )->justReturn( '' ); - when( 'home_url' )->returnArg(); + when( 'home_url' )->justReturn( 'https://example.com' ); when( 'wc_get_checkout_url' )->justReturn( 'https://example.com/checkout/' ); when( 'esc_url_raw' )->returnArg(); when( 'wp_generate_password' )->justReturn( 'nonce-abc123' ); diff --git a/tests/PHPUnit/bootstrap.php b/tests/PHPUnit/bootstrap.php index 8888f7dddf..a0e0aac815 100644 --- a/tests/PHPUnit/bootstrap.php +++ b/tests/PHPUnit/bootstrap.php @@ -9,6 +9,7 @@ require_once TESTS_ROOT_DIR . '/stubs/WC_Payment_Gateway.php'; require_once TESTS_ROOT_DIR . '/stubs/WC_Payment_Gateway_CC.php'; require_once TESTS_ROOT_DIR . '/stubs/WC_Ajax.php'; +require_once TESTS_ROOT_DIR . '/stubs/WP_Http.php'; require_once TESTS_ROOT_DIR . '/stubs/WC_Checkout.php'; require_once TESTS_ROOT_DIR . '/stubs/WC_Cart.php'; require_once TESTS_ROOT_DIR . '/stubs/WC_Integration.php'; diff --git a/tests/stubs/WC_Ajax.php b/tests/stubs/WC_Ajax.php index 9cdfb8a836..ec97b0d2fb 100644 --- a/tests/stubs/WC_Ajax.php +++ b/tests/stubs/WC_Ajax.php @@ -3,7 +3,17 @@ class WC_Ajax { + /** + * When set, get_endpoint() mimics WooCommerce and returns a path that includes + * the home path, e.g. "/shop/?wc-ajax=". Tests must reset it. + */ + public static string $home_path = ''; + public static function get_endpoint(string $someEndpoint) : string { + if ( '' === self::$home_path ) { return $someEndpoint; + } + + return self::$home_path . '/?wc-ajax=' . $someEndpoint; } -} \ No newline at end of file +} diff --git a/tests/stubs/WP_Http.php b/tests/stubs/WP_Http.php new file mode 100644 index 0000000000..7c7f184fca --- /dev/null +++ b/tests/stubs/WP_Http.php @@ -0,0 +1,70 @@ + 1 ) { + $path = preg_replace( '![^/]+/\.\./!', '', $path ); + } + + $path = preg_replace( '!^/(\.\./)+!', '/', $path ); + } + + if ( ! empty( $relative_url_parts['query'] ) ) { + $path .= '?' . $relative_url_parts['query']; + } + + if ( ! empty( $relative_url_parts['fragment'] ) ) { + $path .= '#' . $relative_url_parts['fragment']; + } + + return $absolute_path . $path; + } +}