Commit fae52607c7b for woocommerce

commit fae52607c7bae0b35b5d4ce5d8cc5a568112d8a8
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Mon Nov 23 21:30:41 2026 +0100

    Require valid PayPal shipping callback credentials (#71242)

    Forward-port the corresponding WooCommerce 11.3.1 fix to trunk.

diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-buttons-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-buttons-controller.php
index 7fe84d1c55d..2a25b2ac9af 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-buttons-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-buttons-controller.php
@@ -196,17 +196,15 @@ class WC_REST_Paypal_Buttons_Controller extends WC_REST_Controller {
 			return new WP_REST_Response( array( 'error' => 'Invalid PayPal order' ), 404 );
 		}

-		// If order is already in draft status, do nothing and return success.
-		if ( $order->has_status( OrderStatus::CHECKOUT_DRAFT ) ) {
-			return new WP_REST_Response( array( 'success' => true ), 200 );
-		}
-
-		// If order is not pending, return an error.
-		if ( ! $order->has_status( OrderStatus::PENDING ) ) {
+		if ( ! $order->has_status( array( OrderStatus::PENDING, OrderStatus::CHECKOUT_DRAFT ) ) ) {
 			return new WP_REST_Response( array( 'error' => 'Order is not pending' ), 409 );
 		}

-		$order->update_status( OrderStatus::CHECKOUT_DRAFT );
+		// Draft orders also accept shipping callbacks, so cancellation must revoke their credential.
+		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, '' );
+		if ( $order->has_status( OrderStatus::PENDING ) ) {
+			$order->update_status( OrderStatus::CHECKOUT_DRAFT );
+		}
 		$order->save();

 		return new WP_REST_Response( array( 'success' => true ), 200 );
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller.php
index f8e5a715670..d0001afb032 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller.php
@@ -87,26 +87,18 @@ class WC_REST_Paypal_Standard_Controller extends WC_REST_Controller {
 			return false;
 		}

-		// If shipping callback token is not stored in order meta, return true for this order as the token is not generated for the original order.
-		// We will not validate the token if the order did not generate a token in the create order request.
-		// This is done to prevent orders created before the shipping callback token feature was introduced from being blocked from updating their shipping details.
-		if ( ! $order->meta_exists( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN ) ) {
-			return true;
-		}
-
-		// Validate the token.
 		$token = $request->get_param( 'token' );
-		if ( empty( $token ) ) {
+		if ( ! is_string( $token ) || '' === $token ) {
 			return false;
 		}

 		$shipping_callback_token = $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, true );

-		if ( empty( $shipping_callback_token ) || ! hash_equals( $token, $shipping_callback_token ) ) {
+		if ( ! is_string( $shipping_callback_token ) || '' === $shipping_callback_token ) {
 			return false;
 		}

-		return true;
+		return hash_equals( $shipping_callback_token, $token );
 	}

 	/**
diff --git a/plugins/woocommerce/src/Gateways/PayPal/Request.php b/plugins/woocommerce/src/Gateways/PayPal/Request.php
index d7d14faf98e..07c81ddb3bf 100644
--- a/plugins/woocommerce/src/Gateways/PayPal/Request.php
+++ b/plugins/woocommerce/src/Gateways/PayPal/Request.php
@@ -754,6 +754,10 @@ class Request {
 				'callback_events' => array( 'SHIPPING_ADDRESS', 'SHIPPING_OPTIONS' ),
 				'callback_url'    => $this->normalize_url_for_paypal( $callback_url ),
 			);
+		} else {
+			// Revoke any callback credential from a previous payment attempt.
+			$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, '' );
+			$order->save_meta_data();
 		}

 		// If the request is from PayPal JS SDK (Buttons), we need a cancel URL that is compatible with App Switch.
diff --git a/plugins/woocommerce/src/Gateways/PayPal/WebhookHandler.php b/plugins/woocommerce/src/Gateways/PayPal/WebhookHandler.php
index cbcf764697e..14c50fe39d3 100644
--- a/plugins/woocommerce/src/Gateways/PayPal/WebhookHandler.php
+++ b/plugins/woocommerce/src/Gateways/PayPal/WebhookHandler.php
@@ -85,9 +85,7 @@ class WebhookHandler {
 		if ( PayPalConstants::STATUS_APPROVED === $status ) {
 			\WC_Gateway_Paypal::log( 'PayPal payment approved. Order ID: ' . $order->get_id() );
 			$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, $status );
-			// Clear the shipping callback token by setting it to an empty string.
-			// This is done to prevent the token from being used again for the same order.
-			// We are not deleting the meta key as we use the existence of the meta key to determine if the token was ever generated for this order.
+			// Shipping updates are no longer allowed after approval.
 			$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, '' );
 			$order->save();
 			$order->add_order_note(
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller-callback-auth-test.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller-callback-auth-test.php
new file mode 100644
index 00000000000..855318d82e4
--- /dev/null
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller-callback-auth-test.php
@@ -0,0 +1,340 @@
+<?php
+declare( strict_types = 1 );
+
+use Automattic\WooCommerce\Enums\OrderStatus;
+use Automattic\WooCommerce\Gateways\PayPal\Constants as PayPalConstants;
+
+// phpcs:disable Squiz.Classes.ClassFileName.NoMatch, SlevomatCodingStandard.Files.TypeNameMatchesFileName.NoMatchBetweenTypeNameAndFileName, Squiz.Classes.ValidClassName.NotCamelCaps -- Match legacy controller test naming.
+
+/**
+ * Tests for PayPal Standard shipping callback authorization.
+ */
+class WC_REST_Paypal_Standard_Controller_Callback_Auth_Test extends WC_REST_Unit_Test_Case {
+
+	/**
+	 * The System Under Test.
+	 *
+	 * @var WC_REST_Paypal_Standard_Controller
+	 */
+	private $sut;
+
+	/**
+	 * Customer replaced by the shipping callback.
+	 *
+	 * @var WC_Customer|null
+	 */
+	private $original_customer;
+
+	/**
+	 * Session replaced by wc_load_cart() under the inherited mock handler filter.
+	 *
+	 * @var WC_Session|null
+	 */
+	private $original_session;
+
+	/**
+	 * Set up the controller and an anonymous checkout session.
+	 */
+	public function setUp(): void {
+		parent::setUp();
+		$this->sut               = new WC_REST_Paypal_Standard_Controller();
+		$this->original_customer = WC()->customer;
+		$this->original_session  = WC()->session;
+		wp_set_current_user( 0 );
+		wc_load_cart();
+		add_filter(
+			'pre_http_request',
+			static function () {
+				return new WP_Error( 'network_disabled' );
+			}
+		);
+	}
+
+	/**
+	 * Restore the customer and session singletons after the inherited cleanup.
+	 */
+	public function tearDown(): void {
+		try {
+			parent::tearDown();
+		} finally {
+			WC()->customer = $this->original_customer;
+			WC()->session  = $this->original_session;
+		}
+	}
+
+	/**
+	 * @testdox Invalid callback credentials are rejected without changing order or session data.
+	 * @testWith [null, null]
+	 *           [null, "callback-token"]
+	 *           ["", "callback-token"]
+	 *           ["callback-token", null]
+	 *           ["callback-token", ""]
+	 *           ["callback-token", "wrong-token"]
+	 *           ["callback-token", ["callback-token"]]
+	 *           ["callback-token", 123]
+	 *           ["callback-token", true]
+	 *           [["callback-token"], "callback-token"]
+	 *
+	 * @param mixed $stored_token Stored credential, or null for missing metadata.
+	 * @param mixed $provided_token Request credential, or null for a missing parameter.
+	 */
+	public function test_invalid_callback_credentials( $stored_token, $provided_token ): void {
+		$order = $this->create_callback_order();
+		if ( null !== $stored_token ) {
+			$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, $stored_token );
+			$order->save();
+		}
+		$request = $this->create_callback_request( $order );
+		if ( null !== $provided_token ) {
+			$request->set_param( 'token', $provided_token );
+		}
+		$order_data   = wc_get_order( $order->get_id() )->get_data();
+		$session_data = clone WC()->session;
+
+		$response = $this->server->dispatch( $request );
+
+		$this->assertSame( 401, $response->get_status() );
+		$this->assertSame( 'rest_forbidden', $response->get_data()['code'] );
+		$this->assertEquals( $order_data, wc_get_order( $order->get_id() )->get_data() );
+		$this->assertEquals( $session_data, WC()->session );
+	}
+
+	/**
+	 * @testdox A valid token cannot bypass the order key, provider ID, status or transaction checks.
+	 * @testWith ["order_key", 401]
+	 *           ["provider_id", 422]
+	 *           ["status", 422]
+	 *           ["transaction_id", 422]
+	 *
+	 * @param string $invalid_field Field to invalidate.
+	 * @param int    $expected_status Expected response status.
+	 */
+	public function test_existing_callback_checks( string $invalid_field, int $expected_status ): void {
+		$order = $this->create_callback_order();
+		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, 'callback-token' );
+		if ( 'status' === $invalid_field ) {
+			$order->set_status( OrderStatus::COMPLETED );
+		} elseif ( 'transaction_id' === $invalid_field ) {
+			$order->set_transaction_id( 'test-transaction' );
+		}
+		$order->save();
+		$request = $this->create_callback_request( $order );
+		$request->set_param( 'token', 'callback-token' );
+		if ( 'order_key' === $invalid_field ) {
+			$request->set_param(
+				'purchase_units',
+				array(
+					array(
+						'custom_id' => wp_json_encode(
+							array(
+								'order_id'  => $order->get_id(),
+								'order_key' => 'wrong-key',
+							)
+						),
+					),
+				)
+			);
+		} elseif ( 'provider_id' === $invalid_field ) {
+			$request->set_param( 'id', 'another-provider-order' );
+		}
+		$order_data   = wc_get_order( $order->get_id() )->get_data();
+		$session_data = clone WC()->session;
+
+		$response = $this->server->dispatch( $request );
+
+		$this->assertSame( $expected_status, $response->get_status() );
+		$this->assertEquals( $order_data, wc_get_order( $order->get_id() )->get_data() );
+		$this->assertEquals( $session_data, WC()->session );
+	}
+
+	/**
+	 * @testdox Valid pending callbacks can update shipping repeatedly before token revocation.
+	 */
+	public function test_repeated_callbacks_and_revocation(): void {
+		$zone = new WC_Shipping_Zone();
+		$zone->set_zone_name( 'Callback test zone' );
+		$zone->add_location( 'US', 'country' );
+		$zone->save();
+		$zone->add_shipping_method( 'free_shipping' );
+
+		$order = $this->create_callback_order();
+		$order->add_product( WC_Helper_Product::create_simple_product(), 1 );
+		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, 'callback-token' );
+		$order->calculate_totals();
+		$request = $this->create_callback_request( $order );
+		$request->set_param( 'token', 'callback-token' );
+
+		foreach ( array( '94105', '94107' ) as $postcode ) {
+			$address                = $request->get_param( 'shipping_address' );
+			$address['postal_code'] = $postcode;
+			$request->set_param( 'shipping_address', $address );
+			$response = $this->server->dispatch( $request );
+
+			$this->assertSame( 200, $response->get_status() );
+			$this->assertSame( $postcode, wc_get_order( $order->get_id() )->get_shipping_postcode() );
+			$this->assertTrue( $this->sut->validate_shipping_callback_request( $request ) );
+		}
+
+		$order = wc_get_order( $order->get_id() );
+		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, '' );
+		$order->save();
+		$this->assertSame( 401, $this->server->dispatch( $request )->get_status() );
+	}
+
+	/**
+	 * @testdox Cancellation revokes callbacks for pending and already-draft orders and remains repeatable.
+	 * @testWith ["pending"]
+	 *           ["checkout-draft"]
+	 *
+	 * @param string $order_status Order status before cancellation.
+	 */
+	public function test_cancellation_revokes_callback_credentials( string $order_status ): void {
+		$order = $this->create_callback_order();
+		$order->set_status( $order_status );
+		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, 'callback-token' );
+		$order->save();
+		$callback = $this->create_callback_request( $order );
+		$callback->set_param( 'token', 'callback-token' );
+		$this->assertTrue( $this->sut->validate_shipping_callback_request( $callback ), 'The active attempt must initially permit its credential.' );
+
+		$cancel = $this->create_cancel_request( $order );
+		foreach ( array( 'initial cancellation', 'cancellation retry' ) as $attempt ) {
+			$this->assertSame( 200, $this->server->dispatch( $cancel )->get_status(), $attempt );
+			$saved_order = wc_get_order( $order->get_id() );
+			$this->assertSame( OrderStatus::CHECKOUT_DRAFT, $saved_order->get_status(), $attempt );
+			$this->assertSame( '', $saved_order->get_meta( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN ), $attempt );
+			$this->assertSame( 'provider-order', $saved_order->get_meta( PayPalConstants::PAYPAL_ORDER_META_ORDER_ID ), 'Keep the provider reference for retries and accounting.' );
+			$this->assertSame( 401, $this->server->dispatch( $callback )->get_status(), 'Canceled attempts must not change shipping.' );
+			$this->assertSame( '10001', wc_get_order( $order->get_id() )->get_shipping_postcode(), 'Cancellation must preserve the existing destination.' );
+		}
+	}
+
+	/**
+	 * @testdox Rejected cancellation preserves the callback credential and order data.
+	 * @testWith ["nonce", 401]
+	 *           ["provider_id", 404]
+	 *           ["status", 409]
+	 *
+	 * @param string $invalid_field Field to invalidate.
+	 * @param int    $expected_status Expected response status.
+	 */
+	public function test_rejected_cancellation_preserves_callback_credentials( string $invalid_field, int $expected_status ): void {
+		$order = $this->create_callback_order();
+		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, 'callback-token' );
+		if ( 'status' === $invalid_field ) {
+			$order->set_status( OrderStatus::COMPLETED );
+		}
+		$order->save();
+		$cancel = $this->create_cancel_request( $order );
+		if ( 'nonce' === $invalid_field ) {
+			$cancel->set_header( 'Nonce', 'invalid-nonce' );
+		} elseif ( 'provider_id' === $invalid_field ) {
+			$cancel->set_body(
+				wp_json_encode(
+					array(
+						'order_id'        => $order->get_id(),
+						'paypal_order_id' => 'another-provider-order',
+					)
+				)
+			);
+		}
+		$order_data = wc_get_order( $order->get_id() )->get_data();
+
+		$this->assertSame( $expected_status, $this->server->dispatch( $cancel )->get_status() );
+		$this->assertEquals( $order_data, wc_get_order( $order->get_id() )->get_data(), 'A rejected cancellation must not modify the order or its credential.' );
+	}
+
+	/**
+	 * @testdox A callback credential from a different order cannot authorize shipping updates.
+	 */
+	public function test_callback_credential_is_bound_to_its_order(): void {
+		$order = $this->create_callback_order();
+		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, 'first-order-token' );
+		$order->save();
+		$other_order = $this->create_callback_order();
+		$other_order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_ORDER_ID, 'other-provider-order' );
+		$other_order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, 'other-order-token' );
+		$other_order->save();
+		$callback = $this->create_callback_request( $order );
+		$callback->set_param( 'token', $other_order->get_meta( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN ) );
+		$order_data = wc_get_order( $order->get_id() )->get_data();
+
+		$this->assertSame( 401, $this->server->dispatch( $callback )->get_status() );
+		$this->assertEquals( $order_data, wc_get_order( $order->get_id() )->get_data(), 'A different order credential must not modify this order.' );
+	}
+
+	/**
+	 * Build an authorized cancellation request for the current provider order.
+	 *
+	 * @param WC_Order $order Order whose attempt is being canceled.
+	 * @return WP_REST_Request
+	 */
+	private function create_cancel_request( WC_Order $order ): WP_REST_Request {
+		$request = new WP_REST_Request( 'POST', '/wc/v3/paypal-buttons/cancel-payment' );
+		$request->set_header( 'Content-Type', 'application/json' );
+		$request->set_header( 'Nonce', wp_create_nonce( 'wc_gateway_paypal_standard_cancel_payment' ) );
+		$request->set_body(
+			wp_json_encode(
+				array(
+					'order_id'        => $order->get_id(),
+					'paypal_order_id' => $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_ORDER_ID ),
+				)
+			)
+		);
+		return $request;
+	}
+
+	/**
+	 * Create an unpaid order with a provider order ID.
+	 *
+	 * @return WC_Order
+	 */
+	private function create_callback_order(): WC_Order {
+		$order = new WC_Order();
+		$order->set_status( OrderStatus::PENDING );
+		$order->set_shipping_postcode( '10001' );
+		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_ORDER_ID, 'provider-order' );
+		// Match an editable attempt when the companion address-integrity example is applied.
+		$order->update_meta_data(
+			'_paypal_address_context',
+			array(
+				'address_source'      => 'shipping',
+				'shipping_preference' => PayPalConstants::SHIPPING_GET_FROM_FILE,
+			)
+		);
+		$order->save();
+		return $order;
+	}
+
+	/**
+	 * Build an otherwise valid shipping callback without a token.
+	 *
+	 * @param WC_Order $order Order receiving the update.
+	 * @return WP_REST_Request
+	 */
+	private function create_callback_request( WC_Order $order ): WP_REST_Request {
+		$request = new WP_REST_Request( 'POST', '/wc/v3/paypal-standard/update-shipping' );
+		$request->set_body_params(
+			array(
+				'id'               => 'provider-order',
+				'purchase_units'   => array(
+					array(
+						'custom_id' => wp_json_encode(
+							array(
+								'order_id'  => $order->get_id(),
+								'order_key' => $order->get_order_key(),
+							)
+						),
+					),
+				),
+				'shipping_address' => array(
+					'country_code' => 'US',
+					'admin_area_1' => 'CA',
+					'admin_area_2' => 'San Francisco',
+					'postal_code'  => '94105',
+				),
+			)
+		);
+		return $request;
+	}
+}
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller-test.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller-test.php
index c09caa0b541..253975fa8f3 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller-test.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-standard-controller-test.php
@@ -2,6 +2,7 @@
 declare( strict_types = 1 );

 use Automattic\WooCommerce\Enums\OrderStatus;
+use Automattic\WooCommerce\Gateways\PayPal\Constants as PayPalConstants;

 /**
  * Tests for the POST /wc/v3/paypal-standard/update-shipping endpoint
@@ -13,6 +14,8 @@ class WC_REST_Paypal_Standard_Controller_Test extends WC_REST_Unit_Test_Case {

 	private const PAYPAL_ORDER_ID = 'PAYPAL-ORDER-123';

+	private const SHIPPING_CALLBACK_TOKEN = 'shipping-callback-token';
+
 	private const TEST_ORDER_ITEM_META_KEY = 'test_custom_meta';

 	/**
@@ -528,6 +531,7 @@ class WC_REST_Paypal_Standard_Controller_Test extends WC_REST_Unit_Test_Case {

 		$order->set_status( OrderStatus::PENDING );
 		$order->update_meta_data( '_paypal_order_id', $paypal_order_id );
+		$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, self::SHIPPING_CALLBACK_TOKEN );
 		$order->calculate_totals( true );
 		$order->save();
 	}
@@ -601,6 +605,7 @@ class WC_REST_Paypal_Standard_Controller_Test extends WC_REST_Unit_Test_Case {

 		$request = new WP_REST_Request( 'POST', '/wc/v3/paypal-standard/update-shipping' );
 		$request->set_header( 'Content-Type', 'application/json' );
+		$request->set_query_params( array( 'token' => self::SHIPPING_CALLBACK_TOKEN ) );
 		$request->set_body( wp_json_encode( $body ) );

 		return $this->server->dispatch( $request );
diff --git a/plugins/woocommerce/tests/php/src/Gateways/PayPal/RequestTest.php b/plugins/woocommerce/tests/php/src/Gateways/PayPal/RequestTest.php
index 1380d2b8c62..c3128892510 100644
--- a/plugins/woocommerce/tests/php/src/Gateways/PayPal/RequestTest.php
+++ b/plugins/woocommerce/tests/php/src/Gateways/PayPal/RequestTest.php
@@ -137,6 +137,73 @@ class RequestTest extends \WC_Unit_Test_Case {
 		$this->assertNull( $result, 'create_paypal_order should return null when SET_PROVIDED_ADDRESS is set but the selected shipping country is unsupported' );
 	}

+	/**
+	 * @testdox New checkout attempts revoke or rotate callback credentials when shipping preferences change.
+	 * @testWith [false]
+	 *           [true]
+	 *
+	 * @param bool $virtual Whether the second attempt requires no shipping.
+	 */
+	public function test_create_paypal_order_resets_shipping_callback_credentials( bool $virtual ): void {
+		$order = \WC_Helper_Order::create_order();
+		$order->set_address( $order->get_address( 'billing' ), 'shipping' );
+		$order->save();
+		$settings = array(
+			'address_override' => 'no',
+			'send_shipping'    => 'yes',
+		);
+		update_option( 'woocommerce_paypal_settings', $settings );
+		$contexts = array();
+		add_filter(
+			'pre_http_request',
+			function ( $value, $args, $url ) use ( &$contexts ) {
+				$body       = json_decode( $args['body'], true );
+				$contexts[] = $body['order']['payment_source']['paypal']['experience_context'];
+				$response   = $this->create_paypal_order_success( $value, $args, $url );
+				return is_array( $response ) ? $response : new \WP_Error( 'network_disabled' );
+			},
+			10,
+			3
+		);
+
+		$sut = new PayPalRequest( new \WC_Gateway_Paypal() );
+		$this->assertNotNull( $sut->create_paypal_order( $order ) );
+		$first_token = wc_get_order( $order->get_id() )->get_meta( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, true );
+		$this->assertIsString( $first_token );
+		$this->assertNotSame( '', $first_token );
+		$this->assertStringContainsString( 'token=' . $first_token, $contexts[0]['order_update_callback_config']['callback_url'] );
+
+		if ( $virtual ) {
+			foreach ( $order->get_items() as $item ) {
+				$product = $item->get_product();
+				$product->set_virtual( true );
+				$product->save();
+			}
+		} else {
+			$settings['address_override'] = 'yes';
+			update_option( 'woocommerce_paypal_settings', $settings );
+		}
+		$sut = new PayPalRequest( new \WC_Gateway_Paypal() );
+		$this->assertNotNull( $sut->create_paypal_order( $order ) );
+		$this->assertSame( $virtual ? PayPalConstants::SHIPPING_NO_SHIPPING : PayPalConstants::SHIPPING_SET_PROVIDED_ADDRESS, $contexts[1]['shipping_preference'] );
+		$this->assertArrayNotHasKey( 'order_update_callback_config', $contexts[1] );
+		$this->assertSame( '', wc_get_order( $order->get_id() )->get_meta( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, true ) );
+
+		foreach ( $order->get_items() as $item ) {
+			$product = $item->get_product();
+			$product->set_virtual( false );
+			$product->save();
+		}
+		$settings['address_override'] = 'no';
+		update_option( 'woocommerce_paypal_settings', $settings );
+		$sut = new PayPalRequest( new \WC_Gateway_Paypal() );
+		$this->assertNotNull( $sut->create_paypal_order( $order ) );
+		$next_token = wc_get_order( $order->get_id() )->get_meta( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, true );
+		$this->assertNotSame( '', $next_token );
+		$this->assertNotSame( $first_token, $next_token );
+		$this->assertStringContainsString( 'token=' . $next_token, $contexts[2]['order_update_callback_config']['callback_url'] );
+	}
+
 	/**
 	 * Check that the create_paypal_order params are correct.
 	 *
diff --git a/plugins/woocommerce/tests/php/src/Gateways/PayPal/WebhookHandlerTest.php b/plugins/woocommerce/tests/php/src/Gateways/PayPal/WebhookHandlerTest.php
index ef40211785f..0ae4a8b96b5 100644
--- a/plugins/woocommerce/tests/php/src/Gateways/PayPal/WebhookHandlerTest.php
+++ b/plugins/woocommerce/tests/php/src/Gateways/PayPal/WebhookHandlerTest.php
@@ -69,13 +69,14 @@ class WebhookHandlerTest extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * Test process_checkout_order_approved with valid data.
+	 * @testdox Processing checkout approval revokes the shipping callback credential.
 	 *
 	 * @return void
 	 */
 	public function test_process_checkout_order_approved_with_valid_data(): void {
 		$test_order = \WC_Helper_Order::create_order();
 		$test_order->set_payment_method( 'paypal' );
+		$test_order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, 'callback-token' );
 		$test_order->save();

 		$custom_id_data = array(
@@ -113,6 +114,7 @@ class WebhookHandlerTest extends \WC_Unit_Test_Case {
 		// Verify order was updated.
 		$test_order = wc_get_order( $test_order->get_id() );
 		$this->assertEquals( 'APPROVED', $test_order->get_meta( PayPalConstants::PAYPAL_ORDER_META_STATUS, true ) );
+		$this->assertSame( '', $test_order->get_meta( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, true ) );

 		// Clean up.
 		$test_order->delete( true );