Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/ppcp-axo/src/Gateway/AxoGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() )
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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=' ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
1 change: 1 addition & 0 deletions tests/PHPUnit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
12 changes: 11 additions & 1 deletion tests/stubs/WC_Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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=<endpoint>". 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;
}
}
}
70 changes: 70 additions & 0 deletions tests/stubs/WP_Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
declare( strict_types=1 );

class WP_Http {

/**
* Mirrors WP_Http::make_absolute_url() from WordPress core.
*
* @param string $maybe_relative_path Relative or absolute URL.
* @param string $url Base URL.
*/
public static function make_absolute_url( $maybe_relative_path, $url ) {
if ( empty( $url ) ) {
return $maybe_relative_path;
}

$url_parts = parse_url( $url );
if ( ! $url_parts ) {
return $maybe_relative_path;
}

$relative_url_parts = parse_url( $maybe_relative_path );
if ( ! $relative_url_parts ) {
return $maybe_relative_path;
}

if ( ! empty( $relative_url_parts['scheme'] ) ) {
return $maybe_relative_path;
}

$absolute_path = $url_parts['scheme'] . '://';

if ( isset( $relative_url_parts['host'] ) ) {
$absolute_path .= $relative_url_parts['host'];
if ( isset( $relative_url_parts['port'] ) ) {
$absolute_path .= ':' . $relative_url_parts['port'];
}
} else {
$absolute_path .= $url_parts['host'];
if ( isset( $url_parts['port'] ) ) {
$absolute_path .= ':' . $url_parts['port'];
}
}

$path = ! empty( $url_parts['path'] ) ? $url_parts['path'] : '/';

if ( ! empty( $relative_url_parts['path'] ) && '/' === $relative_url_parts['path'][0] ) {
$path = $relative_url_parts['path'];
} elseif ( ! empty( $relative_url_parts['path'] ) ) {
$path = substr( $path, 0, strrpos( $path, '/' ) + 1 );
$path .= $relative_url_parts['path'];

while ( strpos( $path, '../' ) > 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;
}
}
Loading