Commit b6bfb4f7d83 for woocommerce
commit b6bfb4f7d83a3925fe850884cc3e00d509a2cad2
Author: Luigi Teschio <gigitux@gmail.com>
Date: Mon Nov 23 21:29:48 2026 +0100
Authorize existing-order payments before processing (#71259)
Forward-port the corresponding WooCommerce 11.3.1 fix to trunk.
diff --git a/plugins/woocommerce/includes/class-wc-form-handler.php b/plugins/woocommerce/includes/class-wc-form-handler.php
index f478d954edd..0bf5fa9419f 100644
--- a/plugins/woocommerce/includes/class-wc-form-handler.php
+++ b/plugins/woocommerce/includes/class-wc-form-handler.php
@@ -10,6 +10,7 @@ use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Enums\OrderStatus;
use Automattic\WooCommerce\Enums\PaymentGatewayFeature;
use Automattic\WooCommerce\Enums\ProductType;
+use Automattic\WooCommerce\Internal\Utilities\Users;
defined( 'ABSPATH' ) || exit;
@@ -828,11 +829,15 @@ class WC_Form_Handler {
// Pay for existing order.
$order_key = is_string( $_GET['key'] ) ? wp_unslash( $_GET['key'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
- $order_id = absint( $wp->query_vars['order-pay'] );
+ $order_id = absint( $wp->query_vars['order-pay'] ?? 0 );
$order = wc_get_order( $order_id );
// needs_payment() is true for a checkout-draft, but a draft has not passed checkout validation yet.
if ( $order instanceof WC_Order && $order_id === $order->get_id() && hash_equals( $order->get_order_key(), $order_key ) && $order->needs_payment() && ! $order->has_status( OrderStatus::CHECKOUT_DRAFT ) ) {
+ // The order-pay page explains the refusal: login form, ownership error or email verification.
+ if ( ! current_user_can( 'pay_for_order', $order_id ) || Users::should_user_verify_order_email( $order_id, null, 'order-pay' ) ) {
+ return;
+ }
do_action( 'woocommerce_before_pay_action', $order );
diff --git a/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php b/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php
index 32cf2bfb78b..c4b4b188d8e 100644
--- a/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php
+++ b/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php
@@ -437,12 +437,13 @@ class WC_Shortcode_Checkout {
*/
private static function guest_should_verify_email( WC_Order $order, string $context ): bool {
// If we cannot match the order with the current user, ask that they verify their email address.
- $nonce_is_valid = wp_verify_nonce( filter_input( INPUT_POST, 'check_submission' ), 'wc_verify_email' );
+ $nonce = is_string( $_POST['check_submission'] ?? null ) ? sanitize_text_field( wp_unslash( $_POST['check_submission'] ) ) : '';
+ $nonce_is_valid = wp_verify_nonce( $nonce, 'wc_verify_email' );
$supplied_email = null;
$order_id = $order->get_id();
- if ( $nonce_is_valid ) {
- $supplied_email = sanitize_email( wp_unslash( filter_input( INPUT_POST, 'email' ) ) );
+ if ( $nonce_is_valid && is_string( $_POST['email'] ?? null ) ) {
+ $supplied_email = sanitize_email( wp_unslash( $_POST['email'] ) );
}
return Users::should_user_verify_order_email( $order_id, $supplied_email, $context );
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 3771eb67dff..201aeba470b 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -30353,18 +30353,6 @@ parameters:
count: 1
path: includes/shortcodes/class-wc-shortcode-checkout.php
- -
- message: '#^Parameter \#1 \$email of function sanitize_email expects string, string\|false\|null given\.$#'
- identifier: argument.type
- count: 1
- path: includes/shortcodes/class-wc-shortcode-checkout.php
-
- -
- message: '#^Parameter \#1 \$nonce of function wp_verify_nonce expects string, string\|false\|null given\.$#'
- identifier: argument.type
- count: 1
- path: includes/shortcodes/class-wc-shortcode-checkout.php
-
-
message: '#^Parameter \#2 \$user_string of function hash_equals expects string, array\|string given\.$#'
identifier: argument.type
@@ -64372,30 +64360,6 @@ parameters:
count: 1
path: src/Internal/Utilities/URL.php
- -
- message: '#^@param Automattic\\WooCommerce\\Internal\\Utilities\\WC_Order \$order does not accept actual type of parameter\: WC_Order\|WC_Order_Refund\|false\.$#'
- identifier: parameter.phpDocType
- count: 1
- path: src/Internal/Utilities/Users.php
-
- -
- message: '#^Cannot call method get_billing_email\(\) on WC_Order\|WC_Order_Refund\|false\.$#'
- identifier: method.nonObject
- count: 1
- path: src/Internal/Utilities/Users.php
-
- -
- message: '#^Cannot call method get_customer_id\(\) on WC_Order\|WC_Order_Refund\|false\.$#'
- identifier: method.nonObject
- count: 1
- path: src/Internal/Utilities/Users.php
-
- -
- message: '#^Cannot call method get_date_created\(\) on WC_Order\|WC_Order_Refund\|false\.$#'
- identifier: method.nonObject
- count: 1
- path: src/Internal/Utilities/Users.php
-
-
message: '#^One or more @param tags has an invalid name or invalid syntax\.$#'
identifier: phpDoc.parseError
diff --git a/plugins/woocommerce/src/Internal/Utilities/Users.php b/plugins/woocommerce/src/Internal/Utilities/Users.php
index 357ed8d3a52..b5785206f00 100644
--- a/plugins/woocommerce/src/Internal/Utilities/Users.php
+++ b/plugins/woocommerce/src/Internal/Utilities/Users.php
@@ -3,6 +3,7 @@
namespace Automattic\WooCommerce\Internal\Utilities;
use Automattic\WooCommerce\Proxies\LegacyProxy;
+use WC_Order;
use WP_Error, WP_User;
/**
@@ -79,7 +80,11 @@ class Users {
* @return bool
*/
public static function should_user_verify_order_email( $order_id, $supplied_email = null, $context = 'view' ) {
- $order = wc_get_order( $order_id );
+ $order = wc_get_order( $order_id );
+ if ( ! $order instanceof WC_Order ) {
+ return true;
+ }
+
$billing_email = $order->get_billing_email();
$customer_id = $order->get_customer_id();
@@ -131,8 +136,23 @@ class Users {
$can_view_orders = current_user_can( 'read_private_shop_orders' );
$session_email_match = ! empty( $session_email ) && 0 === strcasecmp( $session_email, $billing_email );
$supplied_email_match = ! empty( $supplied_email ) && 0 === strcasecmp( $supplied_email, $billing_email );
+ $verified_payment = false;
+ $payment_email_hash = '';
+
+ if ( 'order-pay' === $context && ! $customer_id && $session instanceof \WC_Session ) {
+ $payment_email_data = wp_json_encode( array( get_current_blog_id(), $order->get_id(), $order->get_order_key(), strtolower( $billing_email ), $session->get_customer_id() ) );
+ if ( is_string( $payment_email_data ) ) {
+ $payment_email_hash = hash( 'sha256', $payment_email_data );
+ $verification = $session->get( 'order_pay_email_verification' );
+ $verified_payment = is_array( $verification )
+ && is_string( $verification['hash'] ?? null )
+ && is_int( $verification['expires'] ?? null )
+ && $verification['expires'] > time()
+ && hash_equals( $payment_email_hash, $verification['hash'] );
+ }
+ }
- $email_verification_required = ! $session_email_match && ! $supplied_email_match && ! $can_view_orders;
+ $email_verification_required = ! $session_email_match && ! $supplied_email_match && ! $can_view_orders && ! $verified_payment;
/**
* Provides an opportunity to override the (potential) requirement for shoppers to verify their email address
@@ -148,7 +168,20 @@ class Users {
* @param WC_Order $order The relevant order.
* @param string $context The context under which we are performing this check.
*/
- return (bool) apply_filters( 'woocommerce_order_email_verification_required', $email_verification_required, $order, $context );
+ $email_verification_required = (bool) apply_filters( 'woocommerce_order_email_verification_required', $email_verification_required, $order, $context );
+
+ // The email challenge and payment form are separate requests. Remember only an explicitly verified guest email.
+ if ( ! $email_verification_required && $supplied_email_match && '' !== $payment_email_hash ) {
+ $session->set(
+ 'order_pay_email_verification',
+ array(
+ 'hash' => $payment_email_hash,
+ 'expires' => time() + HOUR_IN_SECONDS,
+ )
+ );
+ }
+
+ return $email_verification_required;
}
/**
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php b/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
index 7c47c9781b8..89c3a53b9ff 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-form-handler-test.php
@@ -158,7 +158,9 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
public function test_pay_action_processes_payable_orders_but_not_checkout_drafts( string $status, bool $expect_processed ): void {
global $wp;
- $order = WC_Helper_Order::create_order( 0 );
+ $customer_id = self::factory()->user->create( array( 'role' => 'customer' ) );
+ wp_set_current_user( $customer_id );
+ $order = WC_Helper_Order::create_order( $customer_id );
$order->set_status( $status );
$order->save();
@@ -1381,6 +1383,354 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
$this->assertSame( $expected, WC_Form_Handler::remove_add_to_cart_from_canonical_redirect( $redirect_url, $requested_url ) );
}
+ /**
+ * @testdox Payment authorization rejects other customers before any payment side effects.
+ * @testWith ["owner", true]
+ * ["other", false]
+ * ["guest", false]
+ * @param string $visitor Visitor identity.
+ * @param bool $allowed Whether payment should reach the gateway.
+ */
+ public function test_pay_action_checks_order_owner( string $visitor, bool $allowed ): void {
+ $owner = self::factory()->user->create( array( 'role' => 'customer' ) );
+ $order = $this->create_payment_order( $owner );
+ wp_set_current_user( 'owner' === $visitor ? $owner : ( 'other' === $visitor ? self::factory()->user->create( array( 'role' => 'customer' ) ) : 0 ) );
+
+ $this->assert_payment_authorization( $order, $allowed );
+ }
+
+ /**
+ * @testdox A denied payment shows only the order-pay page's own reason.
+ * @testWith ["other", "This order cannot be paid for", 1]
+ * ["logged_out", "Please log in to your account below", 0]
+ * ["unverified_guest", "verify the email address associated with the order", 0]
+ * @param string $visitor Visitor identity.
+ * @param string $reason Text the page shows for this refusal.
+ * @param int $refusals Times "This order cannot be paid for" should appear.
+ */
+ public function test_denied_payment_page_shows_one_reason( string $visitor, string $reason, int $refusals ): void {
+ $owner = self::factory()->user->create( array( 'role' => 'customer' ) );
+ $order = $this->create_payment_order( 'unverified_guest' === $visitor ? 0 : $owner );
+ wp_set_current_user( 'other' === $visitor ? self::factory()->user->create( array( 'role' => 'customer' ) ) : 0 );
+ if ( 'unverified_guest' === $visitor ) {
+ WC()->session = new WC_Mock_Session_Handler();
+ }
+
+ $this->assert_payment_authorization( $order, false );
+
+ $original_query_vars = $GLOBALS['wp']->query_vars;
+ $GLOBALS['wp']->query_vars['order-pay'] = $order->get_id();
+ $_GET = array(
+ 'pay_for_order' => 'true',
+ 'key' => $order->get_order_key(),
+ );
+ try {
+ ob_start();
+ WC_Shortcode_Checkout::output( array() );
+ $page = ob_get_clean();
+ } finally {
+ $GLOBALS['wp']->query_vars = $original_query_vars;
+ }
+
+ $this->assertStringContainsString( $reason, $page );
+ $this->assertSame( $refusals, substr_count( $page, 'This order cannot be paid for' ) );
+ }
+
+ /**
+ * @testdox Verified payment proof survives storage and restoration through the production session handler.
+ */
+ public function test_pay_action_restores_verified_guest_session(): void {
+ wp_set_current_user( 0 );
+ $original_cookies = $_COOKIE;
+ $order = $this->create_payment_order();
+ $cookie_name = '';
+ $cookie_value = '';
+ $capture_cookie = static function ( $enabled, $name, $value ) use ( &$cookie_name, &$cookie_value ) {
+ if ( false !== strpos( $name, 'woocommerce_session' ) ) {
+ $cookie_name = $name;
+ $cookie_value = $value;
+ }
+ return false;
+ };
+ add_filter( 'woocommerce_set_cookie_enabled', $capture_cookie, 10, 3 );
+
+ try {
+ $_COOKIE = array();
+ WC()->session = new WC_Session_Handler();
+ WC()->session->init_session_cookie();
+ WC()->session->set_customer_session_cookie( true );
+ $this->assertNotSame( '', $cookie_value, 'The production handler should issue a signed session cookie.' );
+ $this->assertFalse( \Automattic\WooCommerce\Internal\Utilities\Users::should_user_verify_order_email( $order->get_id(), $order->get_billing_email(), 'order-pay' ) );
+ $customer_id = WC()->session->get_customer_id();
+ WC()->session->save_data();
+ wp_cache_delete( WC_Cache_Helper::get_cache_prefix( WC_SESSION_CACHE_GROUP ) . $customer_id, WC_SESSION_CACHE_GROUP );
+
+ // Emulate the next request with the signed cookie and a fresh handler loading the database.
+ $_COOKIE[ $cookie_name ] = $cookie_value;
+ WC()->session = new WC_Session_Handler();
+ WC()->session->init_session_cookie();
+ $this->assertSame( $customer_id, WC()->session->get_customer_id() );
+ $this->assertIsArray( WC()->session->get( 'order_pay_email_verification' ) );
+ $this->assert_payment_authorization( $order, true );
+ } finally {
+ WC()->session->destroy_session();
+ $_COOKIE = $original_cookies;
+ remove_filter( 'woocommerce_set_cookie_enabled', $capture_cookie );
+ }
+ }
+
+ /**
+ * @testdox Guest payment requires a valid challenge carried forward to the separate payment form.
+ * @testWith ["verified", true]
+ * ["unverified", false]
+ * ["wrong_email", false]
+ * ["wrong_nonce", false]
+ * ["changed_key", false]
+ * ["changed_email", false]
+ * ["other_order", false]
+ * ["expired", false]
+ * ["invalid_proof", false]
+ * ["other_session", false]
+ * ["required_filter", false]
+ * ["grace_period", true]
+ * ["grace_disabled", false]
+ * ["no_email", true]
+ * ["verification_disabled", true]
+ * ["session_email", true]
+ * @param string $scenario Verification scenario.
+ * @param bool $allowed Whether payment should reach the gateway.
+ */
+ public function test_pay_action_checks_guest_verification( string $scenario, bool $allowed ): void {
+ wp_set_current_user( 0 );
+ WC()->session = new WC_Mock_Session_Handler();
+ $order = $this->create_payment_order();
+
+ if ( ! in_array( $scenario, array( 'unverified', 'grace_period', 'grace_disabled', 'no_email', 'verification_disabled', 'session_email' ), true ) ) {
+ $_POST = array(
+ 'check_submission' => 'wrong_nonce' === $scenario ? 'invalid' : wp_create_nonce( 'wc_verify_email' ),
+ 'email' => 'wrong_email' === $scenario ? 'other@example.org' : strtoupper( $order->get_billing_email() ),
+ );
+ $original_query_vars = $GLOBALS['wp']->query_vars;
+ $original_customer = WC()->customer;
+ WC()->customer = new WC_Customer( 0, true );
+ $GLOBALS['wp']->query_vars = array( 'order-pay' => $order->get_id() );
+ $_GET = array(
+ 'pay_for_order' => 'true',
+ 'key' => $order->get_order_key(),
+ );
+ ob_start();
+ try {
+ WC_Shortcode_Checkout::output( array() );
+ $output = (string) ob_get_contents();
+ } finally {
+ ob_end_clean();
+ $GLOBALS['wp']->query_vars = $original_query_vars;
+ WC()->customer = $original_customer;
+ }
+ if ( in_array( $scenario, array( 'wrong_email', 'wrong_nonce' ), true ) ) {
+ $this->assertStringContainsString( 'woocommerce-verify-email', $output );
+ $this->assertNull( WC()->session->get( 'order_pay_email_verification' ) );
+ } else {
+ $this->assertStringContainsString( 'id="order_review"', $output );
+ $this->assertIsArray( WC()->session->get( 'order_pay_email_verification' ) );
+ }
+ }
+
+ switch ( $scenario ) {
+ case 'no_email':
+ $order->set_billing_email( '' );
+ break;
+ case 'invalid_proof':
+ WC()->session->set(
+ 'order_pay_email_verification',
+ array(
+ 'hash' => 'invalid',
+ 'expires' => time() + HOUR_IN_SECONDS,
+ )
+ );
+ break;
+ case 'changed_key':
+ $order->set_order_key( 'wc_order_replaced_key' );
+ break;
+ case 'changed_email':
+ $order->set_billing_email( 'changed@example.org' );
+ break;
+ case 'other_order':
+ $order = $this->create_payment_order();
+ break;
+ case 'expired':
+ $proof = WC()->session->get( 'order_pay_email_verification' );
+ $proof['expires'] = time() - 1;
+ WC()->session->set( 'order_pay_email_verification', $proof );
+ break;
+ case 'other_session':
+ $proof = WC()->session->get( 'order_pay_email_verification' );
+ WC()->session = $this->getMockBuilder( WC_Mock_Session_Handler::class )->onlyMethods( array( 'get_customer_id' ) )->getMock();
+ WC()->session->method( 'get_customer_id' )->willReturn( 'different-session' );
+ WC()->session->set( 'order_pay_email_verification', $proof );
+ break;
+ case 'required_filter':
+ add_filter( 'woocommerce_order_email_verification_required', '__return_true' );
+ break;
+ case 'grace_disabled':
+ add_filter(
+ 'woocommerce_order_email_verification_grace_period',
+ static function () {
+ return -1;
+ }
+ );
+ // Fall through to create a new order with verification still required.
+ case 'grace_period':
+ $order->set_date_created( time() );
+ break;
+ case 'verification_disabled':
+ add_filter( 'woocommerce_order_email_verification_required', '__return_false' );
+ break;
+ case 'session_email':
+ WC()->session->set(
+ 'customer',
+ array(
+ 'id' => '0',
+ 'date_modified' => '',
+ 'email' => $order->get_billing_email(),
+ )
+ );
+ break;
+ }
+ $order->save();
+
+ if ( 'verified' === $scenario ) {
+ $this->assertTrue( \Automattic\WooCommerce\Internal\Utilities\Users::should_user_verify_order_email( $order->get_id(), null, 'order-received' ), 'Payment proof must not authorize another context.' );
+ }
+ $this->assert_payment_authorization( $order, $allowed );
+ }
+
+ /**
+ * @testdox Invalid order identifiers, keys and payment states never reach payment hooks or gateways.
+ * @testWith ["missing_id"]
+ * ["zero_id"]
+ * ["invalid_id"]
+ * ["invalid_key"]
+ * ["paid"]
+ * @param string $scenario Invalid payment request.
+ */
+ public function test_pay_action_rejects_invalid_orders( string $scenario ): void {
+ $order = $this->create_payment_order();
+ if ( 'paid' === $scenario ) {
+ $order->set_status( OrderStatus::COMPLETED );
+ $order->save();
+ }
+ $this->assert_payment_authorization( $order, false, $scenario );
+ }
+
+ /**
+ * Create a payable order outside the email verification grace period.
+ *
+ * @param int $customer_id Order owner, or zero for a guest.
+ * @return WC_Order
+ */
+ private function create_payment_order( int $customer_id = 0 ): WC_Order {
+ $order = new WC_Order();
+ $order->set_customer_id( $customer_id );
+ $order->set_billing_email( 'payer@example.org' );
+ $order->set_billing_country( 'US' );
+ $order->set_billing_state( 'NY' );
+ $order->set_billing_city( 'New York' );
+ $order->set_billing_postcode( '10001' );
+ $order->set_date_created( time() - DAY_IN_SECONDS );
+ $order->set_payment_method( 'bacs' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_manage_stock( true );
+ $product->set_stock_quantity( 10 );
+ $product->save();
+ $order->add_product( $product, 1 );
+ $order->set_total( 10 );
+ $order->save();
+ return $order;
+ }
+
+ /**
+ * Dispatch a payment POST and assert the gateway boundary and denied-request state.
+ *
+ * @param WC_Order $order Order to pay.
+ * @param bool $allowed Whether payment is authorized.
+ * @param string $scenario Optional invalid request scenario.
+ */
+ private function assert_payment_authorization( WC_Order $order, bool $allowed, string $scenario = '' ): void {
+ $gateway = $this->getMockBuilder( WC_Payment_Gateway::class )
+ ->onlyMethods( array( 'validate_fields', 'process_payment' ) )
+ ->getMockForAbstractClass();
+ $gateway->id = 'authorization_test';
+ $gateway->expects( $allowed ? $this->once() : $this->never() )->method( 'validate_fields' );
+ $gateway->expects( $allowed ? $this->once() : $this->never() )->method( 'process_payment' )
+ ->with( $order->get_id() )->willReturn( array( 'result' => 'failure' ) );
+ $gateway_selections = 0;
+ add_filter(
+ 'woocommerce_available_payment_gateways',
+ static function () use ( $gateway, &$gateway_selections ) {
+ ++$gateway_selections;
+ return array( $gateway->id => $gateway );
+ }
+ );
+
+ $original_customer = WC()->customer;
+ $original_query_vars = $GLOBALS['wp']->query_vars;
+ WC()->customer = new WC_Customer( 0, true );
+ WC()->customer->set_billing_country( 'CA' );
+ WC()->customer->set_billing_state( 'ON' );
+ WC()->customer->set_billing_city( 'Toronto' );
+ WC()->customer->set_billing_postcode( 'M5V 1A1' );
+ WC()->customer->save();
+ $customer_before = WC()->session->get( 'customer' );
+ $before_actions = did_action( 'woocommerce_before_pay_action' );
+ $after_actions = did_action( 'woocommerce_after_pay_action' );
+ $status = $order->get_status();
+ $stock_reduced = $order->get_data_store()->get_stock_reduced( $order->get_id() );
+ $stock_quantities = array();
+ foreach ( $order->get_items() as $item ) {
+ $stock_quantities[ $item->get_product_id() ] = $item->get_product()->get_stock_quantity();
+ }
+
+ $GLOBALS['wp']->query_vars['order-pay'] = $order->get_id();
+ if ( 'missing_id' === $scenario ) {
+ unset( $GLOBALS['wp']->query_vars['order-pay'] );
+ } elseif ( 'zero_id' === $scenario ) {
+ $GLOBALS['wp']->query_vars['order-pay'] = 0;
+ } elseif ( 'invalid_id' === $scenario ) {
+ $GLOBALS['wp']->query_vars['order-pay'] = PHP_INT_MAX;
+ }
+ $_GET = array( 'key' => 'invalid_key' === $scenario ? 'invalid' : $order->get_order_key() );
+ $_POST = array(
+ 'woocommerce_pay' => '1',
+ 'payment_method' => $gateway->id,
+ );
+ $_REQUEST = array( 'woocommerce-pay-nonce' => wp_create_nonce( 'woocommerce-pay' ) );
+ $buffer_level = ob_get_level();
+ try {
+ WC_Form_Handler::pay_action();
+ $this->assertSame( $allowed ? 1 : 0, $gateway_selections );
+ $this->assertSame( $before_actions + ( $allowed ? 1 : 0 ), did_action( 'woocommerce_before_pay_action' ) );
+ $this->assertSame( $after_actions + ( $allowed ? 1 : 0 ), did_action( 'woocommerce_after_pay_action' ) );
+ if ( ! $allowed ) {
+ $this->assertSame( 0, wc_notice_count( 'error' ), 'The order-pay page explains a denied payment; the handler must not queue a second notice.' );
+ $this->assertSame( $customer_before, WC()->session->get( 'customer' ), 'Denied payment must not copy the order billing location into the session.' );
+ $saved_order = wc_get_order( $order->get_id() );
+ $this->assertSame( 'bacs', $saved_order->get_payment_method() );
+ $this->assertSame( $status, $saved_order->get_status() );
+ $this->assertSame( $stock_reduced, $saved_order->get_data_store()->get_stock_reduced( $order->get_id() ) );
+ foreach ( $saved_order->get_items() as $item ) {
+ $this->assertSame( $stock_quantities[ $item->get_product_id() ], $item->get_product()->get_stock_quantity() );
+ }
+ }
+ } finally {
+ while ( ob_get_level() > $buffer_level ) {
+ ob_end_clean();
+ }
+ WC()->customer = $original_customer;
+ $GLOBALS['wp']->query_vars = $original_query_vars;
+ }
+ }
+
/**
* @testdox add_to_cart_action() validates the same product ID it adds for each item of a grouped product form.
*
@@ -1672,7 +2022,9 @@ class WC_Form_Handler_Test extends WC_Unit_Test_Case {
public function test_pay_action_names_the_payment_problem( array $available, string $posted, string $expected ): void {
global $wp;
- $order = WC_Helper_Order::create_order();
+ $customer_id = self::factory()->user->create( array( 'role' => 'customer' ) );
+ wp_set_current_user( $customer_id );
+ $order = WC_Helper_Order::create_order( $customer_id );
add_filter(
'woocommerce_available_payment_gateways',
function () use ( $available ) {