From fad88d669da674544710f92a2e0be2583c4b6387 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 16:06:23 -0300 Subject: [PATCH 01/71] Introducing a Stripe order helper class --- includes/class-wc-stripe-order-helper.php | 327 ++++++++++++++++++++++ 1 file changed, 327 insertions(+) create mode 100644 includes/class-wc-stripe-order-helper.php diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php new file mode 100644 index 0000000000..3ea3787803 --- /dev/null +++ b/includes/class-wc-stripe-order-helper.php @@ -0,0 +1,327 @@ +get_meta( WC_Stripe_Order_Metas::META_STRIPE_CURRENCY, true ); + } + + /** + * Updates the Stripe currency for order. + * + * @since 9.9.0 + * + * @param WC_Order $order + * @param string $currency + */ + public static function update_stripe_currency( $order, $currency ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_CURRENCY, $currency ); + } + + /** + * Gets the Stripe fee for order. With legacy check. + * + * @since 9.9.0 + * + * @param WC_Order $order + * @return string $amount + */ + public static function get_stripe_fee( $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $amount = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_FEE, true ); + + // If not found let's check for legacy name. + if ( empty( $amount ) ) { + $amount = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_FEE, true ); + + // If found update to new name. + if ( $amount ) { + self::update_stripe_fee( $order, $amount ); + } + } + + return $amount; + } + + /** + * Updates the Stripe fee for order. + * + * @since 9.9.0 + * + * @param WC_Order $order + * @param float $amount + */ + public static function update_stripe_fee( $order = null, $amount = 0.0 ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_FEE, $amount ); + } + + /** + * Deletes the Stripe fee for order. + * + * @since 9.9.0 + * + * @param WC_Order $order + */ + public static function delete_stripe_fee( $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $order->delete_meta_data( WC_Stripe_Order_Metas::META_STRIPE_FEE ); + $order->delete_meta_data( WC_Stripe_Order_Metas::LEGACY_META_STRIPE_FEE ); + } + + /** + * Gets the Stripe net for order. With legacy check. + * + * @since 9.9.0 + * + * @param WC_Order $order + * @return string $amount + */ + public static function get_stripe_net( $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $amount = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_NET, true ); + + // If not found let's check for legacy name. + if ( empty( $amount ) ) { + $amount = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_NET, true ); + + // If found update to new name. + if ( $amount ) { + self::update_stripe_net( $order, $amount ); + } + } + + return $amount; + } + + /** + * Updates the Stripe net for order. + * + * @since 9.9.0 + * + * @param WC_Order $order + * @param float $amount + */ + public static function update_stripe_net( $order = null, $amount = 0.0 ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_NET, $amount ); + } + + /** + * Deletes the Stripe net for order. + * + * @since 9.9.0 + * + * @param WC_Order $order + */ + public static function delete_stripe_net( $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $order->delete_meta_data( WC_Stripe_Order_Metas::META_STRIPE_NET ); + $order->delete_meta_data( WC_Stripe_Order_Metas::LEGACY_META_STRIPE_NET ); + } + + /** + * Gets the order by Stripe source ID. + * + * @since 9.9.0 + * + * @param string $source_id + */ + public static function get_order_by_source_id( $source_id ) { + return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_SOURCE_ID, $source_id ); + } + + /** + * Gets the order by Stripe charge ID. + * + * @since 9.9.0 + * + * @param string $charge_id + */ + public static function get_order_by_charge_id( $charge_id ) { + return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_CHARGE_ID, $charge_id ); + } + + /** + * Gets the order by Stripe refund ID. + * + * @since 9.9.0 + * + * @param string $refund_id + */ + public static function get_order_by_refund_id( $refund_id ) { + return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_REFUND_ID, $refund_id ); + } + + /** + * Gets the order by Stripe PaymentIntent ID. + * + * @since 9.9.0 + * + * @param string $intent_id The ID of the intent. + * @return WC_Order|bool Either an order or false when not found. + */ + public static function get_order_by_intent_id( $intent_id ) { + return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_INTENT_ID, $intent_id ); + } + + /** + * Gets the order by Stripe SetupIntent ID. + * + * @since 9.9.0 + * + * @param string $intent_id The ID of the intent. + * @return WC_Order|bool Either an order or false when not found. + */ + public static function get_order_by_setup_intent_id( $intent_id ) { + return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_SETUP_INTENT, $intent_id ); + } + + /** + * Adds payment intent id and order note to order if payment intent is not already saved + * + * @since 9.9.0 + * + * @param $payment_intent_id + * @param $order WC_Order + */ + public static function add_payment_intent_to_order( $payment_intent_id, $order ) { + $old_intent_id = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_INTENT_ID ); + + if ( $old_intent_id === $payment_intent_id ) { + return; + } + + $order->add_order_note( + sprintf( + /* translators: $1%s payment intent ID */ + __( 'Stripe payment intent created (Payment Intent ID: %1$s)', 'woocommerce-gateway-stripe' ), + $payment_intent_id + ) + ); + + $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_INTENT_ID, $payment_intent_id ); + $order->save(); + } + + /** + * Adds metadata to the order to indicate that the payment is awaiting action. + * + * This meta is primarily used to prevent orders from being cancelled by WooCommerce's hold stock settings. + * + * @since 9.9.0 + * + * @param WC_Order $order The order to add the metadata to. + * @param bool $save Whether to save the order after adding the metadata. + * + * @return void + */ + public static function set_payment_awaiting_action( $order, $save = true ) { + $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_PAYMENT_AWAITING_ACTION, wc_bool_to_string( true ) ); + + if ( $save ) { + $order->save(); + } + } + + /** + * Removes the metadata from the order that was used to indicate that the payment was awaiting action. + * + * @since 9.9.0 + * + * @param WC_Order $order The order to remove the metadata from. + * @param bool $save Whether to save the order after removing the metadata. + * + * @return void + */ + public static function remove_payment_awaiting_action( $order, $save = true ) { + $order->delete_meta_data( WC_Stripe_Order_Metas::META_STRIPE_PAYMENT_AWAITING_ACTION ); + + if ( $save ) { + $order->save(); + } + } + + /** + * Queries for an order by a specific meta key and value. + * + * @param $meta_key string The meta key to search for. + * @param $meta_value string The meta value to search for. + * @return bool|WC_Order + */ + private static function get_by_meta( $meta_key, $meta_value ) { + global $wpdb; + + if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { + $params = [ 'limit' => 1 ]; + // Check if the meta key is a transaction ID. If so, use the transaction ID to query the order, instead of the meta when HPOS is enabled. + if ( WC_Stripe_Order_Metas::META_STRIPE_CHARGE_ID === $meta_key ) { + $params['transaction_id'] = $meta_value; + } else { + $params['meta_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + [ + 'key' => $meta_key, + 'value' => $meta_value, + ], + ]; + } + + $orders = wc_get_orders( $params ); + $order_id = current( $orders ) ? current( $orders )->get_id() : false; + } else { + $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $meta_value, $meta_key ) ); + } + + if ( ! empty( $order_id ) ) { + $order = wc_get_order( $order_id ); + } + + if ( ! empty( $order ) && $order->get_status() !== OrderStatus::TRASH ) { + return $order; + } + + return false; + } +} From 2ff8bacc862ca87f1d650d10b132532dcdb5650f Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 19:17:39 -0300 Subject: [PATCH 02/71] Order meta keys as constants --- includes/class-wc-stripe-order-helper.php | 119 ++++++++++++++++++---- 1 file changed, 97 insertions(+), 22 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 3ea3787803..707b272cb0 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -10,6 +10,81 @@ * WC_Stripe_Order_Helper class. */ class WC_Stripe_Order_Helper { + /** + * Meta key for Stripe currency. + * + * @string + */ + private const META_STRIPE_CURRENCY = '_stripe_currency'; + + /** + * Meta key for Stripe fee. + * + * @string + */ + private const META_STRIPE_FEE = '_stripe_fee'; + + /** + * Meta key for Stripe fee (legacy version). + * + * @string + */ + private const LEGACY_META_STRIPE_FEE = 'Stripe Fee'; + + /** + * Meta key for Stripe net. + * + * @string + */ + private const META_STRIPE_NET = '_stripe_net'; + + /** + * Meta key for Stripe net (legacy version). + * + * @string + */ + private const LEGACY_META_STRIPE_NET = 'Net Revenue From Stripe'; + + /** + * Meta key for Stripe source ID. + * + * @string + */ + private const META_STRIPE_SOURCE_ID = '_stripe_source_id'; + + /** + * Meta key for Stripe charge ID. + * + * @string + */ + private const META_STRIPE_CHARGE_ID = '_stripe_charge_id'; + + /** + * Meta key for Stripe refund ID. + * + * @string + */ + private const META_STRIPE_REFUND_ID = '_stripe_refund_id'; + + /** + * Meta key for Stripe intent ID. + * + * @string + */ + private const META_STRIPE_INTENT_ID = '_stripe_intent_id'; + + /** + * Meta key for Stripe setup intent ID. + */ + private const META_STRIPE_SETUP_INTENT = '_stripe_setup_intent'; + + /** + * Meta key for payment awaiting action. + * + * @string + */ + private const META_STRIPE_PAYMENT_AWAITING_ACTION = '_stripe_payment_awaiting_action'; + /** * Gets the Stripe currency for order. * @@ -23,7 +98,7 @@ public static function get_stripe_currency( $order = null ) { return false; } - return $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_CURRENCY, true ); + return $order->get_meta( self::META_STRIPE_CURRENCY, true ); } /** @@ -39,7 +114,7 @@ public static function update_stripe_currency( $order, $currency ) { return false; } - $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_CURRENCY, $currency ); + $order->update_meta_data( self::META_STRIPE_CURRENCY, $currency ); } /** @@ -55,11 +130,11 @@ public static function get_stripe_fee( $order = null ) { return false; } - $amount = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_FEE, true ); + $amount = $order->get_meta( self::META_STRIPE_FEE, true ); // If not found let's check for legacy name. if ( empty( $amount ) ) { - $amount = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_FEE, true ); + $amount = $order->get_meta( self::META_STRIPE_FEE, true ); // If found update to new name. if ( $amount ) { @@ -83,7 +158,7 @@ public static function update_stripe_fee( $order = null, $amount = 0.0 ) { return false; } - $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_FEE, $amount ); + $order->update_meta_data( self::META_STRIPE_FEE, $amount ); } /** @@ -98,8 +173,8 @@ public static function delete_stripe_fee( $order = null ) { return false; } - $order->delete_meta_data( WC_Stripe_Order_Metas::META_STRIPE_FEE ); - $order->delete_meta_data( WC_Stripe_Order_Metas::LEGACY_META_STRIPE_FEE ); + $order->delete_meta_data( self::META_STRIPE_FEE ); + $order->delete_meta_data( self::LEGACY_META_STRIPE_FEE ); } /** @@ -115,11 +190,11 @@ public static function get_stripe_net( $order = null ) { return false; } - $amount = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_NET, true ); + $amount = $order->get_meta( self::META_STRIPE_NET, true ); // If not found let's check for legacy name. if ( empty( $amount ) ) { - $amount = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_NET, true ); + $amount = $order->get_meta( self::META_STRIPE_NET, true ); // If found update to new name. if ( $amount ) { @@ -143,7 +218,7 @@ public static function update_stripe_net( $order = null, $amount = 0.0 ) { return false; } - $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_NET, $amount ); + $order->update_meta_data( self::META_STRIPE_NET, $amount ); } /** @@ -158,8 +233,8 @@ public static function delete_stripe_net( $order = null ) { return false; } - $order->delete_meta_data( WC_Stripe_Order_Metas::META_STRIPE_NET ); - $order->delete_meta_data( WC_Stripe_Order_Metas::LEGACY_META_STRIPE_NET ); + $order->delete_meta_data( self::META_STRIPE_NET ); + $order->delete_meta_data( self::LEGACY_META_STRIPE_NET ); } /** @@ -170,7 +245,7 @@ public static function delete_stripe_net( $order = null ) { * @param string $source_id */ public static function get_order_by_source_id( $source_id ) { - return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_SOURCE_ID, $source_id ); + return self::get_by_meta( self::META_STRIPE_SOURCE_ID, $source_id ); } /** @@ -181,7 +256,7 @@ public static function get_order_by_source_id( $source_id ) { * @param string $charge_id */ public static function get_order_by_charge_id( $charge_id ) { - return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_CHARGE_ID, $charge_id ); + return self::get_by_meta( self::META_STRIPE_CHARGE_ID, $charge_id ); } /** @@ -192,7 +267,7 @@ public static function get_order_by_charge_id( $charge_id ) { * @param string $refund_id */ public static function get_order_by_refund_id( $refund_id ) { - return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_REFUND_ID, $refund_id ); + return self::get_by_meta( self::META_STRIPE_REFUND_ID, $refund_id ); } /** @@ -204,7 +279,7 @@ public static function get_order_by_refund_id( $refund_id ) { * @return WC_Order|bool Either an order or false when not found. */ public static function get_order_by_intent_id( $intent_id ) { - return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_INTENT_ID, $intent_id ); + return self::get_by_meta( self::META_STRIPE_INTENT_ID, $intent_id ); } /** @@ -216,7 +291,7 @@ public static function get_order_by_intent_id( $intent_id ) { * @return WC_Order|bool Either an order or false when not found. */ public static function get_order_by_setup_intent_id( $intent_id ) { - return self::get_by_meta( WC_Stripe_Order_Metas::META_STRIPE_SETUP_INTENT, $intent_id ); + return self::get_by_meta( self::META_STRIPE_SETUP_INTENT, $intent_id ); } /** @@ -228,7 +303,7 @@ public static function get_order_by_setup_intent_id( $intent_id ) { * @param $order WC_Order */ public static function add_payment_intent_to_order( $payment_intent_id, $order ) { - $old_intent_id = $order->get_meta( WC_Stripe_Order_Metas::META_STRIPE_INTENT_ID ); + $old_intent_id = $order->get_meta( self::META_STRIPE_INTENT_ID ); if ( $old_intent_id === $payment_intent_id ) { return; @@ -242,7 +317,7 @@ public static function add_payment_intent_to_order( $payment_intent_id, $order ) ) ); - $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_INTENT_ID, $payment_intent_id ); + $order->update_meta_data( self::META_STRIPE_INTENT_ID, $payment_intent_id ); $order->save(); } @@ -259,7 +334,7 @@ public static function add_payment_intent_to_order( $payment_intent_id, $order ) * @return void */ public static function set_payment_awaiting_action( $order, $save = true ) { - $order->update_meta_data( WC_Stripe_Order_Metas::META_STRIPE_PAYMENT_AWAITING_ACTION, wc_bool_to_string( true ) ); + $order->update_meta_data( self::META_STRIPE_PAYMENT_AWAITING_ACTION, wc_bool_to_string( true ) ); if ( $save ) { $order->save(); @@ -277,7 +352,7 @@ public static function set_payment_awaiting_action( $order, $save = true ) { * @return void */ public static function remove_payment_awaiting_action( $order, $save = true ) { - $order->delete_meta_data( WC_Stripe_Order_Metas::META_STRIPE_PAYMENT_AWAITING_ACTION ); + $order->delete_meta_data( self::META_STRIPE_PAYMENT_AWAITING_ACTION ); if ( $save ) { $order->save(); @@ -297,7 +372,7 @@ private static function get_by_meta( $meta_key, $meta_value ) { if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { $params = [ 'limit' => 1 ]; // Check if the meta key is a transaction ID. If so, use the transaction ID to query the order, instead of the meta when HPOS is enabled. - if ( WC_Stripe_Order_Metas::META_STRIPE_CHARGE_ID === $meta_key ) { + if ( self::META_STRIPE_CHARGE_ID === $meta_key ) { $params['transaction_id'] = $meta_value; } else { $params['meta_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query From 2f73d5e797ae216ba5402c0df0c99472cc03b02d Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 19:30:36 -0300 Subject: [PATCH 03/71] Adding deprecation annotation --- includes/class-wc-stripe-helper.php | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 5ed51f9e87..884ded0a09 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -95,6 +95,8 @@ public static function get_stripe_currency( $order = null ) { * @since 4.1.0 * @param object $order * @param string $currency + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::update_stripe_currency()` instead. */ public static function update_stripe_currency( $order, $currency ) { if ( is_null( $order ) ) { @@ -110,6 +112,8 @@ public static function update_stripe_currency( $order, $currency ) { * @since 4.1.0 * @param object $order * @return string $amount + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::get_stripe_fee()` instead. */ public static function get_stripe_fee( $order = null ) { if ( is_null( $order ) ) { @@ -137,6 +141,8 @@ public static function get_stripe_fee( $order = null ) { * @since 4.1.0 * @param object $order * @param float $amount + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::update_stripe_fee()` instead. */ public static function update_stripe_fee( $order = null, $amount = 0.0 ) { if ( is_null( $order ) ) { @@ -151,6 +157,8 @@ public static function update_stripe_fee( $order = null, $amount = 0.0 ) { * * @since 4.1.0 * @param object $order + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::delete_stripe_fee()` instead. */ public static function delete_stripe_fee( $order = null ) { if ( is_null( $order ) ) { @@ -167,6 +175,8 @@ public static function delete_stripe_fee( $order = null ) { * @since 4.1.0 * @param object $order * @return string $amount + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::get_stripe_net()` instead. */ public static function get_stripe_net( $order = null ) { if ( is_null( $order ) ) { @@ -194,6 +204,8 @@ public static function get_stripe_net( $order = null ) { * @since 4.1.0 * @param object $order * @param float $amount + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::update_stripe_net()` instead. */ public static function update_stripe_net( $order = null, $amount = 0.0 ) { if ( is_null( $order ) ) { @@ -208,6 +220,8 @@ public static function update_stripe_net( $order = null, $amount = 0.0 ) { * * @since 4.1.0 * @param object $order + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::delete_stripe_net()` instead. */ public static function delete_stripe_net( $order = null ) { if ( is_null( $order ) ) { @@ -885,6 +899,8 @@ public static function get_webhook_url() { * @since 4.0.0 * @version 4.0.0 * @param string $source_id + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_order_by_source_id() instead. */ public static function get_order_by_source_id( $source_id ) { global $wpdb; @@ -919,6 +935,8 @@ public static function get_order_by_source_id( $source_id ) { * @since 4.0.0 * @since 4.1.16 Return false if charge_id is empty. * @param string $charge_id + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_order_by_charge_id() instead. */ public static function get_order_by_charge_id( $charge_id ) { global $wpdb; @@ -951,6 +969,8 @@ public static function get_order_by_charge_id( $charge_id ) { * * @since 7.5.0 * @param string $refund_id + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_order_by_refund_id() instead. */ public static function get_order_by_refund_id( $refund_id ) { global $wpdb; @@ -985,6 +1005,8 @@ public static function get_order_by_refund_id( $refund_id ) { * @since 4.2 * @param string $intent_id The ID of the intent. * @return WC_Order|bool Either an order or false when not found. + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_order_by_intent_id() instead. */ public static function get_order_by_intent_id( $intent_id ) { global $wpdb; @@ -1023,6 +1045,8 @@ public static function get_order_by_intent_id( $intent_id ) { * @since 4.3 * @param string $intent_id The ID of the intent. * @return WC_Order|bool Either an order or false when not found. + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_order_by_setup_intent_id() instead. */ public static function get_order_by_setup_intent_id( $intent_id ) { global $wpdb; @@ -1284,6 +1308,8 @@ private static function should_load_scripts_for_prb_location( $location ) { * * @param $payment_intent_id * @param $order + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::add_payment_intent_to_order() instead. */ public static function add_payment_intent_to_order( $payment_intent_id, $order ) { @@ -1437,6 +1463,8 @@ public static function get_stripe_gateway_ids() { * @param bool $save Whether to save the order after adding the metadata. * * @return void + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::set_payment_awaiting_action() instead. */ public static function set_payment_awaiting_action( $order, $save = true ) { $order->update_meta_data( self::PAYMENT_AWAITING_ACTION_META, wc_bool_to_string( true ) ); @@ -1453,6 +1481,8 @@ public static function set_payment_awaiting_action( $order, $save = true ) { * @param bool $save Whether to save the order after removing the metadata. * * @return void + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::remove_payment_awaiting_action() instead. */ public static function remove_payment_awaiting_action( $order, $save = true ) { $order->delete_meta_data( self::PAYMENT_AWAITING_ACTION_META ); From 2d376f5833acdccc7c4f4befea8affe0a274b241 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 19:40:24 -0300 Subject: [PATCH 04/71] Additional methods --- includes/class-wc-stripe-helper.php | 12 ++ includes/class-wc-stripe-order-helper.php | 179 ++++++++++++++++++++++ 2 files changed, 191 insertions(+) diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 884ded0a09..dbb2371775 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -80,6 +80,8 @@ public static function delete_main_stripe_settings() { * @since 4.1.0 * @param object $order * @return string $currency + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::get_stripe_currency()` instead. */ public static function get_stripe_currency( $order = null ) { if ( is_null( $order ) ) { @@ -1082,6 +1084,8 @@ public static function get_order_by_setup_intent_id( $intent_id ) { * * @param WC_Order $order The order to generate the suffix for. * @return string The statement descriptor suffix ("#{order-number}"). + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_dynamic_statement_descriptor_suffix() instead. */ public static function get_dynamic_statement_descriptor_suffix( $order ) { $prefix = WC_Stripe::get_instance()->account->get_card_statement_prefix(); @@ -1108,6 +1112,8 @@ public static function get_dynamic_statement_descriptor_suffix( $order ) { * @since 4.0.0 * @param string $statement_descriptor Statement descriptor. * @return string $statement_descriptor Sanitized statement descriptor. + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::clean_statement_descriptor() instead. */ public static function clean_statement_descriptor( $statement_descriptor = '' ) { $disallowed_characters = [ '<', '>', '\\', '*', '"', "'", '/', '(', ')', '{', '}' ]; @@ -1425,6 +1431,8 @@ public static function get_payment_method_from_intent( $intent ) { * @param WC_Order $order The order to fetch the Stripe intent from. * * @return string|bool The intent ID if found, false otherwise. + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_intent_id_from_order() instead. */ public static function get_intent_id_from_order( $order ) { $intent_id = $order->get_meta( '_stripe_intent_id' ); @@ -1937,6 +1945,8 @@ public static function has_gateway_plugin_active( $plugin_id ) { * @param string|null $selected_payment_type The selected payment type, which is generally applicable for updates. If null, we will use the stored payment type for the order. * * @throws Exception Throws an exception if the intent is not valid for the order. + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::validate_intent_for_order() instead. */ public static function validate_intent_for_order( $order, $intent, ?string $selected_payment_type = null ): void { $intent_id = null; @@ -2039,6 +2049,8 @@ public static function is_connected( $mode = null ) { * * @param $order WC_Order The order to check. * @return bool + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::is_stripe_gateway_order() instead. */ public static function is_stripe_gateway_order( $order ) { return WC_Gateway_Stripe::ID === substr( (string) $order->get_payment_method(), 0, 6 ); diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 707b272cb0..0058595ae6 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -359,6 +359,185 @@ public static function remove_payment_awaiting_action( $order, $save = true ) { } } + /** + * Gets the dynamic bank statement descriptor suffix. + * + * Stripe will automatically append this suffix to the merchant account's bank statement prefix. + * + * @since 9.9.0 + * + * @param WC_Order $order The order to generate the suffix for. + * @return string The statement descriptor suffix ("#{order-number}"). + */ + public static function get_dynamic_statement_descriptor_suffix( $order ) { + $prefix = WC_Stripe::get_instance()->account->get_card_statement_prefix(); + $suffix = ''; + + if ( method_exists( $order, 'get_order_number' ) && ! empty( $order->get_order_number() ) ) { + $suffix = '#' . $order->get_order_number(); + + // Stripe requires at least 1 latin (alphabet) character in the suffix so we add an extra `O` before the order number. + if ( 0 === preg_match( '/[a-zA-Z]/', $suffix ) ) { + $suffix = 'O ' . $suffix; + } + } + + // Make sure that the prefix + suffix is limited at 22 characters. + return self::clean_statement_descriptor( substr( trim( $suffix ), 0, 22 - strlen( $prefix . '* ' ) ) ); + } + + /** + * Sanitize statement descriptor text. + * + * Stripe requires max of 22 characters and no special characters. + * + * @since 9.9.0 + * + * @param string $statement_descriptor Statement descriptor. + * @return string $statement_descriptor Sanitized statement descriptor. + */ + public static function clean_statement_descriptor( $statement_descriptor = '' ) { + $disallowed_characters = [ '<', '>', '\\', '*', '"', "'", '/', '(', ')', '{', '}' ]; + + // Strip any tags. + $statement_descriptor = strip_tags( $statement_descriptor ); + + // Strip any HTML entities. + // Props https://stackoverflow.com/questions/657643/how-to-remove-html-special-chars . + $statement_descriptor = preg_replace( '/&#?[a-z0-9]{2,8};/i', '', $statement_descriptor ); + + // Next, remove any remaining disallowed characters. + $statement_descriptor = str_replace( $disallowed_characters, '', $statement_descriptor ); + + // Remove non-Latin characters, excluding numbers, whitespaces and especial characters. + $statement_descriptor = preg_replace( '/[^a-zA-Z0-9\s\x{00C0}-\x{00FF}\p{P}]/u', '', $statement_descriptor ); + + // Trim any whitespace at the ends and limit to 22 characters. + $statement_descriptor = substr( trim( $statement_descriptor ), 0, 22 ); + + return $statement_descriptor; + } + + /** + * Returns the payment intent or setup intent ID from a given order object. + * + * @since 9.9.0 + * + * @param WC_Order $order The order to fetch the Stripe intent from. + * + * @return string|bool The intent ID if found, false otherwise. + */ + public static function get_intent_id_from_order( $order ) { + $intent_id = $order->get_meta( '_stripe_intent_id' ); + + if ( ! $intent_id ) { + $intent_id = $order->get_meta( '_stripe_setup_intent' ); + } + + return $intent_id ?? false; + } + + /** + * Checks if the given payment intent is valid for the order. + * This checks the currency, amount, and payment method types. + * The function will log a critical error if there is a mismatch. + * + * @since 9.9.0 + * + * @param WC_Order $order The order to check. + * @param object|string $intent The payment intent to check, can either be an object or an intent ID. + * @param string|null $selected_payment_type The selected payment type, which is generally applicable for updates. If null, we will use the stored payment type for the order. + * + * @throws Exception Throws an exception if the intent is not valid for the order. + */ + public static function validate_intent_for_order( $order, $intent, ?string $selected_payment_type = null ): void { + $intent_id = null; + if ( is_string( $intent ) ) { + $intent_id = $intent; + $is_setup_intent = substr( $intent_id, 0, 4 ) === 'seti'; + if ( $is_setup_intent ) { + $intent = WC_Stripe_API::retrieve( 'setup_intents/' . $intent_id . '?expand[]=payment_method' ); + } else { + $intent = WC_Stripe_API::retrieve( 'payment_intents/' . $intent_id . '?expand[]=payment_method' ); + } + } + + if ( ! is_object( $intent ) ) { + throw new Exception( __( "We're not able to process this request. Please try again later.", 'woocommerce-gateway-stripe' ) ); + } + + if ( null === $intent_id ) { + $intent_id = $intent->id ?? null; + } + + // Make sure we actually fetched the intent. + if ( ! empty( $intent->error ) ) { + WC_Stripe_Logger::error( + 'Error: failed to fetch requested Stripe intent', + [ + 'intent_id' => $intent_id, + 'error' => $intent->error, + ] + ); + throw new Exception( __( "We're not able to process this request. Please try again later.", 'woocommerce-gateway-stripe' ) ); + } + + if ( null === $selected_payment_type ) { + $selected_payment_type = $order->get_meta( '_stripe_upe_payment_type', true ); + } + + // If we don't have a selected payment type, that implies we have no stored value and a new payment type is permitted. + $is_valid_payment_type = empty( $selected_payment_type ) || ( ! empty( $intent->payment_method_types ) && in_array( $selected_payment_type, $intent->payment_method_types, true ) ); + $order_currency = strtolower( $order->get_currency() ); + $order_amount = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $order->get_currency() ); + $order_intent_id = self::get_intent_id_from_order( $order ); + + if ( 'payment_intent' === $intent->object ) { + $is_valid = $order_currency === $intent->currency + && $is_valid_payment_type + && $order_amount === $intent->amount + && ( ! $order_intent_id || $order_intent_id === $intent->id ); + } else { + // Setup intents don't have an amount or currency. + $is_valid = $is_valid_payment_type + && ( ! $order_intent_id || $order_intent_id === $intent->id ); + } + + // Return early if we have a valid intent. + if ( $is_valid ) { + return; + } + + $permitted_payment_types = implode( '/', $intent->payment_method_types ); + WC_Stripe_Logger::critical( + "Error: Invalid payment intent for order. Intent: {$intent->currency} {$intent->amount} via {$permitted_payment_types}, Order: {$order_currency} {$order_amount} {$selected_payment_type}", + [ + 'order_id' => $order->get_id(), + 'intent_id' => $intent->id, + 'intent_currency' => $intent->currency, + 'intent_amount' => $intent->amount, + 'intent_payment_method_types' => $intent->payment_method_types, + 'selected_payment_type' => $selected_payment_type, + 'order_currency' => $order->get_currency(), + 'order_total' => $order->get_total(), + ] + ); + + throw new Exception( __( "We're not able to process this request. Please try again later.", 'woocommerce-gateway-stripe' ) ); + } + + /** + * Checks if the order is using a Stripe payment method. + * + * @since 9.9.0 + * + * @param $order WC_Order The order to check. + * @return bool + */ + public static function is_stripe_gateway_order( $order ) { + return WC_Gateway_Stripe::ID === substr( (string) $order->get_payment_method(), 0, 6 ); + } + /** * Queries for an order by a specific meta key and value. * From ce7f7ffe911f8ff30f22d18a88d5ef7c303279ce Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 19:57:11 -0300 Subject: [PATCH 05/71] Replacing occurrences --- ...ract-wc-stripe-payment-gateway-voucher.php | 2 +- .../abstract-wc-stripe-payment-gateway.php | 20 +++++------ includes/class-wc-gateway-stripe.php | 8 ++--- includes/class-wc-stripe-helper.php | 4 +-- .../class-wc-stripe-intent-controller.php | 8 ++--- includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-order-helper.php | 34 +----------------- includes/class-wc-stripe-webhook-handler.php | 36 +++++++++---------- .../class-wc-stripe-subscriptions-helper.php | 2 +- .../compat/trait-wc-stripe-subscriptions.php | 4 +-- .../class-wc-stripe-upe-payment-gateway.php | 10 +++--- tests/phpunit/WC_Stripe_Helper_Test.php | 3 +- .../WC_Stripe_Webhook_Handler_Test.php | 3 +- 13 files changed, 52 insertions(+), 84 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php index 0ff21cbdb7..06190ab6dc 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php @@ -268,7 +268,7 @@ public function process_payment( $order_id, $retry = true, $force_save_save = fa $order->update_status( OrderStatus::PENDING, __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); $order->save(); - WC_Stripe_Helper::add_payment_intent_to_order( $intent->id, $order ); + WC_Stripe_Order_Helper::add_payment_intent_to_order( $intent->id, $order ); return [ 'result' => 'success', diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 6e96d1c820..05030b0588 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -474,7 +474,7 @@ public function generate_payment_request( $order, $prepared_payment_method ) { if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { $post_data['capture'] = $capture ? 'true' : 'false'; if ( $is_short_statement_descriptor_enabled ) { - $post_data['statement_descriptor_suffix'] = WC_Stripe_Helper::get_dynamic_statement_descriptor_suffix( $order ); + $post_data['statement_descriptor_suffix'] = WC_Stripe_Order_Helper::get_dynamic_statement_descriptor_suffix( $order ); } } @@ -546,7 +546,7 @@ public function generate_payment_request( $order, $prepared_payment_method ) { public function process_response( $response, $order ) { WC_Stripe_Logger::log( 'Processing response: ' . print_r( $response, true ) ); - $potential_order = WC_Stripe_Helper::get_order_by_charge_id( $response->id ); + $potential_order = WC_Stripe_Order_Helper::get_order_by_charge_id( $response->id ); if ( $potential_order && $potential_order->get_id() !== $order->get_id() ) { WC_Stripe_Logger::log( 'Aborting, transaction already consumed by another order.' ); $localized_message = __( 'Payment processing failed. Please retry.', 'woocommerce-gateway-stripe' ); @@ -1106,22 +1106,22 @@ public function update_fees( $order, $balance_transaction_id ) { if ( isset( $balance_transaction ) && isset( $balance_transaction->fee ) ) { // Fees and Net needs to both come from Stripe to be accurate as the returned // values are in the local currency of the Stripe account, not from WC. - $fee_refund = ! empty( $balance_transaction->fee ) ? WC_Stripe_Helper::format_balance_fee( $balance_transaction, 'fee' ) : 0; - $net_refund = ! empty( $balance_transaction->net ) ? WC_Stripe_Helper::format_balance_fee( $balance_transaction, 'net' ) : 0; + $fee_refund = ! empty( $balance_transaction->fee ) ? WC_Stripe_Order_Helper::format_balance_fee( $balance_transaction, 'fee' ) : 0; + $net_refund = ! empty( $balance_transaction->net ) ? WC_Stripe_Order_Helper::format_balance_fee( $balance_transaction, 'net' ) : 0; // Current data fee & net. - $fee_current = WC_Stripe_Helper::get_stripe_fee( $order ); - $net_current = WC_Stripe_Helper::get_stripe_net( $order ); + $fee_current = WC_Stripe_Order_Helper::get_stripe_fee( $order ); + $net_current = WC_Stripe_Order_Helper::get_stripe_net( $order ); // Calculation. $fee = (float) $fee_current + (float) $fee_refund; $net = (float) $net_current + (float) $net_refund; - WC_Stripe_Helper::update_stripe_fee( $order, $fee ); - WC_Stripe_Helper::update_stripe_net( $order, $net ); + WC_Stripe_Order_Helper::update_stripe_fee( $order, $fee ); + WC_Stripe_Order_Helper::update_stripe_net( $order, $net ); $currency = ! empty( $balance_transaction->currency ) ? strtoupper( $balance_transaction->currency ) : null; - WC_Stripe_Helper::update_stripe_currency( $order, $currency ); + WC_Stripe_Order_Helper::update_stripe_currency( $order, $currency ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); @@ -1680,7 +1680,7 @@ public function save_intent_to_order( $order, $intent ) { } if ( 'payment_intent' === $intent->object ) { - WC_Stripe_Helper::add_payment_intent_to_order( $intent->id, $order ); + WC_Stripe_Order_Helper::add_payment_intent_to_order( $intent->id, $order ); // TODO: Refactor and add mandate ID support for other payment methods, if necessary. // The mandate ID is not available for the intent object, so we need to fetch the charge. diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 24211f9977..868faaa14b 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -559,8 +559,8 @@ public function display_order_fee( $order_id ) { $order = wc_get_order( $order_id ); - $fee = WC_Stripe_Helper::get_stripe_fee( $order ); - $currency = WC_Stripe_Helper::get_stripe_currency( $order ); + $fee = WC_Stripe_Order_Helper::get_stripe_fee( $order ); + $currency = WC_Stripe_Order_Helper::get_stripe_currency( $order ); if ( ! $fee || ! $currency ) { return; @@ -596,8 +596,8 @@ public function display_order_payout( $order_id ) { $order = wc_get_order( $order_id ); - $net = WC_Stripe_Helper::get_stripe_net( $order ); - $currency = WC_Stripe_Helper::get_stripe_currency( $order ); + $net = WC_Stripe_Order_Helper::get_stripe_net( $order ); + $currency = WC_Stripe_Order_Helper::get_stripe_currency( $order ); if ( ! $net || ! $currency ) { return; diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index dbb2371775..bc9d71199b 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1112,8 +1112,6 @@ public static function get_dynamic_statement_descriptor_suffix( $order ) { * @since 4.0.0 * @param string $statement_descriptor Statement descriptor. * @return string $statement_descriptor Sanitized statement descriptor. - * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::clean_statement_descriptor() instead. */ public static function clean_statement_descriptor( $statement_descriptor = '' ) { $disallowed_characters = [ '<', '>', '\\', '*', '"', "'", '/', '(', ')', '{', '}' ]; @@ -1988,7 +1986,7 @@ public static function validate_intent_for_order( $order, $intent, ?string $sele $is_valid_payment_type = empty( $selected_payment_type ) || ( ! empty( $intent->payment_method_types ) && in_array( $selected_payment_type, $intent->payment_method_types, true ) ); $order_currency = strtolower( $order->get_currency() ); $order_amount = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $order->get_currency() ); - $order_intent_id = self::get_intent_id_from_order( $order ); + $order_intent_id = WC_Stripe_Order_Helper::get_intent_id_from_order( $order ); if ( 'payment_intent' === $intent->object ) { $is_valid = $order_currency === $intent->currency diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index eede946884..a23244687f 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -409,7 +409,7 @@ public function update_payment_intent_ajax() { $save_payment_method = isset( $_POST['save_payment_method'] ) ? 'yes' === wc_clean( wp_unslash( $_POST['save_payment_method'] ) ) : false; $selected_upe_payment_type = ! empty( $_POST['selected_upe_payment_type'] ) ? wc_clean( wp_unslash( $_POST['selected_upe_payment_type'] ) ) : ''; - $order_from_payment = WC_Stripe_Helper::get_order_by_intent_id( $payment_intent_id ); + $order_from_payment = WC_Stripe_Order_Helper::get_order_by_intent_id( $payment_intent_id ); if ( ! $order_from_payment || $order_from_payment->get_id() !== $order_id ) { throw new Exception( __( 'Unable to verify your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); } @@ -465,7 +465,7 @@ public function update_intent( $intent_id = '', $order_id = null, $save_payment_ } $selected_payment_type = '' !== $selected_upe_payment_type && is_string( $selected_upe_payment_type ) ? $selected_upe_payment_type : null; - WC_Stripe_Helper::validate_intent_for_order( $order, $intent_id, $selected_payment_type ); + WC_Stripe_Order_Helper::validate_intent_for_order( $order, $intent_id, $selected_payment_type ); $gateway = $this->get_upe_gateway(); $amount = $order->get_total(); @@ -557,7 +557,7 @@ public function update_intent( $intent_id = '', $order_id = null, $save_payment_ $order->update_status( OrderStatus::PENDING, __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); } $order->save(); - WC_Stripe_Helper::add_payment_intent_to_order( $intent_id, $order ); + WC_Stripe_Order_Helper::add_payment_intent_to_order( $intent_id, $order ); } return [ @@ -724,7 +724,7 @@ public function update_failed_order_ajax() { $intent_id = isset( $_POST['intent_id'] ) ? wc_clean( wp_unslash( $_POST['intent_id'] ) ) : ''; $order = wc_get_order( $order_id ); - $order_from_payment = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); + $order_from_payment = WC_Stripe_Order_Helper::get_order_by_intent_id( $intent_id ); if ( ! $order_from_payment || $order_from_payment->get_id() !== $order_id ) { wp_send_json_error( __( 'Unable to verify your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); } diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 4760060003..50b0712829 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -471,7 +471,7 @@ public function prevent_cancelling_orders_awaiting_action( $cancel_order, $order } // Bail if payment method is not stripe or `stripe_{apm_method}` or doesn't have an intent yet. - if ( ! WC_Stripe_Helper::is_stripe_gateway_order( $order ) || ! $this->get_intent_from_order( $order ) ) { + if ( ! WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) || ! $this->get_intent_from_order( $order ) ) { return $cancel_order; } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 0058595ae6..0e9ebd234c 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -383,39 +383,7 @@ public static function get_dynamic_statement_descriptor_suffix( $order ) { } // Make sure that the prefix + suffix is limited at 22 characters. - return self::clean_statement_descriptor( substr( trim( $suffix ), 0, 22 - strlen( $prefix . '* ' ) ) ); - } - - /** - * Sanitize statement descriptor text. - * - * Stripe requires max of 22 characters and no special characters. - * - * @since 9.9.0 - * - * @param string $statement_descriptor Statement descriptor. - * @return string $statement_descriptor Sanitized statement descriptor. - */ - public static function clean_statement_descriptor( $statement_descriptor = '' ) { - $disallowed_characters = [ '<', '>', '\\', '*', '"', "'", '/', '(', ')', '{', '}' ]; - - // Strip any tags. - $statement_descriptor = strip_tags( $statement_descriptor ); - - // Strip any HTML entities. - // Props https://stackoverflow.com/questions/657643/how-to-remove-html-special-chars . - $statement_descriptor = preg_replace( '/&#?[a-z0-9]{2,8};/i', '', $statement_descriptor ); - - // Next, remove any remaining disallowed characters. - $statement_descriptor = str_replace( $disallowed_characters, '', $statement_descriptor ); - - // Remove non-Latin characters, excluding numbers, whitespaces and especial characters. - $statement_descriptor = preg_replace( '/[^a-zA-Z0-9\s\x{00C0}-\x{00FF}\p{P}]/u', '', $statement_descriptor ); - - // Trim any whitespace at the ends and limit to 22 characters. - $statement_descriptor = substr( trim( $statement_descriptor ), 0, 22 ); - - return $statement_descriptor; + return WC_Stripe_Helper::clean_statement_descriptor( substr( trim( $suffix ), 0, 22 - strlen( $prefix . '* ' ) ) ); } /** diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 83f27360cc..f24611c019 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -405,7 +405,7 @@ public function process_webhook_payment( $notification, $retry = true ) { * @param object $notification */ public function process_webhook_dispute( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $notification->data->object->charge ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge ); @@ -449,7 +449,7 @@ public function process_webhook_dispute( $notification ) { * @param object $notification */ public function process_webhook_dispute_closed( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $notification->data->object->charge ); $status = $notification->data->object->status; if ( ! $order ) { @@ -504,7 +504,7 @@ public function process_webhook_dispute_closed( $notification ) { * @param object $notification */ public function process_webhook_capture( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $notification->data->object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); @@ -579,7 +579,7 @@ public function process_webhook_charge_succeeded( $notification ) { return; } - $order = WC_Stripe_Helper::get_order_by_charge_id( $charge->id ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $charge->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $charge->id ); @@ -640,7 +640,7 @@ public function process_webhook_charge_succeeded( $notification ) { * @param object $notification */ public function process_webhook_charge_failed( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $notification->data->object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); @@ -678,11 +678,11 @@ public function process_webhook_charge_failed( $notification ) { * @param object $notification */ public function process_webhook_source_canceled( $notification ) { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $notification->data->object->id ); // If can't find order by charge ID, try source ID. if ( ! $order ) { - $order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id ); + $order = WC_Stripe_Order_Helper::get_order_by_source_id( $notification->data->object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge/source ID: ' . $notification->data->object->id ); @@ -718,11 +718,11 @@ public function process_webhook_source_canceled( $notification ) { */ public function process_webhook_refund( $notification ) { $refund_object = $this->get_refund_object( $notification ); - $order = WC_Stripe_Helper::get_order_by_refund_id( $refund_object->id ); + $order = WC_Stripe_Order_Helper::get_order_by_refund_id( $refund_object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via refund ID: ' . $refund_object->id ); - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $notification->data->object->id ); } // Set the order being processed for the `wc_stripe_webhook_received` action later. @@ -735,7 +735,7 @@ public function process_webhook_refund( $notification ) { $order_id = $order->get_id(); - if ( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ) { + if ( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $captured = $order->get_meta( '_stripe_charge_captured' ); $refund_id = $order->get_meta( '_stripe_refund_id' ); @@ -809,7 +809,7 @@ public function process_webhook_refund( $notification ) { */ public function process_webhook_refund_updated( $notification ) { $refund_object = $notification->data->object; - $order = WC_Stripe_Helper::get_order_by_charge_id( $refund_object->charge ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $refund_object->charge ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order to update refund via charge ID: ' . $refund_object->charge ); @@ -821,7 +821,7 @@ public function process_webhook_refund_updated( $notification ) { $order_id = $order->get_id(); - if ( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ) { + if ( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $refund_id = $order->get_meta( '_stripe_refund_id' ); $currency = $order->get_currency(); @@ -907,14 +907,14 @@ public function process_webhook_refund_updated( $notification ) { */ public function process_review_opened( $notification ) { if ( isset( $notification->data->object->payment_intent ) ) { - $order = WC_Stripe_Helper::get_order_by_intent_id( $notification->data->object->payment_intent ); + $order = WC_Stripe_Order_Helper::get_order_by_intent_id( $notification->data->object->payment_intent ); if ( ! $order ) { WC_Stripe_Logger::log( '[Review Opened] Could not find order via intent ID: ' . $notification->data->object->payment_intent ); return; } } else { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $notification->data->object->charge ); if ( ! $order ) { WC_Stripe_Logger::log( '[Review Opened] Could not find order via charge ID: ' . $notification->data->object->charge ); @@ -951,14 +951,14 @@ public function process_review_opened( $notification ) { */ public function process_review_closed( $notification ) { if ( isset( $notification->data->object->payment_intent ) ) { - $order = WC_Stripe_Helper::get_order_by_intent_id( $notification->data->object->payment_intent ); + $order = WC_Stripe_Order_Helper::get_order_by_intent_id( $notification->data->object->payment_intent ); if ( ! $order ) { WC_Stripe_Logger::log( '[Review Closed] Could not find order via intent ID: ' . $notification->data->object->payment_intent ); return; } } else { - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge ); + $order = WC_Stripe_Order_Helper::get_order_by_charge_id( $notification->data->object->charge ); if ( ! $order ) { WC_Stripe_Logger::log( '[Review Closed] Could not find order via charge ID: ' . $notification->data->object->charge ); @@ -1190,7 +1190,7 @@ public function process_payment_intent( $notification ) { public function process_setup_intent( $notification ) { $intent = $notification->data->object; - $order = WC_Stripe_Helper::get_order_by_setup_intent_id( $intent->id ); + $order = WC_Stripe_Order_Helper::get_order_by_setup_intent_id( $intent->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via setup intent ID: ' . $intent->id ); @@ -1537,7 +1537,7 @@ private function get_order_from_intent( $intent ) { } // Fall back to finding the order via the intent ID. - return WC_Stripe_Helper::get_order_by_intent_id( $intent->id ); + return WC_Stripe_Order_Helper::get_order_by_intent_id( $intent->id ); } /** diff --git a/includes/compat/class-wc-stripe-subscriptions-helper.php b/includes/compat/class-wc-stripe-subscriptions-helper.php index e30a879b86..2bc9b61962 100644 --- a/includes/compat/class-wc-stripe-subscriptions-helper.php +++ b/includes/compat/class-wc-stripe-subscriptions-helper.php @@ -151,7 +151,7 @@ public static function is_subscription_payment_method_detached( $subscription ) return false; } - if ( ! WC_Stripe_Helper::is_stripe_gateway_order( $subscription ) ) { + if ( ! WC_Stripe_Order_Helper::is_stripe_gateway_order( $subscription ) ) { // If the payment method is not a Stripe method, we don't need to check further. return false; } diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 6e5b543743..c5a4be5067 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -657,8 +657,8 @@ public function delete_resubscribe_meta( $resubscribe_order ) { * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription */ public function delete_renewal_meta( $renewal_order ) { - WC_Stripe_Helper::delete_stripe_fee( $renewal_order ); - WC_Stripe_Helper::delete_stripe_net( $renewal_order ); + WC_Stripe_Order_Helper::delete_stripe_fee( $renewal_order ); + WC_Stripe_Order_Helper::delete_stripe_net( $renewal_order ); // Delete payment intent ID. $renewal_order->delete_meta_data( '_stripe_intent_id' ); diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 95e73a3423..0e1b0eba9d 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -920,7 +920,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // Use the dynamic + short statement descriptor if enabled and it's a card payment. if ( WC_Stripe_Payment_Methods::CARD === $selected_upe_payment_type && $is_short_statement_descriptor_enabled ) { - $request['statement_descriptor_suffix'] = WC_Stripe_Helper::get_dynamic_statement_descriptor_suffix( $order ); + $request['statement_descriptor_suffix'] = WC_Stripe_Order_Helper::get_dynamic_statement_descriptor_suffix( $order ); } $customer = $this->get_stripe_customer_from_order( $order ); @@ -972,7 +972,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = null // $prepared_source parameter is not necessary for adding mandate information. ); - WC_Stripe_Helper::add_payment_intent_to_order( $payment_intent_id, $order ); + WC_Stripe_Order_Helper::add_payment_intent_to_order( $payment_intent_id, $order ); $order->update_status( OrderStatus::PENDING, __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); $order->update_meta_data( '_stripe_upe_payment_type', $selected_upe_payment_type ); @@ -1589,7 +1589,7 @@ public function maybe_process_upe_redirect() { * @return bool */ private function is_order_associated_to_payment_intent( int $order_id, string $intent_id ): bool { - $order_from_payment_intent = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); + $order_from_payment_intent = WC_Stripe_Order_Helper::get_order_by_intent_id( $intent_id ); return $order_from_payment_intent && $order_from_payment_intent->get_id() === $order_id; } @@ -1708,7 +1708,7 @@ public function process_order_for_confirmed_intent( $order, $intent_id, $save_pa // Validates the intent can be applied to the order. try { - WC_Stripe_Helper::validate_intent_for_order( $order, $intent ); + WC_Stripe_Order_Helper::validate_intent_for_order( $order, $intent ); } catch ( Exception $e ) { throw new Exception( __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); } @@ -2532,7 +2532,7 @@ protected function prepare_payment_information_from_request( WC_Order $order ) { // Use the dynamic + short statement descriptor if enabled and it's a card payment. $is_short_statement_descriptor_enabled = 'yes' === $this->get_option( 'is_short_statement_descriptor_enabled', 'no' ); if ( WC_Stripe_Payment_Methods::CARD === $selected_payment_type && $is_short_statement_descriptor_enabled ) { - $payment_information['statement_descriptor_suffix'] = WC_Stripe_Helper::get_dynamic_statement_descriptor_suffix( $order ); + $payment_information['statement_descriptor_suffix'] = WC_Stripe_Order_Helper::get_dynamic_statement_descriptor_suffix( $order ); } if ( empty( $payment_method_id ) && ! empty( $_POST['wc-stripe-confirmation-token'] ) ) { diff --git a/tests/phpunit/WC_Stripe_Helper_Test.php b/tests/phpunit/WC_Stripe_Helper_Test.php index 4d09f87802..cfdbb489e4 100644 --- a/tests/phpunit/WC_Stripe_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Helper_Test.php @@ -7,6 +7,7 @@ use WC_Order; use WC_Stripe_Currency_Code; use WC_Stripe_Helper; +use WC_Stripe_Order_Helper; use WC_Stripe_Payment_Methods; use WooCommerce\Stripe\Tests\Helpers\UPE_Test_Helper; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; @@ -218,7 +219,7 @@ public function test_get_order_by_intent_id( $status, $success ) { $intent_id = 'pi_mock'; update_post_meta( $order_id, '_stripe_intent_id', $intent_id ); - $order = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); + $order = WC_Stripe_Order_Helper::get_order_by_intent_id( $intent_id ); if ( $success ) { $this->assertInstanceOf( WC_Order::class, $order ); } else { diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index 48dd0cd2b7..afb66a5c80 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -7,6 +7,7 @@ use WC_Order; use WC_Stripe_Helper; use WC_Stripe_Intent_Status; +use WC_Stripe_Order_Helper; use WC_Stripe_Payment_Methods; use WC_Stripe_Webhook_Handler; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; @@ -580,7 +581,7 @@ public function test_process_webhook_payment_intent_processing() { // Order must be previously set to pending and have at least the payment intent set. $order = WC_Helper_Order::create_order(); - WC_Stripe_Helper::add_payment_intent_to_order( $notification->data->object->id, $order ); + WC_Stripe_Order_Helper::add_payment_intent_to_order( $notification->data->object->id, $order ); $order->set_status( OrderStatus::PENDING ); $order->save(); From c8a4fa8bc881dffbe50825205ee1b7ff4c11e5ac Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 20:00:51 -0300 Subject: [PATCH 06/71] Unit test class --- tests/phpunit/WC_Stripe_Helper_Test.php | 21 ------------ tests/phpunit/WC_Stripe_Order_Helper_Test.php | 32 +++++++++++++++++++ 2 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 tests/phpunit/WC_Stripe_Order_Helper_Test.php diff --git a/tests/phpunit/WC_Stripe_Helper_Test.php b/tests/phpunit/WC_Stripe_Helper_Test.php index cfdbb489e4..a651e40fc4 100644 --- a/tests/phpunit/WC_Stripe_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Helper_Test.php @@ -896,25 +896,4 @@ public function is_webhook_url_provider() { 'webhook URL with extra parameters should match' => [ 'https://example.com/test/?wc-api=wc_stripe&foo=bar', 'https://example.com/test/?wc-api=wc_stripe', true ], ]; } - - /** - * Tests for `is_stripe_gateway_order`. - * - * @return void - */ - public function test_is_stripe_gateway_order() { - // Test with a Stripe order (Klarna). - $order = WC_Helper_Order::create_order(); - $order->set_payment_method( 'stripe_klarna' ); - $this->assertTrue( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ); - - // Test with a non-Stripe order. - $order = WC_Helper_Order::create_order(); - $order->set_payment_method( 'cod' ); - $this->assertFalse( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ); - - // Test with an empty order. - $order = new WC_Order(); - $this->assertFalse( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ); - } } diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php new file mode 100644 index 0000000000..c418bcc64e --- /dev/null +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -0,0 +1,32 @@ +set_payment_method( 'stripe_klarna' ); + $this->assertTrue( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ); + + // Test with a non-Stripe order. + $order = WC_Helper_Order::create_order(); + $order->set_payment_method( 'cod' ); + $this->assertFalse( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ); + + // Test with an empty order. + $order = new WC_Order(); + $this->assertFalse( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ); + } +} From ac3638b2595649757b86b018aff933fbbb00c61c Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 20:01:57 -0300 Subject: [PATCH 07/71] Including class --- includes/class-wc-stripe.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/class-wc-stripe.php b/includes/class-wc-stripe.php index 1e2037d0d4..3eca92633e 100644 --- a/includes/class-wc-stripe.php +++ b/includes/class-wc-stripe.php @@ -125,6 +125,7 @@ public function init() { require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-exception.php'; require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-logger.php'; require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-helper.php'; + include_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-order-helper.php'; require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-database-cache.php'; require_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-payment-method-configurations.php'; include_once WC_STRIPE_PLUGIN_PATH . '/includes/class-wc-stripe-api.php'; From 603aeab0256d42f2dfe396622f58bf7311cbc6ad Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 20:03:50 -0300 Subject: [PATCH 08/71] Tests update --- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index c418bcc64e..dde67bf069 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -2,17 +2,23 @@ namespace WooCommerce\Stripe\Tests; +use WC_Data_Exception; use WC_Stripe_Order_Helper; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; /** - * Class WC_Stripe_Order_Handler tests. + * Class WC_Stripe_Order_Helper + * + * @package WooCommerce/Stripe/WC_Stripe_Order_Helper + * + * Class WC_Stripe_Order_Helper tests. */ class WC_Stripe_Order_Handler_Test extends WP_UnitTestCase { /** * Tests for `is_stripe_gateway_order`. * * @return void + * @throws WC_Data_Exception */ public function test_is_stripe_gateway_order() { // Test with a Stripe order (Klarna). From 1bf7c576ca705db38faddd51b038a021f8342919 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 20:12:27 -0300 Subject: [PATCH 09/71] Tests update --- includes/class-wc-gateway-stripe.php | 4 ++-- includes/class-wc-stripe-intent-controller.php | 4 ++-- includes/class-wc-stripe-webhook-handler.php | 4 ++-- .../payment-methods/class-wc-stripe-upe-payment-gateway.php | 4 ++-- tests/phpunit/WC_Stripe_Order_Handler_Test.php | 3 ++- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 868faaa14b..accbfd4700 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -486,7 +486,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $this->unlock_order_payment( $order ); // If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings. - WC_Stripe_Helper::set_payment_awaiting_action( $order ); + WC_Stripe_Order_Helper::set_payment_awaiting_action( $order ); if ( is_wc_endpoint_url( 'order-pay' ) ) { $redirect_url = add_query_arg( 'wc-stripe-confirmation', 1, $order->get_checkout_payment_url( false ) ); @@ -922,7 +922,7 @@ public function verify_intent_after_checkout( $order ) { * This meta is to prevent stores with short hold stock settings from cancelling orders while waiting for payment to be finalised by Stripe or the customer (i.e. completing 3DS or payment redirects). * Now that payment is confirmed, we can remove this meta. */ - WC_Stripe_Helper::remove_payment_awaiting_action( $order ); + WC_Stripe_Order_Helper::remove_payment_awaiting_action( $order ); } /** diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index a23244687f..352b2ae21c 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -663,7 +663,7 @@ public function update_order_status_ajax() { throw new WC_Stripe_Exception( 'order_not_found', __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); } - $intent_id = WC_Stripe_Helper::get_intent_id_from_order( $order ); + $intent_id = WC_Stripe_Order_Helper::get_intent_id_from_order( $order ); $intent_id_received = isset( $_POST['intent_id'] ) ? wc_clean( wp_unslash( $_POST['intent_id'] ) ) : null; if ( empty( $intent_id_received ) || $intent_id_received !== $intent_id ) { $note = sprintf( @@ -691,7 +691,7 @@ public function update_order_status_ajax() { /* translators: error message */ if ( $order ) { // Remove the awaiting confirmation order meta, don't save the order since it'll be saved in the next `update_status()` call. - WC_Stripe_Helper::remove_payment_awaiting_action( $order, false ); + WC_Stripe_Order_Helper::remove_payment_awaiting_action( $order, false ); $order->update_status( OrderStatus::FAILED ); } diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index f24611c019..f66bb50c11 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -269,7 +269,7 @@ public function process_webhook_payment( $notification, $retry = true ) { return; } - $order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id ); + $order = WC_Stripe_Order_Helper::get_order_by_source_id( $notification->data->object->id ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via source ID: ' . $notification->data->object->id ); @@ -1506,7 +1506,7 @@ private function get_order_from_intent( $intent ) { $order = isset( $data[0], $data[1] ) ? wc_get_order( absint( $data[0] ) ) : false; if ( $order ) { - $intent_id = WC_Stripe_Helper::get_intent_id_from_order( $order ); + $intent_id = WC_Stripe_Order_Helper::get_intent_id_from_order( $order ); // Return the order if the intent ID matches. if ( $intent->id === $intent_id ) { diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 0e1b0eba9d..004d69e797 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1172,7 +1172,7 @@ private function process_payment_with_payment_method( int $order_id ) { } // If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings. - WC_Stripe_Helper::set_payment_awaiting_action( $order, false ); + WC_Stripe_Order_Helper::set_payment_awaiting_action( $order, false ); // Prevent processing the payment intent webhooks while also processing the redirect payment (also prevents duplicate Stripe meta stored on the order). $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); @@ -1774,7 +1774,7 @@ public function process_order_for_confirmed_intent( $order, $intent_id, $save_pa * This meta is to prevent stores with short hold stock settings from cancelling orders while waiting for payment to be finalised by Stripe or the customer (i.e. completing 3DS or payment redirects). * Now that payment is confirmed, we can remove this meta. */ - WC_Stripe_Helper::remove_payment_awaiting_action( $order, false ); + WC_Stripe_Order_Helper::remove_payment_awaiting_action( $order, false ); $order->save(); } diff --git a/tests/phpunit/WC_Stripe_Order_Handler_Test.php b/tests/phpunit/WC_Stripe_Order_Handler_Test.php index 8fb94337cf..af5d20d432 100644 --- a/tests/phpunit/WC_Stripe_Order_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Handler_Test.php @@ -3,6 +3,7 @@ namespace WooCommerce\Stripe\Tests; use DateTime; +use WC_Stripe_Order_Helper; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; use WC_Stripe_Helper; use WC_Stripe_Order_Handler; @@ -29,7 +30,7 @@ public function set_up() { public function test_prevent_cancelling_orders_awaiting_action() { $order = WC_Helper_Order::create_order(); - WC_Stripe_Helper::set_payment_awaiting_action( $order ); + WC_Stripe_Order_Helper::set_payment_awaiting_action( $order ); // Read in a fresh order object with meta like `date_modified` set. $order = wc_get_order( $order->get_id() ); From c68f45b8df15ee67a35bbf8a26ba916db8ab21b2 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 20:21:01 -0300 Subject: [PATCH 10/71] Replace meta key reference --- includes/class-wc-stripe-order-helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 0e9ebd234c..37376447bb 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -396,10 +396,10 @@ public static function get_dynamic_statement_descriptor_suffix( $order ) { * @return string|bool The intent ID if found, false otherwise. */ public static function get_intent_id_from_order( $order ) { - $intent_id = $order->get_meta( '_stripe_intent_id' ); + $intent_id = $order->get_meta( self::META_STRIPE_INTENT_ID ); if ( ! $intent_id ) { - $intent_id = $order->get_meta( '_stripe_setup_intent' ); + $intent_id = $order->get_meta( self::META_STRIPE_SETUP_INTENT ); } return $intent_id ?? false; From a754c8842bba6e4d1cfbf9b0dc3c82560c61c056 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 20:22:00 -0300 Subject: [PATCH 11/71] Changelog and readme entries --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index 42b89e1aa4..2c72cfe638 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 9.9.0 - xxxx-xx-xx = +* Dev - Introduces a new helper class to handle Stripe orders * Dev - Introduces a new helper method to identify Stripe orders * Add - Setting to allow merchants to control the layout of the Optimized Checkout payment element on the checkout page * Fix - Removes the credit card payment method requirement for the Optimized Checkout feature diff --git a/readme.txt b/readme.txt index ba4da5bcce..f49b1aca69 100644 --- a/readme.txt +++ b/readme.txt @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o == Changelog == = 9.9.0 - xxxx-xx-xx = +* Dev - Introduces a new helper class to handle Stripe orders * Dev - Introduces a new helper method to identify Stripe orders * Add - Setting to allow merchants to control the layout of the Optimized Checkout payment element on the checkout page * Fix - Removes the credit card payment method requirement for the Optimized Checkout feature From 5748edce91b4c2615de88182de76d804ede7e19d Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 20:27:07 -0300 Subject: [PATCH 12/71] Fix test class name --- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index dde67bf069..5a956d9b42 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -13,7 +13,7 @@ * * Class WC_Stripe_Order_Helper tests. */ -class WC_Stripe_Order_Handler_Test extends WP_UnitTestCase { +class WC_Stripe_Order_Helper_Test extends WP_UnitTestCase { /** * Tests for `is_stripe_gateway_order`. * From bd49443f1340cd221b1712e5f11174556083969e Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 20:30:28 -0300 Subject: [PATCH 13/71] Fix test class name --- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index 5a956d9b42..e4ca237897 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -2,6 +2,8 @@ namespace WooCommerce\Stripe\Tests; +use WC_Order; +use WP_UnitTestCase; use WC_Data_Exception; use WC_Stripe_Order_Helper; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; From bff2519cc55c80fbaafc076c3773bf55a8a8ede1 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 20:53:15 -0300 Subject: [PATCH 14/71] validate_minimum_order_amount --- .../abstract-wc-stripe-payment-gateway.php | 2 + includes/class-wc-gateway-stripe.php | 2 +- includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-order-helper.php | 14 ++ includes/class-wc-stripe-webhook-handler.php | 2 +- .../compat/trait-wc-stripe-pre-orders.php | 2 +- .../class-wc-gateway-stripe-alipay.php | 2 +- .../class-wc-gateway-stripe-bancontact.php | 2 +- .../class-wc-gateway-stripe-eps.php | 2 +- .../class-wc-gateway-stripe-giropay.php | 2 +- .../class-wc-gateway-stripe-ideal.php | 2 +- .../class-wc-gateway-stripe-multibanco.php | 2 +- .../class-wc-gateway-stripe-p24.php | 2 +- .../class-wc-gateway-stripe-sepa.php | 2 +- .../class-wc-gateway-stripe-sofort.php | 2 +- .../class-wc-stripe-upe-payment-gateway.php | 4 +- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 207 ++++++++++++++++++ 17 files changed, 238 insertions(+), 15 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 05030b0588..ebb6eccb6b 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -380,6 +380,8 @@ public function payment_icons() { * @since 4.0.0 * @version 4.0.0 * @param object $order + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::validate_minimum_order_amount() instead. */ public function validate_minimum_order_amount( $order ) { if ( $order->get_total() * 100 < WC_Stripe_Helper::get_minimum_amount() ) { diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index accbfd4700..426333452d 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -443,7 +443,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = } // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); WC_Stripe_Logger::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 50b0712829..b3a5cda622 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -114,7 +114,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er $response = null; // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); WC_Stripe_Logger::log( "Info: (Redirect) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 37376447bb..673aafb4fa 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -506,6 +506,20 @@ public static function is_stripe_gateway_order( $order ) { return WC_Gateway_Stripe::ID === substr( (string) $order->get_payment_method(), 0, 6 ); } + /** + * Validates that the order meets the minimum order amount + * set by Stripe. + * + * @since 9.9.0 + * @param WC_Order $order + */ + public static function validate_minimum_order_amount( $order ) { + if ( $order->get_total() * 100 < WC_Stripe_Helper::get_minimum_amount() ) { + /* translators: 1) amount (including currency symbol) */ + throw new WC_Stripe_Exception( 'Did not meet minimum amount', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe_Helper::get_minimum_amount() / 100 ) ) ); + } + } + /** * Queries for an order by a specific meta key and value. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index f66bb50c11..4c8f877fdf 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -300,7 +300,7 @@ public function process_webhook_payment( $notification, $retry = true ) { $response = null; // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); WC_Stripe_Logger::log( "Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); diff --git a/includes/compat/trait-wc-stripe-pre-orders.php b/includes/compat/trait-wc-stripe-pre-orders.php index bc5946abec..a54547a2c5 100644 --- a/includes/compat/trait-wc-stripe-pre-orders.php +++ b/includes/compat/trait-wc-stripe-pre-orders.php @@ -203,7 +203,7 @@ public function process_pre_order( $order_id ) { $order = wc_get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); // @phpstan-ignore-line (minimum amount is defined in the classes that use this trait) + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // @phpstan-ignore-line (minimum amount is defined in the classes that use this trait) $prepared_source = $this->prepare_source( get_current_user_id(), true ); // @phpstan-ignore-line (prepare_source is defined in the classes that use this trait) diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index d771d4dbd8..0c9dcf5765 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -258,7 +258,7 @@ public function process_payment( $order_id, $retry = true, $force_save_save = fa $order = wc_get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index 6ce7d793ca..f4ff6dd89c 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -246,7 +246,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index cf878df4c5..49c1cbb49c 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -245,7 +245,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index f2753048e9..f83ab37ad3 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -241,7 +241,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index 82d79045cd..ebb61f2e87 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -245,7 +245,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index 3f7c9f8527..e6d5a80224 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -349,7 +349,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index 8c3eab980a..541b75f61f 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -242,7 +242,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-sepa.php b/includes/payment-methods/class-wc-gateway-stripe-sepa.php index e924f9d716..9d02fe222c 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sepa.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sepa.php @@ -315,7 +315,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = if ( $order->get_total() > 0 ) { // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); WC_Stripe_Logger::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index c916d5248a..ef99823dc9 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -250,7 +250,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 004d69e797..945909e25f 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1107,7 +1107,7 @@ private function process_payment_with_payment_method( int $order_id ) { if ( $payment_needed ) { // Throw an exception if the minimum order amount isn't met. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); // Create a payment intent, or update an existing one associated with the order. $payment_intent = $this->process_payment_intent_for_order( $order, $payment_information ); @@ -1361,7 +1361,7 @@ public function process_payment_with_saved_payment_method( $order_id, $can_retry if ( $payment_needed ) { // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); $request_details = $this->generate_payment_request( $order, $prepared_payment_method ); $endpoint = false !== $intent ? "payment_intents/$intent->id" : 'payment_intents'; diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index e4ca237897..8f30d62efe 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -2,7 +2,9 @@ namespace WooCommerce\Stripe\Tests; +use Automattic\WooCommerce\Enums\OrderStatus; use WC_Order; +use WC_Stripe_Exception; use WP_UnitTestCase; use WC_Data_Exception; use WC_Stripe_Order_Helper; @@ -16,6 +18,211 @@ * Class WC_Stripe_Order_Helper tests. */ class WC_Stripe_Order_Helper_Test extends WP_UnitTestCase { + /** + * Tests for `query`, `get_by_order_source_id`, `get_by_order_charge_id`, `get_by_order_refund_id`, + * `get_by_order_intent_id`, and `get_by_order_setup_intent_id`. + * + * @return void + */ + public function test_retrieve() { + // setup + $source_id = 'src_123'; + $charge_id = 'ch_123'; + $refund_id = 're_123'; + $intent_id = 'pi_123'; + $setup_intent_id = 'seti_123'; + + $order = WC_Helper_Order::create_order(); + + $order->set_source_id( $source_id ); + $order->set_transaction_id( $charge_id ); + $order->set_refund_id( $refund_id ); + $order->set_intent_id( $intent_id ); + $order->set_setup_intent( $setup_intent_id ); + $order->save_meta_data(); + $order->save(); + + // query + $orders = WC_Stripe_Order::query( [ 'status' => OrderStatus::PENDING ] ); + $this->assertEquals( $order, $orders[0] ); + + // get_order_by_source_id + $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_source_id( $source_id ) ); + + // get_order_by_charge_id + $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_charge_id( $charge_id ) ); + + // get_order_by_refund_id + $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_refund_id( $refund_id ) ); + + // get_order_by_intent_id + $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_intent_id( $intent_id ) ); + + // get_order_by_setup_intent_id + $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_setup_intent_id( $setup_intent_id ) ); + } + + /** + * Tests for getters and setters. + * + * @return void + */ + public function test_properties() { + $order = WC_Helper_Order::create_order(); + + $order->set_source_id( 'src_123' ); + $order->set_transaction_id( 'ch_123' ); + $order->set_refund_id( 're_123' ); + $order->set_intent_id( 'pi_123' ); + $order->set_setup_intent( 'seti_123' ); + $order->set_stripe_currency( 'usd' ); + $order->set_card_brand( 'visa' ); + $order->set_status_before_hold( OrderStatus::PENDING ); + $order->set_mandate_id( 'mandate_123' ); + $order->set_upe_payment_type( 'card' ); + $order->set_stripe_customer_id( 'cus_123' ); + $order->set_multibanco_data( + [ + 'entity' => '123', + 'reference' => '123', + 'amount' => 100, + ] + ); + $order->set_upe_redirect_processed( true ); + $order->set_upe_waiting_for_redirect( true ); + $order->set_charge_captured( true ); + $order->set_status_final( true ); + + $order->set_fee( 100 ); + $order->set_net( 100 ); + + $order->save_meta_data(); + $order->save(); + + $this->assertEquals( 'src_123', $order->get_source_id() ); + $this->assertEquals( 'ch_123', $order->get_transaction_id() ); + $this->assertEquals( 're_123', $order->get_refund_id() ); + $this->assertEquals( 'pi_123', $order->get_intent_id() ); + $this->assertEquals( 'seti_123', $order->get_setup_intent() ); + $this->assertEquals( 'usd', $order->get_stripe_currency() ); + $this->assertEquals( 'visa', $order->get_card_brand() ); + $this->assertEquals( OrderStatus::PENDING, $order->get_status_before_hold() ); + $this->assertEquals( 'mandate_123', $order->get_mandate_id() ); + $this->assertEquals( 'card', $order->get_upe_payment_type() ); + $this->assertEquals( 'cus_123', $order->get_stripe_customer_id() ); + $this->assertEquals( + [ + 'entity' => '123', + 'reference' => '123', + 'amount' => 100, + ], + $order->get_multibanco_data() + ); + $this->assertTrue( $order->is_upe_redirect_processed() ); + $this->assertTrue( $order->is_upe_waiting_for_redirect() ); + $this->assertTrue( $order->is_charge_captured() ); + $this->assertTrue( $order->is_status_final() ); + + // Tests for `get_payment_awaiting_action`, `set_payment_awaiting_action`, and `remove_payment_awaiting_action`. + $order->set_payment_awaiting_action( true ); + $this->assertTrue( $order->is_payment_awaiting_action() ); + + $order->remove_payment_awaiting_action( true ); + $this->assertFalse( $order->is_payment_awaiting_action() ); + + $this->assertEquals( 100, $order->get_fee() ); + $this->assertEquals( 100, $order->get_net() ); + + $order->delete_fee(); + $order->delete_net(); + $order->save_meta_data(); + + $this->assertEmpty( $order->get_fee() ); + $this->assertEmpty( $order->get_net() ); + } + + /** + * Tests for `lock_refund`, `get_lock_refund`, `unlock_refund`, `lock_payment`, `get_lock_payment`, and `unlock_payment`. + * + * @return void + */ + public function test_lockers() { + // setup + $order = WC_Helper_Order::create_order(); + + // refund + $order->lock_refund(); + $this->assertTrue( $order->get_lock_refund() > 0 ); + $order->unlock_refund(); + $this->assertEmpty( $order->get_lock_refund() ); + + // payment + $order->lock_payment(); + $this->assertTrue( $order->get_lock_payment() > 0 ); + $order->unlock_payment(); + $this->assertEmpty( $order->get_lock_payment() ); + } + + /** + * Tests for `add_payment_intent_to_order`. + * + * @return void + */ + public function test_add_payment_intent_to_order() { + // setup + $order = WC_Helper_Order::create_order(); + $order_id = $order->get_id(); + + // add_payment_intent_to_order + $intent_id = 'pi_123'; + $order->add_payment_intent_to_order( $intent_id ); + $this->assertEquals( $intent_id, $order->get_intent_id() ); + + $note = wc_get_order_notes( + [ + 'order_id' => $order_id, + 'limit' => 1, + ] + )[0]; + $this->assertStringContainsString( 'Stripe payment intent created (Payment Intent ID: pi_123)', $note->content ); + } + + /** + * Test for `validate_minimum_amount`. + * + * @return void + */ + public function test_validate_minimum_amount() { + $order = WC_Helper_Order::create_order(); + $order->set_total( 0.01 ); + $order->save(); + + $this->expectException( WC_Stripe_Exception::class ); + $this->expectExceptionMessage( 'Did not meet minimum amount' ); + + $order->validate_minimum_amount(); + } + + /** + * Tests for `get_owner_details`. + * + * @return void + */ + public function test_get_owner_details() { + $order = WC_Helper_Order::create_order(); + $order->set_billing_phone( '+1 123 1234' ); + $order->set_billing_first_name( 'John' ); + $order->set_billing_last_name( 'Doe' ); + $order->set_billing_email( 'test@example.com' ); + $order->save_meta_data(); + + $owner_details = $order->get_owner_details(); + + $this->assertEquals( '+1 123 1234', $owner_details->phone ); + $this->assertEquals( 'John Doe', $owner_details->name ); + $this->assertEquals( 'test@example.com', $owner_details->email ); + } + /** * Tests for `is_stripe_gateway_order`. * From 3875e7c7c37fe194fdefa4d589737eba6823a1cd Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 21 Aug 2025 21:03:46 -0300 Subject: [PATCH 15/71] Fix tests --- .../abstract-wc-stripe-payment-gateway.php | 4 ++ includes/class-wc-stripe-order-helper.php | 67 +++++++++++++++++++ .../class-wc-gateway-stripe-alipay.php | 2 +- .../class-wc-gateway-stripe-bancontact.php | 2 +- .../class-wc-gateway-stripe-eps.php | 2 +- .../class-wc-gateway-stripe-giropay.php | 2 +- .../class-wc-gateway-stripe-ideal.php | 2 +- .../class-wc-gateway-stripe-multibanco.php | 2 +- .../class-wc-gateway-stripe-p24.php | 2 +- .../class-wc-gateway-stripe-sofort.php | 2 +- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 28 ++++---- 11 files changed, 93 insertions(+), 22 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index ebb6eccb6b..0af9181e96 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -426,6 +426,8 @@ public function get_stripe_customer_id( $order ) { * @version 4.0.0 * @param object $order * @param int $id Stripe session id. + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_stripe_return_url() instead. */ public function get_stripe_return_url( $order = null, $id = null ) { if ( is_object( $order ) ) { @@ -725,6 +727,8 @@ public function send_failed_refund_emails( $order ) { * @version 4.0.0 * @param object $order * @return object $details + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_owner_details() instead. */ public function get_owner_details( $order ) { $billing_first_name = $order->get_billing_first_name(); diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 673aafb4fa..371fc4e932 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -405,6 +405,46 @@ public static function get_intent_id_from_order( $order ) { return $intent_id ?? false; } + /** + * Get owner details. + * + * @since 9.9.0 + * + * @param object $order + * @return object $details + */ + public static function get_owner_details( $order ) { + $billing_first_name = $order->get_billing_first_name(); + $billing_last_name = $order->get_billing_last_name(); + + $details = []; + + $name = $billing_first_name . ' ' . $billing_last_name; + $email = $order->get_billing_email(); + $phone = $order->get_billing_phone(); + + if ( ! empty( $phone ) ) { + $details['phone'] = $phone; + } + + if ( ! empty( $name ) ) { + $details['name'] = $name; + } + + if ( ! empty( $email ) ) { + $details['email'] = $email; + } + + $details['address']['line1'] = $order->get_billing_address_1(); + $details['address']['line2'] = $order->get_billing_address_2(); + $details['address']['state'] = $order->get_billing_state(); + $details['address']['city'] = $order->get_billing_city(); + $details['address']['postal_code'] = $order->get_billing_postcode(); + $details['address']['country'] = $order->get_billing_country(); + + return (object) apply_filters( 'wc_stripe_owner_details', $details, $order ); + } + /** * Checks if the given payment intent is valid for the order. * This checks the currency, amount, and payment method types. @@ -520,6 +560,33 @@ public static function validate_minimum_order_amount( $order ) { } } + /** + * Builds the return URL from redirects. + * + * @since 9.9.0 + * + * @param object $order + * @param int $id Stripe session id. + */ + public function get_stripe_return_url( $order = null, $id = null ) { + if ( is_object( $order ) ) { + if ( empty( $id ) ) { + $id = uniqid(); + } + + $order_id = $order->get_id(); + + $args = [ + 'utm_nooverride' => '1', + 'order_id' => $order_id, + ]; + + return wp_sanitize_redirect( esc_url_raw( add_query_arg( $args, $this->get_return_url( $order ) ) ) ); + } + + return wp_sanitize_redirect( esc_url_raw( add_query_arg( [ 'utm_nooverride' => '1' ], $this->get_return_url() ) ) ); + } + /** * Queries for an order by a specific meta key and value. * diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index 0c9dcf5765..4de02f3b0c 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -230,7 +230,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::ALIPAY; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index f4ff6dd89c..1958f1cec6 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -217,7 +217,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::BANCONTACT; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; $post_data['bancontact'] = [ 'preferred_language' => $this->get_locale() ]; diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index 49c1cbb49c..4cffd45a52 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -217,7 +217,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::EPS; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index f83ab37ad3..d0b13dac63 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -213,7 +213,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::GIROPAY; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index ebb61f2e87..b2e568b103 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -217,7 +217,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::IDEAL; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index e6d5a80224..43652569eb 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -321,7 +321,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::MULTIBANCO; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index 541b75f61f..b5fc235e8d 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -218,7 +218,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::P24; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; WC_Stripe_Logger::log( 'Info: Begin creating P24 source' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index ef99823dc9..31c15c0a06 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -218,7 +218,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::SOFORT; - $post_data['owner'] = $this->get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; $post_data['sofort'] = [ 'country' => $bank_country, diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index 8f30d62efe..f968976ba1 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -124,21 +124,21 @@ public function test_properties() { $this->assertTrue( $order->is_status_final() ); // Tests for `get_payment_awaiting_action`, `set_payment_awaiting_action`, and `remove_payment_awaiting_action`. - $order->set_payment_awaiting_action( true ); + WC_Stripe_Order_Helper::set_payment_awaiting_action( $order ); $this->assertTrue( $order->is_payment_awaiting_action() ); - $order->remove_payment_awaiting_action( true ); + WC_Stripe_Order_Helper::remove_payment_awaiting_action( $order ); $this->assertFalse( $order->is_payment_awaiting_action() ); - $this->assertEquals( 100, $order->get_fee() ); - $this->assertEquals( 100, $order->get_net() ); + $this->assertEquals( 100, WC_Stripe_Order_Helper::get_stripe_fee( $order ) ); + $this->assertEquals( 100, WC_Stripe_Order_Helper::get_stripe_net( $order ) ); - $order->delete_fee(); - $order->delete_net(); + WC_Stripe_Order_Helper::delete_stripe_fee(); + WC_Stripe_Order_Helper::delete_stripe_net(); $order->save_meta_data(); - $this->assertEmpty( $order->get_fee() ); - $this->assertEmpty( $order->get_net() ); + $this->assertEmpty( WC_Stripe_Order_Helper::get_stripe_fee( $order ) ); + $this->assertEmpty( WC_Stripe_Order_Helper::get_stripe_net( $order ) ); } /** @@ -175,8 +175,8 @@ public function test_add_payment_intent_to_order() { // add_payment_intent_to_order $intent_id = 'pi_123'; - $order->add_payment_intent_to_order( $intent_id ); - $this->assertEquals( $intent_id, $order->get_intent_id() ); + WC_Stripe_Order_Helper::add_payment_intent_to_order( $intent_id, $order ); + $this->assertEquals( $intent_id, WC_Stripe_Order_Helper::get_intent_id_from_order( $order ) ); $note = wc_get_order_notes( [ @@ -188,11 +188,11 @@ public function test_add_payment_intent_to_order() { } /** - * Test for `validate_minimum_amount`. + * Test for `validate_minimum_order_amount`. * * @return void */ - public function test_validate_minimum_amount() { + public function test_validate_minimum_order_amount() { $order = WC_Helper_Order::create_order(); $order->set_total( 0.01 ); $order->save(); @@ -200,7 +200,7 @@ public function test_validate_minimum_amount() { $this->expectException( WC_Stripe_Exception::class ); $this->expectExceptionMessage( 'Did not meet minimum amount' ); - $order->validate_minimum_amount(); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); } /** @@ -216,7 +216,7 @@ public function test_get_owner_details() { $order->set_billing_email( 'test@example.com' ); $order->save_meta_data(); - $owner_details = $order->get_owner_details(); + $owner_details = WC_Stripe_Order_Helper::get_owner_details( $order ); $this->assertEquals( '+1 123 1234', $owner_details->phone ); $this->assertEquals( 'John Doe', $owner_details->name ); From a29267fd212176472991121caee2d2981897cd81 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 22 Aug 2025 09:56:57 -0300 Subject: [PATCH 16/71] Order lock updates --- .../abstract-wc-stripe-payment-gateway.php | 12 ++ includes/class-wc-stripe-order-helper.php | 113 ++++++++++++++++++ tests/phpunit/WC_Stripe_Order_Helper_Test.php | 11 +- 3 files changed, 131 insertions(+), 5 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 0af9181e96..66aefb68b0 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1767,6 +1767,8 @@ private function get_intent( $intent_type, $intent_id ) { * @since 4.2 * @param WC_Order $order The order that is being paid. * @return bool A flag that indicates whether the order is already locked. + * + * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::lock_order_payment(). */ public function lock_order_payment( $order ) { if ( $this->is_order_payment_locked( $order ) ) { @@ -1787,6 +1789,8 @@ public function lock_order_payment( $order ) { * * @since 4.2 * @param WC_Order $order The order that is being unlocked. + * + * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::unlock_order_payment(). */ public function unlock_order_payment( $order ) { $order->delete_meta_data( '_stripe_lock_payment' ); @@ -1798,6 +1802,8 @@ public function unlock_order_payment( $order ) { * * @param WC_Order $order The order to retrieve the lock for * @return mixed + * + * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::get_order_payment_lock(). */ protected function get_order_existing_lock( $order ) { $order->read_meta_data( true ); @@ -1809,6 +1815,8 @@ protected function get_order_existing_lock( $order ) { * * @param WC_Order $order The order to check the lock for * @return bool + * + * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::is_order_payment_locked(). */ protected function is_order_payment_locked( $order ) { $existing_lock = $this->get_order_existing_lock( $order ); @@ -1831,6 +1839,8 @@ protected function is_order_payment_locked( $order ) { * @since 9.1.0 * @param WC_Order $order The order that is being refunded. * @return bool A flag that indicates whether the order is already locked. + * + * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::lock_order_refund(). */ public function lock_order_refund( $order ) { $order->read_meta_data( true ); @@ -1859,6 +1869,8 @@ public function lock_order_refund( $order ) { * * @since 9.1.0 * @param WC_Order $order The order that is being unlocked. + * + * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::unlock_order_refund(). */ public function unlock_order_refund( $order ) { $order->delete_meta_data( '_stripe_lock_refund' ); diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 371fc4e932..ef4e1acf79 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -587,6 +587,119 @@ public function get_stripe_return_url( $order = null, $id = null ) { return wp_sanitize_redirect( esc_url_raw( add_query_arg( [ 'utm_nooverride' => '1' ], $this->get_return_url() ) ) ); } + /** + * Locks an order for payment intent processing for 5 minutes. + * + * @since 9.9.0 + * + * @param WC_Order $order The order that is being paid. + * @return bool A flag that indicates whether the order is already locked. + */ + public static function lock_order_payment( $order ) { + if ( self::is_order_payment_locked( $order ) ) { + // If the order is already locked, return true. + return true; + } + + $new_lock = ( time() + 5 * MINUTE_IN_SECONDS ); + + $order->update_meta_data( '_stripe_lock_payment', $new_lock ); + $order->save_meta_data(); + + return false; + } + + /** + * Unlocks an order for processing by payment intents. + * + * @since 9.9.0 + * + * @since 4.2 + * @param WC_Order $order The order that is being unlocked. + */ + public static function unlock_order_payment( $order ) { + $order->delete_meta_data( '_stripe_lock_payment' ); + $order->save_meta_data(); + } + + /** + * Retrieves the existing lock for an order. + * + * @since 9.9.0 + * + * @param WC_Order $order The order to retrieve the lock for + * @return mixed + */ + protected static function get_order_existing_lock( $order ) { + $order->read_meta_data( true ); + return $order->get_meta( '_stripe_lock_payment', true ); + } + + /** + * Checks if an order is locked for payment processing. + * + * @since 9.9.0 + * + * @param WC_Order $order The order to check the lock for + * @return bool + */ + protected static function is_order_payment_locked( $order ) { + $existing_lock = self::get_order_existing_lock( $order ); + if ( $existing_lock ) { + $parts = explode( '|', $existing_lock ); // Format is: "{expiry_timestamp}" + $expiration = (int) $parts[0]; + + // If the lock is still active, return true. + if ( time() <= $expiration ) { + return true; + } + } + + return false; + } + + /** + * Locks an order for refund processing for 5 minutes. + * + * @since 9.9.0 + * + * @param WC_Order $order The order that is being refunded. + * @return bool A flag that indicates whether the order is already locked. + */ + public static function lock_order_refund( $order ) { + $order->read_meta_data( true ); + + $existing_lock = $order->get_meta( '_stripe_lock_refund', true ); + + if ( $existing_lock ) { + $expiration = (int) $existing_lock; + + // If the lock is still active, return true. + if ( time() <= $expiration ) { + return true; + } + } + + $new_lock = time() + 5 * MINUTE_IN_SECONDS; + + $order->update_meta_data( '_stripe_lock_refund', $new_lock ); + $order->save_meta_data(); + + return false; + } + + /** + * Unlocks an order for processing refund. + * + * @since 9.9.0 + * + * @param WC_Order $order The order that is being unlocked. + */ + public static function unlock_order_refund( $order ) { + $order->delete_meta_data( '_stripe_lock_refund' ); + $order->save_meta_data(); + } + /** * Queries for an order by a specific meta key and value. * diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index f968976ba1..5aa2f5c460 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -151,15 +151,15 @@ public function test_lockers() { $order = WC_Helper_Order::create_order(); // refund - $order->lock_refund(); - $this->assertTrue( $order->get_lock_refund() > 0 ); - $order->unlock_refund(); + WC_Stripe_Order_Helper::lock_order_refund( $order ); + $this->assertTrue( WC_Stripe_Order_Helper::get_order_lock_refund( $order ) > 0 ); + WC_Stripe_Order_Helper::unlock_order_refund( $order ); $this->assertEmpty( $order->get_lock_refund() ); // payment - $order->lock_payment(); + WC_Stripe_Order_Helper::lock_order_payment( $order ); $this->assertTrue( $order->get_lock_payment() > 0 ); - $order->unlock_payment(); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); $this->assertEmpty( $order->get_lock_payment() ); } @@ -191,6 +191,7 @@ public function test_add_payment_intent_to_order() { * Test for `validate_minimum_order_amount`. * * @return void + * @throws WC_Data_Exception */ public function test_validate_minimum_order_amount() { $order = WC_Helper_Order::create_order(); From 29abdf3ef69eb7fe25da48963431408eceb1e115 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 22 Aug 2025 10:16:25 -0300 Subject: [PATCH 17/71] Order lock updates --- .../abstract-wc-stripe-payment-gateway.php | 10 +++---- includes/class-wc-gateway-stripe.php | 10 +++---- includes/class-wc-stripe-order-handler.php | 6 ++--- includes/class-wc-stripe-order-helper.php | 27 ------------------- includes/class-wc-stripe-webhook-handler.php | 12 ++++----- .../compat/trait-wc-stripe-subscriptions.php | 4 +-- .../class-wc-stripe-upe-payment-gateway.php | 8 +++--- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 8 ++---- 8 files changed, 26 insertions(+), 59 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 66aefb68b0..710cfd2197 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -426,8 +426,6 @@ public function get_stripe_customer_id( $order ) { * @version 4.0.0 * @param object $order * @param int $id Stripe session id. - * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_stripe_return_url() instead. */ public function get_stripe_return_url( $order = null, $id = null ) { if ( is_object( $order ) ) { @@ -1226,12 +1224,12 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } if ( ! $intent_cancelled && 'yes' === $captured ) { - $this->lock_order_refund( $order ); + WC_Stripe_Order_Helper::lock_order_payment( $order ); $response = WC_Stripe_API::request( $request, 'refunds' ); } } catch ( WC_Stripe_Exception $e ) { WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); - $this->unlock_order_refund( $order ); + WC_Stripe_Order_Helper::unlock_order_refund( $order ); return new WP_Error( 'stripe_error', @@ -1245,7 +1243,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { if ( ! empty( $response->error ) ) { // @phpstan-ignore-line (return statement is added) WC_Stripe_Logger::log( 'Error: ' . $response->error->message ); - $this->unlock_order_refund( $order ); + WC_Stripe_Order_Helper::unlock_order_refund( $order ); return new WP_Error( 'stripe_error', @@ -1287,7 +1285,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { $refund_message = sprintf( __( 'Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe' ), $formatted_amount, $response->id, $reason ); $order->add_order_note( $refund_message ); - $this->unlock_order_refund( $order ); + WC_Stripe_Order_Helper::unlock_order_refund( $order ); WC_Stripe_Logger::log( 'Success: ' . html_entity_decode( wp_strip_all_tags( $refund_message ), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ); diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 426333452d..7d1c665765 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -455,7 +455,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // Confirm the intent after locking the order to make sure webhooks will not interfere. if ( empty( $intent->error ) ) { - $this->lock_order_payment( $order, $intent ); + WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ); $intent = $this->confirm_intent( $intent, $order, $prepared_source ); } @@ -469,7 +469,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = return $this->retry_after_error( $intent, $order, $retry, $force_save_source, $previous_error, $use_order_source ); } - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); $this->throw_localized_message( $intent, $order ); } @@ -483,7 +483,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // If the intent requires a 3DS flow, redirect to it. if ( WC_Stripe_Intent_Status::REQUIRES_ACTION === $intent->status ) { - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); // If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings. WC_Stripe_Order_Helper::set_payment_awaiting_action( $order ); @@ -521,7 +521,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = } // Unlock the order. - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); // Return thank you page redirect. return [ @@ -916,7 +916,7 @@ public function verify_intent_after_checkout( $order ) { $this->handle_intent_verification_failure( $order, $intent ); } - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); /** * This meta is to prevent stores with short hold stock settings from cancelling orders while waiting for payment to be finalised by Stripe or the customer (i.e. completing 3DS or payment redirects). diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index b3a5cda622..e18cd40e2b 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -184,7 +184,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er // We want to retry. if ( $this->is_retryable_error( $response->error ) ) { // Unlock the order before retrying. - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); if ( $retry ) { // Don't do anymore retries after this. @@ -238,7 +238,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er $order->update_status( OrderStatus::FAILED, sprintf( __( 'Stripe payment failed: %s', 'woocommerce-gateway-stripe' ), $e->getLocalizedMessage() ) ); // Unlock the order. - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); wc_add_notice( $e->getLocalizedMessage(), 'error' ); wp_safe_redirect( wc_get_checkout_url() ); @@ -246,7 +246,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er } // Unlock the order. - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); } /** diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index ef4e1acf79..0db9690e25 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -560,33 +560,6 @@ public static function validate_minimum_order_amount( $order ) { } } - /** - * Builds the return URL from redirects. - * - * @since 9.9.0 - * - * @param object $order - * @param int $id Stripe session id. - */ - public function get_stripe_return_url( $order = null, $id = null ) { - if ( is_object( $order ) ) { - if ( empty( $id ) ) { - $id = uniqid(); - } - - $order_id = $order->get_id(); - - $args = [ - 'utm_nooverride' => '1', - 'order_id' => $order_id, - ]; - - return wp_sanitize_redirect( esc_url_raw( add_query_arg( $args, $this->get_return_url( $order ) ) ) ); - } - - return wp_sanitize_redirect( esc_url_raw( add_query_arg( [ 'utm_nooverride' => '1' ], $this->get_return_url() ) ) ); - } - /** * Locks an order for payment intent processing for 5 minutes. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 4c8f877fdf..aaa72ea115 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -332,7 +332,7 @@ public function process_webhook_payment( $notification, $retry = true ) { // We want to retry. if ( $this->is_retryable_error( $response->error ) ) { // Unlock the order before retrying. - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); if ( $retry ) { // Don't do anymore retries after this. @@ -393,7 +393,7 @@ public function process_webhook_payment( $notification, $retry = true ) { } } - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); } /** @@ -761,7 +761,7 @@ public function process_webhook_refund( $notification ) { return; } - if ( $this->lock_order_refund( $order ) ) { + if ( WC_Stripe_Order_Helper::lock_order_refund( $order ) ) { return; } @@ -794,7 +794,7 @@ public function process_webhook_refund( $notification ) { $this->update_fees( $order, $refund_object->balance_transaction ); } - $this->unlock_order_refund( $order ); + WC_Stripe_Order_Helper::unlock_order_refund( $order ); /* translators: 1) amount (including currency symbol) 2) transaction id 3) refund message */ $order->add_order_note( sprintf( __( 'Refunded %1$s - Refund ID: %2$s - %3$s', 'woocommerce-gateway-stripe' ), $amount, $refund_object->id, $reason ) ); @@ -1185,7 +1185,7 @@ public function process_payment_intent( $notification ) { break; } - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); } public function process_setup_intent( $notification ) { @@ -1258,7 +1258,7 @@ public function process_setup_intent( $notification ) { $this->send_failed_order_email( $order_id, $status_update ); } - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); } /** diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index c5a4be5067..5729bb8639 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -517,7 +517,7 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = /* translators: error message */ $renewal_order->update_status( OrderStatus::FAILED ); - $this->unlock_order_payment( $renewal_order ); + WC_Stripe_Order_Helper::unlock_order_payment( $renewal_order ); return; } @@ -589,7 +589,7 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order ); } - $this->unlock_order_payment( $renewal_order ); + WC_Stripe_Order_Helper::unlock_order_payment( $renewal_order ); } /** diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 945909e25f..473e9e5d1e 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1198,7 +1198,7 @@ private function process_payment_with_payment_method( int $order_id ) { $redirect = $this->get_return_url( $order ); } - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); return array_merge( [ @@ -1209,7 +1209,7 @@ private function process_payment_with_payment_method( int $order_id ) { ); } catch ( WC_Stripe_Exception $e ) { // Ensure the order is unlocked in case of an exception. - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); return $this->handle_process_payment_error( $e, $order ); } } @@ -1660,7 +1660,7 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme $this->process_order_for_confirmed_intent( $order, $intent_id, $save_payment_method ); } catch ( Exception $e ) { - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); /* translators: localized exception message */ @@ -1678,7 +1678,7 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme exit; } finally { - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); } } diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index 5aa2f5c460..52d2802657 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -19,8 +19,8 @@ */ class WC_Stripe_Order_Helper_Test extends WP_UnitTestCase { /** - * Tests for `query`, `get_by_order_source_id`, `get_by_order_charge_id`, `get_by_order_refund_id`, - * `get_by_order_intent_id`, and `get_by_order_setup_intent_id`. + * Tests for `get_order_by_source_id`, `get_order_by_charge_id`, `get_order_by_refund_id`, + * `get_order_by_intent_id`, and `get_order_by_setup_intent_id`. * * @return void */ @@ -42,10 +42,6 @@ public function test_retrieve() { $order->save_meta_data(); $order->save(); - // query - $orders = WC_Stripe_Order::query( [ 'status' => OrderStatus::PENDING ] ); - $this->assertEquals( $order, $orders[0] ); - // get_order_by_source_id $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_source_id( $source_id ) ); From c38e7817b603f44ac6d0e9bbcc8abfc9987cae48 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 22 Aug 2025 10:30:24 -0300 Subject: [PATCH 18/71] Order lock updates --- includes/class-wc-gateway-stripe.php | 2 +- includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-order-helper.php | 22 ++++++--- includes/class-wc-stripe-webhook-handler.php | 6 +-- .../compat/trait-wc-stripe-subscriptions.php | 2 +- .../class-wc-stripe-upe-payment-gateway.php | 4 +- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 8 ++-- .../WC_Stripe_Payment_Gateway_Test.php | 47 ------------------- 8 files changed, 28 insertions(+), 65 deletions(-) diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 7d1c665765..2814669d1d 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -897,7 +897,7 @@ public function verify_intent_after_checkout( $order ) { return; } - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ) ) { return; } diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index e18cd40e2b..ee5ed8a1de 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -119,7 +119,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er WC_Stripe_Logger::log( "Info: (Redirect) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); // Lock the order or return if the order is already locked. - if ( $this->lock_order_payment( $order ) ) { + if ( WC_Stripe_Order_Helper::lock_order_payment( $order ) ) { return; } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 0db9690e25..133fb1c640 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -603,7 +603,7 @@ public static function unlock_order_payment( $order ) { * @param WC_Order $order The order to retrieve the lock for * @return mixed */ - protected static function get_order_existing_lock( $order ) { + public static function get_order_existing_payment_lock( $order ) { $order->read_meta_data( true ); return $order->get_meta( '_stripe_lock_payment', true ); } @@ -617,7 +617,7 @@ protected static function get_order_existing_lock( $order ) { * @return bool */ protected static function is_order_payment_locked( $order ) { - $existing_lock = self::get_order_existing_lock( $order ); + $existing_lock = self::get_order_existing_payment_lock( $order ); if ( $existing_lock ) { $parts = explode( '|', $existing_lock ); // Format is: "{expiry_timestamp}" $expiration = (int) $parts[0]; @@ -640,10 +640,7 @@ protected static function is_order_payment_locked( $order ) { * @return bool A flag that indicates whether the order is already locked. */ public static function lock_order_refund( $order ) { - $order->read_meta_data( true ); - - $existing_lock = $order->get_meta( '_stripe_lock_refund', true ); - + $existing_lock = self::get_order_existing_refund_lock( $order ); if ( $existing_lock ) { $expiration = (int) $existing_lock; @@ -661,6 +658,19 @@ public static function lock_order_refund( $order ) { return false; } + /** + * Retrieves the existing refund lock for an order. + * + * @since 9.9.0 + * + * @param $order WC_Order The order to retrieve the lock for + * @return mixed + */ + public static function get_order_existing_refund_lock( $order ) { + $order->read_meta_data( true ); + return $order->get_meta( '_stripe_lock_refund', true ); + } + /** * Unlocks an order for processing refund. * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index aaa72ea115..a3d0a7e271 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -283,7 +283,7 @@ public function process_webhook_payment( $notification, $retry = true ) { $is_pending_receiver = ( 'receiver' === $notification->data->object->flow ); - if ( $this->lock_order_payment( $order ) ) { + if ( WC_Stripe_Order_Helper::lock_order_payment( $order ) ) { return; } @@ -1090,7 +1090,7 @@ public function process_payment_intent( $notification ) { // Set the order being processed for the `wc_stripe_webhook_received` action later. $this->resolved_order = $order; - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ) ) { return; } @@ -1228,7 +1228,7 @@ public function process_setup_intent( $notification ) { return; } - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ) ) { return; } diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 5729bb8639..90d95cc8ca 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -456,7 +456,7 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = $is_authentication_required = false; } else { - $this->lock_order_payment( $renewal_order ); + WC_Stripe_Order_Helper::lock_order_payment( $renewal_order ); $response = $this->create_and_confirm_intent_for_off_session( $renewal_order, $prepared_source, $amount ); $is_authentication_required = $this->is_authentication_required_for_payment( $response ); } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 473e9e5d1e..47bc0f15cd 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1049,7 +1049,7 @@ private function process_payment_with_payment_method( int $order_id ) { $this->validate_selected_payment_method_type( $payment_information, $order->get_billing_country() ); // Attempt to acquire lock, bail if already locked - $is_order_payment_locked = $this->lock_order_payment( $order ); + $is_order_payment_locked = WC_Stripe_Order_Helper::lock_order_payment( $order ); if ( $is_order_payment_locked ) { // If the request is already being processed, return an error. return [ @@ -1650,7 +1650,7 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme try { // First check if the order is already being processed by another request. - $locked = $this->lock_order_payment( $order ); + $locked = WC_Stripe_Order_Helper::lock_order_payment( $order ); if ( $locked ) { WC_Stripe_Logger::log( "Skip processing UPE redirect payment for order $order_id for the amount of {$order->get_total()}, order payment is already being processed (locked)" ); return; diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index 52d2802657..f372b5aacb 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -148,15 +148,15 @@ public function test_lockers() { // refund WC_Stripe_Order_Helper::lock_order_refund( $order ); - $this->assertTrue( WC_Stripe_Order_Helper::get_order_lock_refund( $order ) > 0 ); + $this->assertTrue( WC_Stripe_Order_Helper::get_order_existing_refund_lock( $order ) > 0 ); WC_Stripe_Order_Helper::unlock_order_refund( $order ); - $this->assertEmpty( $order->get_lock_refund() ); + $this->assertEmpty( WC_Stripe_Order_Helper::get_order_existing_payment_lock( $order ) ); // payment WC_Stripe_Order_Helper::lock_order_payment( $order ); - $this->assertTrue( $order->get_lock_payment() > 0 ); + $this->assertTrue( WC_Stripe_Order_Helper::get_order_existing_payment_lock( $order ) > 0 ); WC_Stripe_Order_Helper::unlock_order_payment( $order ); - $this->assertEmpty( $order->get_lock_payment() ); + $this->assertEmpty( WC_Stripe_Order_Helper::get_order_existing_payment_lock( $order ) ); } /** diff --git a/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php b/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php index 1d77e80c3b..7fffc435fb 100644 --- a/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php +++ b/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php @@ -769,53 +769,6 @@ public function test_render_subscription_payment_method( string $payment_method_ $this->assertEquals( $expected_result, $result ); } - /** - * Tests for `lock_order_payment` method. - */ - public function test_lock_order_payment() { - $order_1 = WC_Helper_Order::create_order(); - $locked = $this->gateway->lock_order_payment( $order_1 ); - - $this->assertFalse( $locked ); - $current_lock = $order_1->get_meta( '_stripe_lock_payment' ); - $this->assertEqualsWithDelta( (int) $current_lock, ( time() + 5 * MINUTE_IN_SECONDS ), 3 ); - - $locked = $this->gateway->lock_order_payment( $order_1 ); - $this->assertTrue( $locked ); - - // lock with an intent ID. - $order_2 = WC_Helper_Order::create_order(); - $intent_id = 'pi_123intent'; - - $locked = $this->gateway->lock_order_payment( $order_2, $intent_id ); - $current_lock = $order_2->get_meta( '_stripe_lock_payment' ); - - $this->assertFalse( $locked ); - $locked = $this->gateway->lock_order_payment( $order_2, $intent_id ); - $this->assertTrue( $locked ); - $locked = $this->gateway->lock_order_payment( $order_2 ); // test that you don't need to pass the intent ID to check lock. - $this->assertTrue( $locked ); - - // test expired locks. - $order_3 = WC_Helper_Order::create_order(); - $order_3->update_meta_data( '_stripe_lock_payment', time() - 1 ); - $order_3->save_meta_data(); - - $locked = $this->gateway->lock_order_payment( $order_3, $intent_id ); - $current_lock = $order_3->get_meta( '_stripe_lock_payment' ); - - $this->assertFalse( $locked ); - $this->assertEqualsWithDelta( (int) $current_lock, ( time() + 5 * MINUTE_IN_SECONDS ), 3 ); - - // test two instances of the same order, one locked and one not. - $order_4 = WC_Helper_Order::create_order(); - $dup_order = wc_get_order( $order_4->get_id() ); - - $this->gateway->lock_order_payment( $order_4 ); - $dup_locked = $this->gateway->lock_order_payment( $dup_order ); - $this->assertTrue( $dup_locked ); // Confirms lock from $order_4 prevents payment on $dup_order. - } - /** * Tests zero amount refunds. */ From 8fe231c856bb27dd43f779638ac4dda90d269caf Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 22 Aug 2025 10:42:48 -0300 Subject: [PATCH 19/71] Payment awaiting action methods --- .../abstract-wc-stripe-payment-gateway.php | 2 +- includes/class-wc-stripe-helper.php | 2 - includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-order-helper.php | 39 ++++++------------- .../class-wc-stripe-upe-payment-gateway.php | 4 +- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 4 +- 6 files changed, 18 insertions(+), 35 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 710cfd2197..727ae184f0 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -476,7 +476,7 @@ public function generate_payment_request( $order, $prepared_payment_method ) { if ( WC_Stripe_Helper::payment_method_allows_manual_capture( $order->get_payment_method() ) ) { $post_data['capture'] = $capture ? 'true' : 'false'; if ( $is_short_statement_descriptor_enabled ) { - $post_data['statement_descriptor_suffix'] = WC_Stripe_Order_Helper::get_dynamic_statement_descriptor_suffix( $order ); + $post_data['statement_descriptor_suffix'] = WC_Stripe_Helper::get_dynamic_statement_descriptor_suffix( $order ); } } diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index bc9d71199b..675693b552 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1084,8 +1084,6 @@ public static function get_order_by_setup_intent_id( $intent_id ) { * * @param WC_Order $order The order to generate the suffix for. * @return string The statement descriptor suffix ("#{order-number}"). - * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_dynamic_statement_descriptor_suffix() instead. */ public static function get_dynamic_statement_descriptor_suffix( $order ) { $prefix = WC_Stripe::get_instance()->account->get_card_statement_prefix(); diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index ee5ed8a1de..f04204da37 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -476,7 +476,7 @@ public function prevent_cancelling_orders_awaiting_action( $cancel_order, $order } // If the order is awaiting action and was modified within the last day, don't cancel it. - if ( wc_string_to_bool( $order->get_meta( WC_Stripe_Helper::PAYMENT_AWAITING_ACTION_META, true ) ) && $order->get_date_modified( 'edit' )->getTimestamp() > strtotime( '-1 day' ) ) { + if ( WC_Stripe_Order_Helper::is_payment_awaiting_action( $order ) && $order->get_date_modified( 'edit' )->getTimestamp() > strtotime( '-1 day' ) ) { $cancel_order = false; } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 133fb1c640..81e4f72983 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -341,6 +341,18 @@ public static function set_payment_awaiting_action( $order, $save = true ) { } } + /** + * Checks if the order is awaiting action for payment. + * + * @since 9.9.0 + * + * @param $order + * @return bool + */ + public static function is_payment_awaiting_action( $order ) { + return wc_string_to_bool( $order->get_meta( self::META_STRIPE_PAYMENT_AWAITING_ACTION, true ) ); + } + /** * Removes the metadata from the order that was used to indicate that the payment was awaiting action. * @@ -359,33 +371,6 @@ public static function remove_payment_awaiting_action( $order, $save = true ) { } } - /** - * Gets the dynamic bank statement descriptor suffix. - * - * Stripe will automatically append this suffix to the merchant account's bank statement prefix. - * - * @since 9.9.0 - * - * @param WC_Order $order The order to generate the suffix for. - * @return string The statement descriptor suffix ("#{order-number}"). - */ - public static function get_dynamic_statement_descriptor_suffix( $order ) { - $prefix = WC_Stripe::get_instance()->account->get_card_statement_prefix(); - $suffix = ''; - - if ( method_exists( $order, 'get_order_number' ) && ! empty( $order->get_order_number() ) ) { - $suffix = '#' . $order->get_order_number(); - - // Stripe requires at least 1 latin (alphabet) character in the suffix so we add an extra `O` before the order number. - if ( 0 === preg_match( '/[a-zA-Z]/', $suffix ) ) { - $suffix = 'O ' . $suffix; - } - } - - // Make sure that the prefix + suffix is limited at 22 characters. - return WC_Stripe_Helper::clean_statement_descriptor( substr( trim( $suffix ), 0, 22 - strlen( $prefix . '* ' ) ) ); - } - /** * Returns the payment intent or setup intent ID from a given order object. * diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 47bc0f15cd..26d39eae8e 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -920,7 +920,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // Use the dynamic + short statement descriptor if enabled and it's a card payment. if ( WC_Stripe_Payment_Methods::CARD === $selected_upe_payment_type && $is_short_statement_descriptor_enabled ) { - $request['statement_descriptor_suffix'] = WC_Stripe_Order_Helper::get_dynamic_statement_descriptor_suffix( $order ); + $request['statement_descriptor_suffix'] = WC_Stripe_Helper::get_dynamic_statement_descriptor_suffix( $order ); } $customer = $this->get_stripe_customer_from_order( $order ); @@ -2532,7 +2532,7 @@ protected function prepare_payment_information_from_request( WC_Order $order ) { // Use the dynamic + short statement descriptor if enabled and it's a card payment. $is_short_statement_descriptor_enabled = 'yes' === $this->get_option( 'is_short_statement_descriptor_enabled', 'no' ); if ( WC_Stripe_Payment_Methods::CARD === $selected_payment_type && $is_short_statement_descriptor_enabled ) { - $payment_information['statement_descriptor_suffix'] = WC_Stripe_Order_Helper::get_dynamic_statement_descriptor_suffix( $order ); + $payment_information['statement_descriptor_suffix'] = WC_Stripe_Helper::get_dynamic_statement_descriptor_suffix( $order ); } if ( empty( $payment_method_id ) && ! empty( $_POST['wc-stripe-confirmation-token'] ) ) { diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index f372b5aacb..a02eafce05 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -121,10 +121,10 @@ public function test_properties() { // Tests for `get_payment_awaiting_action`, `set_payment_awaiting_action`, and `remove_payment_awaiting_action`. WC_Stripe_Order_Helper::set_payment_awaiting_action( $order ); - $this->assertTrue( $order->is_payment_awaiting_action() ); + $this->assertTrue( WC_Stripe_Order_Helper::is_payment_awaiting_action( $order ) ); WC_Stripe_Order_Helper::remove_payment_awaiting_action( $order ); - $this->assertFalse( $order->is_payment_awaiting_action() ); + $this->assertFalse( WC_Stripe_Order_Helper::is_payment_awaiting_action( $order ) ); $this->assertEquals( 100, WC_Stripe_Order_Helper::get_stripe_fee( $order ) ); $this->assertEquals( 100, WC_Stripe_Order_Helper::get_stripe_net( $order ) ); From e4844ee862fc11a1f62e836bd8944f1b812e7d1f Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 22 Aug 2025 10:52:33 -0300 Subject: [PATCH 20/71] Fix tests --- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 70 +++---------------- 1 file changed, 9 insertions(+), 61 deletions(-) diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index a02eafce05..52671d88a0 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -2,7 +2,6 @@ namespace WooCommerce\Stripe\Tests; -use Automattic\WooCommerce\Enums\OrderStatus; use WC_Order; use WC_Stripe_Exception; use WP_UnitTestCase; @@ -23,6 +22,7 @@ class WC_Stripe_Order_Helper_Test extends WP_UnitTestCase { * `get_order_by_intent_id`, and `get_order_by_setup_intent_id`. * * @return void + * @throws WC_Data_Exception */ public function test_retrieve() { // setup @@ -34,11 +34,11 @@ public function test_retrieve() { $order = WC_Helper_Order::create_order(); - $order->set_source_id( $source_id ); $order->set_transaction_id( $charge_id ); - $order->set_refund_id( $refund_id ); - $order->set_intent_id( $intent_id ); - $order->set_setup_intent( $setup_intent_id ); + $order->update_meta_data( '_stripe_source_id', $source_id ); + $order->update_meta_data( '_stripe_refund_id', $refund_id ); + $order->update_meta_data( '_stripe_intent_id', $intent_id ); + $order->update_meta_data( '_stripe_setup_intent_id', $setup_intent_id ); $order->save_meta_data(); $order->save(); @@ -66,60 +66,7 @@ public function test_retrieve() { public function test_properties() { $order = WC_Helper_Order::create_order(); - $order->set_source_id( 'src_123' ); - $order->set_transaction_id( 'ch_123' ); - $order->set_refund_id( 're_123' ); - $order->set_intent_id( 'pi_123' ); - $order->set_setup_intent( 'seti_123' ); - $order->set_stripe_currency( 'usd' ); - $order->set_card_brand( 'visa' ); - $order->set_status_before_hold( OrderStatus::PENDING ); - $order->set_mandate_id( 'mandate_123' ); - $order->set_upe_payment_type( 'card' ); - $order->set_stripe_customer_id( 'cus_123' ); - $order->set_multibanco_data( - [ - 'entity' => '123', - 'reference' => '123', - 'amount' => 100, - ] - ); - $order->set_upe_redirect_processed( true ); - $order->set_upe_waiting_for_redirect( true ); - $order->set_charge_captured( true ); - $order->set_status_final( true ); - - $order->set_fee( 100 ); - $order->set_net( 100 ); - - $order->save_meta_data(); - $order->save(); - - $this->assertEquals( 'src_123', $order->get_source_id() ); - $this->assertEquals( 'ch_123', $order->get_transaction_id() ); - $this->assertEquals( 're_123', $order->get_refund_id() ); - $this->assertEquals( 'pi_123', $order->get_intent_id() ); - $this->assertEquals( 'seti_123', $order->get_setup_intent() ); - $this->assertEquals( 'usd', $order->get_stripe_currency() ); - $this->assertEquals( 'visa', $order->get_card_brand() ); - $this->assertEquals( OrderStatus::PENDING, $order->get_status_before_hold() ); - $this->assertEquals( 'mandate_123', $order->get_mandate_id() ); - $this->assertEquals( 'card', $order->get_upe_payment_type() ); - $this->assertEquals( 'cus_123', $order->get_stripe_customer_id() ); - $this->assertEquals( - [ - 'entity' => '123', - 'reference' => '123', - 'amount' => 100, - ], - $order->get_multibanco_data() - ); - $this->assertTrue( $order->is_upe_redirect_processed() ); - $this->assertTrue( $order->is_upe_waiting_for_redirect() ); - $this->assertTrue( $order->is_charge_captured() ); - $this->assertTrue( $order->is_status_final() ); - - // Tests for `get_payment_awaiting_action`, `set_payment_awaiting_action`, and `remove_payment_awaiting_action`. + // Tests for `is_payment_awaiting_action`, `set_payment_awaiting_action`, and `remove_payment_awaiting_action`. WC_Stripe_Order_Helper::set_payment_awaiting_action( $order ); $this->assertTrue( WC_Stripe_Order_Helper::is_payment_awaiting_action( $order ) ); @@ -129,8 +76,8 @@ public function test_properties() { $this->assertEquals( 100, WC_Stripe_Order_Helper::get_stripe_fee( $order ) ); $this->assertEquals( 100, WC_Stripe_Order_Helper::get_stripe_net( $order ) ); - WC_Stripe_Order_Helper::delete_stripe_fee(); - WC_Stripe_Order_Helper::delete_stripe_net(); + WC_Stripe_Order_Helper::delete_stripe_fee( $order ); + WC_Stripe_Order_Helper::delete_stripe_net( $order ); $order->save_meta_data(); $this->assertEmpty( WC_Stripe_Order_Helper::get_stripe_fee( $order ) ); @@ -204,6 +151,7 @@ public function test_validate_minimum_order_amount() { * Tests for `get_owner_details`. * * @return void + * @throws WC_Data_Exception */ public function test_get_owner_details() { $order = WC_Helper_Order::create_order(); From d750e3b1f2a67cde0c4a65dfbd7748302a11478a Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 15 Sep 2025 17:00:52 -0300 Subject: [PATCH 21/71] Revert unnecessary changes --- .../abstract-wc-stripe-payment-gateway.php | 2 +- .../class-wc-stripe-intent-controller.php | 4 +- includes/class-wc-stripe-order-helper.php | 98 ------------------- tests/phpunit/WC_Stripe_Helper_Test.php | 3 +- 4 files changed, 4 insertions(+), 103 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 727ae184f0..a548370c92 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -548,7 +548,7 @@ public function generate_payment_request( $order, $prepared_payment_method ) { public function process_response( $response, $order ) { WC_Stripe_Logger::log( 'Processing response: ' . print_r( $response, true ) ); - $potential_order = WC_Stripe_Order_Helper::get_order_by_charge_id( $response->id ); + $potential_order = WC_Stripe_Helper::get_order_by_charge_id( $response->id ); if ( $potential_order && $potential_order->get_id() !== $order->get_id() ) { WC_Stripe_Logger::log( 'Aborting, transaction already consumed by another order.' ); $localized_message = __( 'Payment processing failed. Please retry.', 'woocommerce-gateway-stripe' ); diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 4a603e393b..9edf0f3531 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -429,7 +429,7 @@ public function update_payment_intent_ajax() { $save_payment_method = isset( $_POST['save_payment_method'] ) ? 'yes' === wc_clean( wp_unslash( $_POST['save_payment_method'] ) ) : false; $selected_upe_payment_type = ! empty( $_POST['selected_upe_payment_type'] ) ? wc_clean( wp_unslash( $_POST['selected_upe_payment_type'] ) ) : ''; - $order_from_payment = WC_Stripe_Order_Helper::get_order_by_intent_id( $payment_intent_id ); + $order_from_payment = WC_Stripe_Helper::get_order_by_intent_id( $payment_intent_id ); if ( ! $order_from_payment || $order_from_payment->get_id() !== $order_id ) { throw new Exception( __( 'Unable to verify your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); } @@ -744,7 +744,7 @@ public function update_failed_order_ajax() { $intent_id = isset( $_POST['intent_id'] ) ? wc_clean( wp_unslash( $_POST['intent_id'] ) ) : ''; $order = wc_get_order( $order_id ); - $order_from_payment = WC_Stripe_Order_Helper::get_order_by_intent_id( $intent_id ); + $order_from_payment = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); if ( ! $order_from_payment || $order_from_payment->get_id() !== $order_id ) { wp_send_json_error( __( 'Unable to verify your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) ); } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 81e4f72983..5e919f5efe 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -237,63 +237,6 @@ public static function delete_stripe_net( $order = null ) { $order->delete_meta_data( self::LEGACY_META_STRIPE_NET ); } - /** - * Gets the order by Stripe source ID. - * - * @since 9.9.0 - * - * @param string $source_id - */ - public static function get_order_by_source_id( $source_id ) { - return self::get_by_meta( self::META_STRIPE_SOURCE_ID, $source_id ); - } - - /** - * Gets the order by Stripe charge ID. - * - * @since 9.9.0 - * - * @param string $charge_id - */ - public static function get_order_by_charge_id( $charge_id ) { - return self::get_by_meta( self::META_STRIPE_CHARGE_ID, $charge_id ); - } - - /** - * Gets the order by Stripe refund ID. - * - * @since 9.9.0 - * - * @param string $refund_id - */ - public static function get_order_by_refund_id( $refund_id ) { - return self::get_by_meta( self::META_STRIPE_REFUND_ID, $refund_id ); - } - - /** - * Gets the order by Stripe PaymentIntent ID. - * - * @since 9.9.0 - * - * @param string $intent_id The ID of the intent. - * @return WC_Order|bool Either an order or false when not found. - */ - public static function get_order_by_intent_id( $intent_id ) { - return self::get_by_meta( self::META_STRIPE_INTENT_ID, $intent_id ); - } - - /** - * Gets the order by Stripe SetupIntent ID. - * - * @since 9.9.0 - * - * @param string $intent_id The ID of the intent. - * @return WC_Order|bool Either an order or false when not found. - */ - public static function get_order_by_setup_intent_id( $intent_id ) { - return self::get_by_meta( self::META_STRIPE_SETUP_INTENT, $intent_id ); - } - /** * Adds payment intent id and order note to order if payment intent is not already saved * @@ -667,45 +610,4 @@ public static function unlock_order_refund( $order ) { $order->delete_meta_data( '_stripe_lock_refund' ); $order->save_meta_data(); } - - /** - * Queries for an order by a specific meta key and value. - * - * @param $meta_key string The meta key to search for. - * @param $meta_value string The meta value to search for. - * @return bool|WC_Order - */ - private static function get_by_meta( $meta_key, $meta_value ) { - global $wpdb; - - if ( WC_Stripe_Woo_Compat_Utils::is_custom_orders_table_enabled() ) { - $params = [ 'limit' => 1 ]; - // Check if the meta key is a transaction ID. If so, use the transaction ID to query the order, instead of the meta when HPOS is enabled. - if ( self::META_STRIPE_CHARGE_ID === $meta_key ) { - $params['transaction_id'] = $meta_value; - } else { - $params['meta_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query - [ - 'key' => $meta_key, - 'value' => $meta_value, - ], - ]; - } - - $orders = wc_get_orders( $params ); - $order_id = current( $orders ) ? current( $orders )->get_id() : false; - } else { - $order_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts as posts LEFT JOIN $wpdb->postmeta as meta ON posts.ID = meta.post_id WHERE meta.meta_value = %s AND meta.meta_key = %s", $meta_value, $meta_key ) ); - } - - if ( ! empty( $order_id ) ) { - $order = wc_get_order( $order_id ); - } - - if ( ! empty( $order ) && $order->get_status() !== OrderStatus::TRASH ) { - return $order; - } - - return false; - } } diff --git a/tests/phpunit/WC_Stripe_Helper_Test.php b/tests/phpunit/WC_Stripe_Helper_Test.php index a651e40fc4..7206db3be4 100644 --- a/tests/phpunit/WC_Stripe_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Helper_Test.php @@ -7,7 +7,6 @@ use WC_Order; use WC_Stripe_Currency_Code; use WC_Stripe_Helper; -use WC_Stripe_Order_Helper; use WC_Stripe_Payment_Methods; use WooCommerce\Stripe\Tests\Helpers\UPE_Test_Helper; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; @@ -219,7 +218,7 @@ public function test_get_order_by_intent_id( $status, $success ) { $intent_id = 'pi_mock'; update_post_meta( $order_id, '_stripe_intent_id', $intent_id ); - $order = WC_Stripe_Order_Helper::get_order_by_intent_id( $intent_id ); + $order = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); if ( $success ) { $this->assertInstanceOf( WC_Order::class, $order ); } else { From d69d56cd8218c6adfe9adf6a82911bdaf4f95593 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 15 Sep 2025 17:05:01 -0300 Subject: [PATCH 22/71] Revert unnecessary changes --- .../payment-methods/class-wc-stripe-upe-payment-gateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 0b38533b1c..95a00113bd 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1591,7 +1591,7 @@ public function maybe_process_upe_redirect() { * @return bool */ private function is_order_associated_to_payment_intent( int $order_id, string $intent_id ): bool { - $order_from_payment_intent = WC_Stripe_Order_Helper::get_order_by_intent_id( $intent_id ); + $order_from_payment_intent = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); return $order_from_payment_intent && $order_from_payment_intent->get_id() === $order_id; } From df37c497c113bd532150a9ec8da699540aff187b Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 15 Sep 2025 17:18:20 -0300 Subject: [PATCH 23/71] Fix merge --- includes/class-wc-stripe-helper.php | 32 ++++++++++++++++++-- includes/class-wc-stripe-webhook-handler.php | 26 ++++++++-------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 777cf55a80..d6248c38c3 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -80,6 +80,8 @@ public static function delete_main_stripe_settings() { * @since 4.1.0 * @param object $order * @return string $currency + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::get_stripe_currency()` instead. */ public static function get_stripe_currency( $order = null ) { if ( is_null( $order ) ) { @@ -95,6 +97,8 @@ public static function get_stripe_currency( $order = null ) { * @since 4.1.0 * @param object $order * @param string $currency + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::update_stripe_currency()` instead. */ public static function update_stripe_currency( $order, $currency ) { if ( is_null( $order ) ) { @@ -110,6 +114,8 @@ public static function update_stripe_currency( $order, $currency ) { * @since 4.1.0 * @param object $order * @return string $amount + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::get_stripe_fee()` instead. */ public static function get_stripe_fee( $order = null ) { if ( is_null( $order ) ) { @@ -124,7 +130,7 @@ public static function get_stripe_fee( $order = null ) { // If found update to new name. if ( $amount ) { - self::update_stripe_fee( $order, $amount ); + WC_Stripe_Order_Helper::update_stripe_fee( $order, $amount ); } } @@ -137,6 +143,8 @@ public static function get_stripe_fee( $order = null ) { * @since 4.1.0 * @param object $order * @param float $amount + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::update_stripe_fee()` instead. */ public static function update_stripe_fee( $order = null, $amount = 0.0 ) { if ( is_null( $order ) ) { @@ -151,6 +159,8 @@ public static function update_stripe_fee( $order = null, $amount = 0.0 ) { * * @since 4.1.0 * @param object $order + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::delete_stripe_fee()` instead. */ public static function delete_stripe_fee( $order = null ) { if ( is_null( $order ) ) { @@ -167,6 +177,8 @@ public static function delete_stripe_fee( $order = null ) { * @since 4.1.0 * @param object $order * @return string $amount + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::get_stripe_net()` instead. */ public static function get_stripe_net( $order = null ) { if ( is_null( $order ) ) { @@ -181,7 +193,7 @@ public static function get_stripe_net( $order = null ) { // If found update to new name. if ( $amount ) { - self::update_stripe_net( $order, $amount ); + WC_Stripe_Order_Helper::update_stripe_net( $order, $amount ); } } @@ -194,6 +206,8 @@ public static function get_stripe_net( $order = null ) { * @since 4.1.0 * @param object $order * @param float $amount + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::update_stripe_net()` instead. */ public static function update_stripe_net( $order = null, $amount = 0.0 ) { if ( is_null( $order ) ) { @@ -208,6 +222,8 @@ public static function update_stripe_net( $order = null, $amount = 0.0 ) { * * @since 4.1.0 * @param object $order + * + * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::delete_stripe_net()` instead. */ public static function delete_stripe_net( $order = null ) { if ( is_null( $order ) ) { @@ -1284,6 +1300,8 @@ private static function should_load_scripts_for_prb_location( $location ) { * * @param $payment_intent_id * @param $order + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::add_payment_intent_to_order() instead. */ public static function add_payment_intent_to_order( $payment_intent_id, $order ) { @@ -1399,6 +1417,8 @@ public static function get_payment_method_from_intent( $intent ) { * @param WC_Order $order The order to fetch the Stripe intent from. * * @return string|bool The intent ID if found, false otherwise. + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_intent_id_from_order() instead. */ public static function get_intent_id_from_order( $order ) { $intent_id = $order->get_meta( '_stripe_intent_id' ); @@ -1437,6 +1457,8 @@ public static function get_stripe_gateway_ids() { * @param bool $save Whether to save the order after adding the metadata. * * @return void + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::set_payment_awaiting_action() instead. */ public static function set_payment_awaiting_action( $order, $save = true ) { $order->update_meta_data( self::PAYMENT_AWAITING_ACTION_META, wc_bool_to_string( true ) ); @@ -1453,6 +1475,8 @@ public static function set_payment_awaiting_action( $order, $save = true ) { * @param bool $save Whether to save the order after removing the metadata. * * @return void + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::remove_payment_awaiting_action() instead. */ public static function remove_payment_awaiting_action( $order, $save = true ) { $order->delete_meta_data( self::PAYMENT_AWAITING_ACTION_META ); @@ -1907,6 +1931,8 @@ public static function has_gateway_plugin_active( $plugin_id ) { * @param string|null $selected_payment_type The selected payment type, which is generally applicable for updates. If null, we will use the stored payment type for the order. * * @throws Exception Throws an exception if the intent is not valid for the order. + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::validate_intent_for_order() instead. */ public static function validate_intent_for_order( $order, $intent, ?string $selected_payment_type = null ): void { $intent_id = null; @@ -2011,6 +2037,8 @@ public static function is_connected( $mode = null ) { * * @param $order WC_Order The order to check. * @return bool + * + * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::is_stripe_gateway_order() instead. */ public static function is_stripe_gateway_order( $order ) { return WC_Gateway_Stripe::ID === substr( (string) $order->get_payment_method(), 0, 6 ); diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 4a8b1d4050..c5a415cb77 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -283,7 +283,7 @@ public function process_webhook_payment( $notification, $retry = true ) { $is_pending_receiver = ( 'receiver' === $notification->data->object->flow ); - if ( $this->lock_order_payment( $order ) ) { + if ( WC_Stripe_Order_Helper::lock_order_payment( $order ) ) { return; } @@ -300,7 +300,7 @@ public function process_webhook_payment( $notification, $retry = true ) { $response = null; // This will throw exception if not valid. - $this->validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); WC_Stripe_Logger::log( "Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); @@ -332,7 +332,7 @@ public function process_webhook_payment( $notification, $retry = true ) { // We want to retry. if ( $this->is_retryable_error( $response->error ) ) { // Unlock the order before retrying. - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); if ( $retry ) { // Don't do anymore retries after this. @@ -393,7 +393,7 @@ public function process_webhook_payment( $notification, $retry = true ) { } } - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); } /** @@ -735,7 +735,7 @@ public function process_webhook_refund( $notification ) { $order_id = $order->get_id(); - if ( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ) { + if ( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $captured = $order->get_meta( '_stripe_charge_captured' ); $refund_id = $order->get_meta( '_stripe_refund_id' ); @@ -761,7 +761,7 @@ public function process_webhook_refund( $notification ) { return; } - if ( $this->lock_order_refund( $order ) ) { + if ( WC_Stripe_Order_Helper::lock_order_refund( $order ) ) { return; } @@ -794,7 +794,7 @@ public function process_webhook_refund( $notification ) { $this->update_fees( $order, $refund_object->balance_transaction ); } - $this->unlock_order_refund( $order ); + WC_Stripe_Order_Helper::unlock_order_refund( $order ); /* translators: 1) amount (including currency symbol) 2) transaction id 3) refund message */ $order->add_order_note( sprintf( __( 'Refunded %1$s - Refund ID: %2$s - %3$s', 'woocommerce-gateway-stripe' ), $amount, $refund_object->id, $reason ) ); @@ -821,7 +821,7 @@ public function process_webhook_refund_updated( $notification ) { $order_id = $order->get_id(); - if ( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ) { + if ( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $refund_id = $order->get_meta( '_stripe_refund_id' ); $currency = $order->get_currency(); @@ -1090,7 +1090,7 @@ public function process_payment_intent( $notification ) { // Set the order being processed for the `wc_stripe_webhook_received` action later. $this->resolved_order = $order; - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ) ) { return; } @@ -1185,7 +1185,7 @@ public function process_payment_intent( $notification ) { break; } - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); } public function process_setup_intent( $notification ) { @@ -1228,7 +1228,7 @@ public function process_setup_intent( $notification ) { return; } - if ( $this->lock_order_payment( $order, $intent ) ) { + if ( WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ) ) { return; } @@ -1258,7 +1258,7 @@ public function process_setup_intent( $notification ) { $this->send_failed_order_email( $order_id, $status_update ); } - $this->unlock_order_payment( $order ); + WC_Stripe_Order_Helper::unlock_order_payment( $order ); } /** @@ -1505,7 +1505,7 @@ private function get_order_from_intent( $intent ) { $order = isset( $data[0], $data[1] ) ? wc_get_order( absint( $data[0] ) ) : false; if ( $order ) { - $intent_id = WC_Stripe_Helper::get_intent_id_from_order( $order ); + $intent_id = WC_Stripe_Order_Helper::get_intent_id_from_order( $order ); // Return the order if the intent ID matches. if ( $intent->id === $intent_id ) { From 67c79d68e32746673b1c1b2ef861f46d2fd5e675 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 15 Sep 2025 17:21:28 -0300 Subject: [PATCH 24/71] Update deprecated comments --- .../abstract-wc-stripe-payment-gateway.php | 14 +++++----- includes/class-wc-stripe-helper.php | 28 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index a548370c92..08105325f6 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -381,7 +381,7 @@ public function payment_icons() { * @version 4.0.0 * @param object $order * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::validate_minimum_order_amount() instead. + * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::validate_minimum_order_amount() instead. */ public function validate_minimum_order_amount( $order ) { if ( $order->get_total() * 100 < WC_Stripe_Helper::get_minimum_amount() ) { @@ -726,7 +726,7 @@ public function send_failed_refund_emails( $order ) { * @param object $order * @return object $details * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_owner_details() instead. + * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::get_owner_details() instead. */ public function get_owner_details( $order ) { $billing_first_name = $order->get_billing_first_name(); @@ -1766,7 +1766,7 @@ private function get_intent( $intent_type, $intent_id ) { * @param WC_Order $order The order that is being paid. * @return bool A flag that indicates whether the order is already locked. * - * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::lock_order_payment(). + * @deprecated 10.0.0 Deprecated in favor of WC_Stripe_Order_Helper::lock_order_payment(). */ public function lock_order_payment( $order ) { if ( $this->is_order_payment_locked( $order ) ) { @@ -1788,7 +1788,7 @@ public function lock_order_payment( $order ) { * @since 4.2 * @param WC_Order $order The order that is being unlocked. * - * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::unlock_order_payment(). + * @deprecated 10.0.0 Deprecated in favor of WC_Stripe_Order_Helper::unlock_order_payment(). */ public function unlock_order_payment( $order ) { $order->delete_meta_data( '_stripe_lock_payment' ); @@ -1801,7 +1801,7 @@ public function unlock_order_payment( $order ) { * @param WC_Order $order The order to retrieve the lock for * @return mixed * - * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::get_order_payment_lock(). + * @deprecated 10.0.0 Deprecated in favor of WC_Stripe_Order_Helper::get_order_payment_lock(). */ protected function get_order_existing_lock( $order ) { $order->read_meta_data( true ); @@ -1814,7 +1814,7 @@ protected function get_order_existing_lock( $order ) { * @param WC_Order $order The order to check the lock for * @return bool * - * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::is_order_payment_locked(). + * @deprecated 10.0.0 Deprecated in favor of WC_Stripe_Order_Helper::is_order_payment_locked(). */ protected function is_order_payment_locked( $order ) { $existing_lock = $this->get_order_existing_lock( $order ); @@ -1838,7 +1838,7 @@ protected function is_order_payment_locked( $order ) { * @param WC_Order $order The order that is being refunded. * @return bool A flag that indicates whether the order is already locked. * - * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::lock_order_refund(). + * @deprecated 10.0.0 Deprecated in favor of WC_Stripe_Order_Helper::lock_order_refund(). */ public function lock_order_refund( $order ) { $order->read_meta_data( true ); diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index d6248c38c3..9da0bc9a39 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -81,7 +81,7 @@ public static function delete_main_stripe_settings() { * @param object $order * @return string $currency * - * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::get_stripe_currency()` instead. + * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::get_stripe_currency()` instead. */ public static function get_stripe_currency( $order = null ) { if ( is_null( $order ) ) { @@ -98,7 +98,7 @@ public static function get_stripe_currency( $order = null ) { * @param object $order * @param string $currency * - * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::update_stripe_currency()` instead. + * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::update_stripe_currency()` instead. */ public static function update_stripe_currency( $order, $currency ) { if ( is_null( $order ) ) { @@ -115,7 +115,7 @@ public static function update_stripe_currency( $order, $currency ) { * @param object $order * @return string $amount * - * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::get_stripe_fee()` instead. + * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::get_stripe_fee()` instead. */ public static function get_stripe_fee( $order = null ) { if ( is_null( $order ) ) { @@ -144,7 +144,7 @@ public static function get_stripe_fee( $order = null ) { * @param object $order * @param float $amount * - * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::update_stripe_fee()` instead. + * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::update_stripe_fee()` instead. */ public static function update_stripe_fee( $order = null, $amount = 0.0 ) { if ( is_null( $order ) ) { @@ -160,7 +160,7 @@ public static function update_stripe_fee( $order = null, $amount = 0.0 ) { * @since 4.1.0 * @param object $order * - * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::delete_stripe_fee()` instead. + * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::delete_stripe_fee()` instead. */ public static function delete_stripe_fee( $order = null ) { if ( is_null( $order ) ) { @@ -178,7 +178,7 @@ public static function delete_stripe_fee( $order = null ) { * @param object $order * @return string $amount * - * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::get_stripe_net()` instead. + * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::get_stripe_net()` instead. */ public static function get_stripe_net( $order = null ) { if ( is_null( $order ) ) { @@ -207,7 +207,7 @@ public static function get_stripe_net( $order = null ) { * @param object $order * @param float $amount * - * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::update_stripe_net()` instead. + * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::update_stripe_net()` instead. */ public static function update_stripe_net( $order = null, $amount = 0.0 ) { if ( is_null( $order ) ) { @@ -223,7 +223,7 @@ public static function update_stripe_net( $order = null, $amount = 0.0 ) { * @since 4.1.0 * @param object $order * - * @deprecated 9.9.0 Use `WC_Stripe_Order_Helper::delete_stripe_net()` instead. + * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::delete_stripe_net()` instead. */ public static function delete_stripe_net( $order = null ) { if ( is_null( $order ) ) { @@ -1301,7 +1301,7 @@ private static function should_load_scripts_for_prb_location( $location ) { * @param $payment_intent_id * @param $order * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::add_payment_intent_to_order() instead. + * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::add_payment_intent_to_order() instead. */ public static function add_payment_intent_to_order( $payment_intent_id, $order ) { @@ -1418,7 +1418,7 @@ public static function get_payment_method_from_intent( $intent ) { * * @return string|bool The intent ID if found, false otherwise. * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::get_intent_id_from_order() instead. + * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::get_intent_id_from_order() instead. */ public static function get_intent_id_from_order( $order ) { $intent_id = $order->get_meta( '_stripe_intent_id' ); @@ -1458,7 +1458,7 @@ public static function get_stripe_gateway_ids() { * * @return void * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::set_payment_awaiting_action() instead. + * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::set_payment_awaiting_action() instead. */ public static function set_payment_awaiting_action( $order, $save = true ) { $order->update_meta_data( self::PAYMENT_AWAITING_ACTION_META, wc_bool_to_string( true ) ); @@ -1476,7 +1476,7 @@ public static function set_payment_awaiting_action( $order, $save = true ) { * * @return void * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::remove_payment_awaiting_action() instead. + * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::remove_payment_awaiting_action() instead. */ public static function remove_payment_awaiting_action( $order, $save = true ) { $order->delete_meta_data( self::PAYMENT_AWAITING_ACTION_META ); @@ -1932,7 +1932,7 @@ public static function has_gateway_plugin_active( $plugin_id ) { * * @throws Exception Throws an exception if the intent is not valid for the order. * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::validate_intent_for_order() instead. + * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::validate_intent_for_order() instead. */ public static function validate_intent_for_order( $order, $intent, ?string $selected_payment_type = null ): void { $intent_id = null; @@ -2038,7 +2038,7 @@ public static function is_connected( $mode = null ) { * @param $order WC_Order The order to check. * @return bool * - * @deprecated 9.9.0 Use WC_Stripe_Order_Helper::is_stripe_gateway_order() instead. + * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::is_stripe_gateway_order() instead. */ public static function is_stripe_gateway_order( $order ) { return WC_Gateway_Stripe::ID === substr( (string) $order->get_payment_method(), 0, 6 ); From fa19b8e4b2ef850f2910e7fa590070cd76c9957f Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 10:45:19 -0300 Subject: [PATCH 25/71] Update includes/abstracts/abstract-wc-stripe-payment-gateway.php Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> --- includes/abstracts/abstract-wc-stripe-payment-gateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 08105325f6..ff24a2ed8f 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1868,7 +1868,7 @@ public function lock_order_refund( $order ) { * @since 9.1.0 * @param WC_Order $order The order that is being unlocked. * - * @deprecated 9.9.0 Deprecated in favor of WC_Stripe_Order_Helper::unlock_order_refund(). + * @deprecated 10.0.0 Deprecated in favor of WC_Stripe_Order_Helper::unlock_order_refund(). */ public function unlock_order_refund( $order ) { $order->delete_meta_data( '_stripe_lock_refund' ); From 26df456222206d0a668984ccc6795c5e909b434d Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 10:45:31 -0300 Subject: [PATCH 26/71] Update includes/class-wc-stripe-order-helper.php Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> --- includes/class-wc-stripe-order-helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 5e919f5efe..c50611e157 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -13,7 +13,7 @@ class WC_Stripe_Order_Helper { /** * Meta key for Stripe currency. * - * @string + * @var string */ private const META_STRIPE_CURRENCY = '_stripe_currency'; From a222bcae1391f12e61168ca07e4be80fcfc00c55 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 10:45:38 -0300 Subject: [PATCH 27/71] Update includes/class-wc-stripe-order-helper.php Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> --- includes/class-wc-stripe-order-helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index c50611e157..55023185cb 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -338,7 +338,7 @@ public static function get_intent_id_from_order( $order ) { * * @since 9.9.0 * - * @param object $order + * @param WC_Order $order * @return object $details */ public static function get_owner_details( $order ) { From cfe10bb97eebce805bf2971246b14ebe23054760 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 10:45:49 -0300 Subject: [PATCH 28/71] Update includes/class-wc-stripe-order-helper.php Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> --- includes/class-wc-stripe-order-helper.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 55023185cb..43da8764c9 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -515,7 +515,6 @@ public static function lock_order_payment( $order ) { * * @since 9.9.0 * - * @since 4.2 * @param WC_Order $order The order that is being unlocked. */ public static function unlock_order_payment( $order ) { From 357fa894b962a91d097307a91e25b7dff9da0617 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 11:01:49 -0300 Subject: [PATCH 29/71] Removing invalid tests --- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 41 ------------------- 1 file changed, 41 deletions(-) diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index 52671d88a0..9d1ad342bc 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -17,47 +17,6 @@ * Class WC_Stripe_Order_Helper tests. */ class WC_Stripe_Order_Helper_Test extends WP_UnitTestCase { - /** - * Tests for `get_order_by_source_id`, `get_order_by_charge_id`, `get_order_by_refund_id`, - * `get_order_by_intent_id`, and `get_order_by_setup_intent_id`. - * - * @return void - * @throws WC_Data_Exception - */ - public function test_retrieve() { - // setup - $source_id = 'src_123'; - $charge_id = 'ch_123'; - $refund_id = 're_123'; - $intent_id = 'pi_123'; - $setup_intent_id = 'seti_123'; - - $order = WC_Helper_Order::create_order(); - - $order->set_transaction_id( $charge_id ); - $order->update_meta_data( '_stripe_source_id', $source_id ); - $order->update_meta_data( '_stripe_refund_id', $refund_id ); - $order->update_meta_data( '_stripe_intent_id', $intent_id ); - $order->update_meta_data( '_stripe_setup_intent_id', $setup_intent_id ); - $order->save_meta_data(); - $order->save(); - - // get_order_by_source_id - $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_source_id( $source_id ) ); - - // get_order_by_charge_id - $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_charge_id( $charge_id ) ); - - // get_order_by_refund_id - $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_refund_id( $refund_id ) ); - - // get_order_by_intent_id - $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_intent_id( $intent_id ) ); - - // get_order_by_setup_intent_id - $this->assertEquals( $order, WC_Stripe_Order_Helper::get_order_by_setup_intent_id( $setup_intent_id ) ); - } - /** * Tests for getters and setters. * From 297762c8483a2a7e5c24705aaec05ca886342c54 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 11:05:04 -0300 Subject: [PATCH 30/71] Revert invalid method calls --- includes/abstracts/abstract-wc-stripe-payment-gateway.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index ff24a2ed8f..d7d235728f 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1110,8 +1110,8 @@ public function update_fees( $order, $balance_transaction_id ) { if ( isset( $balance_transaction ) && isset( $balance_transaction->fee ) ) { // Fees and Net needs to both come from Stripe to be accurate as the returned // values are in the local currency of the Stripe account, not from WC. - $fee_refund = ! empty( $balance_transaction->fee ) ? WC_Stripe_Order_Helper::format_balance_fee( $balance_transaction, 'fee' ) : 0; - $net_refund = ! empty( $balance_transaction->net ) ? WC_Stripe_Order_Helper::format_balance_fee( $balance_transaction, 'net' ) : 0; + $fee_refund = ! empty( $balance_transaction->fee ) ? WC_Stripe_Helper::format_balance_fee( $balance_transaction, 'fee' ) : 0; + $net_refund = ! empty( $balance_transaction->net ) ? WC_Stripe_Helper::format_balance_fee( $balance_transaction, 'net' ) : 0; // Current data fee & net. $fee_current = WC_Stripe_Order_Helper::get_stripe_fee( $order ); From 0ffe7b37735109b0bf78924892610e0b87c1cd08 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 11:06:49 -0300 Subject: [PATCH 31/71] Update @since comment --- includes/class-wc-stripe-order-helper.php | 48 +++++++++++------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 43da8764c9..ab75785e7a 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -88,7 +88,7 @@ class WC_Stripe_Order_Helper { /** * Gets the Stripe currency for order. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order * @return string $currency @@ -104,7 +104,7 @@ public static function get_stripe_currency( $order = null ) { /** * Updates the Stripe currency for order. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order * @param string $currency @@ -120,7 +120,7 @@ public static function update_stripe_currency( $order, $currency ) { /** * Gets the Stripe fee for order. With legacy check. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order * @return string $amount @@ -148,7 +148,7 @@ public static function get_stripe_fee( $order = null ) { /** * Updates the Stripe fee for order. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order * @param float $amount @@ -164,7 +164,7 @@ public static function update_stripe_fee( $order = null, $amount = 0.0 ) { /** * Deletes the Stripe fee for order. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order */ @@ -180,7 +180,7 @@ public static function delete_stripe_fee( $order = null ) { /** * Gets the Stripe net for order. With legacy check. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order * @return string $amount @@ -208,7 +208,7 @@ public static function get_stripe_net( $order = null ) { /** * Updates the Stripe net for order. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order * @param float $amount @@ -224,7 +224,7 @@ public static function update_stripe_net( $order = null, $amount = 0.0 ) { /** * Deletes the Stripe net for order. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order */ @@ -240,7 +240,7 @@ public static function delete_stripe_net( $order = null ) { /** * Adds payment intent id and order note to order if payment intent is not already saved * - * @since 9.9.0 + * @since 10.0.0 * * @param $payment_intent_id * @param $order WC_Order @@ -269,7 +269,7 @@ public static function add_payment_intent_to_order( $payment_intent_id, $order ) * * This meta is primarily used to prevent orders from being cancelled by WooCommerce's hold stock settings. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order to add the metadata to. * @param bool $save Whether to save the order after adding the metadata. @@ -287,7 +287,7 @@ public static function set_payment_awaiting_action( $order, $save = true ) { /** * Checks if the order is awaiting action for payment. * - * @since 9.9.0 + * @since 10.0.0 * * @param $order * @return bool @@ -299,7 +299,7 @@ public static function is_payment_awaiting_action( $order ) { /** * Removes the metadata from the order that was used to indicate that the payment was awaiting action. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order to remove the metadata from. * @param bool $save Whether to save the order after removing the metadata. @@ -317,7 +317,7 @@ public static function remove_payment_awaiting_action( $order, $save = true ) { /** * Returns the payment intent or setup intent ID from a given order object. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order to fetch the Stripe intent from. * @@ -336,7 +336,7 @@ public static function get_intent_id_from_order( $order ) { /** * Get owner details. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order * @return object $details @@ -378,7 +378,7 @@ public static function get_owner_details( $order ) { * This checks the currency, amount, and payment method types. * The function will log a critical error if there is a mismatch. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order to check. * @param object|string $intent The payment intent to check, can either be an object or an intent ID. @@ -465,7 +465,7 @@ public static function validate_intent_for_order( $order, $intent, ?string $sele /** * Checks if the order is using a Stripe payment method. * - * @since 9.9.0 + * @since 10.0.0 * * @param $order WC_Order The order to check. * @return bool @@ -478,7 +478,7 @@ public static function is_stripe_gateway_order( $order ) { * Validates that the order meets the minimum order amount * set by Stripe. * - * @since 9.9.0 + * @since 10.0.0 * @param WC_Order $order */ public static function validate_minimum_order_amount( $order ) { @@ -491,7 +491,7 @@ public static function validate_minimum_order_amount( $order ) { /** * Locks an order for payment intent processing for 5 minutes. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order that is being paid. * @return bool A flag that indicates whether the order is already locked. @@ -513,7 +513,7 @@ public static function lock_order_payment( $order ) { /** * Unlocks an order for processing by payment intents. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order that is being unlocked. */ @@ -525,7 +525,7 @@ public static function unlock_order_payment( $order ) { /** * Retrieves the existing lock for an order. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order to retrieve the lock for * @return mixed @@ -538,7 +538,7 @@ public static function get_order_existing_payment_lock( $order ) { /** * Checks if an order is locked for payment processing. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order to check the lock for * @return bool @@ -561,7 +561,7 @@ protected static function is_order_payment_locked( $order ) { /** * Locks an order for refund processing for 5 minutes. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order that is being refunded. * @return bool A flag that indicates whether the order is already locked. @@ -588,7 +588,7 @@ public static function lock_order_refund( $order ) { /** * Retrieves the existing refund lock for an order. * - * @since 9.9.0 + * @since 10.0.0 * * @param $order WC_Order The order to retrieve the lock for * @return mixed @@ -601,7 +601,7 @@ public static function get_order_existing_refund_lock( $order ) { /** * Unlocks an order for processing refund. * - * @since 9.9.0 + * @since 10.0.0 * * @param WC_Order $order The order that is being unlocked. */ From b636c2b3c6dcb706a684d3baf1a34dcc9a963438 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 11:09:34 -0300 Subject: [PATCH 32/71] Update includes/class-wc-stripe-order-helper.php Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> --- includes/class-wc-stripe-order-helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index ab75785e7a..dd4d349e2f 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -194,7 +194,7 @@ public static function get_stripe_net( $order = null ) { // If not found let's check for legacy name. if ( empty( $amount ) ) { - $amount = $order->get_meta( self::META_STRIPE_NET, true ); + $amount = $order->get_meta( self::LEGACY_META_STRIPE_NET, true ); // If found update to new name. if ( $amount ) { From cf2c42d19fc90a1bb90fce9d4b5723446924ce24 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 11:09:44 -0300 Subject: [PATCH 33/71] Update includes/class-wc-stripe-order-helper.php Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> --- includes/class-wc-stripe-order-helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index dd4d349e2f..9ca8644fd4 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -134,7 +134,7 @@ public static function get_stripe_fee( $order = null ) { // If not found let's check for legacy name. if ( empty( $amount ) ) { - $amount = $order->get_meta( self::META_STRIPE_FEE, true ); + $amount = $order->get_meta( self::LEGACY_META_STRIPE_FEE, true ); // If found update to new name. if ( $amount ) { From 655c2f94ec087db65e41bfe23e03e0f7599741e9 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 11:13:28 -0300 Subject: [PATCH 34/71] Removing unused order lock param --- includes/class-wc-gateway-stripe.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index efab88b314..f9dfa1fe1c 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -455,7 +455,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // Confirm the intent after locking the order to make sure webhooks will not interfere. if ( empty( $intent->error ) ) { - WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ); + WC_Stripe_Order_Helper::lock_order_payment( $order ); $intent = $this->confirm_intent( $intent, $order, $prepared_source ); } @@ -898,7 +898,7 @@ public function verify_intent_after_checkout( $order ) { return; } - if ( WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ) ) { + if ( WC_Stripe_Order_Helper::lock_order_payment( $order ) ) { return; } From 66bd3f3260ce4841a0be251b611fc87587fbad5c Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 17 Sep 2025 16:41:51 -0300 Subject: [PATCH 35/71] Minor improvements to the refund lock code --- includes/class-wc-stripe-order-helper.php | 79 ++++++++++++++--------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 9ca8644fd4..d9519583a5 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -535,29 +535,6 @@ public static function get_order_existing_payment_lock( $order ) { return $order->get_meta( '_stripe_lock_payment', true ); } - /** - * Checks if an order is locked for payment processing. - * - * @since 10.0.0 - * - * @param WC_Order $order The order to check the lock for - * @return bool - */ - protected static function is_order_payment_locked( $order ) { - $existing_lock = self::get_order_existing_payment_lock( $order ); - if ( $existing_lock ) { - $parts = explode( '|', $existing_lock ); // Format is: "{expiry_timestamp}" - $expiration = (int) $parts[0]; - - // If the lock is still active, return true. - if ( time() <= $expiration ) { - return true; - } - } - - return false; - } - /** * Locks an order for refund processing for 5 minutes. * @@ -567,14 +544,9 @@ protected static function is_order_payment_locked( $order ) { * @return bool A flag that indicates whether the order is already locked. */ public static function lock_order_refund( $order ) { - $existing_lock = self::get_order_existing_refund_lock( $order ); - if ( $existing_lock ) { - $expiration = (int) $existing_lock; - - // If the lock is still active, return true. - if ( time() <= $expiration ) { - return true; - } + if ( self::is_order_refund_locked( $order ) ) { + // If the order is already locked, return true. + return true; } $new_lock = time() + 5 * MINUTE_IN_SECONDS; @@ -609,4 +581,49 @@ public static function unlock_order_refund( $order ) { $order->delete_meta_data( '_stripe_lock_refund' ); $order->save_meta_data(); } + + /** + * Checks if an order is locked for payment processing. + * + * @since 10.0.0 + * + * @param WC_Order $order The order to check the lock for + * @return bool + */ + protected static function is_order_payment_locked( $order ) { + $existing_lock = self::get_order_existing_payment_lock( $order ); + if ( $existing_lock ) { + $parts = explode( '|', $existing_lock ); // Format is: "{expiry_timestamp}" + $expiration = (int) $parts[0]; + + // If the lock is still active, return true. + if ( time() <= $expiration ) { + return true; + } + } + + return false; + } + + /** + * Checks if an order is locked for refund. + * + * @since 10.0.0 + * + * @param WC_Order $order The order to check the lock for + * @return bool + */ + protected static function is_order_refund_locked( $order ) { + $existing_lock = self::get_order_existing_refund_lock( $order ); + if ( $existing_lock ) { + $expiration = (int) $existing_lock; + + // If the lock is still active, return true. + if ( time() <= $expiration ) { + return true; + } + } + + return false; + } } From cf2546c9b4217a52c15ddf0c400a588d44da6fc2 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 07:53:11 -0300 Subject: [PATCH 36/71] Adding typed params and return values + singleton pattern --- includes/class-wc-stripe-order-helper.php | 98 +++++++++++++++-------- 1 file changed, 63 insertions(+), 35 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index d9519583a5..c553d00222 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -1,7 +1,5 @@ get_meta( self::META_STRIPE_INTENT_ID ); if ( $old_intent_id === $payment_intent_id ) { @@ -276,7 +304,7 @@ public static function add_payment_intent_to_order( $payment_intent_id, $order ) * * @return void */ - public static function set_payment_awaiting_action( $order, $save = true ) { + public function set_payment_awaiting_action( WC_Order $order, bool $save = true ): void { $order->update_meta_data( self::META_STRIPE_PAYMENT_AWAITING_ACTION, wc_bool_to_string( true ) ); if ( $save ) { @@ -289,10 +317,10 @@ public static function set_payment_awaiting_action( $order, $save = true ) { * * @since 10.0.0 * - * @param $order + * @param WC_Order $order The order to check. * @return bool */ - public static function is_payment_awaiting_action( $order ) { + public function is_payment_awaiting_action( WC_Order $order ): bool { return wc_string_to_bool( $order->get_meta( self::META_STRIPE_PAYMENT_AWAITING_ACTION, true ) ); } @@ -306,7 +334,7 @@ public static function is_payment_awaiting_action( $order ) { * * @return void */ - public static function remove_payment_awaiting_action( $order, $save = true ) { + public function remove_payment_awaiting_action( WC_Order $order, bool $save = true ): void { $order->delete_meta_data( self::META_STRIPE_PAYMENT_AWAITING_ACTION ); if ( $save ) { @@ -323,7 +351,7 @@ public static function remove_payment_awaiting_action( $order, $save = true ) { * * @return string|bool The intent ID if found, false otherwise. */ - public static function get_intent_id_from_order( $order ) { + public function get_intent_id_from_order( WC_Order $order ) { $intent_id = $order->get_meta( self::META_STRIPE_INTENT_ID ); if ( ! $intent_id ) { @@ -341,7 +369,7 @@ public static function get_intent_id_from_order( $order ) { * @param WC_Order $order * @return object $details */ - public static function get_owner_details( $order ) { + public function get_owner_details( WC_Order $order ): object { $billing_first_name = $order->get_billing_first_name(); $billing_last_name = $order->get_billing_last_name(); @@ -386,7 +414,7 @@ public static function get_owner_details( $order ) { * * @throws Exception Throws an exception if the intent is not valid for the order. */ - public static function validate_intent_for_order( $order, $intent, ?string $selected_payment_type = null ): void { + public function validate_intent_for_order( WC_Order $order, $intent, ?string $selected_payment_type = null ): void { $intent_id = null; if ( is_string( $intent ) ) { $intent_id = $intent; @@ -470,7 +498,7 @@ public static function validate_intent_for_order( $order, $intent, ?string $sele * @param $order WC_Order The order to check. * @return bool */ - public static function is_stripe_gateway_order( $order ) { + public function is_stripe_gateway_order( WC_Order $order ): bool { return WC_Gateway_Stripe::ID === substr( (string) $order->get_payment_method(), 0, 6 ); } @@ -481,7 +509,7 @@ public static function is_stripe_gateway_order( $order ) { * @since 10.0.0 * @param WC_Order $order */ - public static function validate_minimum_order_amount( $order ) { + public function validate_minimum_order_amount( WC_Order $order ): void { if ( $order->get_total() * 100 < WC_Stripe_Helper::get_minimum_amount() ) { /* translators: 1) amount (including currency symbol) */ throw new WC_Stripe_Exception( 'Did not meet minimum amount', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe_Helper::get_minimum_amount() / 100 ) ) ); @@ -496,7 +524,7 @@ public static function validate_minimum_order_amount( $order ) { * @param WC_Order $order The order that is being paid. * @return bool A flag that indicates whether the order is already locked. */ - public static function lock_order_payment( $order ) { + public function lock_order_payment( WC_Order $order ): bool { if ( self::is_order_payment_locked( $order ) ) { // If the order is already locked, return true. return true; @@ -517,7 +545,7 @@ public static function lock_order_payment( $order ) { * * @param WC_Order $order The order that is being unlocked. */ - public static function unlock_order_payment( $order ) { + public function unlock_order_payment( WC_Order $order ): void { $order->delete_meta_data( '_stripe_lock_payment' ); $order->save_meta_data(); } @@ -530,7 +558,7 @@ public static function unlock_order_payment( $order ) { * @param WC_Order $order The order to retrieve the lock for * @return mixed */ - public static function get_order_existing_payment_lock( $order ) { + public function get_order_existing_payment_lock( WC_Order $order ) { $order->read_meta_data( true ); return $order->get_meta( '_stripe_lock_payment', true ); } @@ -543,7 +571,7 @@ public static function get_order_existing_payment_lock( $order ) { * @param WC_Order $order The order that is being refunded. * @return bool A flag that indicates whether the order is already locked. */ - public static function lock_order_refund( $order ) { + public function lock_order_refund( WC_Order $order ): bool { if ( self::is_order_refund_locked( $order ) ) { // If the order is already locked, return true. return true; @@ -565,7 +593,7 @@ public static function lock_order_refund( $order ) { * @param $order WC_Order The order to retrieve the lock for * @return mixed */ - public static function get_order_existing_refund_lock( $order ) { + public function get_order_existing_refund_lock( WC_Order $order ) { $order->read_meta_data( true ); return $order->get_meta( '_stripe_lock_refund', true ); } @@ -577,7 +605,7 @@ public static function get_order_existing_refund_lock( $order ) { * * @param WC_Order $order The order that is being unlocked. */ - public static function unlock_order_refund( $order ) { + public function unlock_order_refund( WC_Order $order ) { $order->delete_meta_data( '_stripe_lock_refund' ); $order->save_meta_data(); } @@ -590,7 +618,7 @@ public static function unlock_order_refund( $order ) { * @param WC_Order $order The order to check the lock for * @return bool */ - protected static function is_order_payment_locked( $order ) { + protected function is_order_payment_locked( WC_Order $order ): bool { $existing_lock = self::get_order_existing_payment_lock( $order ); if ( $existing_lock ) { $parts = explode( '|', $existing_lock ); // Format is: "{expiry_timestamp}" @@ -613,7 +641,7 @@ protected static function is_order_payment_locked( $order ) { * @param WC_Order $order The order to check the lock for * @return bool */ - protected static function is_order_refund_locked( $order ) { + protected function is_order_refund_locked( WC_Order $order ): bool { $existing_lock = self::get_order_existing_refund_lock( $order ); if ( $existing_lock ) { $expiration = (int) $existing_lock; From fd604fbe27f5c15c6e13152d5a8207ca0e347eaa Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 07:55:12 -0300 Subject: [PATCH 37/71] Implementing the new singleton pattern --- .../abstract-wc-stripe-payment-gateway.php | 15 +++++---- includes/class-wc-gateway-stripe.php | 18 +++++++---- .../class-wc-stripe-intent-controller.php | 8 +++-- includes/class-wc-stripe-order-handler.php | 18 +++++++---- includes/class-wc-stripe-webhook-handler.php | 32 ++++++++++++------- .../class-wc-stripe-subscriptions-helper.php | 2 +- .../compat/trait-wc-stripe-subscriptions.php | 8 +++-- .../class-wc-gateway-stripe-alipay.php | 2 +- .../class-wc-gateway-stripe-bancontact.php | 4 +-- .../class-wc-gateway-stripe-eps.php | 2 +- .../class-wc-gateway-stripe-giropay.php | 2 +- .../class-wc-gateway-stripe-ideal.php | 2 +- .../class-wc-gateway-stripe-multibanco.php | 2 +- .../class-wc-gateway-stripe-p24.php | 2 +- .../class-wc-gateway-stripe-sofort.php | 2 +- .../class-wc-stripe-upe-payment-gateway.php | 30 ++++++++++------- 16 files changed, 88 insertions(+), 61 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index d7d235728f..763aa10b89 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1100,7 +1100,7 @@ public function save_source_to_order( $order, $source ) { * * @since 4.0.0 * @version 4.0.6 - * @param object $order The order object + * @param WC_Order $order The order object * @param int $balance_transaction_id */ public function update_fees( $order, $balance_transaction_id ) { @@ -1114,18 +1114,19 @@ public function update_fees( $order, $balance_transaction_id ) { $net_refund = ! empty( $balance_transaction->net ) ? WC_Stripe_Helper::format_balance_fee( $balance_transaction, 'net' ) : 0; // Current data fee & net. - $fee_current = WC_Stripe_Order_Helper::get_stripe_fee( $order ); - $net_current = WC_Stripe_Order_Helper::get_stripe_net( $order ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $fee_current = $order_helper->get_stripe_fee( $order ); + $net_current = $order_helper->get_stripe_net( $order ); // Calculation. $fee = (float) $fee_current + (float) $fee_refund; $net = (float) $net_current + (float) $net_refund; - WC_Stripe_Order_Helper::update_stripe_fee( $order, $fee ); - WC_Stripe_Order_Helper::update_stripe_net( $order, $net ); + $order_helper->update_stripe_fee( $order, $fee ); + $order_helper->update_stripe_net( $order, $net ); $currency = ! empty( $balance_transaction->currency ) ? strtoupper( $balance_transaction->currency ) : null; - WC_Stripe_Order_Helper::update_stripe_currency( $order, $currency ); + $order_helper->update_stripe_currency( $order, $currency ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); @@ -1684,7 +1685,7 @@ public function save_intent_to_order( $order, $intent ) { } if ( 'payment_intent' === $intent->object ) { - WC_Stripe_Order_Helper::add_payment_intent_to_order( $intent->id, $order ); + WC_Stripe_Order_Helper::get_instance()->add_payment_intent_to_order( $intent->id, $order ); // TODO: Refactor and add mandate ID support for other payment methods, if necessary. // The mandate ID is not available for the intent object, so we need to fetch the charge. diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index f9dfa1fe1c..9db4632523 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -560,8 +560,9 @@ public function display_order_fee( $order_id ) { $order = wc_get_order( $order_id ); - $fee = WC_Stripe_Order_Helper::get_stripe_fee( $order ); - $currency = WC_Stripe_Order_Helper::get_stripe_currency( $order ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $fee = $order_helper->get_stripe_fee( $order ); + $currency = $order_helper->get_stripe_currency( $order ); if ( ! $fee || ! $currency ) { return; @@ -597,8 +598,9 @@ public function display_order_payout( $order_id ) { $order = wc_get_order( $order_id ); - $net = WC_Stripe_Order_Helper::get_stripe_net( $order ); - $currency = WC_Stripe_Order_Helper::get_stripe_currency( $order ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $net = $order_helper->get_stripe_net( $order ); + $currency = $order_helper->get_stripe_currency( $order ); if ( ! $net || ! $currency ) { return; @@ -898,7 +900,9 @@ public function verify_intent_after_checkout( $order ) { return; } - if ( WC_Stripe_Order_Helper::lock_order_payment( $order ) ) { + $order_helper = WC_Stripe_Order_Helper::get_instance(); + + if ( $order_helper->lock_order_payment( $order ) ) { return; } @@ -917,13 +921,13 @@ public function verify_intent_after_checkout( $order ) { $this->handle_intent_verification_failure( $order, $intent ); } - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); /** * This meta is to prevent stores with short hold stock settings from cancelling orders while waiting for payment to be finalised by Stripe or the customer (i.e. completing 3DS or payment redirects). * Now that payment is confirmed, we can remove this meta. */ - WC_Stripe_Order_Helper::remove_payment_awaiting_action( $order ); + $order_helper->remove_payment_awaiting_action( $order ); } /** diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 9edf0f3531..32e1115597 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -670,7 +670,9 @@ public function init_setup_intent( $payment_method_type = null ) { * @throws WC_Stripe_Exception */ public function update_order_status_ajax() { - $order = false; + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $order = false; + try { $is_nonce_valid = check_ajax_referer( 'wc_stripe_update_order_status_nonce', false, false ); if ( ! $is_nonce_valid ) { @@ -683,7 +685,7 @@ public function update_order_status_ajax() { throw new WC_Stripe_Exception( 'order_not_found', __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); } - $intent_id = WC_Stripe_Order_Helper::get_intent_id_from_order( $order ); + $intent_id = $order_helper->get_intent_id_from_order( $order ); $intent_id_received = isset( $_POST['intent_id'] ) ? wc_clean( wp_unslash( $_POST['intent_id'] ) ) : null; if ( empty( $intent_id_received ) || $intent_id_received !== $intent_id ) { $note = sprintf( @@ -711,7 +713,7 @@ public function update_order_status_ajax() { /* translators: error message */ if ( $order ) { // Remove the awaiting confirmation order meta, don't save the order since it'll be saved in the next `update_status()` call. - WC_Stripe_Order_Helper::remove_payment_awaiting_action( $order, false ); + $order_helper->remove_payment_awaiting_action( $order, false ); $order->update_status( OrderStatus::FAILED ); } diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index f04204da37..8cbec247cc 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -113,13 +113,15 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er // Result from Stripe API request. $response = null; + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + $order_helper->validate_minimum_order_amount( $order ); WC_Stripe_Logger::log( "Info: (Redirect) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); // Lock the order or return if the order is already locked. - if ( WC_Stripe_Order_Helper::lock_order_payment( $order ) ) { + if ( $order_helper->lock_order_payment( $order ) ) { return; } @@ -184,7 +186,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er // We want to retry. if ( $this->is_retryable_error( $response->error ) ) { // Unlock the order before retrying. - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); if ( $retry ) { // Don't do anymore retries after this. @@ -238,7 +240,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er $order->update_status( OrderStatus::FAILED, sprintf( __( 'Stripe payment failed: %s', 'woocommerce-gateway-stripe' ), $e->getLocalizedMessage() ) ); // Unlock the order. - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); wc_add_notice( $e->getLocalizedMessage(), 'error' ); wp_safe_redirect( wc_get_checkout_url() ); @@ -246,7 +248,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er } // Unlock the order. - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); } /** @@ -470,13 +472,15 @@ public function prevent_cancelling_orders_awaiting_action( $cancel_order, $order return $cancel_order; } + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // Bail if payment method is not stripe or `stripe_{apm_method}` or doesn't have an intent yet. - if ( ! WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) || ! $this->get_intent_from_order( $order ) ) { + if ( ! $order_helper->is_stripe_gateway_order( $order ) || ! $this->get_intent_from_order( $order ) ) { return $cancel_order; } // If the order is awaiting action and was modified within the last day, don't cancel it. - if ( WC_Stripe_Order_Helper::is_payment_awaiting_action( $order ) && $order->get_date_modified( 'edit' )->getTimestamp() > strtotime( '-1 day' ) ) { + if ( $order_helper->is_payment_awaiting_action( $order ) && $order->get_date_modified( 'edit' )->getTimestamp() > strtotime( '-1 day' ) ) { $cancel_order = false; } diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index c5a415cb77..b10da9fa13 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -283,7 +283,9 @@ public function process_webhook_payment( $notification, $retry = true ) { $is_pending_receiver = ( 'receiver' === $notification->data->object->flow ); - if ( WC_Stripe_Order_Helper::lock_order_payment( $order ) ) { + $order_helper = WC_Stripe_Order_Helper::get_instance(); + + if ( $order_helper->lock_order_payment( $order ) ) { return; } @@ -300,7 +302,7 @@ public function process_webhook_payment( $notification, $retry = true ) { $response = null; // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + $order_helper->validate_minimum_order_amount( $order ); WC_Stripe_Logger::log( "Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); @@ -393,7 +395,7 @@ public function process_webhook_payment( $notification, $retry = true ) { } } - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); } /** @@ -735,7 +737,9 @@ public function process_webhook_refund( $notification ) { $order_id = $order->get_id(); - if ( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ) { + $order_helper = WC_Stripe_Order_Helper::get_instance(); + + if ( $order_helper->is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $captured = $order->get_meta( '_stripe_charge_captured' ); $refund_id = $order->get_meta( '_stripe_refund_id' ); @@ -761,7 +765,7 @@ public function process_webhook_refund( $notification ) { return; } - if ( WC_Stripe_Order_Helper::lock_order_refund( $order ) ) { + if ( $order_helper->lock_order_refund( $order ) ) { return; } @@ -794,7 +798,7 @@ public function process_webhook_refund( $notification ) { $this->update_fees( $order, $refund_object->balance_transaction ); } - WC_Stripe_Order_Helper::unlock_order_refund( $order ); + $order_helper->unlock_order_refund( $order ); /* translators: 1) amount (including currency symbol) 2) transaction id 3) refund message */ $order->add_order_note( sprintf( __( 'Refunded %1$s - Refund ID: %2$s - %3$s', 'woocommerce-gateway-stripe' ), $amount, $refund_object->id, $reason ) ); @@ -821,7 +825,7 @@ public function process_webhook_refund_updated( $notification ) { $order_id = $order->get_id(); - if ( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ) { + if ( WC_Stripe_Order_Helper::get_instance()->is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $refund_id = $order->get_meta( '_stripe_refund_id' ); $currency = $order->get_currency(); @@ -1090,7 +1094,9 @@ public function process_payment_intent( $notification ) { // Set the order being processed for the `wc_stripe_webhook_received` action later. $this->resolved_order = $order; - if ( WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ) ) { + $order_helper = WC_Stripe_Order_Helper::get_instance(); + + if ( $order_helper->lock_order_payment( $order ) ) { return; } @@ -1185,7 +1191,7 @@ public function process_payment_intent( $notification ) { break; } - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); } public function process_setup_intent( $notification ) { @@ -1228,7 +1234,9 @@ public function process_setup_intent( $notification ) { return; } - if ( WC_Stripe_Order_Helper::lock_order_payment( $order, $intent ) ) { + $order_helper = WC_Stripe_Order_Helper::get_instance(); + + if ( $order_helper->lock_order_payment( $order ) ) { return; } @@ -1258,7 +1266,7 @@ public function process_setup_intent( $notification ) { $this->send_failed_order_email( $order_id, $status_update ); } - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); } /** @@ -1505,7 +1513,7 @@ private function get_order_from_intent( $intent ) { $order = isset( $data[0], $data[1] ) ? wc_get_order( absint( $data[0] ) ) : false; if ( $order ) { - $intent_id = WC_Stripe_Order_Helper::get_intent_id_from_order( $order ); + $intent_id = WC_Stripe_Order_Helper::get_instance()->get_intent_id_from_order( $order ); // Return the order if the intent ID matches. if ( $intent->id === $intent_id ) { diff --git a/includes/compat/class-wc-stripe-subscriptions-helper.php b/includes/compat/class-wc-stripe-subscriptions-helper.php index 2bc9b61962..cb096e57c8 100644 --- a/includes/compat/class-wc-stripe-subscriptions-helper.php +++ b/includes/compat/class-wc-stripe-subscriptions-helper.php @@ -151,7 +151,7 @@ public static function is_subscription_payment_method_detached( $subscription ) return false; } - if ( ! WC_Stripe_Order_Helper::is_stripe_gateway_order( $subscription ) ) { + if ( ! WC_Stripe_Order_Helper::get_instance()->is_stripe_gateway_order( $subscription ) ) { // If the payment method is not a Stripe method, we don't need to check further. return false; } diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 6cfab71cba..6871f774f1 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -446,6 +446,8 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = $prepared_source->source = ''; } + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // If the payment gateway is SEPA, use the charges API. // TODO: Remove when SEPA is migrated to payment intents. if ( 'stripe_sepa' === $this->id ) { @@ -456,7 +458,7 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = $is_authentication_required = false; } else { - WC_Stripe_Order_Helper::lock_order_payment( $renewal_order ); + $order_helper->lock_order_payment( $renewal_order ); $response = $this->create_and_confirm_intent_for_off_session( $renewal_order, $prepared_source, $amount ); $is_authentication_required = $this->is_authentication_required_for_payment( $response ); } @@ -517,7 +519,7 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = /* translators: error message */ $renewal_order->update_status( OrderStatus::FAILED ); - WC_Stripe_Order_Helper::unlock_order_payment( $renewal_order ); + $order_helper->unlock_order_payment( $renewal_order ); return; } @@ -589,7 +591,7 @@ public function process_subscription_payment( $amount, $renewal_order, $retry = do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order ); } - WC_Stripe_Order_Helper::unlock_order_payment( $renewal_order ); + $order_helper->unlock_order_payment( $renewal_order ); } /** diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index 4de02f3b0c..494890bfbf 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -230,7 +230,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::ALIPAY; - $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index 1958f1cec6..256b63103a 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -207,7 +207,7 @@ public function payment_fields() { * * @since 4.0.0 * @version 4.0.0 - * @param object $order + * @param WC_Order $order * @return mixed */ public function create_source( $order ) { @@ -217,7 +217,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::BANCONTACT; - $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; $post_data['bancontact'] = [ 'preferred_language' => $this->get_locale() ]; diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index 4cffd45a52..ec831bc1dc 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -217,7 +217,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::EPS; - $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index d0b13dac63..63ac5166c9 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -213,7 +213,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::GIROPAY; - $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index b2e568b103..9dddadcc24 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -217,7 +217,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::IDEAL; - $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index 43652569eb..260b4943e8 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -321,7 +321,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::MULTIBANCO; - $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; if ( ! empty( $this->statement_descriptor ) ) { diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index b5fc235e8d..196fb926e6 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -218,7 +218,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::P24; - $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; WC_Stripe_Logger::log( 'Info: Begin creating P24 source' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index 31c15c0a06..52b049d537 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -218,7 +218,7 @@ public function create_source( $order ) { $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); $post_data['currency'] = strtolower( $currency ); $post_data['type'] = WC_Stripe_Payment_Methods::SOFORT; - $post_data['owner'] = WC_Stripe_Order_Helper::get_owner_details( $order ); + $post_data['owner'] = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); $post_data['redirect'] = [ 'return_url' => $return_url ]; $post_data['sofort'] = [ 'country' => $bank_country, diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 95a00113bd..628950e416 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -979,7 +979,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = null // $prepared_source parameter is not necessary for adding mandate information. ); - WC_Stripe_Order_Helper::add_payment_intent_to_order( $payment_intent_id, $order ); + WC_Stripe_Order_Helper::get_instance()->add_payment_intent_to_order( $payment_intent_id, $order ); $order->update_status( OrderStatus::PENDING, __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); $order->update_meta_data( '_stripe_upe_payment_type', $selected_upe_payment_type ); @@ -1048,6 +1048,8 @@ private function process_payment_with_payment_method( int $order_id ) { return $this->process_change_subscription_payment_with_deferred_intent( $order_id ); } + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $order = wc_get_order( $order_id ); try { @@ -1056,7 +1058,7 @@ private function process_payment_with_payment_method( int $order_id ) { $this->validate_selected_payment_method_type( $payment_information, $order->get_billing_country() ); // Attempt to acquire lock, bail if already locked - $is_order_payment_locked = WC_Stripe_Order_Helper::lock_order_payment( $order ); + $is_order_payment_locked = $order_helper->lock_order_payment( $order ); if ( $is_order_payment_locked ) { // If the request is already being processed, return an error. return [ @@ -1109,7 +1111,7 @@ private function process_payment_with_payment_method( int $order_id ) { if ( $payment_needed ) { // Throw an exception if the minimum order amount isn't met. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + $order_helper->validate_minimum_order_amount( $order ); // Create a payment intent, or update an existing one associated with the order. $payment_intent = $this->process_payment_intent_for_order( $order, $payment_information ); @@ -1174,7 +1176,7 @@ private function process_payment_with_payment_method( int $order_id ) { } // If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings. - WC_Stripe_Order_Helper::set_payment_awaiting_action( $order, false ); + $order_helper->set_payment_awaiting_action( $order, false ); // Prevent processing the payment intent webhooks while also processing the redirect payment (also prevents duplicate Stripe meta stored on the order). $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); @@ -1200,7 +1202,7 @@ private function process_payment_with_payment_method( int $order_id ) { $redirect = $this->get_return_url( $order ); } - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); return array_merge( [ @@ -1211,7 +1213,7 @@ private function process_payment_with_payment_method( int $order_id ) { ); } catch ( WC_Stripe_Exception $e ) { // Ensure the order is unlocked in case of an exception. - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); return $this->handle_process_payment_error( $e, $order ); } } @@ -1363,7 +1365,7 @@ public function process_payment_with_saved_payment_method( $order_id, $can_retry if ( $payment_needed ) { // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); $request_details = $this->generate_payment_request( $order, $prepared_payment_method ); $endpoint = false !== $intent ? "payment_intents/$intent->id" : 'payment_intents'; @@ -1650,9 +1652,11 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme return; } + $order_helper = WC_Stripe_Order_Helper::get_instance(); + try { // First check if the order is already being processed by another request. - $locked = WC_Stripe_Order_Helper::lock_order_payment( $order ); + $locked = $order_helper->lock_order_payment( $order ); if ( $locked ) { WC_Stripe_Logger::log( "Skip processing UPE redirect payment for order $order_id for the amount of {$order->get_total()}, order payment is already being processed (locked)" ); return; @@ -1662,7 +1666,7 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme $this->process_order_for_confirmed_intent( $order, $intent_id, $save_payment_method ); } catch ( Exception $e ) { - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); /* translators: localized exception message */ @@ -1680,7 +1684,7 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme exit; } finally { - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); } } @@ -1708,9 +1712,11 @@ public function process_order_for_confirmed_intent( $order, $intent_id, $save_pa throw new WC_Stripe_Exception( __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); } + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // Validates the intent can be applied to the order. try { - WC_Stripe_Order_Helper::validate_intent_for_order( $order, $intent ); + $order_helper->validate_intent_for_order( $order, $intent ); } catch ( Exception $e ) { throw new Exception( __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); } @@ -1776,7 +1782,7 @@ public function process_order_for_confirmed_intent( $order, $intent_id, $save_pa * This meta is to prevent stores with short hold stock settings from cancelling orders while waiting for payment to be finalised by Stripe or the customer (i.e. completing 3DS or payment redirects). * Now that payment is confirmed, we can remove this meta. */ - WC_Stripe_Order_Helper::remove_payment_awaiting_action( $order, false ); + $order_helper->remove_payment_awaiting_action( $order, false ); $order->save(); } From f5d10a8bf29c6a482cfe652c6d1074eac6cdbb46 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 07:55:32 -0300 Subject: [PATCH 38/71] Fix tests --- .../WC_Stripe_UPE_Payment_Gateway_Test.php | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index e93c73e495..8ef19fd3d0 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -4,6 +4,7 @@ use Automattic\WooCommerce\Enums\OrderStatus; use Exception; +use WC_Stripe_Order_Helper; use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper; use WC_Stripe_Database_Cache; use WooCommerce\Stripe\Tests\Helpers\PMC_Test_Helper; @@ -203,8 +204,6 @@ public function set_up() { 'has_pre_order', 'is_subscriptions_enabled', 'update_saved_payment_method', - 'lock_order_payment', - 'unlock_order_payment', ] ) ->getMock(); @@ -239,11 +238,25 @@ public function set_up() { ->will( $this->returnValue( 'cus_mock' ) ); - $this->mock_gateway->expects( $this->any() ) + + $order_helper = $this->createPartialMock( + WC_Stripe_Order_Helper::class, + [ 'lock_order_payment', 'unlock_order_payment' ] + ); + + $order_helper->expects( $this->any() ) + ->method( 'lock_order_payment' ) + ->will( + $this->returnValue( false ) + ); + + $order_helper->expects( $this->any() ) ->method( 'unlock_order_payment' ) ->will( $this->returnValue( null ) ); + + WC_Stripe_Order_Helper::set_instance( $order_helper ); } public function tear_down() { @@ -1001,11 +1014,6 @@ public function test_process_redirect_payment_returns_valid_response() { $payment_intent_mock['payment_method'] = $payment_method_mock; $payment_intent_mock['latest_charge'] = 'ch_mock'; - $this->mock_gateway->expects( $this->any() ) - ->method( 'lock_order_payment' ) - ->will( - $this->returnValue( false ) - ); $this->mock_gateway->expects( $this->once() ) ->method( 'stripe_request' ) ->with( "payment_intents/$payment_intent_id?expand[]=payment_method" ) @@ -1125,35 +1133,28 @@ public function test_process_redirect_payment_only_runs_once() { */ public function test_process_redirect_payment_locks_order() { $payment_intent_id = 'pi_mock'; - $payment_method_id = 'pm_mock'; - $customer_id = 'cus_mock'; $order = WC_Helper_Order::create_order(); $order_id = $order->get_id(); - list( $amount, $description, $metadata ) = $this->get_order_details( $order ); $order->set_payment_method( WC_Stripe_UPE_Payment_Gateway::ID ); $order->save(); - $payment_method_mock = self::MOCK_CARD_PAYMENT_METHOD_TEMPLATE; - $payment_method_mock['id'] = $payment_method_id; - $payment_method_mock['customer'] = $customer_id; - $payment_method_mock['card']['exp_year'] = intval( gmdate( 'Y' ) ) + 1; - - $payment_intent_mock = self::MOCK_CARD_PAYMENT_INTENT_TEMPLATE; - $payment_intent_mock['id'] = $payment_intent_id; - $payment_intent_mock['amount'] = $amount; - $payment_intent_mock['last_payment_error'] = []; - $payment_intent_mock['payment_method'] = $payment_method_mock; - $payment_intent_mock['latest_charge'] = 'ch_mock'; + $order_helper = $this->createPartialMock( + WC_Stripe_Order_Helper::class, + [ 'lock_order_payment', 'unlock_order_payment' ] + ); - $this->mock_gateway->expects( $this->once() ) + $order_helper->expects( $this->once() ) ->method( 'lock_order_payment' ) ->will( $this->returnValue( true ) ); - $this->mock_gateway->expects( $this->once() ) + + $order_helper->expects( $this->once() ) ->method( 'unlock_order_payment' ); + WC_Stripe_Order_Helper::set_instance( $order_helper ); + // Expect the process to bail early. $this->mock_gateway->expects( $this->never() ) ->method( 'stripe_request' ) From ba27b2bbae75949b066d8b538edfc5dac6d28cf3 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 07:58:26 -0300 Subject: [PATCH 39/71] Adding missing type declarations to constants --- includes/class-wc-stripe-order-helper.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index c553d00222..0ca93303b3 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -18,68 +18,70 @@ class WC_Stripe_Order_Helper { /** * Meta key for Stripe fee. * - * @string + * @var string */ private const META_STRIPE_FEE = '_stripe_fee'; /** * Meta key for Stripe fee (legacy version). * - * @string + * @var string */ private const LEGACY_META_STRIPE_FEE = 'Stripe Fee'; /** * Meta key for Stripe net. * - * @string + * @var string */ private const META_STRIPE_NET = '_stripe_net'; /** * Meta key for Stripe net (legacy version). * - * @string + * @var string */ private const LEGACY_META_STRIPE_NET = 'Net Revenue From Stripe'; /** * Meta key for Stripe source ID. * - * @string + * @var string */ private const META_STRIPE_SOURCE_ID = '_stripe_source_id'; /** * Meta key for Stripe charge ID. * - * @string + * @var string */ private const META_STRIPE_CHARGE_ID = '_stripe_charge_id'; /** * Meta key for Stripe refund ID. * - * @string + * @var string */ private const META_STRIPE_REFUND_ID = '_stripe_refund_id'; /** * Meta key for Stripe intent ID. * - * @string + * @var string */ private const META_STRIPE_INTENT_ID = '_stripe_intent_id'; /** * Meta key for Stripe setup intent ID. + * + * @var string */ private const META_STRIPE_SETUP_INTENT = '_stripe_setup_intent'; /** * Meta key for payment awaiting action. * - * @string + * @var string */ private const META_STRIPE_PAYMENT_AWAITING_ACTION = '_stripe_payment_awaiting_action'; From 970657957422e3e576bfd913b19c58e4fd7a7d84 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 08:02:19 -0300 Subject: [PATCH 40/71] Fix static implementations --- includes/class-wc-gateway-stripe.php | 14 ++++++++------ includes/class-wc-stripe-order-helper.php | 4 ++-- .../WC_Stripe_Klarna_Payment_Token_Test.php | 1 - tests/phpunit/WC_Stripe_Webhook_Handler_Test.php | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 9db4632523..c7631d30ba 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -442,8 +442,10 @@ public function process_payment( $order_id, $retry = true, $force_save_source = return $this->complete_free_order( $order, $prepared_source, $force_save_source ); } + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + $order_helper->validate_minimum_order_amount( $order ); WC_Stripe_Logger::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); @@ -455,7 +457,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // Confirm the intent after locking the order to make sure webhooks will not interfere. if ( empty( $intent->error ) ) { - WC_Stripe_Order_Helper::lock_order_payment( $order ); + $order_helper->lock_order_payment( $order ); $intent = $this->confirm_intent( $intent, $order, $prepared_source ); } @@ -469,7 +471,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = return $this->retry_after_error( $intent, $order, $retry, $force_save_source, $previous_error, $use_order_source ); } - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); $this->throw_localized_message( $intent, $order ); } @@ -483,10 +485,10 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // If the intent requires a 3DS flow, redirect to it. if ( WC_Stripe_Intent_Status::REQUIRES_ACTION === $intent->status ) { - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); // If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings. - WC_Stripe_Order_Helper::set_payment_awaiting_action( $order ); + $order_helper->set_payment_awaiting_action( $order ); if ( is_wc_endpoint_url( 'order-pay' ) ) { $redirect_url = add_query_arg( 'wc-stripe-confirmation', 1, $order->get_checkout_payment_url( false ) ); @@ -522,7 +524,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = } // Unlock the order. - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); // Return thank you page redirect. return [ diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 0ca93303b3..ed393adf9c 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -621,7 +621,7 @@ public function unlock_order_refund( WC_Order $order ) { * @return bool */ protected function is_order_payment_locked( WC_Order $order ): bool { - $existing_lock = self::get_order_existing_payment_lock( $order ); + $existing_lock = $this->get_order_existing_payment_lock( $order ); if ( $existing_lock ) { $parts = explode( '|', $existing_lock ); // Format is: "{expiry_timestamp}" $expiration = (int) $parts[0]; @@ -644,7 +644,7 @@ protected function is_order_payment_locked( WC_Order $order ): bool { * @return bool */ protected function is_order_refund_locked( WC_Order $order ): bool { - $existing_lock = self::get_order_existing_refund_lock( $order ); + $existing_lock = $this->get_order_existing_refund_lock( $order ); if ( $existing_lock ) { $expiration = (int) $existing_lock; diff --git a/tests/phpunit/PaymentTokens/WC_Stripe_Klarna_Payment_Token_Test.php b/tests/phpunit/PaymentTokens/WC_Stripe_Klarna_Payment_Token_Test.php index ff32a8daad..f880332ec8 100644 --- a/tests/phpunit/PaymentTokens/WC_Stripe_Klarna_Payment_Token_Test.php +++ b/tests/phpunit/PaymentTokens/WC_Stripe_Klarna_Payment_Token_Test.php @@ -86,7 +86,6 @@ public function test_is_equal_payment_method() { $this->assertTrue( $this->token->is_equal_payment_method( $payment_method ) ); // Different DOB. - $this->token->set_type( WC_Stripe_Payment_Methods::KLARNA ); $payment_method->id = 'pm_123'; $payment_method->klarna->dob->day = 2; $this->assertFalse( $this->token->is_equal_payment_method( $payment_method ) ); diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index b0fbc48970..83185bbdd7 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -581,7 +581,7 @@ public function test_process_webhook_payment_intent_processing() { // Order must be previously set to pending and have at least the payment intent set. $order = WC_Helper_Order::create_order(); - WC_Stripe_Order_Helper::add_payment_intent_to_order( $notification->data->object->id, $order ); + WC_Stripe_Order_Helper::get_instance()->add_payment_intent_to_order( $notification->data->object->id, $order ); $order->set_status( OrderStatus::PENDING ); $order->save(); From 2aeebbd9f15e6ab8dbd2dbd8c07ad8ef95962e20 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 08:06:07 -0300 Subject: [PATCH 41/71] Fix wrong lock method call --- .../abstracts/abstract-wc-stripe-payment-gateway.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 763aa10b89..14a7cd7deb 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1192,6 +1192,8 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { return true; } + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $request['charge'] = $charge_id; WC_Stripe_Logger::log( "Info: Beginning refund for order {$charge_id} for the amount of {$amount}" ); $response = new stdClass(); @@ -1225,12 +1227,12 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } if ( ! $intent_cancelled && 'yes' === $captured ) { - WC_Stripe_Order_Helper::lock_order_payment( $order ); + $order_helper->lock_order_refund( $order ); $response = WC_Stripe_API::request( $request, 'refunds' ); } } catch ( WC_Stripe_Exception $e ) { WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); - WC_Stripe_Order_Helper::unlock_order_refund( $order ); + $order_helper->unlock_order_refund( $order ); return new WP_Error( 'stripe_error', @@ -1244,7 +1246,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { if ( ! empty( $response->error ) ) { // @phpstan-ignore-line (return statement is added) WC_Stripe_Logger::log( 'Error: ' . $response->error->message ); - WC_Stripe_Order_Helper::unlock_order_refund( $order ); + $order_helper->unlock_order_refund( $order ); return new WP_Error( 'stripe_error', @@ -1286,7 +1288,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { $refund_message = sprintf( __( 'Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe' ), $formatted_amount, $response->id, $reason ); $order->add_order_note( $refund_message ); - WC_Stripe_Order_Helper::unlock_order_refund( $order ); + $order_helper->unlock_order_refund( $order ); WC_Stripe_Logger::log( 'Success: ' . html_entity_decode( wp_strip_all_tags( $refund_message ), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) ); From 463fd2a52257cc533de5ecaa6ca3ffa34fe1a572 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 08:08:18 -0300 Subject: [PATCH 42/71] Fix static implementations --- includes/class-wc-stripe-order-helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index ed393adf9c..c279b5f83a 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -527,7 +527,7 @@ public function validate_minimum_order_amount( WC_Order $order ): void { * @return bool A flag that indicates whether the order is already locked. */ public function lock_order_payment( WC_Order $order ): bool { - if ( self::is_order_payment_locked( $order ) ) { + if ( $this->is_order_payment_locked( $order ) ) { // If the order is already locked, return true. return true; } @@ -574,7 +574,7 @@ public function get_order_existing_payment_lock( WC_Order $order ) { * @return bool A flag that indicates whether the order is already locked. */ public function lock_order_refund( WC_Order $order ): bool { - if ( self::is_order_refund_locked( $order ) ) { + if ( $this->is_order_refund_locked( $order ) ) { // If the order is already locked, return true. return true; } From 7670d319ae15ad45aa7f8b9a549ce491362bb8f0 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 08:14:12 -0300 Subject: [PATCH 43/71] Fix static implementations --- includes/class-wc-stripe-order-helper.php | 6 +- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 58 +++++++++++-------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index c279b5f83a..34dfdd98cc 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -90,7 +90,7 @@ class WC_Stripe_Order_Helper { * * @var null|WC_Stripe_Order_Helper */ - private static $instance = null; + private static ?WC_Stripe_Order_Helper $instance = null; /** * Gets the singleton instance of the class. @@ -181,7 +181,7 @@ public function get_stripe_fee( ?WC_Order $order = null ) { * @since 10.0.0 * * @param WC_Order|null $order - * @param float $amount + * @param float $amount */ public function update_stripe_fee( ?WC_Order $order = null, float $amount = 0.0 ) { if ( is_null( $order ) ) { @@ -607,7 +607,7 @@ public function get_order_existing_refund_lock( WC_Order $order ) { * * @param WC_Order $order The order that is being unlocked. */ - public function unlock_order_refund( WC_Order $order ) { + public function unlock_order_refund( WC_Order $order ): void { $order->delete_meta_data( '_stripe_lock_refund' ); $order->save_meta_data(); } diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index 9d1ad342bc..3500e94be0 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -25,22 +25,24 @@ class WC_Stripe_Order_Helper_Test extends WP_UnitTestCase { public function test_properties() { $order = WC_Helper_Order::create_order(); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // Tests for `is_payment_awaiting_action`, `set_payment_awaiting_action`, and `remove_payment_awaiting_action`. - WC_Stripe_Order_Helper::set_payment_awaiting_action( $order ); - $this->assertTrue( WC_Stripe_Order_Helper::is_payment_awaiting_action( $order ) ); + $order_helper->set_payment_awaiting_action( $order ); + $this->assertTrue( $order_helper->is_payment_awaiting_action( $order ) ); - WC_Stripe_Order_Helper::remove_payment_awaiting_action( $order ); - $this->assertFalse( WC_Stripe_Order_Helper::is_payment_awaiting_action( $order ) ); + $order_helper->remove_payment_awaiting_action( $order ); + $this->assertFalse( $order_helper->is_payment_awaiting_action( $order ) ); - $this->assertEquals( 100, WC_Stripe_Order_Helper::get_stripe_fee( $order ) ); - $this->assertEquals( 100, WC_Stripe_Order_Helper::get_stripe_net( $order ) ); + $this->assertEquals( 100, $order_helper->get_stripe_fee( $order ) ); + $this->assertEquals( 100, $order_helper->get_stripe_net( $order ) ); - WC_Stripe_Order_Helper::delete_stripe_fee( $order ); - WC_Stripe_Order_Helper::delete_stripe_net( $order ); + $order_helper->delete_stripe_fee( $order ); + $order_helper->delete_stripe_net( $order ); $order->save_meta_data(); - $this->assertEmpty( WC_Stripe_Order_Helper::get_stripe_fee( $order ) ); - $this->assertEmpty( WC_Stripe_Order_Helper::get_stripe_net( $order ) ); + $this->assertEmpty( $order_helper->get_stripe_fee( $order ) ); + $this->assertEmpty( $order_helper->get_stripe_net( $order ) ); } /** @@ -52,17 +54,19 @@ public function test_lockers() { // setup $order = WC_Helper_Order::create_order(); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // refund - WC_Stripe_Order_Helper::lock_order_refund( $order ); - $this->assertTrue( WC_Stripe_Order_Helper::get_order_existing_refund_lock( $order ) > 0 ); - WC_Stripe_Order_Helper::unlock_order_refund( $order ); - $this->assertEmpty( WC_Stripe_Order_Helper::get_order_existing_payment_lock( $order ) ); + $order_helper->lock_order_refund( $order ); + $this->assertTrue( $order_helper->get_order_existing_refund_lock( $order ) > 0 ); + $order_helper->unlock_order_refund( $order ); + $this->assertEmpty( $order_helper->get_order_existing_payment_lock( $order ) ); // payment - WC_Stripe_Order_Helper::lock_order_payment( $order ); - $this->assertTrue( WC_Stripe_Order_Helper::get_order_existing_payment_lock( $order ) > 0 ); - WC_Stripe_Order_Helper::unlock_order_payment( $order ); - $this->assertEmpty( WC_Stripe_Order_Helper::get_order_existing_payment_lock( $order ) ); + $order_helper->lock_order_payment( $order ); + $this->assertTrue( $order_helper->get_order_existing_payment_lock( $order ) > 0 ); + $order_helper->unlock_order_payment( $order ); + $this->assertEmpty( $order_helper->get_order_existing_payment_lock( $order ) ); } /** @@ -75,10 +79,12 @@ public function test_add_payment_intent_to_order() { $order = WC_Helper_Order::create_order(); $order_id = $order->get_id(); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // add_payment_intent_to_order $intent_id = 'pi_123'; - WC_Stripe_Order_Helper::add_payment_intent_to_order( $intent_id, $order ); - $this->assertEquals( $intent_id, WC_Stripe_Order_Helper::get_intent_id_from_order( $order ) ); + $order_helper->add_payment_intent_to_order( $intent_id, $order ); + $this->assertEquals( $intent_id, $order_helper->get_intent_id_from_order( $order ) ); $note = wc_get_order_notes( [ @@ -103,7 +109,7 @@ public function test_validate_minimum_order_amount() { $this->expectException( WC_Stripe_Exception::class ); $this->expectExceptionMessage( 'Did not meet minimum amount' ); - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); } /** @@ -120,7 +126,7 @@ public function test_get_owner_details() { $order->set_billing_email( 'test@example.com' ); $order->save_meta_data(); - $owner_details = WC_Stripe_Order_Helper::get_owner_details( $order ); + $owner_details = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); $this->assertEquals( '+1 123 1234', $owner_details->phone ); $this->assertEquals( 'John Doe', $owner_details->name ); @@ -134,18 +140,20 @@ public function test_get_owner_details() { * @throws WC_Data_Exception */ public function test_is_stripe_gateway_order() { + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // Test with a Stripe order (Klarna). $order = WC_Helper_Order::create_order(); $order->set_payment_method( 'stripe_klarna' ); - $this->assertTrue( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ); + $this->assertTrue( $order_helper->is_stripe_gateway_order( $order ) ); // Test with a non-Stripe order. $order = WC_Helper_Order::create_order(); $order->set_payment_method( 'cod' ); - $this->assertFalse( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ); + $this->assertFalse( $order_helper->is_stripe_gateway_order( $order ) ); // Test with an empty order. $order = new WC_Order(); - $this->assertFalse( WC_Stripe_Order_Helper::is_stripe_gateway_order( $order ) ); + $this->assertFalse( $order_helper->is_stripe_gateway_order( $order ) ); } } From 305e0cb7f0358b4ea768a6edf2a6f70ae7418c89 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 08:49:57 -0300 Subject: [PATCH 44/71] Fix tests --- includes/class-wc-stripe-helper.php | 8 ++++---- includes/class-wc-stripe-order-helper.php | 8 ++++---- tests/phpunit/WC_Stripe_Order_Handler_Test.php | 2 +- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 15 +++++++++------ 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 9da0bc9a39..7c00d7e458 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -112,7 +112,7 @@ public static function update_stripe_currency( $order, $currency ) { * Gets the Stripe fee for order. With legacy check. * * @since 4.1.0 - * @param object $order + * @param WC_Order $order * @return string $amount * * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::get_stripe_fee()` instead. @@ -130,7 +130,7 @@ public static function get_stripe_fee( $order = null ) { // If found update to new name. if ( $amount ) { - WC_Stripe_Order_Helper::update_stripe_fee( $order, $amount ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_fee( $order, $amount ); } } @@ -175,7 +175,7 @@ public static function delete_stripe_fee( $order = null ) { * Gets the Stripe net for order. With legacy check. * * @since 4.1.0 - * @param object $order + * @param WC_Order $order * @return string $amount * * @deprecated 10.0.0 Use `WC_Stripe_Order_Helper::get_stripe_net()` instead. @@ -193,7 +193,7 @@ public static function get_stripe_net( $order = null ) { // If found update to new name. if ( $amount ) { - WC_Stripe_Order_Helper::update_stripe_net( $order, $amount ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_net( $order, $amount ); } } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 34dfdd98cc..e6017733ea 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -168,7 +168,7 @@ public function get_stripe_fee( ?WC_Order $order = null ) { // If found update to new name. if ( $amount ) { - self::update_stripe_fee( $order, $amount ); + $this->update_stripe_fee( $order, $amount ); } } @@ -228,7 +228,7 @@ public function get_stripe_net( ?WC_Order $order = null ) { // If found update to new name. if ( $amount ) { - self::update_stripe_net( $order, $amount ); + $this->update_stripe_net( $order, $amount ); } } @@ -241,9 +241,9 @@ public function get_stripe_net( ?WC_Order $order = null ) { * @since 10.0.0 * * @param WC_Order|null $order - * @param float $amount + * @param float $amount */ - public function update_stripe_net( ?WC_Order $order = null, $amount = 0.0 ) { + public function update_stripe_net( ?WC_Order $order = null, float $amount = 0.0 ) { if ( is_null( $order ) ) { return false; } diff --git a/tests/phpunit/WC_Stripe_Order_Handler_Test.php b/tests/phpunit/WC_Stripe_Order_Handler_Test.php index af5d20d432..03d6004a55 100644 --- a/tests/phpunit/WC_Stripe_Order_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Handler_Test.php @@ -30,7 +30,7 @@ public function set_up() { public function test_prevent_cancelling_orders_awaiting_action() { $order = WC_Helper_Order::create_order(); - WC_Stripe_Order_Helper::set_payment_awaiting_action( $order ); + WC_Stripe_Order_Helper::get_instance()->set_payment_awaiting_action( $order ); // Read in a fresh order object with meta like `date_modified` set. $order = wc_get_order( $order->get_id() ); diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index 3500e94be0..5f25b7f147 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -22,7 +22,7 @@ class WC_Stripe_Order_Helper_Test extends WP_UnitTestCase { * * @return void */ - public function test_properties() { + public function test_properties(): void { $order = WC_Helper_Order::create_order(); $order_helper = WC_Stripe_Order_Helper::get_instance(); @@ -34,6 +34,9 @@ public function test_properties() { $order_helper->remove_payment_awaiting_action( $order ); $this->assertFalse( $order_helper->is_payment_awaiting_action( $order ) ); + $order_helper->update_stripe_fee( $order, 100 ); + $order_helper->update_stripe_net( $order, 100 ); + $this->assertEquals( 100, $order_helper->get_stripe_fee( $order ) ); $this->assertEquals( 100, $order_helper->get_stripe_net( $order ) ); @@ -50,7 +53,7 @@ public function test_properties() { * * @return void */ - public function test_lockers() { + public function test_lockers(): void { // setup $order = WC_Helper_Order::create_order(); @@ -74,7 +77,7 @@ public function test_lockers() { * * @return void */ - public function test_add_payment_intent_to_order() { + public function test_add_payment_intent_to_order(): void { // setup $order = WC_Helper_Order::create_order(); $order_id = $order->get_id(); @@ -101,7 +104,7 @@ public function test_add_payment_intent_to_order() { * @return void * @throws WC_Data_Exception */ - public function test_validate_minimum_order_amount() { + public function test_validate_minimum_order_amount(): void { $order = WC_Helper_Order::create_order(); $order->set_total( 0.01 ); $order->save(); @@ -118,7 +121,7 @@ public function test_validate_minimum_order_amount() { * @return void * @throws WC_Data_Exception */ - public function test_get_owner_details() { + public function test_get_owner_details(): void { $order = WC_Helper_Order::create_order(); $order->set_billing_phone( '+1 123 1234' ); $order->set_billing_first_name( 'John' ); @@ -139,7 +142,7 @@ public function test_get_owner_details() { * @return void * @throws WC_Data_Exception */ - public function test_is_stripe_gateway_order() { + public function test_is_stripe_gateway_order(): void { $order_helper = WC_Stripe_Order_Helper::get_instance(); // Test with a Stripe order (Klarna). From 8d1a7223bee659ad31fc84a45f6a36c268bb69c6 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 09:12:39 -0300 Subject: [PATCH 45/71] Fix remaining static calls --- includes/class-wc-stripe-intent-controller.php | 6 ++++-- includes/class-wc-stripe-webhook-handler.php | 2 +- includes/compat/trait-wc-stripe-subscriptions.php | 5 +++-- includes/payment-methods/class-wc-gateway-stripe-alipay.php | 2 +- .../payment-methods/class-wc-gateway-stripe-bancontact.php | 2 +- includes/payment-methods/class-wc-gateway-stripe-eps.php | 2 +- .../payment-methods/class-wc-gateway-stripe-giropay.php | 2 +- includes/payment-methods/class-wc-gateway-stripe-ideal.php | 2 +- .../payment-methods/class-wc-gateway-stripe-multibanco.php | 2 +- includes/payment-methods/class-wc-gateway-stripe-p24.php | 2 +- includes/payment-methods/class-wc-gateway-stripe-sofort.php | 2 +- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 2 +- tests/phpunit/WC_Stripe_Webhook_Handler_Test.php | 5 ++++- 13 files changed, 21 insertions(+), 15 deletions(-) diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 32e1115597..3432512543 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -484,8 +484,10 @@ public function update_intent( $intent_id = '', $order_id = null, $save_payment_ ]; } + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $selected_payment_type = '' !== $selected_upe_payment_type && is_string( $selected_upe_payment_type ) ? $selected_upe_payment_type : null; - WC_Stripe_Order_Helper::validate_intent_for_order( $order, $intent_id, $selected_payment_type ); + $order_helper->validate_intent_for_order( $order, $intent_id, $selected_payment_type ); $gateway = $this->get_upe_gateway(); $amount = $order->get_total(); @@ -577,7 +579,7 @@ public function update_intent( $intent_id = '', $order_id = null, $save_payment_ $order->update_status( OrderStatus::PENDING, __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); } $order->save(); - WC_Stripe_Order_Helper::add_payment_intent_to_order( $intent_id, $order ); + $order_helper->add_payment_intent_to_order( $intent_id, $order ); } return [ diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index b10da9fa13..b64cef04bd 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -334,7 +334,7 @@ public function process_webhook_payment( $notification, $retry = true ) { // We want to retry. if ( $this->is_retryable_error( $response->error ) ) { // Unlock the order before retrying. - WC_Stripe_Order_Helper::unlock_order_payment( $order ); + $order_helper->unlock_order_payment( $order ); if ( $retry ) { // Don't do anymore retries after this. diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 6871f774f1..9e252a2405 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -659,8 +659,9 @@ public function delete_resubscribe_meta( $resubscribe_order ) { * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription */ public function delete_renewal_meta( $renewal_order ) { - WC_Stripe_Order_Helper::delete_stripe_fee( $renewal_order ); - WC_Stripe_Order_Helper::delete_stripe_net( $renewal_order ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $order_helper->delete_stripe_fee( $renewal_order ); + $order_helper->delete_stripe_net( $renewal_order ); // Delete payment intent ID. $renewal_order->delete_meta_data( '_stripe_intent_id' ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index 494890bfbf..c484854f94 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -258,7 +258,7 @@ public function process_payment( $order_id, $retry = true, $force_save_save = fa $order = wc_get_order( $order_id ); // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index 256b63103a..47a90cd40d 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -246,7 +246,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index ec831bc1dc..97d30beb69 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -245,7 +245,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index 63ac5166c9..4f45b9d154 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -241,7 +241,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index 9dddadcc24..b4f7976fad 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -245,7 +245,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index 260b4943e8..16b47d785b 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -349,7 +349,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index 196fb926e6..ff8c1a9d3c 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -242,7 +242,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index 52b049d537..6e13fbf2c8 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -250,7 +250,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $order = wc_get_order( $order_id ); // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); // This comes from the create account checkbox in the checkout page. $create_account = ! empty( $_POST['createaccount'] ) ? true : false; diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index 5f25b7f147..183954ca94 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -63,7 +63,7 @@ public function test_lockers(): void { $order_helper->lock_order_refund( $order ); $this->assertTrue( $order_helper->get_order_existing_refund_lock( $order ) > 0 ); $order_helper->unlock_order_refund( $order ); - $this->assertEmpty( $order_helper->get_order_existing_payment_lock( $order ) ); + $this->assertEmpty( $order_helper->get_order_existing_refund_lock( $order ) ); // payment $order_helper->lock_order_payment( $order ); diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index 83185bbdd7..2d95de3e2e 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -507,12 +507,15 @@ public function test_process_payment_intent( $order = WC_Helper_Order::create_order(); $order->set_status( $order_status ); + if ( $order_locked ) { - $order->update_meta_data( '_stripe_lock_payment', ( time() + MINUTE_IN_SECONDS ) ); + WC_Stripe_Order_Helper::get_instance()->lock_order_payment( $order ); } + if ( $order_status_final ) { $order->update_meta_data( '_stripe_status_final', true ); } + $order->update_meta_data( '_stripe_upe_payment_type', $payment_type ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save_meta_data(); From a871fdfb94d8311617f204cbf7b8a759620ab709 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 09:27:37 -0300 Subject: [PATCH 46/71] Fix tests --- tests/phpunit/WC_Stripe_Order_Helper_Test.php | 85 +++++++++++-------- 1 file changed, 50 insertions(+), 35 deletions(-) diff --git a/tests/phpunit/WC_Stripe_Order_Helper_Test.php b/tests/phpunit/WC_Stripe_Order_Helper_Test.php index 183954ca94..f0af135a0d 100644 --- a/tests/phpunit/WC_Stripe_Order_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Helper_Test.php @@ -17,6 +17,24 @@ * Class WC_Stripe_Order_Helper tests. */ class WC_Stripe_Order_Helper_Test extends WP_UnitTestCase { + /** + * Order helper instance. + * + * @var WC_Stripe_Order_Helper + */ + protected $helper; + + /** + * @inheritDoc + * @return void + */ + public function set_up(): void { + parent::set_up(); + + // Ensure the helper is reset before each test. + $this->helper = new WC_Stripe_Order_Helper(); + } + /** * Tests for getters and setters. * @@ -25,31 +43,32 @@ class WC_Stripe_Order_Helper_Test extends WP_UnitTestCase { public function test_properties(): void { $order = WC_Helper_Order::create_order(); - $order_helper = WC_Stripe_Order_Helper::get_instance(); - // Tests for `is_payment_awaiting_action`, `set_payment_awaiting_action`, and `remove_payment_awaiting_action`. - $order_helper->set_payment_awaiting_action( $order ); - $this->assertTrue( $order_helper->is_payment_awaiting_action( $order ) ); + $this->helper->set_payment_awaiting_action( $order ); + $this->assertTrue( $this->helper->is_payment_awaiting_action( $order ) ); - $order_helper->remove_payment_awaiting_action( $order ); - $this->assertFalse( $order_helper->is_payment_awaiting_action( $order ) ); + $this->helper->remove_payment_awaiting_action( $order ); + $this->assertFalse( $this->helper->is_payment_awaiting_action( $order ) ); - $order_helper->update_stripe_fee( $order, 100 ); - $order_helper->update_stripe_net( $order, 100 ); + // Tests for `update_stripe_fee`, `get_stripe_fee`, `delete_stripe_fee`, + // `update_stripe_net`, `get_stripe_net`, and `delete_stripe_net`. + $this->helper->update_stripe_fee( $order, 100 ); + $this->helper->update_stripe_net( $order, 100 ); - $this->assertEquals( 100, $order_helper->get_stripe_fee( $order ) ); - $this->assertEquals( 100, $order_helper->get_stripe_net( $order ) ); + $this->assertEquals( 100, $this->helper->get_stripe_fee( $order ) ); + $this->assertEquals( 100, $this->helper->get_stripe_net( $order ) ); - $order_helper->delete_stripe_fee( $order ); - $order_helper->delete_stripe_net( $order ); + $this->helper->delete_stripe_fee( $order ); + $this->helper->delete_stripe_net( $order ); $order->save_meta_data(); - $this->assertEmpty( $order_helper->get_stripe_fee( $order ) ); - $this->assertEmpty( $order_helper->get_stripe_net( $order ) ); + $this->assertEmpty( $this->helper->get_stripe_fee( $order ) ); + $this->assertEmpty( $this->helper->get_stripe_net( $order ) ); } /** - * Tests for `lock_refund`, `get_lock_refund`, `unlock_refund`, `lock_payment`, `get_lock_payment`, and `unlock_payment`. + * Tests for `lock_order_refund`, `get_order_existing_refund_lock`, `unlock_order_refund`, + * `lock_order_payment`, `get_order_existing_payment_lock`, and `unlock_order_payment`. * * @return void */ @@ -57,19 +76,17 @@ public function test_lockers(): void { // setup $order = WC_Helper_Order::create_order(); - $order_helper = WC_Stripe_Order_Helper::get_instance(); - // refund - $order_helper->lock_order_refund( $order ); - $this->assertTrue( $order_helper->get_order_existing_refund_lock( $order ) > 0 ); - $order_helper->unlock_order_refund( $order ); - $this->assertEmpty( $order_helper->get_order_existing_refund_lock( $order ) ); + $this->helper->lock_order_refund( $order ); + $this->assertTrue( $this->helper->get_order_existing_refund_lock( $order ) > 0 ); + $this->helper->unlock_order_refund( $order ); + $this->assertEmpty( $this->helper->get_order_existing_refund_lock( $order ) ); // payment - $order_helper->lock_order_payment( $order ); - $this->assertTrue( $order_helper->get_order_existing_payment_lock( $order ) > 0 ); - $order_helper->unlock_order_payment( $order ); - $this->assertEmpty( $order_helper->get_order_existing_payment_lock( $order ) ); + $this->helper->lock_order_payment( $order ); + $this->assertTrue( $this->helper->get_order_existing_payment_lock( $order ) > 0 ); + $this->helper->unlock_order_payment( $order ); + $this->assertEmpty( $this->helper->get_order_existing_payment_lock( $order ) ); } /** @@ -82,12 +99,10 @@ public function test_add_payment_intent_to_order(): void { $order = WC_Helper_Order::create_order(); $order_id = $order->get_id(); - $order_helper = WC_Stripe_Order_Helper::get_instance(); - // add_payment_intent_to_order $intent_id = 'pi_123'; - $order_helper->add_payment_intent_to_order( $intent_id, $order ); - $this->assertEquals( $intent_id, $order_helper->get_intent_id_from_order( $order ) ); + $this->helper->add_payment_intent_to_order( $intent_id, $order ); + $this->assertEquals( $intent_id, $this->helper->get_intent_id_from_order( $order ) ); $note = wc_get_order_notes( [ @@ -112,7 +127,7 @@ public function test_validate_minimum_order_amount(): void { $this->expectException( WC_Stripe_Exception::class ); $this->expectExceptionMessage( 'Did not meet minimum amount' ); - WC_Stripe_Order_Helper::get_instance()->validate_minimum_order_amount( $order ); + $this->helper->validate_minimum_order_amount( $order ); } /** @@ -129,7 +144,7 @@ public function test_get_owner_details(): void { $order->set_billing_email( 'test@example.com' ); $order->save_meta_data(); - $owner_details = WC_Stripe_Order_Helper::get_instance()->get_owner_details( $order ); + $owner_details = $this->helper->get_owner_details( $order ); $this->assertEquals( '+1 123 1234', $owner_details->phone ); $this->assertEquals( 'John Doe', $owner_details->name ); @@ -143,20 +158,20 @@ public function test_get_owner_details(): void { * @throws WC_Data_Exception */ public function test_is_stripe_gateway_order(): void { - $order_helper = WC_Stripe_Order_Helper::get_instance(); + $this->helper = WC_Stripe_Order_Helper::get_instance(); // Test with a Stripe order (Klarna). $order = WC_Helper_Order::create_order(); $order->set_payment_method( 'stripe_klarna' ); - $this->assertTrue( $order_helper->is_stripe_gateway_order( $order ) ); + $this->assertTrue( $this->helper->is_stripe_gateway_order( $order ) ); // Test with a non-Stripe order. $order = WC_Helper_Order::create_order(); $order->set_payment_method( 'cod' ); - $this->assertFalse( $order_helper->is_stripe_gateway_order( $order ) ); + $this->assertFalse( $this->helper->is_stripe_gateway_order( $order ) ); // Test with an empty order. $order = new WC_Order(); - $this->assertFalse( $order_helper->is_stripe_gateway_order( $order ) ); + $this->assertFalse( $this->helper->is_stripe_gateway_order( $order ) ); } } From fb5c599af8f9be27ea3d6043a133686a4808b8a0 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 09:53:29 -0300 Subject: [PATCH 47/71] Fix tests --- .../WC_Stripe_UPE_Payment_Gateway_Test.php | 5 +---- .../WC_Stripe_Webhook_Handler_Test.php | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index 8ef19fd3d0..e6f4106d95 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -251,10 +251,7 @@ public function set_up() { ); $order_helper->expects( $this->any() ) - ->method( 'unlock_order_payment' ) - ->will( - $this->returnValue( null ) - ); + ->method( 'unlock_order_payment' ); WC_Stripe_Order_Helper::set_instance( $order_helper ); } diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index 2d95de3e2e..9fe9bb0e58 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -55,7 +55,22 @@ class WC_Stripe_Webhook_Handler_Test extends WP_UnitTestCase { */ public function set_up() { parent::set_up(); + $this->mock_webhook_handler(); + + $order_helper = $this->createPartialMock( + WC_Stripe_Order_Helper::class, + [ 'lock_order_payment', 'unlock_order_payment' ] + ); + + $order_helper->expects( $this->any() ) + ->method( 'lock_order_payment' ) + ->willReturn( false ); + + $order_helper->expects( $this->any() ) + ->method( 'unlock_order_payment' ); + + WC_Stripe_Order_Helper::set_instance( $order_helper ); } /** @@ -507,15 +522,12 @@ public function test_process_payment_intent( $order = WC_Helper_Order::create_order(); $order->set_status( $order_status ); - if ( $order_locked ) { - WC_Stripe_Order_Helper::get_instance()->lock_order_payment( $order ); + $order->update_meta_data( '_stripe_lock_payment', ( time() + MINUTE_IN_SECONDS ) ); } - if ( $order_status_final ) { $order->update_meta_data( '_stripe_status_final', true ); } - $order->update_meta_data( '_stripe_upe_payment_type', $payment_type ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save_meta_data(); From 7dd4763d704ab5e29a3b21041b0b7ba18347fc27 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Thu, 18 Sep 2025 10:27:25 -0300 Subject: [PATCH 48/71] Fix tests --- includes/class-wc-stripe-order-helper.php | 4 ++-- tests/phpunit/WC_Stripe_Order_Handler_Test.php | 1 - tests/phpunit/WC_Stripe_Payment_Gateway_Test.php | 2 -- tests/phpunit/WC_Stripe_Webhook_Handler_Test.php | 6 ++++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index e6017733ea..05b4532fbd 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -108,10 +108,10 @@ public static function get_instance(): ?self { /** * Sets the singleton instance of the class. * - * @param WC_Stripe_Order_Helper $instance + * @param WC_Stripe_Order_Helper|null $instance * @return void */ - public static function set_instance( self $instance ) { + public static function set_instance( ?self $instance ) { self::$instance = $instance; } diff --git a/tests/phpunit/WC_Stripe_Order_Handler_Test.php b/tests/phpunit/WC_Stripe_Order_Handler_Test.php index 03d6004a55..27cf76e4e9 100644 --- a/tests/phpunit/WC_Stripe_Order_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Order_Handler_Test.php @@ -5,7 +5,6 @@ use DateTime; use WC_Stripe_Order_Helper; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; -use WC_Stripe_Helper; use WC_Stripe_Order_Handler; use WC_Stripe_UPE_Payment_Gateway; use WP_UnitTestCase; diff --git a/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php b/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php index 7fffc435fb..e05fa0ec9b 100644 --- a/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php +++ b/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php @@ -3,10 +3,8 @@ namespace WooCommerce\Stripe\Tests; use PHPUnit\Framework\MockObject\MockObject; -use stdClass; use WC_Gateway_Stripe; use WC_Gateway_Stripe_Giropay; -use WC_Stripe_Customer; use WC_Stripe_Exception; use WC_Stripe_Helper; use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper; diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index 9fe9bb0e58..dcd8e4f398 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -5,7 +5,6 @@ use Automattic\WooCommerce\Enums\OrderStatus; use WC_Data_Exception; use WC_Order; -use WC_Stripe_Helper; use WC_Stripe_Intent_Status; use WC_Stripe_Order_Helper; use WC_Stripe_Payment_Methods; @@ -522,6 +521,10 @@ public function test_process_payment_intent( $order = WC_Helper_Order::create_order(); $order->set_status( $order_status ); + + // Reset WC_Stripe_Order_Helper instance to avoid issues with other tests. + WC_Stripe_Order_Helper::set_instance( null ); + if ( $order_locked ) { $order->update_meta_data( '_stripe_lock_payment', ( time() + MINUTE_IN_SECONDS ) ); } @@ -623,7 +626,6 @@ public function test_process_webhook_payment_intent_processing() { $this->assertStringContainsString( 'Stripe charge awaiting payment: ch_mock.', $notes[0]->content ); } - /** * Provider for `test_process_payment_intent`. * From 1f9feae54e9f24edc73deacd28fdcd2cbb0f19aa Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Tue, 23 Sep 2025 10:58:05 -0300 Subject: [PATCH 49/71] Handling source ID meta in the order helper --- .../abstract-wc-stripe-payment-gateway.php | 7 +-- includes/admin/class-wc-stripe-privacy.php | 17 +++---- includes/class-wc-stripe-order-helper.php | 45 +++++++++++++++++++ .../compat/trait-wc-stripe-pre-orders.php | 4 +- .../compat/trait-wc-stripe-subscriptions.php | 15 ++++--- .../class-wc-stripe-upe-payment-gateway.php | 5 ++- 6 files changed, 69 insertions(+), 24 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 14a7cd7deb..3e1373d363 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1020,14 +1020,15 @@ public function prepare_order_source( $order = null ) { $stripe_customer->set_id( $stripe_customer_id ); } - $source_id = $order->get_meta( '_stripe_source_id', true ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $source_id = $order_helper->get_stripe_source( $order ); // Since 4.0.0, we changed card to source so we need to account for that. if ( empty( $source_id ) ) { $source_id = $order->get_meta( '_stripe_card_id', true ); // Take this opportunity to update the key name. - $order->update_meta_data( '_stripe_source_id', $source_id ); + $order_helper->update_stripe_source( $order, $source_id ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); @@ -1084,7 +1085,7 @@ public function save_source_to_order( $order, $source ) { } if ( $source->source ) { - $order->update_meta_data( '_stripe_source_id', $source->source ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_source( $order, $source->source ); } if ( is_callable( [ $order, 'save' ] ) ) { diff --git a/includes/admin/class-wc-stripe-privacy.php b/includes/admin/class-wc-stripe-privacy.php index de01186b96..ef0f0a3805 100644 --- a/includes/admin/class-wc-stripe-privacy.php +++ b/includes/admin/class-wc-stripe-privacy.php @@ -119,11 +119,8 @@ public function get_privacy_message() { public function order_data_exporter( $email_address, $page = 1 ) { $done = false; $data_to_export = []; - - $orders = $this->get_stripe_orders( $email_address, (int) $page ); - - $done = true; - + $orders = $this->get_stripe_orders( $email_address, (int) $page ); + $done = true; if ( 0 < count( $orders ) ) { foreach ( $orders as $order ) { $data_to_export[] = [ @@ -133,7 +130,7 @@ public function order_data_exporter( $email_address, $page = 1 ) { 'data' => [ [ 'name' => __( 'Stripe payment id', 'woocommerce-gateway-stripe' ), - 'value' => $order->get_meta( '_stripe_source_id', true ), + 'value' => WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $order ), ], [ 'name' => __( 'Stripe customer id', 'woocommerce-gateway-stripe' ), @@ -367,9 +364,8 @@ protected function maybe_handle_subscription( $order ) { } $renewal_orders = class_exists( 'WC_Subscriptions_Renewal_Order' ) ? WC_Subscriptions_Renewal_Order::get_renewal_orders( $order->get_id(), 'WC_Order' ) : []; - foreach ( $renewal_orders as $renewal_order ) { - $renewal_order->delete_meta_data( '_stripe_source_id' ); + WC_Stripe_Order_Helper::get_instance()->delete_stripe_source( $renewal_order ); $renewal_order->delete_meta_data( '_stripe_refund_id' ); $renewal_order->delete_meta_data( '_stripe_customer_id' ); } @@ -388,7 +384,8 @@ protected function maybe_handle_subscription( $order ) { * @return array */ protected function maybe_handle_order( $order ) { - $stripe_source_id = $order->get_meta( '_stripe_source_id', true ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $stripe_source_id = $order_helper->get_stripe_source( $order ); $stripe_refund_id = $order->get_meta( '_stripe_refund_id', true ); $stripe_customer_id = $order->get_meta( '_stripe_customer_id', true ); @@ -401,7 +398,7 @@ protected function maybe_handle_order( $order ) { return [ false, false, [] ]; } - $order->delete_meta_data( '_stripe_source_id' ); + $order_helper->delete_stripe_source( $order ); $order->delete_meta_data( '_stripe_refund_id' ); $order->delete_meta_data( '_stripe_customer_id' ); diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 05b4532fbd..44bfc415d6 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -267,6 +267,51 @@ public function delete_stripe_net( ?WC_Order $order = null ) { $order->delete_meta_data( self::LEGACY_META_STRIPE_NET ); } + /** + * Gets the Stripe source for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return array|false|mixed|string + */ + public function get_stripe_source( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + return $order->get_meta( self::META_STRIPE_SOURCE_ID, true ); + } + + /** + * Updates the Stripe source for order. + * + * @param WC_Order|null $order + * @param string $source_id + * @return false|void + */ + public function update_stripe_source( ?WC_Order $order = null, string $source_id = '' ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( self::META_STRIPE_SOURCE_ID, $source_id ); + } + + /** + * Deletes the Stripe source for order. + * + * @param WC_Order|null $order + * @return false|void + */ + public function delete_stripe_source( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $order->delete_meta_data( self::META_STRIPE_SOURCE_ID ); + } + /** * Adds payment intent id and order note to order if payment intent is not already saved * diff --git a/includes/compat/trait-wc-stripe-pre-orders.php b/includes/compat/trait-wc-stripe-pre-orders.php index a54547a2c5..5a1b3151d2 100644 --- a/includes/compat/trait-wc-stripe-pre-orders.php +++ b/includes/compat/trait-wc-stripe-pre-orders.php @@ -170,10 +170,10 @@ class_exists( 'WC_Pre_Orders_Order' ) && /** * Remove order meta. * - * @param object $order + * @param WC_Order $order */ public function remove_order_source_before_retry( $order ) { - $order->delete_meta_data( '_stripe_source_id' ); + WC_Stripe_Order_Helper::get_instance()->delete_stripe_source( $order ); $order->delete_meta_data( '_stripe_card_id' ); $order->save(); } diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 9e252a2405..72354f3ff5 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -640,11 +640,12 @@ public function maybe_update_source_on_subscription_order( $order, $source, $pay /** * Don't transfer Stripe customer/token meta to resubscribe orders. * - * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription + * @param WC_Order $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription */ public function delete_resubscribe_meta( $resubscribe_order ) { + WC_Stripe_Order_Helper::get_instance()->delete_stripe_source( $resubscribe_order ); + $resubscribe_order->delete_meta_data( '_stripe_customer_id' ); - $resubscribe_order->delete_meta_data( '_stripe_source_id' ); // For BW compat will remove in future. $resubscribe_order->delete_meta_data( '_stripe_card_id' ); // Delete payment intent ID. @@ -679,7 +680,7 @@ public function delete_renewal_meta( $renewal_order ) { */ public function update_failing_payment_method( $subscription, $renewal_order ) { $subscription->update_meta_data( '_stripe_customer_id', $renewal_order->get_meta( '_stripe_customer_id', true ) ); - $subscription->update_meta_data( '_stripe_source_id', $renewal_order->get_meta( '_stripe_source_id', true ) ); + $subscription->update_meta_data( '_stripe_source_id', WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $renewal_order ) ); $subscription->save(); } @@ -838,7 +839,6 @@ public function add_subscription_information_to_intent( $request, $order, $prepa */ private function get_mandate_for_subscription( $order, $payment_method ) { $renewal_order_ids = $order->get_related_orders( 'ids' ); - foreach ( $renewal_order_ids as $renewal_order_id ) { $renewal_order = wc_get_order( $renewal_order_id ); if ( ! $renewal_order instanceof WC_Order ) { @@ -846,7 +846,7 @@ private function get_mandate_for_subscription( $order, $payment_method ) { } $mandate = $renewal_order->get_meta( '_stripe_mandate_id', true ); - $renewal_order_payment_method = $renewal_order->get_meta( '_stripe_source_id', true ); + $renewal_order_payment_method = WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $renewal_order ); // Return from the most recent renewal order with a valid mandate. Mandate is created against a payment method // in Stripe so the payment method should also match to reuse the mandate. @@ -1007,16 +1007,17 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->get_parent() ) { + $order_helper = WC_Stripe_Order_Helper::get_instance(); $parent_order = wc_get_order( $subscription->get_parent_id() ); $stripe_customer_id = $parent_order->get_meta( '_stripe_customer_id', true ); - $stripe_source_id = $parent_order->get_meta( '_stripe_source_id', true ); + $stripe_source_id = $order_helper->get_stripe_source( $parent_order ); // For BW compat will remove in future. if ( empty( $stripe_source_id ) ) { $stripe_source_id = $parent_order->get_meta( '_stripe_card_id', true ); // Take this opportunity to update the key name. - $parent_order->update_meta_data( '_stripe_source_id', $stripe_source_id ); + $order_helper->update_stripe_source( $parent_order, $stripe_source_id ); $parent_order->save(); } } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 628950e416..3c9772d7ad 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1815,8 +1815,9 @@ public function save_payment_method_to_order( $order, $payment_method ) { if ( $payment_method->customer ) { $order->update_meta_data( '_stripe_customer_id', $payment_method->customer ); } + // Save the payment method id as `source_id`, because we use both `sources` and `payment_methods` APIs. - $order->update_meta_data( '_stripe_source_id', $payment_method->payment_method ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_source( $order, $payment_method->payment_method ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); @@ -2889,7 +2890,7 @@ protected function handle_saving_payment_method( WC_Order $order, $payment_metho */ public function set_payment_method_id_for_order( WC_Order $order, string $payment_method_id ) { // Save the payment method id as `source_id`, because we use both `sources` and `payment_methods` APIs. - $order->update_meta_data( '_stripe_source_id', $payment_method_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_source( $order, $payment_method_id ); $order->save_meta_data(); } From 8b5bbacea30cc6082a5da0b535139c48dfcf882b Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Tue, 23 Sep 2025 13:09:23 -0300 Subject: [PATCH 50/71] Handling payment intent, setup intent and refund --- .../abstract-wc-stripe-payment-gateway.php | 16 +- includes/admin/class-wc-stripe-privacy.php | 9 +- includes/class-wc-stripe-helper.php | 13 +- .../class-wc-stripe-intent-controller.php | 2 +- includes/class-wc-stripe-order-helper.php | 144 +++++++++++++++++- includes/class-wc-stripe-webhook-handler.php | 12 +- .../compat/trait-wc-stripe-subscriptions.php | 7 +- 7 files changed, 166 insertions(+), 37 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 3e1373d363..916622a2e9 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1279,7 +1279,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } } - $order->update_meta_data( '_stripe_refund_id', $response->id ); + $order_helper->update_stripe_refund( $order, $response->id ); if ( isset( $response->balance_transaction ) ) { $this->update_fees( $order, $response->balance_transaction ); @@ -1687,8 +1687,9 @@ public function save_intent_to_order( $order, $intent ) { return; } + $order_helper = WC_Stripe_Order_Helper::get_instance(); if ( 'payment_intent' === $intent->object ) { - WC_Stripe_Order_Helper::get_instance()->add_payment_intent_to_order( $intent->id, $order ); + $order_helper->add_payment_intent_to_order( $intent->id, $order ); // TODO: Refactor and add mandate ID support for other payment methods, if necessary. // The mandate ID is not available for the intent object, so we need to fetch the charge. @@ -1701,7 +1702,7 @@ public function save_intent_to_order( $order, $intent ) { $order->update_meta_data( '_stripe_mandate_id', $charge->payment_method_details->acss_debit->mandate ); } } elseif ( 'setup_intent' === $intent->object ) { - $order->update_meta_data( '_stripe_setup_intent', $intent->id ); + $order_helper->update_stripe_setup_intent( $order ); // Add mandate for free trial subscriptions. if ( isset( $intent->mandate ) ) { @@ -1722,15 +1723,14 @@ public function save_intent_to_order( $order, $intent ) { * @return obect|bool Either the intent object or `false`. */ public function get_intent_from_order( $order ) { - $intent_id = $order->get_meta( '_stripe_intent_id' ); - + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $intent_id = $order_helper->get_stripe_intent( $order ); if ( $intent_id ) { return $this->get_intent( 'payment_intents', $intent_id ); } // The order doesn't have a payment intent, but it may have a setup intent. - $intent_id = $order->get_meta( '_stripe_setup_intent' ); - + $intent_id = $order_helper->get_stripe_setup_intent( $order ); if ( $intent_id ) { return $this->get_intent( 'setup_intents', $intent_id ); } @@ -1919,7 +1919,7 @@ public function setup_intent( $order, $prepared_source ) { if ( is_wp_error( $setup_intent ) ) { WC_Stripe_Logger::log( "Unable to create SetupIntent for Order #$order_id: " . print_r( $setup_intent, true ) ); } elseif ( WC_Stripe_Intent_Status::REQUIRES_ACTION === $setup_intent->status ) { - $order->update_meta_data( '_stripe_setup_intent', $setup_intent->id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_setup_intent( $setup_intent->id ); $order->save(); return $setup_intent->client_secret; diff --git a/includes/admin/class-wc-stripe-privacy.php b/includes/admin/class-wc-stripe-privacy.php index ef0f0a3805..f748f78bda 100644 --- a/includes/admin/class-wc-stripe-privacy.php +++ b/includes/admin/class-wc-stripe-privacy.php @@ -365,8 +365,9 @@ protected function maybe_handle_subscription( $order ) { $renewal_orders = class_exists( 'WC_Subscriptions_Renewal_Order' ) ? WC_Subscriptions_Renewal_Order::get_renewal_orders( $order->get_id(), 'WC_Order' ) : []; foreach ( $renewal_orders as $renewal_order ) { - WC_Stripe_Order_Helper::get_instance()->delete_stripe_source( $renewal_order ); - $renewal_order->delete_meta_data( '_stripe_refund_id' ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $order_helper->delete_stripe_source( $renewal_order ); + $order_helper->delete_stripe_refund( $renewal_order ); $renewal_order->delete_meta_data( '_stripe_customer_id' ); } @@ -386,7 +387,7 @@ protected function maybe_handle_subscription( $order ) { protected function maybe_handle_order( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $stripe_source_id = $order_helper->get_stripe_source( $order ); - $stripe_refund_id = $order->get_meta( '_stripe_refund_id', true ); + $stripe_refund_id = $order_helper->get_stripe_refund( $order ); $stripe_customer_id = $order->get_meta( '_stripe_customer_id', true ); if ( ! $this->is_retention_expired( $order->get_date_created()->getTimestamp() ) ) { @@ -399,7 +400,7 @@ protected function maybe_handle_order( $order ) { } $order_helper->delete_stripe_source( $order ); - $order->delete_meta_data( '_stripe_refund_id' ); + $order_helper->delete_stripe_refund( $order ); $order->delete_meta_data( '_stripe_customer_id' ); return [ true, false, [ __( 'Stripe personal data erased.', 'woocommerce-gateway-stripe' ) ] ]; diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index 7c00d7e458..fc1fe811b7 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1304,9 +1304,8 @@ private static function should_load_scripts_for_prb_location( $location ) { * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::add_payment_intent_to_order() instead. */ public static function add_payment_intent_to_order( $payment_intent_id, $order ) { - - $old_intent_id = $order->get_meta( '_stripe_intent_id' ); - + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $old_intent_id = $order_helper->get_stripe_intent( $order ); if ( $old_intent_id === $payment_intent_id ) { return; } @@ -1319,7 +1318,7 @@ public static function add_payment_intent_to_order( $payment_intent_id, $order ) ) ); - $order->update_meta_data( '_stripe_intent_id', $payment_intent_id ); + $order_helper->update_stripe_intent( $order ); $order->save(); } @@ -1421,10 +1420,10 @@ public static function get_payment_method_from_intent( $intent ) { * @deprecated 10.0.0 Use WC_Stripe_Order_Helper::get_intent_id_from_order() instead. */ public static function get_intent_id_from_order( $order ) { - $intent_id = $order->get_meta( '_stripe_intent_id' ); - + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $intent_id = $order_helper->get_stripe_intent( $order ); if ( ! $intent_id ) { - $intent_id = $order->get_meta( '_stripe_setup_intent' ); + $intent_id = $order_helper->get_stripe_setup_intent( $order ); } return $intent_id ?? false; diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 3432512543..855339ad77 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -127,7 +127,7 @@ public function verify_intent() { } // Validate the intent being verified. - $order_intent_id = $order->get_meta( '_stripe_intent_id', true ); + $order_intent_id = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ); if ( ! $order_intent_id || ! isset( $_GET['intent_id'] ) || $order_intent_id !== $_GET['intent_id'] ) { throw new WC_Stripe_Exception( 'invalid_intent', __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 44bfc415d6..22931170ec 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -50,13 +50,6 @@ class WC_Stripe_Order_Helper { */ private const META_STRIPE_SOURCE_ID = '_stripe_source_id'; - /** - * Meta key for Stripe charge ID. - * - * @var string - */ - private const META_STRIPE_CHARGE_ID = '_stripe_charge_id'; - /** * Meta key for Stripe refund ID. * @@ -273,7 +266,7 @@ public function delete_stripe_net( ?WC_Order $order = null ) { * @since 10.0.0 * * @param WC_Order|null $order - * @return array|false|mixed|string + * @return false|string|null */ public function get_stripe_source( ?WC_Order $order = null ) { if ( is_null( $order ) ) { @@ -286,6 +279,8 @@ public function get_stripe_source( ?WC_Order $order = null ) { /** * Updates the Stripe source for order. * + * @since 10.0.0 + * * @param WC_Order|null $order * @param string $source_id * @return false|void @@ -301,6 +296,8 @@ public function update_stripe_source( ?WC_Order $order = null, string $source_id /** * Deletes the Stripe source for order. * + * @since 10.0.0 + * * @param WC_Order|null $order * @return false|void */ @@ -312,6 +309,137 @@ public function delete_stripe_source( ?WC_Order $order = null ) { $order->delete_meta_data( self::META_STRIPE_SOURCE_ID ); } + /** + * Updates the Stripe refund for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|string|null + */ + public function get_stripe_refund( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + return $order->get_meta( self::META_STRIPE_REFUND_ID, true ); + } + + /** + * Updates the Stripe refund for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @param string $refund_id + * @return false|void + */ + public function update_stripe_refund( ?WC_Order $order = null, string $refund_id = '' ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( self::META_STRIPE_REFUND_ID, $refund_id ); + } + + /** + * Deletes the Stripe refund for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|void + */ + public function delete_stripe_refund( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $order->delete_meta_data( self::META_STRIPE_REFUND_ID ); + } + + /** + * Gets the Stripe intent for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|string|null + */ + public function get_stripe_intent( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + return $order->get_meta( self::META_STRIPE_INTENT_ID, true ); + } + + /** + * Updates the Stripe intent for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @param string $intent_id + * @return false|void + */ + public function update_stripe_intent( ?WC_Order $order = null, string $intent_id = '' ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( self::META_STRIPE_INTENT_ID, $intent_id ); + } + + /** + * Deletes the Stripe intent for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|void + */ + public function delete_stripe_intent( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $order->delete_meta_data( self::META_STRIPE_INTENT_ID ); + } + + /** + * Gets the Stripe setup intent for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|string|null + */ + public function get_stripe_setup_intent( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + return $order->get_meta( self::META_STRIPE_SETUP_INTENT, true ); + } + + /** + * Updates the Stripe setup intent for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @param string $intent_id + * @return false|void + */ + public function update_stripe_setup_intent( ?WC_Order $order = null, string $intent_id = '' ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( self::META_STRIPE_SETUP_INTENT, $intent_id ); + } + /** * Adds payment intent id and order note to order if payment intent is not already saved * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index b64cef04bd..52b36ac2ad 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -742,7 +742,7 @@ public function process_webhook_refund( $notification ) { if ( $order_helper->is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $captured = $order->get_meta( '_stripe_charge_captured' ); - $refund_id = $order->get_meta( '_stripe_refund_id' ); + $refund_id = $order_helper->get_stripe_refund( $order ); $currency = $order->get_currency(); $raw_amount = $refund_object->amount; @@ -792,7 +792,7 @@ public function process_webhook_refund( $notification ) { WC_Stripe_Logger::log( $refund->get_error_message() ); } - $order->update_meta_data( '_stripe_refund_id', $refund_object->id ); + $order_helper->update_stripe_refund( $order, $refund_object->id ); if ( isset( $refund_object->balance_transaction ) ) { $this->update_fees( $order, $refund_object->balance_transaction ); @@ -823,11 +823,11 @@ public function process_webhook_refund_updated( $notification ) { // Set the order being processed for the `wc_stripe_webhook_received` action later. $this->resolved_order = $order; - $order_id = $order->get_id(); - - if ( WC_Stripe_Order_Helper::get_instance()->is_stripe_gateway_order( $order ) ) { + $order_id = $order->get_id(); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + if ( $order_helper->is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); - $refund_id = $order->get_meta( '_stripe_refund_id' ); + $refund_id = $order_helper->get_stripe_refund( $order ); $currency = $order->get_currency(); $raw_amount = $refund_object->amount; diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 72354f3ff5..7dd72c7334 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -643,13 +643,14 @@ public function maybe_update_source_on_subscription_order( $order, $source, $pay * @param WC_Order $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription */ public function delete_resubscribe_meta( $resubscribe_order ) { - WC_Stripe_Order_Helper::get_instance()->delete_stripe_source( $resubscribe_order ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $order_helper->delete_stripe_source( $resubscribe_order ); $resubscribe_order->delete_meta_data( '_stripe_customer_id' ); // For BW compat will remove in future. $resubscribe_order->delete_meta_data( '_stripe_card_id' ); // Delete payment intent ID. - $resubscribe_order->delete_meta_data( '_stripe_intent_id' ); + $order_helper->delete_stripe_intent( $resubscribe_order ); $this->delete_renewal_meta( $resubscribe_order ); $resubscribe_order->save(); } @@ -665,7 +666,7 @@ public function delete_renewal_meta( $renewal_order ) { $order_helper->delete_stripe_net( $renewal_order ); // Delete payment intent ID. - $renewal_order->delete_meta_data( '_stripe_intent_id' ); + $order_helper->delete_stripe_intent( $renewal_order ); return $renewal_order; } From d3797fa678f4a64b966456e2c763cb4acbd20cfe Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Tue, 23 Sep 2025 13:41:08 -0300 Subject: [PATCH 51/71] Unit tests --- .../WC_REST_Stripe_Orders_Controller_Test.php | 3 +- .../WC_Stripe_Settings_Controller_Test.php | 6 +- .../WC_Stripe_UPE_Payment_Gateway_Test.php | 79 ++++++++++--------- tests/phpunit/WC_Stripe_Helper_Test.php | 3 +- .../WC_Stripe_Payment_Gateway_Test.php | 12 ++- .../WC_Stripe_Subscription_Initial_Test.php | 3 +- .../WC_Stripe_Subscription_Renewal_Test.php | 5 +- .../WC_Stripe_Webhook_Handler_Test.php | 2 +- 8 files changed, 65 insertions(+), 48 deletions(-) diff --git a/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php b/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php index 73ca9a9a6f..bc31d10f44 100644 --- a/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php +++ b/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php @@ -3,6 +3,7 @@ namespace WooCommerce\Stripe\Tests\Admin; use WC_Stripe_Intent_Status; +use WC_Stripe_Order_Helper; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; use WP_REST_Request; use WP_UnitTestCase; @@ -149,7 +150,7 @@ public function test_capture_payment_success() { $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 'succeeded', $response->get_data()['status'] ); $this->assertEquals( 'ch_12345', $response->get_data()['id'] ); - $this->assertEquals( 'pi_12345', $order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( 'pi_12345', WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ) ); remove_filter( 'pre_http_request', $test_request, 10, 3 ); } diff --git a/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php b/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php index af173784dc..938a80e956 100644 --- a/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php +++ b/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php @@ -6,6 +6,7 @@ use WC_Stripe_Account; use WC_Stripe_Helper; use WC_Stripe_Intent_Status; +use WC_Stripe_Order_Helper; use WC_Stripe_Settings_Controller; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; use WP_UnitTestCase; @@ -93,11 +94,10 @@ public function test_admin_options_when_stripe_is_not_connected() { * @return void */ public function test_add_buttons_action_is_called_on_order_admin_page() { - $order = WC_Helper_Order::create_order(); - $order_id = $order->get_id(); + $order = WC_Helper_Order::create_order(); $intent_id = 'pi_mock'; - update_post_meta( $order_id, '_stripe_intent_id', $intent_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $intent_id ); $intent = (object) [ 'id' => 'pi_123', diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index e6f4106d95..65ffc94f74 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -470,7 +470,7 @@ public function test_process_payment_returns_valid_response() { $currency = $order->get_currency(); $order_id = $order->get_id(); - $order->update_meta_data( '_stripe_intent_id', $payment_intent_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $payment_intent_id ); $order->update_meta_data( '_stripe_upe_payment_type', '' ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save(); @@ -1043,7 +1043,7 @@ public function test_process_redirect_payment_returns_valid_response() { $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $final_order ) ); $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1109,7 +1109,7 @@ public function test_process_redirect_payment_only_runs_once() { // assert successful order processing $this->assertEquals( OrderStatus::PROCESSING, $success_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $success_order->get_payment_method_title() ); - $this->assertEquals( $payment_intent_id, $success_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $success_order ) ); $this->assertTrue( (bool) $success_order->get_meta( '_stripe_upe_redirect_processed', true ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); @@ -1205,7 +1205,7 @@ public function test_checkout_without_payment_uses_setup_intents() { $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $final_order ) ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); } @@ -1264,12 +1264,13 @@ public function test_checkout_saves_payment_method_to_order() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, true ); - $final_order = wc_get_order( $order_id ); + $final_order = wc_get_order( $order_id ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } /** @@ -1333,12 +1334,13 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, true ); - $final_order = wc_get_order( $order_id ); + $final_order = wc_get_order( $order_id ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $generated_payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } /** @@ -1392,11 +1394,11 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); - $final_order = wc_get_order( $order_id ); + $final_order = wc_get_order( $order_id ); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $generated_payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $generated_payment_method_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $final_order ) ); } /** @@ -1629,20 +1631,21 @@ function ( $passed_order ) use ( $order ) { ->method( 'get_latest_charge_from_intent' ) ->willReturn( $this->array_to_object( $charge ) ); - $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_get_order( $order_id ); - $note = wc_get_order_notes( + $response = $this->mock_gateway->process_payment( $order_id ); + $final_order = wc_get_order( $order_id ); + $note = wc_get_order_notes( [ 'order_id' => $order_id, 'limit' => 1, ] )[0]; + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1720,12 +1723,13 @@ function ( $passed_order ) use ( $order ) { $response = $this->mock_gateway->process_payment( $order_id ); $final_order = wc_get_order( $order_id ); $client_secret = $payment_intent_mock->client_secret; + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PENDING, $final_order->get_status() ); // Order status should be pending until 3DS is completed. - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( "/#wc-stripe-confirm-pi:$order_id:$client_secret/", $response['redirect'] ); } @@ -1780,13 +1784,14 @@ function ( $passed_order ) use ( $order ) { ) ); - $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_get_order( $order_id ); + $response = $this->mock_gateway->process_payment( $order_id ); + $final_order = wc_get_order( $order_id ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( 'failure', $response['result'] ); $this->assertEquals( OrderStatus::FAILED, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } /** @@ -1875,20 +1880,21 @@ function ( $passed_order ) use ( $order ) { ->method( 'get_latest_charge_from_intent' ) ->willReturn( $this->array_to_object( $charge ) ); - $response = $this->mock_gateway->process_payment( $order_id ); - $final_order = wc_get_order( $order_id ); - $note = wc_get_order_notes( + $response = $this->mock_gateway->process_payment( $order_id ); + $final_order = wc_get_order( $order_id ); + $note = wc_get_order_notes( [ 'order_id' => $order_id, 'limit' => 1, ] )[0]; + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1992,7 +1998,7 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { $currency = $order->get_currency(); $order_id = $order->get_id(); - $order->update_meta_data( '_stripe_intent_id', $payment_intent_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $payment_intent_id ); $order->update_meta_data( '_stripe_upe_payment_type', '' ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save(); @@ -2370,12 +2376,13 @@ public function test_pre_order_payment_is_successful() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $final_order = wc_get_order( $order_id ); + $final_order = wc_get_order( $order_id ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_intent_id, $final_order->get_meta( '_stripe_intent_id', true ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); } @@ -2437,9 +2444,9 @@ public function test_pre_order_without_payment_uses_setup_intents() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); - $final_order = wc_get_order( $order_id ); + $final_order = wc_get_order( $order_id ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $final_order ) ); $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); } @@ -2671,7 +2678,7 @@ public function test_process_payment_creates_new_intent_when_existing_intent_fai ]; // Save the failed intent ID to the order - $order->update_meta_data( '_stripe_intent_id', $mock_failed_intent->id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $mock_failed_intent->id ); $order->save(); // Mock that we find an existing failed intent on the order @@ -2808,7 +2815,7 @@ function ( $passed_order ) use ( $order ) { )[0]; $this->assertEquals( 'success', $response['result'] ); - $this->assertEquals( $payment_method_id, $final_order->get_meta( '_stripe_source_id', true ) ); + $this->assertEquals( $payment_method_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } diff --git a/tests/phpunit/WC_Stripe_Helper_Test.php b/tests/phpunit/WC_Stripe_Helper_Test.php index 7206db3be4..3d54e0d373 100644 --- a/tests/phpunit/WC_Stripe_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Helper_Test.php @@ -7,6 +7,7 @@ use WC_Order; use WC_Stripe_Currency_Code; use WC_Stripe_Helper; +use WC_Stripe_Order_Helper; use WC_Stripe_Payment_Methods; use WooCommerce\Stripe\Tests\Helpers\UPE_Test_Helper; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; @@ -216,7 +217,7 @@ public function test_get_order_by_intent_id( $status, $success ) { $order->set_status( $status ); $intent_id = 'pi_mock'; - update_post_meta( $order_id, '_stripe_intent_id', $intent_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $intent_id ); $order = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); if ( $success ) { diff --git a/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php b/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php index e05fa0ec9b..1d838cdcc8 100644 --- a/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php +++ b/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php @@ -7,6 +7,7 @@ use WC_Gateway_Stripe_Giropay; use WC_Stripe_Exception; use WC_Stripe_Helper; +use WC_Stripe_Order_Helper; use WooCommerce\Stripe\Tests\Helpers\OC_Test_Helper; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; use WP_Error; @@ -95,7 +96,9 @@ public function test_default_get_payment_intent_from_order() { */ public function test_success_get_payment_intent_from_order() { $order = WC_Helper_Order::create_order(); - $this->updateOrderMeta( $order, '_stripe_intent_id', 'pi_123' ); + + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, 'pi_123' ); + $expected_intent = (object) [ 'id' => 'pi_123' ]; $callback = function ( $preempt, $request_args, $url ) use ( $expected_intent ) { $response = [ @@ -126,7 +129,9 @@ public function test_success_get_payment_intent_from_order() { */ public function test_error_get_payment_intent_from_order() { $order = WC_Helper_Order::create_order(); - $this->updateOrderMeta( $order, '_stripe_intent_id', 'pi_123' ); + + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, 'pi_123' ); + $response_error = (object) [ 'error' => [ 'code' => 'resource_missing', @@ -843,9 +848,10 @@ public function test_process_refund_success() { */ public function test_process_refund_voids_pre_auth_on_cancel() { $order = WC_Helper_Order::create_order(); + $order->set_transaction_id( 'ch_123' ); $this->updateOrderMeta( $order, '_stripe_charge_captured', 'no' ); - $this->updateOrderMeta( $order, '_stripe_intent_id', 'pi_123' ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, 'pi_123' ); $order->save(); $order_id = $order->get_id(); diff --git a/tests/phpunit/WC_Stripe_Subscription_Initial_Test.php b/tests/phpunit/WC_Stripe_Subscription_Initial_Test.php index 670921886c..96eea684c3 100644 --- a/tests/phpunit/WC_Stripe_Subscription_Initial_Test.php +++ b/tests/phpunit/WC_Stripe_Subscription_Initial_Test.php @@ -4,6 +4,7 @@ use WC_Stripe_Helper; use WC_Stripe_Intent_Status; +use WC_Stripe_Order_Helper; use WC_Stripe_Payment_Methods; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; use WP_UnitTestCase; @@ -212,7 +213,7 @@ public function test_initial_intent_parameters() { $this->assertArrayHasKey( 'redirect', $result ); $order = wc_get_order( $order_id ); - $order_data = $order->get_meta( '_stripe_intent_id' ); + $order_data = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ); $this->assertEquals( $order_data, 'pi_123abc' ); diff --git a/tests/phpunit/WC_Stripe_Subscription_Renewal_Test.php b/tests/phpunit/WC_Stripe_Subscription_Renewal_Test.php index 03d39f4357..ce8c0c82d6 100644 --- a/tests/phpunit/WC_Stripe_Subscription_Renewal_Test.php +++ b/tests/phpunit/WC_Stripe_Subscription_Renewal_Test.php @@ -5,6 +5,7 @@ use Automattic\WooCommerce\Enums\OrderStatus; use WC_Stripe_Feature_Flags; use WC_Stripe_Helper; +use WC_Stripe_Order_Helper; use WC_Stripe_Payment_Methods; use WooCommerce\Stripe\Tests\Helpers\WC_Helper_Order; use WP_UnitTestCase; @@ -246,7 +247,7 @@ public function test_renewal_successful() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); $order = wc_get_order( $order_id ); - $order_data = $order->get_meta( '_stripe_intent_id' ); + $order_data = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ); $this->assertEquals( $order_data, 'pi_123abc' ); @@ -366,7 +367,7 @@ public function test_renewal_authorization_required() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); $order = wc_get_order( $order_id ); - $order_data = $order->get_meta( '_stripe_intent_id' ); + $order_data = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ); $order_transaction_id = $order->get_transaction_id(); // Intent was saved to order even though there was an error in the response body. diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index dcd8e4f398..92fe363b64 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -803,7 +803,7 @@ public function test_process_webhook_refund_updated( $notification_status, $emai $order->set_transaction_id( $charge_id ); $order->save(); - $order->update_meta_data( '_stripe_refund_id', $refund_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_refund( $order, $refund_id ); $order->save_meta_data(); $refund_order = WC_Helper_Order::create_order(); From e942cb56449b7d15d58ea84ed5e4c487ae73aab5 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Tue, 23 Sep 2025 13:42:42 -0300 Subject: [PATCH 52/71] Changelog and readme entries --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index ff616142a1..0829c62d4a 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 9.10.0 - xxxx-xx-xx = +* Dev - Stripe Order Helper class now handles refund ID, intent ID, and setup intent ID metas * Dev - Introduces a new helper class to handle Stripe orders * Dev - Fixes a warning thrown when running Klarna payment token PHP Unit tests * Dev - Fixes some possible warnings shown in the browser console when the Optimized Checkout payment element is instantiated with invalid parameters diff --git a/readme.txt b/readme.txt index dc54fed84d..49b52103ce 100644 --- a/readme.txt +++ b/readme.txt @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o == Changelog == = 9.10.0 - xxxx-xx-xx = +* Dev - Stripe Order Helper class now handles refund ID, intent ID, and setup intent ID metas * Dev - Introduces a new helper class to handle Stripe orders * Dev - Fixes a warning thrown when running Klarna payment token PHP Unit tests * Dev - Fixes some possible warnings shown in the browser console when the Optimized Checkout payment element is instantiated with invalid parameters From 206be4fa72c19eadd78f0fc6362a03eb2b3777ed Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 26 Sep 2025 15:37:22 -0300 Subject: [PATCH 53/71] Changelog and readme entries --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index 0829c62d4a..536f3b5ab7 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 9.10.0 - xxxx-xx-xx = +* Dev - Expands the Stripe Order Helper class to handle source ID, refund ID, intent ID, and setup intent ID metas * Dev - Stripe Order Helper class now handles refund ID, intent ID, and setup intent ID metas * Dev - Introduces a new helper class to handle Stripe orders * Dev - Fixes a warning thrown when running Klarna payment token PHP Unit tests diff --git a/readme.txt b/readme.txt index 49b52103ce..ad60805217 100644 --- a/readme.txt +++ b/readme.txt @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o == Changelog == = 9.10.0 - xxxx-xx-xx = +* Dev - Expands the Stripe Order Helper class to handle source ID, refund ID, intent ID, and setup intent ID metas * Dev - Stripe Order Helper class now handles refund ID, intent ID, and setup intent ID metas * Dev - Introduces a new helper class to handle Stripe orders * Dev - Fixes a warning thrown when running Klarna payment token PHP Unit tests From f683c88298bd144d53d31096cd6c904a4d9db869 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 26 Sep 2025 15:41:41 -0300 Subject: [PATCH 54/71] Fix tests --- tests/phpunit/WC_Stripe_Helper_Test.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/WC_Stripe_Helper_Test.php b/tests/phpunit/WC_Stripe_Helper_Test.php index 3d54e0d373..991115a845 100644 --- a/tests/phpunit/WC_Stripe_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Helper_Test.php @@ -218,6 +218,7 @@ public function test_get_order_by_intent_id( $status, $success ) { $intent_id = 'pi_mock'; WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $intent_id ); + $order->save_meta_data(); $order = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); if ( $success ) { From 5511198cc409265a519f9199989634ef62b90688 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 26 Sep 2025 15:57:15 -0300 Subject: [PATCH 55/71] Fix changelog and readme entries --- changelog.txt | 1 - readme.txt | 1 - 2 files changed, 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index 3ce61e7592..bf5f0ccfed 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,7 +2,6 @@ = 10.0.0 - xxxx-xx-xx = * Dev - Expands the Stripe Order Helper class to handle source ID, refund ID, intent ID, and setup intent ID metas -* Dev - Stripe Order Helper class now handles refund ID, intent ID, and setup intent ID metas * Update - Removing the `wc_stripe_is_upe_checkout_enabled` filter, as Legacy Checkout is no longer supported * Dev - Upgrades `jest` to version 29.7.0, `@wordpress/scripts` to 26.19.0, and adds `axios`(version 1.12.2) to the JavaScript development dependencies * Update - Shows the Stripe account connection modal in settings when the merchant did not connect via OAuth along with a new notice diff --git a/readme.txt b/readme.txt index 22ddc56577..b64fd85027 100644 --- a/readme.txt +++ b/readme.txt @@ -112,7 +112,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o = 10.0.0 - xxxx-xx-xx = * Dev - Expands the Stripe Order Helper class to handle source ID, refund ID, intent ID, and setup intent ID metas -* Dev - Stripe Order Helper class now handles refund ID, intent ID, and setup intent ID metas * Update - Removing the `wc_stripe_is_upe_checkout_enabled` filter, as Legacy Checkout is no longer supported * Dev - Upgrades `jest` to version 29.7.0, `@wordpress/scripts` to 26.19.0, and adds `axios`(version 1.12.2) to the JavaScript development dependencies * Update - Shows the Stripe account connection modal in settings when the merchant did not connect via OAuth along with a new notice From a63d49394d545a7ead09ef180cfcf30dac8c0d0e Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 26 Sep 2025 15:59:36 -0300 Subject: [PATCH 56/71] Minor test improvement --- tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php b/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php index 938a80e956..06c7f7cb88 100644 --- a/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php +++ b/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php @@ -98,6 +98,7 @@ public function test_add_buttons_action_is_called_on_order_admin_page() { $intent_id = 'pi_mock'; WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $intent_id ); + $order->save_meta_data(); $intent = (object) [ 'id' => 'pi_123', From a11bbcde1ff4f92c0dac352fcb23a16199321f02 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 29 Sep 2025 18:38:11 -0300 Subject: [PATCH 57/71] Handling more Stripe metas with the order helper class --- .../abstract-wc-stripe-payment-gateway.php | 10 ++-- ...class-wc-rest-stripe-orders-controller.php | 6 +- includes/admin/class-wc-stripe-privacy.php | 11 ++-- includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-order-helper.php | 56 +++++++++++++++++++ includes/class-wc-stripe-webhook-handler.php | 2 +- .../compat/trait-wc-stripe-subscriptions.php | 9 +-- .../class-wc-gateway-stripe-sepa.php | 6 +- .../class-wc-stripe-upe-payment-gateway.php | 7 ++- .../WC_REST_Stripe_Orders_Controller_Test.php | 7 ++- .../WC_Stripe_UPE_Payment_Gateway_Test.php | 29 +++++----- 11 files changed, 107 insertions(+), 38 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 916622a2e9..c3cd078083 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -330,7 +330,7 @@ public function maybe_remove_non_existent_customer( $error, $order ) { } delete_user_option( $order->get_customer_id(), '_stripe_customer_id' ); - $order->delete_meta_data( '_stripe_customer_id' ); + WC_Stripe_Order_Helper::get_instance()->delete_stripe_customer( $order ); $order->save(); return true; @@ -410,7 +410,7 @@ public function get_transaction_url( $order ) { */ public function get_stripe_customer_id( $order ) { // Try to get it via the order first. - $customer = $order->get_meta( '_stripe_customer_id', true ); + $customer = WC_Stripe_Order_Helper::get_instance()->get_stripe_customer( $order ); if ( empty( $customer ) ) { $customer = get_user_option( '_stripe_customer_id', $order->get_customer_id() ); @@ -1079,13 +1079,15 @@ public function check_source( $prepared_source ) { * @param stdClass $source Source information. */ public function save_source_to_order( $order, $source ) { + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // Store source in the order. if ( $source->customer ) { - $order->update_meta_data( '_stripe_customer_id', $source->customer ); + $order_helper->update_stripe_customer( $order, $source->customer ); } if ( $source->source ) { - WC_Stripe_Order_Helper::get_instance()->update_stripe_source( $order, $source->source ); + $order_helper->update_stripe_source( $order, $source->source ); } if ( is_callable( [ $order, 'save' ] ) ) { diff --git a/includes/admin/class-wc-rest-stripe-orders-controller.php b/includes/admin/class-wc-rest-stripe-orders-controller.php index 1526f50ade..df3763c932 100644 --- a/includes/admin/class-wc-rest-stripe-orders-controller.php +++ b/includes/admin/class-wc-rest-stripe-orders-controller.php @@ -125,8 +125,10 @@ public function create_customer( $request ) { } $customer = new WC_Stripe_Customer( $order_user->ID ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // Set the customer ID if known but not already set. - $customer_id = $order->get_meta( '_stripe_customer_id', true ); + $customer_id = $order_helper->update_stripe_customer( $order ); if ( ! $customer->get_id() && $customer_id ) { $customer->set_id( $customer_id ); } @@ -143,7 +145,7 @@ public function create_customer( $request ) { return new WP_Error( 'stripe_error', $e->getMessage() ); } - $order->update_meta_data( '_stripe_customer_id', $customer_id ); + $order_helper->update_stripe_customer( $order, $customer_id ); $order->save(); return rest_ensure_response( [ 'id' => $customer_id ] ); diff --git a/includes/admin/class-wc-stripe-privacy.php b/includes/admin/class-wc-stripe-privacy.php index f748f78bda..45b025b5c7 100644 --- a/includes/admin/class-wc-stripe-privacy.php +++ b/includes/admin/class-wc-stripe-privacy.php @@ -120,6 +120,7 @@ public function order_data_exporter( $email_address, $page = 1 ) { $done = false; $data_to_export = []; $orders = $this->get_stripe_orders( $email_address, (int) $page ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); $done = true; if ( 0 < count( $orders ) ) { foreach ( $orders as $order ) { @@ -130,11 +131,11 @@ public function order_data_exporter( $email_address, $page = 1 ) { 'data' => [ [ 'name' => __( 'Stripe payment id', 'woocommerce-gateway-stripe' ), - 'value' => WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $order ), + 'value' => $order_helper->get_stripe_source( $order ), ], [ 'name' => __( 'Stripe customer id', 'woocommerce-gateway-stripe' ), - 'value' => $order->get_meta( '_stripe_customer_id', true ), + 'value' => $order_helper->get_stripe_customer( $order ), ], ], ]; @@ -368,7 +369,7 @@ protected function maybe_handle_subscription( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $order_helper->delete_stripe_source( $renewal_order ); $order_helper->delete_stripe_refund( $renewal_order ); - $renewal_order->delete_meta_data( '_stripe_customer_id' ); + $order_helper->delete_stripe_customer( $renewal_order ); } $subscription->delete_meta_data( '_stripe_source_id' ); @@ -388,7 +389,7 @@ protected function maybe_handle_order( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $stripe_source_id = $order_helper->get_stripe_source( $order ); $stripe_refund_id = $order_helper->get_stripe_refund( $order ); - $stripe_customer_id = $order->get_meta( '_stripe_customer_id', true ); + $stripe_customer_id = $order_helper->get_stripe_customer( $order ); if ( ! $this->is_retention_expired( $order->get_date_created()->getTimestamp() ) ) { /* translators: %d Order ID */ @@ -401,7 +402,7 @@ protected function maybe_handle_order( $order ) { $order_helper->delete_stripe_source( $order ); $order_helper->delete_stripe_refund( $order ); - $order->delete_meta_data( '_stripe_customer_id' ); + $order_helper->delete_stripe_customer( $order ); return [ true, false, [ __( 'Stripe personal data erased.', 'woocommerce-gateway-stripe' ) ] ]; } diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 8cbec247cc..3daeb115bf 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -170,7 +170,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. if ( $this->is_no_such_customer_error( $response->error ) ) { delete_user_option( $order->get_customer_id(), '_stripe_customer_id' ); - $order->delete_meta_data( '_stripe_customer_id' ); + $order_helper->delete_stripe_customer( $order ); $order->save(); } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 22931170ec..bde7544de0 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -71,6 +71,13 @@ class WC_Stripe_Order_Helper { */ private const META_STRIPE_SETUP_INTENT = '_stripe_setup_intent'; + /** + * Meta key for Stripe customer ID. + * + * @var string + */ + private const META_STRIPE_CUSTOMER_ID = '_stripe_customer_id'; + /** * Meta key for payment awaiting action. * @@ -440,6 +447,55 @@ public function update_stripe_setup_intent( ?WC_Order $order = null, string $int $order->update_meta_data( self::META_STRIPE_SETUP_INTENT, $intent_id ); } + /** + * Gets the Stripe customer for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|string|null + */ + public function get_stripe_customer( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + return $order->get_meta( self::META_STRIPE_CUSTOMER_ID, true ); + } + + /** + * Updates the Stripe customer for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @param string $customer_id + * @return false|void + */ + public function update_stripe_customer( ?WC_Order $order = null, string $customer_id = '' ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( self::META_STRIPE_CUSTOMER_ID, $customer_id ); + } + + /** + * Deletes the Stripe customer for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|void + */ + public function delete_stripe_customer( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $order->delete_meta_data( self::META_STRIPE_CUSTOMER_ID ); + } + /** * Adds payment intent id and order note to order if payment intent is not already saved * diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 52b36ac2ad..da0383a625 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -318,7 +318,7 @@ public function process_webhook_payment( $notification, $retry = true ) { // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. if ( $this->is_no_such_customer_error( $response->error ) ) { delete_user_option( $order->get_customer_id(), '_stripe_customer_id' ); - $order->delete_meta_data( '_stripe_customer_id' ); + $order_helper->delete_stripe_customer( $order ); $order->save(); } diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 7dd72c7334..c20b1fcfe3 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -646,7 +646,7 @@ public function delete_resubscribe_meta( $resubscribe_order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $order_helper->delete_stripe_source( $resubscribe_order ); - $resubscribe_order->delete_meta_data( '_stripe_customer_id' ); + $order_helper->delete_stripe_customer( $resubscribe_order ); // For BW compat will remove in future. $resubscribe_order->delete_meta_data( '_stripe_card_id' ); // Delete payment intent ID. @@ -680,8 +680,9 @@ public function delete_renewal_meta( $renewal_order ) { * @return void */ public function update_failing_payment_method( $subscription, $renewal_order ) { - $subscription->update_meta_data( '_stripe_customer_id', $renewal_order->get_meta( '_stripe_customer_id', true ) ); - $subscription->update_meta_data( '_stripe_source_id', WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $renewal_order ) ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $subscription->update_meta_data( '_stripe_customer_id', $order_helper->get_stripe_customer( $renewal_order ) ); + $subscription->update_meta_data( '_stripe_source_id', $order_helper->get_stripe_source( $renewal_order ) ); $subscription->save(); } @@ -1010,7 +1011,7 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->get_parent() ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $parent_order = wc_get_order( $subscription->get_parent_id() ); - $stripe_customer_id = $parent_order->get_meta( '_stripe_customer_id', true ); + $stripe_customer_id = $order_helper->get_stripe_customer( $parent_order ); $stripe_source_id = $order_helper->get_stripe_source( $parent_order ); // For BW compat will remove in future. diff --git a/includes/payment-methods/class-wc-gateway-stripe-sepa.php b/includes/payment-methods/class-wc-gateway-stripe-sepa.php index 9d02fe222c..a0213905d8 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sepa.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sepa.php @@ -310,12 +310,14 @@ public function process_payment( $order_id, $retry = true, $force_save_source = $this->save_source_to_order( $order, $prepared_source ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + // Result from Stripe API request. $response = null; if ( $order->get_total() > 0 ) { // This will throw exception if not valid. - WC_Stripe_Order_Helper::validate_minimum_order_amount( $order ); + $order_helper->validate_minimum_order_amount( $order ); WC_Stripe_Logger::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); @@ -326,7 +328,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. if ( $this->is_no_such_customer_error( $response->error ) ) { delete_user_option( $order->get_customer_id(), '_stripe_customer_id' ); - $order->delete_meta_data( '_stripe_customer_id' ); + $order_helper->delete_stripe_customer( $order ); $order->save(); } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 7594ade858..bfbb32d1d5 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1811,12 +1811,13 @@ public function prepare_payment_method( $payment_method ) { * @param stdClass $payment_method Stripe Payment Method. */ public function save_payment_method_to_order( $order, $payment_method ) { + $order_helper = WC_Stripe_Order_Helper::get_instance(); if ( $payment_method->customer ) { - $order->update_meta_data( '_stripe_customer_id', $payment_method->customer ); + $order_helper->update_stripe_customer( $order, $payment_method->customer ); } // Save the payment method id as `source_id`, because we use both `sources` and `payment_methods` APIs. - WC_Stripe_Order_Helper::get_instance()->update_stripe_source( $order, $payment_method->payment_method ); + $order_helper->update_stripe_source( $order, $payment_method->payment_method ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); @@ -2913,7 +2914,7 @@ public function set_payment_method_id_for_subscription( $subscription, string $p * @param string $customer_id The value to be set. */ public function set_customer_id_for_order( WC_Order $order, string $customer_id ) { - $order->update_meta_data( '_stripe_customer_id', $customer_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_customer( $order, $customer_id ); $order->save_meta_data(); } diff --git a/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php b/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php index bc31d10f44..922467dd7b 100644 --- a/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php +++ b/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php @@ -78,11 +78,12 @@ public function test_create_customer_guest_order() { public function test_create_customer_with_existing_id() { wp_set_current_user( 1 ); - $order = WC_Helper_Order::create_order(); + $order = WC_Helper_Order::create_order(); + $order_helper = WC_Stripe_Order_Helper::get_instance(); $endpoint = '/' . strval( $order->get_id() ) . '/create_customer'; - $order->add_meta_data( '_stripe_customer_id', 'cus_12345', true ); + $order_helper->update_stripe_customer( $order, 'cus_12345' ); $order->save(); - $this->assertEquals( 'cus_12345', $order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( 'cus_12345', $order_helper->get_stripe_customer( $order ) ); // Mock response from Stripe API using request arguments. $test_request = function ( $preempt, $parsed_args, $url ) { diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index 466e888ee8..afa96b5cee 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -1202,10 +1202,11 @@ public function test_checkout_without_payment_uses_setup_intents() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); $final_order = wc_get_order( $order_id ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $payment_method_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); } @@ -1269,7 +1270,7 @@ public function test_checkout_saves_payment_method_to_order() { $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } @@ -1339,7 +1340,7 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } @@ -1395,10 +1396,11 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); $final_order = wc_get_order( $order_id ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); - $this->assertEquals( $generated_payment_method_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->delete_stripe_customer( $final_order ) ); + $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } /** @@ -1644,7 +1646,7 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1728,7 +1730,7 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PENDING, $final_order->get_status() ); // Order status should be pending until 3DS is completed. $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( "/#wc-stripe-confirm-pi:$order_id:$client_secret/", $response['redirect'] ); } @@ -1893,7 +1895,7 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1981,7 +1983,7 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'failure', $response['result'] ); $this->assertEquals( OrderStatus::FAILED, $final_order->get_status() ); - $this->assertEquals( '', $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( '', WC_Stripe_Order_Helper::get_instance()->get_stripe_customer( $final_order ) ); } /** @@ -2381,7 +2383,7 @@ public function test_pre_order_payment_is_successful() { $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); } @@ -2445,9 +2447,10 @@ public function test_pre_order_without_payment_uses_setup_intents() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $setup_intent_id, true ); $final_order = wc_get_order( $order_id ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); - $this->assertEquals( $payment_method_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $final_order ) ); - $this->assertEquals( $customer_id, $final_order->get_meta( '_stripe_customer_id', true ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); } From 8d73d51f59aa797c1b253e6dcf546e28c962cfce Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 29 Sep 2025 18:43:16 -0300 Subject: [PATCH 58/71] Card ID meta --- .../abstract-wc-stripe-payment-gateway.php | 2 +- includes/class-wc-stripe-order-helper.php | 39 +++++++++++++++++++ .../compat/trait-wc-stripe-pre-orders.php | 5 ++- .../compat/trait-wc-stripe-subscriptions.php | 4 +- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index c3cd078083..06884e6725 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1025,7 +1025,7 @@ public function prepare_order_source( $order = null ) { // Since 4.0.0, we changed card to source so we need to account for that. if ( empty( $source_id ) ) { - $source_id = $order->get_meta( '_stripe_card_id', true ); + $source_id = $order_helper->get_stripe_card( $order ); // Take this opportunity to update the key name. $order_helper->update_stripe_source( $order, $source_id ); diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index bde7544de0..b4f0f776ce 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -78,6 +78,13 @@ class WC_Stripe_Order_Helper { */ private const META_STRIPE_CUSTOMER_ID = '_stripe_customer_id'; + /** + * Meta key for Stripe card ID. + * + * @var string + */ + private const META_STRIPE_CARD_ID = '_stripe_card_id'; + /** * Meta key for payment awaiting action. * @@ -496,6 +503,38 @@ public function delete_stripe_customer( ?WC_Order $order = null ) { $order->delete_meta_data( self::META_STRIPE_CUSTOMER_ID ); } + /** + * Gets the Stripe card for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|string|null + */ + public function get_stripe_card( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + return $order->get_meta( self::META_STRIPE_CARD_ID, true ); + } + + /** + * Deletes the Stripe card for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|void + */ + public function delete_stripe_card( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $order->delete_meta_data( self::META_STRIPE_CARD_ID ); + } + /** * Adds payment intent id and order note to order if payment intent is not already saved * diff --git a/includes/compat/trait-wc-stripe-pre-orders.php b/includes/compat/trait-wc-stripe-pre-orders.php index 5a1b3151d2..8e718e3240 100644 --- a/includes/compat/trait-wc-stripe-pre-orders.php +++ b/includes/compat/trait-wc-stripe-pre-orders.php @@ -173,8 +173,9 @@ class_exists( 'WC_Pre_Orders_Order' ) && * @param WC_Order $order */ public function remove_order_source_before_retry( $order ) { - WC_Stripe_Order_Helper::get_instance()->delete_stripe_source( $order ); - $order->delete_meta_data( '_stripe_card_id' ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $order_helper->delete_stripe_source( $order ); + $order_helper->delete_stripe_card( $order ); $order->save(); } diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index c20b1fcfe3..cf1cc24967 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -648,7 +648,7 @@ public function delete_resubscribe_meta( $resubscribe_order ) { $order_helper->delete_stripe_customer( $resubscribe_order ); // For BW compat will remove in future. - $resubscribe_order->delete_meta_data( '_stripe_card_id' ); + $order_helper->delete_stripe_card( $resubscribe_order ); // Delete payment intent ID. $order_helper->delete_stripe_intent( $resubscribe_order ); $this->delete_renewal_meta( $resubscribe_order ); @@ -1016,7 +1016,7 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis // For BW compat will remove in future. if ( empty( $stripe_source_id ) ) { - $stripe_source_id = $parent_order->get_meta( '_stripe_card_id', true ); + $stripe_source_id = $order_helper->get_stripe_card( $parent_order ); // Take this opportunity to update the key name. $order_helper->update_stripe_source( $parent_order, $stripe_source_id ); From b264be30915371bbc466f31271f2f37c30a29afd Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 29 Sep 2025 18:57:37 -0300 Subject: [PATCH 59/71] UPE payment type --- ...ract-wc-stripe-payment-gateway-voucher.php | 8 ++- includes/class-wc-stripe-helper.php | 2 +- .../class-wc-stripe-intent-controller.php | 4 +- includes/class-wc-stripe-order-helper.php | 58 ++++++++++++++++++- includes/class-wc-stripe-webhook-handler.php | 2 +- .../class-wc-stripe-upe-payment-gateway.php | 10 ++-- ...ss-wc-stripe-upe-payment-method-boleto.php | 2 +- ...c-stripe-upe-payment-method-multibanco.php | 2 +- ...lass-wc-stripe-upe-payment-method-oxxo.php | 2 +- .../WC_Stripe_UPE_Payment_Gateway_Test.php | 11 ++-- .../WC_Stripe_Webhook_Handler_Test.php | 3 +- 11 files changed, 84 insertions(+), 20 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php index 06190ab6dc..e737140521 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php @@ -264,11 +264,13 @@ public function process_payment( $order_id, $retry = true, $force_save_save = fa $intent = $this->create_or_update_payment_intent( $order ); - $order->update_meta_data( '_stripe_upe_payment_type', $this->stripe_id ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + + $order_helper->update_stripe_upe_payment_type( $order, $this->stripe_id ); $order->update_status( OrderStatus::PENDING, __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); $order->save(); - WC_Stripe_Order_Helper::add_payment_intent_to_order( $intent->id, $order ); + $order_helper->add_payment_intent_to_order( $intent->id, $order ); return [ 'result' => 'success', @@ -386,7 +388,7 @@ public function update_payment_intent_ajax() { $intent = $this->create_or_update_payment_intent( $order ); $order->update_status( OrderStatus::PENDING, __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); - $order->update_meta_data( '_stripe_upe_payment_type', $this->stripe_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_upe_payment_type( $order, $this->stripe_id ); $order->save(); wp_send_json( diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index fc1fe811b7..d47c7bea7a 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1966,7 +1966,7 @@ public static function validate_intent_for_order( $order, $intent, ?string $sele } if ( null === $selected_payment_type ) { - $selected_payment_type = $order->get_meta( '_stripe_upe_payment_type', true ); + $selected_payment_type = WC_Stripe_Order_Helper::get_instance()->get_stripe_upe_payment_type( $order ); } // If we don't have a selected payment type, that implies we have no stored value and a new payment type is permitted. diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 855339ad77..1daacfe5cc 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -525,7 +525,7 @@ public function update_intent( $intent_id = '', $order_id = null, $save_payment_ WC_Stripe_UPE_Payment_Method_Link::STRIPE_ID, ]; } - $order->update_meta_data( '_stripe_upe_payment_type', $selected_upe_payment_type ); + $order_helper->update_stripe_upe_payment_type( $order, $selected_upe_payment_type ); } if ( ! empty( $customer ) && $customer->get_id() ) { $request['customer'] = $customer->get_id(); @@ -886,7 +886,7 @@ public function create_and_confirm_payment_intent( $payment_information ) { // Only update the payment_type if we have a reference to the payment type the customer selected. if ( '' !== $selected_payment_type ) { - $order->update_meta_data( '_stripe_upe_payment_type', $selected_payment_type ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_upe_payment_type( $order, $selected_payment_type ); } return $payment_intent; diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index b4f0f776ce..b6dd111ccc 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -85,6 +85,13 @@ class WC_Stripe_Order_Helper { */ private const META_STRIPE_CARD_ID = '_stripe_card_id'; + /** + * Meta key for Stripe UPE payment type. + * + * @var string + */ + private const META_STRIPE_UPE_PAYMENT_TYPE = '_stripe_upe_payment_type'; + /** * Meta key for payment awaiting action. * @@ -535,6 +542,55 @@ public function delete_stripe_card( ?WC_Order $order = null ) { $order->delete_meta_data( self::META_STRIPE_CARD_ID ); } + /** + * Updates the Stripe UPE payment type for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|string|null + */ + public function get_stripe_upe_payment_type( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + return $order->get_meta( self::META_STRIPE_UPE_PAYMENT_TYPE, true ); + } + + /** + * Updates the Stripe UPE payment type for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @param string $payment_type + * @return false|void + */ + public function update_stripe_upe_payment_type( ?WC_Order $order = null, string $payment_type = '' ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( self::META_STRIPE_UPE_PAYMENT_TYPE, $payment_type ); + } + + /** + * Deletes the Stripe UPE payment type for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|void + */ + public function delete_stripe_upe_payment_type( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $order->delete_meta_data( self::META_STRIPE_UPE_PAYMENT_TYPE ); + } + /** * Adds payment intent id and order note to order if payment intent is not already saved * @@ -717,7 +773,7 @@ public function validate_intent_for_order( WC_Order $order, $intent, ?string $se } if ( null === $selected_payment_type ) { - $selected_payment_type = $order->get_meta( '_stripe_upe_payment_type', true ); + $selected_payment_type = WC_Stripe_Order_Helper::get_instance()->get_stripe_upe_payment_type( $order ); } // If we don't have a selected payment type, that implies we have no stored value and a new payment type is permitted. diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index da0383a625..8d459d9446 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -1101,7 +1101,7 @@ public function process_payment_intent( $notification ) { } $order_id = $order->get_id(); - $payment_type_meta = $order->get_meta( '_stripe_upe_payment_type' ); + $payment_type_meta = $order_helper->get_stripe_upe_payment_type( $order ); $is_voucher_payment = in_array( $payment_type_meta, WC_Stripe_Payment_Methods::VOUCHER_PAYMENT_METHODS, true ); $is_wallet_payment = in_array( $payment_type_meta, WC_Stripe_Payment_Methods::WALLET_PAYMENT_METHODS, true ); diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index bfbb32d1d5..9bcfcf87df 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -357,7 +357,7 @@ public function set_cookie_on_current_request( $cookie ) { * @return array|bool */ public function can_refund_order( $order ) { - $upe_payment_type = $order->get_meta( '_stripe_upe_payment_type' ); + $upe_payment_type = WC_Stripe_Order_Helper::get_instance()->get_stripe_upe_payment_type( $order ); if ( ! $upe_payment_type ) { return true; @@ -978,9 +978,11 @@ public function process_payment( $order_id, $retry = true, $force_save_source = null // $prepared_source parameter is not necessary for adding mandate information. ); - WC_Stripe_Order_Helper::get_instance()->add_payment_intent_to_order( $payment_intent_id, $order ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + + $order_helper->add_payment_intent_to_order( $payment_intent_id, $order ); $order->update_status( OrderStatus::PENDING, __( 'Awaiting payment.', 'woocommerce-gateway-stripe' ) ); - $order->update_meta_data( '_stripe_upe_payment_type', $selected_upe_payment_type ); + $order_helper->update_stripe_upe_payment_type( $order, $selected_upe_payment_type ); // TODO: This is a stop-gap to fix a critical issue, see // https://github.com/woocommerce/woocommerce-gateway-stripe/issues/2536. It would @@ -2938,7 +2940,7 @@ public function set_customer_id_for_subscription( $subscription, string $custome * @param string $selected_payment_type The selected payment type. */ private function set_selected_payment_type_for_order( WC_Order $order, string $selected_payment_type ) { - $order->update_meta_data( '_stripe_upe_payment_type', $selected_payment_type ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_upe_payment_type( $order, $selected_payment_type ); $order->save_meta_data(); } /** diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php index a4a88328db..e8010db1c2 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-boleto.php @@ -47,7 +47,7 @@ public function __construct() { * @return mixed */ public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) { - if ( WC_Stripe_Payment_Methods::BOLETO === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) { + if ( WC_Stripe_Payment_Methods::BOLETO === WC_Stripe_Order_Helper::get_instance()->get_stripe_upe_payment_type( $order ) && ! in_array( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) { $allowed_statuses[] = OrderStatus::ON_HOLD; } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php index 40a746d38e..c4aaa1ccd2 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-multibanco.php @@ -140,7 +140,7 @@ public function save_instructions( $order, $payment_intent ) { * @return mixed */ public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) { - if ( WC_Stripe_Payment_Methods::MULTIBANCO === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) { + if ( WC_Stripe_Payment_Methods::MULTIBANCO === WC_Stripe_Order_Helper::get_instance()->get_stripe_upe_payment_type( $order ) && ! in_array( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) { $allowed_statuses[] = OrderStatus::ON_HOLD; } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-oxxo.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-oxxo.php index f30b59a8f6..98913dd66c 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-oxxo.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-oxxo.php @@ -47,7 +47,7 @@ public function __construct() { * @return mixed */ public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) { - if ( WC_Stripe_Payment_Methods::OXXO === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) { + if ( WC_Stripe_Payment_Methods::OXXO === WC_Stripe_Order_Helper::get_instance()->get_stripe_upe_payment_type( $order ) && ! in_array( OrderStatus::ON_HOLD, $allowed_statuses, true ) ) { $allowed_statuses[] = OrderStatus::ON_HOLD; } diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index afa96b5cee..359aec3572 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -470,8 +470,9 @@ public function test_process_payment_returns_valid_response() { $currency = $order->get_currency(); $order_id = $order->get_id(); - WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $payment_intent_id ); - $order->update_meta_data( '_stripe_upe_payment_type', '' ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + $order_helper->update_stripe_intent( $order, $payment_intent_id ); + $order_helper->update_stripe_upe_payment_type( $order, '' ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save(); @@ -2000,8 +2001,10 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { $currency = $order->get_currency(); $order_id = $order->get_id(); - WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $payment_intent_id ); - $order->update_meta_data( '_stripe_upe_payment_type', '' ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); + + $order_helper->update_stripe_intent( $order, $payment_intent_id ); + $order_helper->update_stripe_upe_payment_type( $order, '' ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save(); diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index 92fe363b64..85b39d3a81 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -525,13 +525,14 @@ public function test_process_payment_intent( // Reset WC_Stripe_Order_Helper instance to avoid issues with other tests. WC_Stripe_Order_Helper::set_instance( null ); + $order_helper = WC_Stripe_Order_Helper::get_instance(); if ( $order_locked ) { $order->update_meta_data( '_stripe_lock_payment', ( time() + MINUTE_IN_SECONDS ) ); } if ( $order_status_final ) { $order->update_meta_data( '_stripe_status_final', true ); } - $order->update_meta_data( '_stripe_upe_payment_type', $payment_type ); + $order_helper->update_stripe_upe_payment_type( $order, $payment_type ); $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); $order->save_meta_data(); $order->save(); From 91956e6bc66e76e7f8d2526d54e8aa0dbb7735cf Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 29 Sep 2025 19:04:33 -0300 Subject: [PATCH 60/71] UPE waiting for redirect --- includes/class-wc-stripe-order-helper.php | 46 +++++++++++++++++-- includes/class-wc-stripe-webhook-handler.php | 2 +- .../class-wc-stripe-upe-payment-gateway.php | 6 +-- .../WC_Stripe_UPE_Payment_Gateway_Test.php | 4 +- .../WC_Stripe_Webhook_Handler_Test.php | 2 +- 5 files changed, 50 insertions(+), 10 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index b6dd111ccc..54ff3781e9 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -92,6 +92,13 @@ class WC_Stripe_Order_Helper { */ private const META_STRIPE_UPE_PAYMENT_TYPE = '_stripe_upe_payment_type'; + /** + * Meta key for Stripe UPE waiting for redirect. + * + * @var string + */ + private const META_STRIPE_UPE_WAITING_FOR_REDIRECT = '_stripe_upe_waiting_for_redirect'; + /** * Meta key for payment awaiting action. * @@ -576,19 +583,52 @@ public function update_stripe_upe_payment_type( ?WC_Order $order = null, string } /** - * Deletes the Stripe UPE payment type for order. + * Updates the Stripe UPE waiting for redirect for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return bool|null + */ + public function get_stripe_upe_waiting_for_redirect( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + return $order->get_meta( self::META_STRIPE_UPE_WAITING_FOR_REDIRECT, true ); + } + + /** + * Updates the Stripe UPE waiting for redirect for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @param bool $waiting_for_redirect + * @return false|void + */ + public function update_stripe_upe_waiting_for_redirect( ?WC_Order $order = null, bool $waiting_for_redirect = false ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( self::META_STRIPE_UPE_WAITING_FOR_REDIRECT, $waiting_for_redirect ); + } + + /** + * Deletes the Stripe UPE waiting for redirect for order. * * @since 10.0.0 * * @param WC_Order|null $order * @return false|void */ - public function delete_stripe_upe_payment_type( ?WC_Order $order = null ) { + public function delete_stripe_upe_waiting_for_redirect( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } - $order->delete_meta_data( self::META_STRIPE_UPE_PAYMENT_TYPE ); + $order->delete_meta_data( self::META_STRIPE_UPE_WAITING_FOR_REDIRECT ); } /** diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 8d459d9446..d8bd8c20d7 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -1129,7 +1129,7 @@ public function process_payment_intent( $notification ) { WC_Stripe_Logger::log( "Stripe PaymentIntent $intent->id succeeded for order $order_id" ); $process_webhook_async = apply_filters( 'wc_stripe_process_payment_intent_webhook_async', true, $order, $intent, $notification ); - $is_awaiting_action = $order->get_meta( '_stripe_upe_waiting_for_redirect' ) ?? false; + $is_awaiting_action = $order_helper->get_stripe_upe_waiting_for_redirect( $order ) ?? false; // Process the webhook now if it's for a voucher or wallet payment, or if filtered to process immediately and order is not awaiting action. if ( $is_voucher_payment || $is_wallet_payment || ( ! $process_webhook_async && ! $is_awaiting_action ) ) { diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 9bcfcf87df..03a144ed42 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -988,7 +988,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // https://github.com/woocommerce/woocommerce-gateway-stripe/issues/2536. It would // be better if we removed the need for additional meta data in favor of refactoring // this part of the payment processing. - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order_helper->update_stripe_upe_waiting_for_redirect( $order, true ); $order->save(); @@ -1180,7 +1180,7 @@ private function process_payment_with_payment_method( int $order_id ) { $order_helper->set_payment_awaiting_action( $order, false ); // Prevent processing the payment intent webhooks while also processing the redirect payment (also prevents duplicate Stripe meta stored on the order). - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order_helper->update_stripe_upe_waiting_for_redirect( $order, true ); $order->save(); $redirect = $this->get_redirect_url( $this->get_return_url( $order ), $payment_intent, $payment_information, $order, $payment_needed ); @@ -1777,7 +1777,7 @@ public function process_order_for_confirmed_intent( $order, $intent_id, $save_pa // https://github.com/woocommerce/woocommerce-gateway-stripe/issues/2536. It would // be better if we removed the need for additional meta data in favor of refactoring // this part of the payment processing. - $order->delete_meta_data( '_stripe_upe_waiting_for_redirect' ); + $order_helper->delete_stripe_upe_waiting_for_redirect( $order ); /** * This meta is to prevent stores with short hold stock settings from cancelling orders while waiting for payment to be finalised by Stripe or the customer (i.e. completing 3DS or payment redirects). diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index 359aec3572..368733387e 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -473,7 +473,7 @@ public function test_process_payment_returns_valid_response() { $order_helper = WC_Stripe_Order_Helper::get_instance(); $order_helper->update_stripe_intent( $order, $payment_intent_id ); $order_helper->update_stripe_upe_payment_type( $order, '' ); - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order_helper->update_stripe_upe_waiting_for_redirect( $order, true ); $order->save(); list( $amount, $description, $metadata ) = $this->get_order_details( $order ); @@ -2005,7 +2005,7 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { $order_helper->update_stripe_intent( $order, $payment_intent_id ); $order_helper->update_stripe_upe_payment_type( $order, '' ); - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order_helper->update_stripe_upe_waiting_for_redirect( $order, true ); $order->save(); list( $amount, $description, $metadata ) = $this->get_order_details( $order ); diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index 85b39d3a81..179b533c32 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -533,7 +533,7 @@ public function test_process_payment_intent( $order->update_meta_data( '_stripe_status_final', true ); } $order_helper->update_stripe_upe_payment_type( $order, $payment_type ); - $order->update_meta_data( '_stripe_upe_waiting_for_redirect', true ); + $order_helper->update_stripe_upe_waiting_for_redirect( $order, true ); $order->save_meta_data(); $order->save(); From 1906189d7bae0376eba5cfbb765f340b4a9e1608 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 29 Sep 2025 19:10:19 -0300 Subject: [PATCH 61/71] UPE redirect processed --- includes/class-wc-stripe-order-helper.php | 60 ++++++++++++++++++- .../class-wc-stripe-upe-payment-gateway.php | 8 +-- .../WC_Stripe_UPE_Payment_Gateway_Test.php | 21 +++---- 3 files changed, 73 insertions(+), 16 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 54ff3781e9..af2a78ddf4 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -99,6 +99,13 @@ class WC_Stripe_Order_Helper { */ private const META_STRIPE_UPE_WAITING_FOR_REDIRECT = '_stripe_upe_waiting_for_redirect'; + /** + * Meta key for Stripe UPE redirect processed. + * + * @var string + */ + private const META_STRIPE_UPE_REDIRECT_PROCESSED = '_stripe_upe_redirect_processed'; + /** * Meta key for payment awaiting action. * @@ -550,7 +557,7 @@ public function delete_stripe_card( ?WC_Order $order = null ) { } /** - * Updates the Stripe UPE payment type for order. + * Gets the Stripe UPE payment type for order. * * @since 10.0.0 * @@ -583,7 +590,7 @@ public function update_stripe_upe_payment_type( ?WC_Order $order = null, string } /** - * Updates the Stripe UPE waiting for redirect for order. + * Gets the Stripe UPE waiting for redirect for order. * * @since 10.0.0 * @@ -631,6 +638,55 @@ public function delete_stripe_upe_waiting_for_redirect( ?WC_Order $order = null $order->delete_meta_data( self::META_STRIPE_UPE_WAITING_FOR_REDIRECT ); } + /** + * Gets the Stripe UPE redirect processed for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return bool|null + */ + public function get_stripe_upe_redirect_processed( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + return $order->get_meta( self::META_STRIPE_UPE_REDIRECT_PROCESSED, true ); + } + + /** + * Updates the Stripe UPE redirect processed for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @param bool $redirect_processed + * @return false|void + */ + public function update_stripe_upe_redirect_processed( ?WC_Order $order = null, bool $redirect_processed = false ) { + if ( is_null( $order ) ) { + return false; + } + + $order->update_meta_data( self::META_STRIPE_UPE_REDIRECT_PROCESSED, $redirect_processed ); + } + + /** + * Deletes the Stripe UPE redirect processed for order. + * + * @since 10.0.0 + * + * @param WC_Order|null $order + * @return false|void + */ + public function delete_stripe_upe_redirect_processed( ?WC_Order $order = null ) { + if ( is_null( $order ) ) { + return false; + } + + $order->delete_meta_data( self::META_STRIPE_UPE_REDIRECT_PROCESSED ); + } + /** * Adds payment intent id and order note to order if payment intent is not already saved * diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 03a144ed42..e8a7e30e35 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1649,12 +1649,12 @@ public function process_upe_redirect_payment( $order_id, $intent_id, $save_payme return; } - if ( $order->get_meta( '_stripe_upe_redirect_processed', true ) ) { + $order_helper = WC_Stripe_Order_Helper::get_instance(); + + if ( $order_helper->get_stripe_upe_redirect_processed( $order ) ) { return; } - $order_helper = WC_Stripe_Order_Helper::get_instance(); - try { // First check if the order is already being processed by another request. $locked = $order_helper->lock_order_payment( $order ); @@ -1771,7 +1771,7 @@ public function process_order_for_confirmed_intent( $order, $intent_id, $save_pa $this->save_intent_to_order( $order, $intent ); $this->set_payment_method_title_for_order( $order, $payment_method_type ); - $order->update_meta_data( '_stripe_upe_redirect_processed', true ); + $order_helper->update_stripe_upe_redirect_processed( $order, true ); // TODO: This is a stop-gap to fix a critical issue, see // https://github.com/woocommerce/woocommerce-gateway-stripe/issues/2536. It would diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index 368733387e..bbf16e7971 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -1034,18 +1034,19 @@ public function test_process_redirect_payment_returns_valid_response() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); - $final_order = wc_get_order( $order_id ); - $note = wc_get_order_notes( + $final_order = wc_get_order( $order_id ); + $note = wc_get_order_notes( [ 'order_id' => $order_id, 'limit' => 2, ] )[1]; + $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); - $this->assertEquals( $payment_intent_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $final_order ) ); - $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); + $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1099,19 +1100,19 @@ public function test_process_redirect_payment_only_runs_once() { $this->mock_gateway->process_upe_redirect_payment( $order_id, $payment_intent_id, false ); $success_order = wc_get_order( $order_id ); - - $note = wc_get_order_notes( + $note = wc_get_order_notes( [ 'order_id' => $order_id, 'limit' => 2, ] )[1]; + $order_helper = WC_Stripe_Order_Helper::get_instance(); // assert successful order processing $this->assertEquals( OrderStatus::PROCESSING, $success_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $success_order->get_payment_method_title() ); - $this->assertEquals( $payment_intent_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $success_order ) ); - $this->assertTrue( (bool) $success_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $success_order ) ); + $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $success_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); // simulate an order getting marked as failed as if from a webhook @@ -2388,7 +2389,7 @@ public function test_pre_order_payment_is_successful() { $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $final_order ) ); } /** @@ -2454,7 +2455,7 @@ public function test_pre_order_without_payment_uses_setup_intents() { $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); - $this->assertTrue( (bool) $final_order->get_meta( '_stripe_upe_redirect_processed', true ) ); + $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $final_order ) ); } /** From bc8a456b82e0a2df3645aeb44c13ad5a569a633f Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 1 Oct 2025 14:36:14 -0300 Subject: [PATCH 62/71] Fix tests --- .../PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index bbf16e7971..b909a89c46 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -1358,6 +1358,7 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $order_id = $order->get_id(); list( $amount, $description, $metadata ) = $this->get_order_details( $order ); + $order->set_total( 0 ); $order->set_payment_method( WC_Stripe_UPE_Payment_Gateway::ID ); $order->save(); @@ -1401,7 +1402,7 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $customer_id, $order_helper->delete_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } From ffef3daae8ebbb3959bb6560cc09ed8a7a0ada15 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 1 Oct 2025 14:51:47 -0300 Subject: [PATCH 63/71] Fix method implementation + remove unused method --- .../class-wc-rest-stripe-orders-controller.php | 2 +- includes/class-wc-stripe-order-helper.php | 16 ---------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/includes/admin/class-wc-rest-stripe-orders-controller.php b/includes/admin/class-wc-rest-stripe-orders-controller.php index df3763c932..4a1b5d49fb 100644 --- a/includes/admin/class-wc-rest-stripe-orders-controller.php +++ b/includes/admin/class-wc-rest-stripe-orders-controller.php @@ -128,7 +128,7 @@ public function create_customer( $request ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); // Set the customer ID if known but not already set. - $customer_id = $order_helper->update_stripe_customer( $order ); + $customer_id = $order_helper->get_stripe_customer( $order ); if ( ! $customer->get_id() && $customer_id ) { $customer->set_id( $customer_id ); } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 90c89278c5..bc3809bccc 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -671,22 +671,6 @@ public function update_stripe_upe_redirect_processed( ?WC_Order $order = null, b $order->update_meta_data( self::META_STRIPE_UPE_REDIRECT_PROCESSED, $redirect_processed ); } - /** - * Deletes the Stripe UPE redirect processed for order. - * - * @since 10.0.0 - * - * @param WC_Order|null $order - * @return false|void - */ - public function delete_stripe_upe_redirect_processed( ?WC_Order $order = null ) { - if ( is_null( $order ) ) { - return false; - } - - $order->delete_meta_data( self::META_STRIPE_UPE_REDIRECT_PROCESSED ); - } - /** * Adds payment intent id and order note to order if payment intent is not already saved * From 5ddc294fa1d4e37e85e2f50eb152c2f598a29416 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 1 Oct 2025 14:53:23 -0300 Subject: [PATCH 64/71] Changelog and readme entries --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index 58f8e4c854..98a8f03889 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 10.0.0 - xxxx-xx-xx = +* Dev - Expands the Stripe Order Helper class to handle customer ID, card ID, UPE payment type, and UPE redirect status metas * Dev - Expands the Stripe Order Helper class to handle source ID, refund ID, intent ID, and setup intent ID metas * Update - Removing the `wc_stripe_is_upe_checkout_enabled` filter, as Legacy Checkout is no longer supported * Dev - Upgrades `jest` to version 29.7.0, `@wordpress/scripts` to 26.19.0, and adds `axios`(version 1.12.2) to the JavaScript development dependencies diff --git a/readme.txt b/readme.txt index 3cb827bad1..5fd1ea46f5 100644 --- a/readme.txt +++ b/readme.txt @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o == Changelog == = 10.0.0 - xxxx-xx-xx = +* Dev - Expands the Stripe Order Helper class to handle customer ID, card ID, UPE payment type, and UPE redirect status metas * Dev - Expands the Stripe Order Helper class to handle source ID, refund ID, intent ID, and setup intent ID metas * Update - Removing the `wc_stripe_is_upe_checkout_enabled` filter, as Legacy Checkout is no longer supported * Dev - Upgrades `jest` to version 29.7.0, `@wordpress/scripts` to 26.19.0, and adds `axios`(version 1.12.2) to the JavaScript development dependencies From 368a34f053e586900af369b3242c9d80110159fc Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 8 Oct 2025 11:22:53 -0300 Subject: [PATCH 65/71] Update includes/class-wc-stripe-order-helper.php Co-authored-by: daledupreez --- includes/class-wc-stripe-order-helper.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index bc3809bccc..2037d8d35c 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -476,14 +476,14 @@ public function update_stripe_setup_intent( ?WC_Order $order = null, string $int } /** - * Gets the Stripe customer for order. + * Gets the Stripe customer ID for an order. * * @since 10.0.0 * * @param WC_Order|null $order * @return false|string|null */ - public function get_stripe_customer( ?WC_Order $order = null ) { + public function get_stripe_customer_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } @@ -492,7 +492,7 @@ public function get_stripe_customer( ?WC_Order $order = null ) { } /** - * Updates the Stripe customer for order. + * Updates the Stripe customer ID for an order. * * @since 10.0.0 * @@ -500,7 +500,7 @@ public function get_stripe_customer( ?WC_Order $order = null ) { * @param string $customer_id * @return false|void */ - public function update_stripe_customer( ?WC_Order $order = null, string $customer_id = '' ) { + public function update_stripe_customer_id( ?WC_Order $order = null, string $customer_id = '' ) { if ( is_null( $order ) ) { return false; } @@ -509,14 +509,14 @@ public function update_stripe_customer( ?WC_Order $order = null, string $custome } /** - * Deletes the Stripe customer for order. + * Deletes the Stripe customer ID for an order. * * @since 10.0.0 * * @param WC_Order|null $order * @return false|void */ - public function delete_stripe_customer( ?WC_Order $order = null ) { + public function delete_stripe_customer_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } From c77fab6d53c3597ec83da517266f6a2af93345a4 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 8 Oct 2025 11:29:07 -0300 Subject: [PATCH 66/71] Fix implementation after method renaming + renaming the card_id method and implementations --- .../abstract-wc-stripe-payment-gateway.php | 8 ++++---- ...class-wc-rest-stripe-orders-controller.php | 4 ++-- includes/admin/class-wc-stripe-privacy.php | 8 ++++---- includes/class-wc-stripe-order-handler.php | 2 +- includes/class-wc-stripe-order-helper.php | 4 ++-- includes/class-wc-stripe-webhook-handler.php | 2 +- .../compat/trait-wc-stripe-pre-orders.php | 2 +- .../compat/trait-wc-stripe-subscriptions.php | 10 +++++----- .../class-wc-gateway-stripe-sepa.php | 2 +- .../class-wc-stripe-upe-payment-gateway.php | 4 ++-- .../WC_REST_Stripe_Orders_Controller_Test.php | 4 ++-- .../WC_Stripe_UPE_Payment_Gateway_Test.php | 20 +++++++++---------- 12 files changed, 35 insertions(+), 35 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index 2bebed5eda..f23355b0b7 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -330,7 +330,7 @@ public function maybe_remove_non_existent_customer( $error, $order ) { } delete_user_option( $order->get_customer_id(), '_stripe_customer_id' ); - WC_Stripe_Order_Helper::get_instance()->delete_stripe_customer( $order ); + WC_Stripe_Order_Helper::get_instance()->delete_stripe_customer_id( $order ); $order->save(); return true; @@ -410,7 +410,7 @@ public function get_transaction_url( $order ) { */ public function get_stripe_customer_id( $order ) { // Try to get it via the order first. - $customer = WC_Stripe_Order_Helper::get_instance()->get_stripe_customer( $order ); + $customer = WC_Stripe_Order_Helper::get_instance()->get_stripe_customer_id( $order ); if ( empty( $customer ) ) { $customer = get_user_option( '_stripe_customer_id', $order->get_customer_id() ); @@ -1025,7 +1025,7 @@ public function prepare_order_source( $order = null ) { // Since 4.0.0, we changed card to source so we need to account for that. if ( empty( $source_id ) ) { - $source_id = $order_helper->get_stripe_card( $order ); + $source_id = $order_helper->get_stripe_card_id( $order ); // Take this opportunity to update the key name. $order_helper->update_stripe_source( $order, $source_id ); @@ -1083,7 +1083,7 @@ public function save_source_to_order( $order, $source ) { // Store source in the order. if ( $source->customer ) { - $order_helper->update_stripe_customer( $order, $source->customer ); + $order_helper->update_stripe_customer_id( $order, $source->customer ); } if ( $source->source ) { diff --git a/includes/admin/class-wc-rest-stripe-orders-controller.php b/includes/admin/class-wc-rest-stripe-orders-controller.php index 4a1b5d49fb..5afb79e993 100644 --- a/includes/admin/class-wc-rest-stripe-orders-controller.php +++ b/includes/admin/class-wc-rest-stripe-orders-controller.php @@ -128,7 +128,7 @@ public function create_customer( $request ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); // Set the customer ID if known but not already set. - $customer_id = $order_helper->get_stripe_customer( $order ); + $customer_id = $order_helper->get_stripe_customer_id( $order ); if ( ! $customer->get_id() && $customer_id ) { $customer->set_id( $customer_id ); } @@ -145,7 +145,7 @@ public function create_customer( $request ) { return new WP_Error( 'stripe_error', $e->getMessage() ); } - $order_helper->update_stripe_customer( $order, $customer_id ); + $order_helper->update_stripe_customer_id( $order, $customer_id ); $order->save(); return rest_ensure_response( [ 'id' => $customer_id ] ); diff --git a/includes/admin/class-wc-stripe-privacy.php b/includes/admin/class-wc-stripe-privacy.php index 45b025b5c7..b198b46edf 100644 --- a/includes/admin/class-wc-stripe-privacy.php +++ b/includes/admin/class-wc-stripe-privacy.php @@ -135,7 +135,7 @@ public function order_data_exporter( $email_address, $page = 1 ) { ], [ 'name' => __( 'Stripe customer id', 'woocommerce-gateway-stripe' ), - 'value' => $order_helper->get_stripe_customer( $order ), + 'value' => $order_helper->get_stripe_customer_id( $order ), ], ], ]; @@ -369,7 +369,7 @@ protected function maybe_handle_subscription( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $order_helper->delete_stripe_source( $renewal_order ); $order_helper->delete_stripe_refund( $renewal_order ); - $order_helper->delete_stripe_customer( $renewal_order ); + $order_helper->delete_stripe_customer_id( $renewal_order ); } $subscription->delete_meta_data( '_stripe_source_id' ); @@ -389,7 +389,7 @@ protected function maybe_handle_order( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $stripe_source_id = $order_helper->get_stripe_source( $order ); $stripe_refund_id = $order_helper->get_stripe_refund( $order ); - $stripe_customer_id = $order_helper->get_stripe_customer( $order ); + $stripe_customer_id = $order_helper->get_stripe_customer_id( $order ); if ( ! $this->is_retention_expired( $order->get_date_created()->getTimestamp() ) ) { /* translators: %d Order ID */ @@ -402,7 +402,7 @@ protected function maybe_handle_order( $order ) { $order_helper->delete_stripe_source( $order ); $order_helper->delete_stripe_refund( $order ); - $order_helper->delete_stripe_customer( $order ); + $order_helper->delete_stripe_customer_id( $order ); return [ true, false, [ __( 'Stripe personal data erased.', 'woocommerce-gateway-stripe' ) ] ]; } diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 3daeb115bf..26793c1450 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -170,7 +170,7 @@ public function process_redirect_payment( $order_id, $retry = true, $previous_er // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. if ( $this->is_no_such_customer_error( $response->error ) ) { delete_user_option( $order->get_customer_id(), '_stripe_customer_id' ); - $order_helper->delete_stripe_customer( $order ); + $order_helper->delete_stripe_customer_id( $order ); $order->save(); } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 2037d8d35c..fd4f9c2e05 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -532,7 +532,7 @@ public function delete_stripe_customer_id( ?WC_Order $order = null ) { * @param WC_Order|null $order * @return false|string|null */ - public function get_stripe_card( ?WC_Order $order = null ) { + public function get_stripe_card_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } @@ -548,7 +548,7 @@ public function get_stripe_card( ?WC_Order $order = null ) { * @param WC_Order|null $order * @return false|void */ - public function delete_stripe_card( ?WC_Order $order = null ) { + public function delete_stripe_card_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index d8bd8c20d7..19f8dd9453 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -318,7 +318,7 @@ public function process_webhook_payment( $notification, $retry = true ) { // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. if ( $this->is_no_such_customer_error( $response->error ) ) { delete_user_option( $order->get_customer_id(), '_stripe_customer_id' ); - $order_helper->delete_stripe_customer( $order ); + $order_helper->delete_stripe_customer_id( $order ); $order->save(); } diff --git a/includes/compat/trait-wc-stripe-pre-orders.php b/includes/compat/trait-wc-stripe-pre-orders.php index b38273775c..eda6d180d4 100644 --- a/includes/compat/trait-wc-stripe-pre-orders.php +++ b/includes/compat/trait-wc-stripe-pre-orders.php @@ -175,7 +175,7 @@ class_exists( 'WC_Pre_Orders_Order' ) && public function remove_order_source_before_retry( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $order_helper->delete_stripe_source( $order ); - $order_helper->delete_stripe_card( $order ); + $order_helper->delete_stripe_card_id( $order ); $order->save(); } diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index cf1cc24967..8927fc2be2 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -646,9 +646,9 @@ public function delete_resubscribe_meta( $resubscribe_order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $order_helper->delete_stripe_source( $resubscribe_order ); - $order_helper->delete_stripe_customer( $resubscribe_order ); + $order_helper->delete_stripe_customer_id( $resubscribe_order ); // For BW compat will remove in future. - $order_helper->delete_stripe_card( $resubscribe_order ); + $order_helper->delete_stripe_card_id( $resubscribe_order ); // Delete payment intent ID. $order_helper->delete_stripe_intent( $resubscribe_order ); $this->delete_renewal_meta( $resubscribe_order ); @@ -681,7 +681,7 @@ public function delete_renewal_meta( $renewal_order ) { */ public function update_failing_payment_method( $subscription, $renewal_order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); - $subscription->update_meta_data( '_stripe_customer_id', $order_helper->get_stripe_customer( $renewal_order ) ); + $subscription->update_meta_data( '_stripe_customer_id', $order_helper->get_stripe_customer_id( $renewal_order ) ); $subscription->update_meta_data( '_stripe_source_id', $order_helper->get_stripe_source( $renewal_order ) ); $subscription->save(); } @@ -1011,12 +1011,12 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->get_parent() ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $parent_order = wc_get_order( $subscription->get_parent_id() ); - $stripe_customer_id = $order_helper->get_stripe_customer( $parent_order ); + $stripe_customer_id = $order_helper->get_stripe_customer_id( $parent_order ); $stripe_source_id = $order_helper->get_stripe_source( $parent_order ); // For BW compat will remove in future. if ( empty( $stripe_source_id ) ) { - $stripe_source_id = $order_helper->get_stripe_card( $parent_order ); + $stripe_source_id = $order_helper->get_stripe_card_id( $parent_order ); // Take this opportunity to update the key name. $order_helper->update_stripe_source( $parent_order, $stripe_source_id ); diff --git a/includes/payment-methods/class-wc-gateway-stripe-sepa.php b/includes/payment-methods/class-wc-gateway-stripe-sepa.php index a0213905d8..b7c0bd15bd 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sepa.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sepa.php @@ -328,7 +328,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. if ( $this->is_no_such_customer_error( $response->error ) ) { delete_user_option( $order->get_customer_id(), '_stripe_customer_id' ); - $order_helper->delete_stripe_customer( $order ); + $order_helper->delete_stripe_customer_id( $order ); $order->save(); } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index e8a7e30e35..e28d069ceb 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1815,7 +1815,7 @@ public function prepare_payment_method( $payment_method ) { public function save_payment_method_to_order( $order, $payment_method ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); if ( $payment_method->customer ) { - $order_helper->update_stripe_customer( $order, $payment_method->customer ); + $order_helper->update_stripe_customer_id( $order, $payment_method->customer ); } // Save the payment method id as `source_id`, because we use both `sources` and `payment_methods` APIs. @@ -2916,7 +2916,7 @@ public function set_payment_method_id_for_subscription( $subscription, string $p * @param string $customer_id The value to be set. */ public function set_customer_id_for_order( WC_Order $order, string $customer_id ) { - WC_Stripe_Order_Helper::get_instance()->update_stripe_customer( $order, $customer_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_customer_id( $order, $customer_id ); $order->save_meta_data(); } diff --git a/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php b/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php index 922467dd7b..4726dbdf30 100644 --- a/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php +++ b/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php @@ -81,9 +81,9 @@ public function test_create_customer_with_existing_id() { $order = WC_Helper_Order::create_order(); $order_helper = WC_Stripe_Order_Helper::get_instance(); $endpoint = '/' . strval( $order->get_id() ) . '/create_customer'; - $order_helper->update_stripe_customer( $order, 'cus_12345' ); + $order_helper->update_stripe_customer_id( $order, 'cus_12345' ); $order->save(); - $this->assertEquals( 'cus_12345', $order_helper->get_stripe_customer( $order ) ); + $this->assertEquals( 'cus_12345', $order_helper->get_stripe_customer_id( $order ) ); // Mock response from Stripe API using request arguments. $test_request = function ( $preempt, $parsed_args, $url ) { diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index b909a89c46..ee314e3100 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -1207,7 +1207,7 @@ public function test_checkout_without_payment_uses_setup_intents() { $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); } @@ -1272,7 +1272,7 @@ public function test_checkout_saves_payment_method_to_order() { $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } @@ -1342,7 +1342,7 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } @@ -1402,7 +1402,7 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source( $final_order ) ); } @@ -1649,7 +1649,7 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1733,7 +1733,7 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PENDING, $final_order->get_status() ); // Order status should be pending until 3DS is completed. $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( "/#wc-stripe-confirm-pi:$order_id:$client_secret/", $response['redirect'] ); } @@ -1898,7 +1898,7 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1986,7 +1986,7 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'failure', $response['result'] ); $this->assertEquals( OrderStatus::FAILED, $final_order->get_status() ); - $this->assertEquals( '', WC_Stripe_Order_Helper::get_instance()->get_stripe_customer( $final_order ) ); + $this->assertEquals( '', WC_Stripe_Order_Helper::get_instance()->get_stripe_customer_id( $final_order ) ); } /** @@ -2388,7 +2388,7 @@ public function test_pre_order_payment_is_successful() { $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); - $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $final_order ) ); } @@ -2455,7 +2455,7 @@ public function test_pre_order_without_payment_uses_setup_intents() { $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); - $this->assertEquals( $customer_id, $order_helper->get_stripe_customer( $final_order ) ); + $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $final_order ) ); } From 8f8960ebf32031e1195aed88571977d14a7c0741 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 8 Oct 2025 11:30:01 -0300 Subject: [PATCH 67/71] Update includes/class-wc-stripe-order-helper.php Co-authored-by: daledupreez --- includes/class-wc-stripe-order-helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index fd4f9c2e05..658d7823f4 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -484,7 +484,7 @@ public function update_stripe_setup_intent( ?WC_Order $order = null, string $int * @return false|string|null */ public function get_stripe_customer_id( ?WC_Order $order = null ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } From 10125f0c244606e5201c5c9546a8a8c280e8cd70 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 8 Oct 2025 11:31:07 -0300 Subject: [PATCH 68/71] Changing null check --- includes/class-wc-stripe-order-helper.php | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 658d7823f4..e623adcc36 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -468,7 +468,7 @@ public function get_stripe_setup_intent( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_setup_intent( ?WC_Order $order = null, string $intent_id = '' ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -501,7 +501,7 @@ public function get_stripe_customer_id( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_customer_id( ?WC_Order $order = null, string $customer_id = '' ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -517,7 +517,7 @@ public function update_stripe_customer_id( ?WC_Order $order = null, string $cust * @return false|void */ public function delete_stripe_customer_id( ?WC_Order $order = null ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -533,7 +533,7 @@ public function delete_stripe_customer_id( ?WC_Order $order = null ) { * @return false|string|null */ public function get_stripe_card_id( ?WC_Order $order = null ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -549,7 +549,7 @@ public function get_stripe_card_id( ?WC_Order $order = null ) { * @return false|void */ public function delete_stripe_card_id( ?WC_Order $order = null ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -565,7 +565,7 @@ public function delete_stripe_card_id( ?WC_Order $order = null ) { * @return false|string|null */ public function get_stripe_upe_payment_type( ?WC_Order $order = null ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -582,7 +582,7 @@ public function get_stripe_upe_payment_type( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_upe_payment_type( ?WC_Order $order = null, string $payment_type = '' ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -598,7 +598,7 @@ public function update_stripe_upe_payment_type( ?WC_Order $order = null, string * @return bool|null */ public function get_stripe_upe_waiting_for_redirect( ?WC_Order $order = null ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -615,7 +615,7 @@ public function get_stripe_upe_waiting_for_redirect( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_upe_waiting_for_redirect( ?WC_Order $order = null, bool $waiting_for_redirect = false ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -631,7 +631,7 @@ public function update_stripe_upe_waiting_for_redirect( ?WC_Order $order = null, * @return false|void */ public function delete_stripe_upe_waiting_for_redirect( ?WC_Order $order = null ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -647,7 +647,7 @@ public function delete_stripe_upe_waiting_for_redirect( ?WC_Order $order = null * @return bool|null */ public function get_stripe_upe_redirect_processed( ?WC_Order $order = null ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } @@ -664,7 +664,7 @@ public function get_stripe_upe_redirect_processed( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_upe_redirect_processed( ?WC_Order $order = null, bool $redirect_processed = false ) { - if ( is_null( $order ) ) { + if ( null === $order ) { return false; } From aff3db9c4f49931d8c6c2a7d5c4b578a6b47b177 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 8 Oct 2025 11:40:34 -0300 Subject: [PATCH 69/71] New protected method to handle meta update --- includes/class-wc-stripe-order-helper.php | 83 ++++++++--------------- 1 file changed, 28 insertions(+), 55 deletions(-) diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index e623adcc36..6469564951 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -168,11 +168,7 @@ public function get_stripe_currency( ?WC_Order $order = null ) { * @param string $currency */ public function update_stripe_currency( WC_Order $order, string $currency ) { - if ( is_null( $order ) ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_CURRENCY, $currency ); + return $this->update_order_meta( $order, self::META_STRIPE_CURRENCY, $currency ); } /** @@ -212,11 +208,7 @@ public function get_stripe_fee( ?WC_Order $order = null ) { * @param float $amount */ public function update_stripe_fee( ?WC_Order $order = null, float $amount = 0.0 ) { - if ( is_null( $order ) ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_FEE, $amount ); + return $this->update_order_meta( $order, self::META_STRIPE_FEE, $amount ); } /** @@ -272,11 +264,7 @@ public function get_stripe_net( ?WC_Order $order = null ) { * @param float $amount */ public function update_stripe_net( ?WC_Order $order = null, float $amount = 0.0 ) { - if ( is_null( $order ) ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_NET, $amount ); + return $this->update_order_meta( $order, self::META_STRIPE_NET, $amount ); } /** @@ -321,11 +309,7 @@ public function get_stripe_source( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_source( ?WC_Order $order = null, string $source_id = '' ) { - if ( is_null( $order ) ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_SOURCE_ID, $source_id ); + return $this->update_order_meta( $order, self::META_STRIPE_SOURCE_ID, $source_id ); } /** @@ -370,11 +354,7 @@ public function get_stripe_refund( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_refund( ?WC_Order $order = null, string $refund_id = '' ) { - if ( is_null( $order ) ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_REFUND_ID, $refund_id ); + return $this->update_order_meta( $order, self::META_STRIPE_REFUND_ID, $refund_id ); } /** @@ -419,11 +399,7 @@ public function get_stripe_intent( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_intent( ?WC_Order $order = null, string $intent_id = '' ) { - if ( is_null( $order ) ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_INTENT_ID, $intent_id ); + return $this->update_order_meta( $order, self::META_STRIPE_INTENT_ID, $intent_id ); } /** @@ -468,11 +444,7 @@ public function get_stripe_setup_intent( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_setup_intent( ?WC_Order $order = null, string $intent_id = '' ) { - if ( null === $order ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_SETUP_INTENT, $intent_id ); + return $this->update_order_meta( $order, self::META_STRIPE_SETUP_INTENT, $intent_id ); } /** @@ -501,11 +473,7 @@ public function get_stripe_customer_id( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_customer_id( ?WC_Order $order = null, string $customer_id = '' ) { - if ( null === $order ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_CUSTOMER_ID, $customer_id ); + return $this->update_order_meta( $order, self::META_STRIPE_CUSTOMER_ID, $customer_id ); } /** @@ -582,11 +550,7 @@ public function get_stripe_upe_payment_type( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_upe_payment_type( ?WC_Order $order = null, string $payment_type = '' ) { - if ( null === $order ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_UPE_PAYMENT_TYPE, $payment_type ); + return $this->update_order_meta( $order, self::META_STRIPE_UPE_PAYMENT_TYPE, $payment_type ); } /** @@ -615,11 +579,7 @@ public function get_stripe_upe_waiting_for_redirect( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_upe_waiting_for_redirect( ?WC_Order $order = null, bool $waiting_for_redirect = false ) { - if ( null === $order ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_UPE_WAITING_FOR_REDIRECT, $waiting_for_redirect ); + return $this->update_order_meta( $order, self::META_STRIPE_UPE_WAITING_FOR_REDIRECT, $waiting_for_redirect ); } /** @@ -664,11 +624,7 @@ public function get_stripe_upe_redirect_processed( ?WC_Order $order = null ) { * @return false|void */ public function update_stripe_upe_redirect_processed( ?WC_Order $order = null, bool $redirect_processed = false ) { - if ( null === $order ) { - return false; - } - - $order->update_meta_data( self::META_STRIPE_UPE_REDIRECT_PROCESSED, $redirect_processed ); + return $this->update_order_meta( $order, self::META_STRIPE_UPE_REDIRECT_PROCESSED, $redirect_processed ); } /** @@ -711,6 +667,7 @@ public function add_payment_intent_to_order( string $payment_intent_id, WC_Order * @return void */ public function set_payment_awaiting_action( WC_Order $order, bool $save = true ): void { + $this->update_order_meta( $order, self::META_STRIPE_PAYMENT_AWAITING_ACTION, true ); $order->update_meta_data( self::META_STRIPE_PAYMENT_AWAITING_ACTION, wc_bool_to_string( true ) ); if ( $save ) { @@ -1060,4 +1017,20 @@ protected function is_order_refund_locked( WC_Order $order ): bool { return false; } + + /** + * Updates order meta data. + * + * @param WC_Order|null $order The order to update meta for. + * @param string $key The meta key to update. + * @param mixed $value The meta value to set. + * @return false|void + */ + protected function update_order_meta( ?WC_Order $order, string $key, $value ) { + if ( null === $order ) { + return false; + } + + $order->update_meta_data( $key, $value ); + } } From 29b7ab8e3df9c10317b6837b7ea2d707346ec51d Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 8 Oct 2025 11:51:54 -0300 Subject: [PATCH 70/71] Renaming Order Helper methods to append _id --- .../abstract-wc-stripe-payment-gateway.php | 16 +++---- includes/admin/class-wc-stripe-privacy.php | 14 +++--- includes/class-wc-stripe-helper.php | 8 ++-- .../class-wc-stripe-intent-controller.php | 2 +- includes/class-wc-stripe-order-helper.php | 22 ++++----- includes/class-wc-stripe-webhook-handler.php | 6 +-- .../compat/trait-wc-stripe-pre-orders.php | 2 +- .../compat/trait-wc-stripe-subscriptions.php | 14 +++--- .../class-wc-stripe-upe-payment-gateway.php | 4 +- .../WC_REST_Stripe_Orders_Controller_Test.php | 2 +- .../WC_Stripe_Settings_Controller_Test.php | 2 +- .../WC_Stripe_UPE_Payment_Gateway_Test.php | 46 +++++++++---------- tests/phpunit/WC_Stripe_Helper_Test.php | 2 +- .../WC_Stripe_Payment_Gateway_Test.php | 6 +-- .../WC_Stripe_Subscription_Initial_Test.php | 2 +- .../WC_Stripe_Subscription_Renewal_Test.php | 4 +- .../WC_Stripe_Webhook_Handler_Test.php | 2 +- 17 files changed, 77 insertions(+), 77 deletions(-) diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index f23355b0b7..baaf721506 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1021,14 +1021,14 @@ public function prepare_order_source( $order = null ) { } $order_helper = WC_Stripe_Order_Helper::get_instance(); - $source_id = $order_helper->get_stripe_source( $order ); + $source_id = $order_helper->get_stripe_source_id( $order ); // Since 4.0.0, we changed card to source so we need to account for that. if ( empty( $source_id ) ) { $source_id = $order_helper->get_stripe_card_id( $order ); // Take this opportunity to update the key name. - $order_helper->update_stripe_source( $order, $source_id ); + $order_helper->update_stripe_source_id( $order, $source_id ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); @@ -1087,7 +1087,7 @@ public function save_source_to_order( $order, $source ) { } if ( $source->source ) { - $order_helper->update_stripe_source( $order, $source->source ); + $order_helper->update_stripe_source_id( $order, $source->source ); } if ( is_callable( [ $order, 'save' ] ) ) { @@ -1281,7 +1281,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) { } } - $order_helper->update_stripe_refund( $order, $response->id ); + $order_helper->update_stripe_refund_id( $order, $response->id ); if ( isset( $response->balance_transaction ) ) { $this->update_fees( $order, $response->balance_transaction ); @@ -1704,7 +1704,7 @@ public function save_intent_to_order( $order, $intent ) { $order->update_meta_data( '_stripe_mandate_id', $charge->payment_method_details->acss_debit->mandate ); } } elseif ( 'setup_intent' === $intent->object ) { - $order_helper->update_stripe_setup_intent( $order, $intent->id ); + $order_helper->update_stripe_setup_intent_id( $order, $intent->id ); // Add mandate for free trial subscriptions. if ( isset( $intent->mandate ) ) { @@ -1726,13 +1726,13 @@ public function save_intent_to_order( $order, $intent ) { */ public function get_intent_from_order( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); - $intent_id = $order_helper->get_stripe_intent( $order ); + $intent_id = $order_helper->get_stripe_intent_id( $order ); if ( $intent_id ) { return $this->get_intent( 'payment_intents', $intent_id ); } // The order doesn't have a payment intent, but it may have a setup intent. - $intent_id = $order_helper->get_stripe_setup_intent( $order ); + $intent_id = $order_helper->get_stripe_setup_intent_id( $order ); if ( $intent_id ) { return $this->get_intent( 'setup_intents', $intent_id ); } @@ -1921,7 +1921,7 @@ public function setup_intent( $order, $prepared_source ) { if ( is_wp_error( $setup_intent ) ) { WC_Stripe_Logger::log( "Unable to create SetupIntent for Order #$order_id: " . print_r( $setup_intent, true ) ); } elseif ( WC_Stripe_Intent_Status::REQUIRES_ACTION === $setup_intent->status ) { - WC_Stripe_Order_Helper::get_instance()->update_stripe_setup_intent( $order, $setup_intent->id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_setup_intent_id( $order, $setup_intent->id ); $order->save(); return $setup_intent->client_secret; diff --git a/includes/admin/class-wc-stripe-privacy.php b/includes/admin/class-wc-stripe-privacy.php index b198b46edf..3760eba558 100644 --- a/includes/admin/class-wc-stripe-privacy.php +++ b/includes/admin/class-wc-stripe-privacy.php @@ -131,7 +131,7 @@ public function order_data_exporter( $email_address, $page = 1 ) { 'data' => [ [ 'name' => __( 'Stripe payment id', 'woocommerce-gateway-stripe' ), - 'value' => $order_helper->get_stripe_source( $order ), + 'value' => $order_helper->get_stripe_source_id( $order ), ], [ 'name' => __( 'Stripe customer id', 'woocommerce-gateway-stripe' ), @@ -367,8 +367,8 @@ protected function maybe_handle_subscription( $order ) { $renewal_orders = class_exists( 'WC_Subscriptions_Renewal_Order' ) ? WC_Subscriptions_Renewal_Order::get_renewal_orders( $order->get_id(), 'WC_Order' ) : []; foreach ( $renewal_orders as $renewal_order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); - $order_helper->delete_stripe_source( $renewal_order ); - $order_helper->delete_stripe_refund( $renewal_order ); + $order_helper->delete_stripe_source_id( $renewal_order ); + $order_helper->delete_stripe_refund_id( $renewal_order ); $order_helper->delete_stripe_customer_id( $renewal_order ); } @@ -387,8 +387,8 @@ protected function maybe_handle_subscription( $order ) { */ protected function maybe_handle_order( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); - $stripe_source_id = $order_helper->get_stripe_source( $order ); - $stripe_refund_id = $order_helper->get_stripe_refund( $order ); + $stripe_source_id = $order_helper->get_stripe_source_id( $order ); + $stripe_refund_id = $order_helper->get_stripe_refund_id( $order ); $stripe_customer_id = $order_helper->get_stripe_customer_id( $order ); if ( ! $this->is_retention_expired( $order->get_date_created()->getTimestamp() ) ) { @@ -400,8 +400,8 @@ protected function maybe_handle_order( $order ) { return [ false, false, [] ]; } - $order_helper->delete_stripe_source( $order ); - $order_helper->delete_stripe_refund( $order ); + $order_helper->delete_stripe_source_id( $order ); + $order_helper->delete_stripe_refund_id( $order ); $order_helper->delete_stripe_customer_id( $order ); return [ true, false, [ __( 'Stripe personal data erased.', 'woocommerce-gateway-stripe' ) ] ]; diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index efee587ddf..7a99054117 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1305,7 +1305,7 @@ private static function should_load_scripts_for_prb_location( $location ) { */ public static function add_payment_intent_to_order( $payment_intent_id, $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); - $old_intent_id = $order_helper->get_stripe_intent( $order ); + $old_intent_id = $order_helper->get_stripe_intent_id( $order ); if ( $old_intent_id === $payment_intent_id ) { return; } @@ -1318,7 +1318,7 @@ public static function add_payment_intent_to_order( $payment_intent_id, $order ) ) ); - $order_helper->update_stripe_intent( $order, $payment_intent_id ); + $order_helper->update_stripe_intent_id( $order, $payment_intent_id ); $order->save(); } @@ -1421,9 +1421,9 @@ public static function get_payment_method_from_intent( $intent ) { */ public static function get_intent_id_from_order( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); - $intent_id = $order_helper->get_stripe_intent( $order ); + $intent_id = $order_helper->get_stripe_intent_id( $order ); if ( ! $intent_id ) { - $intent_id = $order_helper->get_stripe_setup_intent( $order ); + $intent_id = $order_helper->get_stripe_setup_intent_id( $order ); } return $intent_id ?? false; diff --git a/includes/class-wc-stripe-intent-controller.php b/includes/class-wc-stripe-intent-controller.php index 1daacfe5cc..07386325eb 100644 --- a/includes/class-wc-stripe-intent-controller.php +++ b/includes/class-wc-stripe-intent-controller.php @@ -127,7 +127,7 @@ public function verify_intent() { } // Validate the intent being verified. - $order_intent_id = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ); + $order_intent_id = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent_id( $order ); if ( ! $order_intent_id || ! isset( $_GET['intent_id'] ) || $order_intent_id !== $_GET['intent_id'] ) { throw new WC_Stripe_Exception( 'invalid_intent', __( "We're not able to process this payment. Please try again later.", 'woocommerce-gateway-stripe' ) ); } diff --git a/includes/class-wc-stripe-order-helper.php b/includes/class-wc-stripe-order-helper.php index 6469564951..76faf2eb1c 100644 --- a/includes/class-wc-stripe-order-helper.php +++ b/includes/class-wc-stripe-order-helper.php @@ -291,7 +291,7 @@ public function delete_stripe_net( ?WC_Order $order = null ) { * @param WC_Order|null $order * @return false|string|null */ - public function get_stripe_source( ?WC_Order $order = null ) { + public function get_stripe_source_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } @@ -308,7 +308,7 @@ public function get_stripe_source( ?WC_Order $order = null ) { * @param string $source_id * @return false|void */ - public function update_stripe_source( ?WC_Order $order = null, string $source_id = '' ) { + public function update_stripe_source_id( ?WC_Order $order = null, string $source_id = '' ) { return $this->update_order_meta( $order, self::META_STRIPE_SOURCE_ID, $source_id ); } @@ -320,7 +320,7 @@ public function update_stripe_source( ?WC_Order $order = null, string $source_id * @param WC_Order|null $order * @return false|void */ - public function delete_stripe_source( ?WC_Order $order = null ) { + public function delete_stripe_source_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } @@ -336,7 +336,7 @@ public function delete_stripe_source( ?WC_Order $order = null ) { * @param WC_Order|null $order * @return false|string|null */ - public function get_stripe_refund( ?WC_Order $order = null ) { + public function get_stripe_refund_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } @@ -353,7 +353,7 @@ public function get_stripe_refund( ?WC_Order $order = null ) { * @param string $refund_id * @return false|void */ - public function update_stripe_refund( ?WC_Order $order = null, string $refund_id = '' ) { + public function update_stripe_refund_id( ?WC_Order $order = null, string $refund_id = '' ) { return $this->update_order_meta( $order, self::META_STRIPE_REFUND_ID, $refund_id ); } @@ -365,7 +365,7 @@ public function update_stripe_refund( ?WC_Order $order = null, string $refund_id * @param WC_Order|null $order * @return false|void */ - public function delete_stripe_refund( ?WC_Order $order = null ) { + public function delete_stripe_refund_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } @@ -381,7 +381,7 @@ public function delete_stripe_refund( ?WC_Order $order = null ) { * @param WC_Order|null $order * @return false|string|null */ - public function get_stripe_intent( ?WC_Order $order = null ) { + public function get_stripe_intent_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } @@ -398,7 +398,7 @@ public function get_stripe_intent( ?WC_Order $order = null ) { * @param string $intent_id * @return false|void */ - public function update_stripe_intent( ?WC_Order $order = null, string $intent_id = '' ) { + public function update_stripe_intent_id( ?WC_Order $order = null, string $intent_id = '' ) { return $this->update_order_meta( $order, self::META_STRIPE_INTENT_ID, $intent_id ); } @@ -410,7 +410,7 @@ public function update_stripe_intent( ?WC_Order $order = null, string $intent_id * @param WC_Order|null $order * @return false|void */ - public function delete_stripe_intent( ?WC_Order $order = null ) { + public function delete_stripe_intent_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } @@ -426,7 +426,7 @@ public function delete_stripe_intent( ?WC_Order $order = null ) { * @param WC_Order|null $order * @return false|string|null */ - public function get_stripe_setup_intent( ?WC_Order $order = null ) { + public function get_stripe_setup_intent_id( ?WC_Order $order = null ) { if ( is_null( $order ) ) { return false; } @@ -443,7 +443,7 @@ public function get_stripe_setup_intent( ?WC_Order $order = null ) { * @param string $intent_id * @return false|void */ - public function update_stripe_setup_intent( ?WC_Order $order = null, string $intent_id = '' ) { + public function update_stripe_setup_intent_id( ?WC_Order $order = null, string $intent_id = '' ) { return $this->update_order_meta( $order, self::META_STRIPE_SETUP_INTENT, $intent_id ); } diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 19f8dd9453..5a6ecd79c3 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -742,7 +742,7 @@ public function process_webhook_refund( $notification ) { if ( $order_helper->is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $captured = $order->get_meta( '_stripe_charge_captured' ); - $refund_id = $order_helper->get_stripe_refund( $order ); + $refund_id = $order_helper->get_stripe_refund_id( $order ); $currency = $order->get_currency(); $raw_amount = $refund_object->amount; @@ -792,7 +792,7 @@ public function process_webhook_refund( $notification ) { WC_Stripe_Logger::log( $refund->get_error_message() ); } - $order_helper->update_stripe_refund( $order, $refund_object->id ); + $order_helper->update_stripe_refund_id( $order, $refund_object->id ); if ( isset( $refund_object->balance_transaction ) ) { $this->update_fees( $order, $refund_object->balance_transaction ); @@ -827,7 +827,7 @@ public function process_webhook_refund_updated( $notification ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); if ( $order_helper->is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); - $refund_id = $order_helper->get_stripe_refund( $order ); + $refund_id = $order_helper->get_stripe_refund_id( $order ); $currency = $order->get_currency(); $raw_amount = $refund_object->amount; diff --git a/includes/compat/trait-wc-stripe-pre-orders.php b/includes/compat/trait-wc-stripe-pre-orders.php index eda6d180d4..ec3e4e9671 100644 --- a/includes/compat/trait-wc-stripe-pre-orders.php +++ b/includes/compat/trait-wc-stripe-pre-orders.php @@ -174,7 +174,7 @@ class_exists( 'WC_Pre_Orders_Order' ) && */ public function remove_order_source_before_retry( $order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); - $order_helper->delete_stripe_source( $order ); + $order_helper->delete_stripe_source_id( $order ); $order_helper->delete_stripe_card_id( $order ); $order->save(); } diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php index 8927fc2be2..2e10aa623b 100644 --- a/includes/compat/trait-wc-stripe-subscriptions.php +++ b/includes/compat/trait-wc-stripe-subscriptions.php @@ -644,13 +644,13 @@ public function maybe_update_source_on_subscription_order( $order, $source, $pay */ public function delete_resubscribe_meta( $resubscribe_order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); - $order_helper->delete_stripe_source( $resubscribe_order ); + $order_helper->delete_stripe_source_id( $resubscribe_order ); $order_helper->delete_stripe_customer_id( $resubscribe_order ); // For BW compat will remove in future. $order_helper->delete_stripe_card_id( $resubscribe_order ); // Delete payment intent ID. - $order_helper->delete_stripe_intent( $resubscribe_order ); + $order_helper->delete_stripe_intent_id( $resubscribe_order ); $this->delete_renewal_meta( $resubscribe_order ); $resubscribe_order->save(); } @@ -666,7 +666,7 @@ public function delete_renewal_meta( $renewal_order ) { $order_helper->delete_stripe_net( $renewal_order ); // Delete payment intent ID. - $order_helper->delete_stripe_intent( $renewal_order ); + $order_helper->delete_stripe_intent_id( $renewal_order ); return $renewal_order; } @@ -682,7 +682,7 @@ public function delete_renewal_meta( $renewal_order ) { public function update_failing_payment_method( $subscription, $renewal_order ) { $order_helper = WC_Stripe_Order_Helper::get_instance(); $subscription->update_meta_data( '_stripe_customer_id', $order_helper->get_stripe_customer_id( $renewal_order ) ); - $subscription->update_meta_data( '_stripe_source_id', $order_helper->get_stripe_source( $renewal_order ) ); + $subscription->update_meta_data( '_stripe_source_id', $order_helper->get_stripe_source_id( $renewal_order ) ); $subscription->save(); } @@ -848,7 +848,7 @@ private function get_mandate_for_subscription( $order, $payment_method ) { } $mandate = $renewal_order->get_meta( '_stripe_mandate_id', true ); - $renewal_order_payment_method = WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $renewal_order ); + $renewal_order_payment_method = WC_Stripe_Order_Helper::get_instance()->get_stripe_source_id( $renewal_order ); // Return from the most recent renewal order with a valid mandate. Mandate is created against a payment method // in Stripe so the payment method should also match to reuse the mandate. @@ -1012,14 +1012,14 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis $order_helper = WC_Stripe_Order_Helper::get_instance(); $parent_order = wc_get_order( $subscription->get_parent_id() ); $stripe_customer_id = $order_helper->get_stripe_customer_id( $parent_order ); - $stripe_source_id = $order_helper->get_stripe_source( $parent_order ); + $stripe_source_id = $order_helper->get_stripe_source_id( $parent_order ); // For BW compat will remove in future. if ( empty( $stripe_source_id ) ) { $stripe_source_id = $order_helper->get_stripe_card_id( $parent_order ); // Take this opportunity to update the key name. - $order_helper->update_stripe_source( $parent_order, $stripe_source_id ); + $order_helper->update_stripe_source_id( $parent_order, $stripe_source_id ); $parent_order->save(); } } diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index e28d069ceb..7d2040708b 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -1819,7 +1819,7 @@ public function save_payment_method_to_order( $order, $payment_method ) { } // Save the payment method id as `source_id`, because we use both `sources` and `payment_methods` APIs. - $order_helper->update_stripe_source( $order, $payment_method->payment_method ); + $order_helper->update_stripe_source_id( $order, $payment_method->payment_method ); if ( is_callable( [ $order, 'save' ] ) ) { $order->save(); @@ -2892,7 +2892,7 @@ protected function handle_saving_payment_method( WC_Order $order, $payment_metho */ public function set_payment_method_id_for_order( WC_Order $order, string $payment_method_id ) { // Save the payment method id as `source_id`, because we use both `sources` and `payment_methods` APIs. - WC_Stripe_Order_Helper::get_instance()->update_stripe_source( $order, $payment_method_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_source_id( $order, $payment_method_id ); $order->save_meta_data(); } diff --git a/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php b/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php index 4726dbdf30..1c9f38729e 100644 --- a/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php +++ b/tests/phpunit/Admin/WC_REST_Stripe_Orders_Controller_Test.php @@ -151,7 +151,7 @@ public function test_capture_payment_success() { $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 'succeeded', $response->get_data()['status'] ); $this->assertEquals( 'ch_12345', $response->get_data()['id'] ); - $this->assertEquals( 'pi_12345', WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ) ); + $this->assertEquals( 'pi_12345', WC_Stripe_Order_Helper::get_instance()->get_stripe_intent_id( $order ) ); remove_filter( 'pre_http_request', $test_request, 10, 3 ); } diff --git a/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php b/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php index 06c7f7cb88..ce922f201c 100644 --- a/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php +++ b/tests/phpunit/Admin/WC_Stripe_Settings_Controller_Test.php @@ -97,7 +97,7 @@ public function test_add_buttons_action_is_called_on_order_admin_page() { $order = WC_Helper_Order::create_order(); $intent_id = 'pi_mock'; - WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $intent_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent_id( $order, $intent_id ); $order->save_meta_data(); $intent = (object) [ diff --git a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php index ee314e3100..fd6c681659 100644 --- a/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php +++ b/tests/phpunit/PaymentMethods/WC_Stripe_UPE_Payment_Gateway_Test.php @@ -471,7 +471,7 @@ public function test_process_payment_returns_valid_response() { $order_id = $order->get_id(); $order_helper = WC_Stripe_Order_Helper::get_instance(); - $order_helper->update_stripe_intent( $order, $payment_intent_id ); + $order_helper->update_stripe_intent_id( $order, $payment_intent_id ); $order_helper->update_stripe_upe_payment_type( $order, '' ); $order_helper->update_stripe_upe_waiting_for_redirect( $order, true ); $order->save(); @@ -1045,7 +1045,7 @@ public function test_process_redirect_payment_returns_valid_response() { $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); - $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent_id( $final_order ) ); $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1111,7 +1111,7 @@ public function test_process_redirect_payment_only_runs_once() { // assert successful order processing $this->assertEquals( OrderStatus::PROCESSING, $success_order->get_status() ); $this->assertEquals( 'Credit / Debit Card', $success_order->get_payment_method_title() ); - $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $success_order ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent_id( $success_order ) ); $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $success_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); @@ -1208,7 +1208,7 @@ public function test_checkout_without_payment_uses_setup_intents() { $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); - $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); } @@ -1271,9 +1271,9 @@ public function test_checkout_saves_payment_method_to_order() { $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent_id( $final_order ) ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); - $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); } /** @@ -1341,9 +1341,9 @@ public function test_checkout_saves_sepa_generated_payment_method_to_order() { $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent_id( $final_order ) ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); - $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); } /** @@ -1403,7 +1403,7 @@ public function test_setup_intent_checkout_saves_sepa_generated_payment_method_t $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); - $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $generated_payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); } /** @@ -1648,9 +1648,9 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent_id( $final_order ) ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); - $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -1732,9 +1732,9 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PENDING, $final_order->get_status() ); // Order status should be pending until 3DS is completed. - $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent_id( $final_order ) ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); - $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); $this->assertMatchesRegularExpression( "/#wc-stripe-confirm-pi:$order_id:$client_secret/", $response['redirect'] ); } @@ -1795,8 +1795,8 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'failure', $response['result'] ); $this->assertEquals( OrderStatus::FAILED, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); - $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent_id( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); } /** @@ -1897,9 +1897,9 @@ function ( $passed_order ) use ( $order ) { $this->assertEquals( 'success', $response['result'] ); $this->assertEquals( OrderStatus::PROCESSING, $final_order->get_status() ); - $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent_id( $final_order ) ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); - $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } @@ -2005,7 +2005,7 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { $order_helper = WC_Stripe_Order_Helper::get_instance(); - $order_helper->update_stripe_intent( $order, $payment_intent_id ); + $order_helper->update_stripe_intent_id( $order, $payment_intent_id ); $order_helper->update_stripe_upe_payment_type( $order, '' ); $order_helper->update_stripe_upe_waiting_for_redirect( $order, true ); $order->save(); @@ -2387,9 +2387,9 @@ public function test_pre_order_payment_is_successful() { $order_helper = WC_Stripe_Order_Helper::get_instance(); $this->assertEquals( 'Credit / Debit Card', $final_order->get_payment_method_title() ); - $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); - $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent( $final_order ) ); + $this->assertEquals( $payment_intent_id, $order_helper->get_stripe_intent_id( $final_order ) ); $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $final_order ) ); } @@ -2454,7 +2454,7 @@ public function test_pre_order_without_payment_uses_setup_intents() { $final_order = wc_get_order( $order_id ); $order_helper = WC_Stripe_Order_Helper::get_instance(); - $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source( $final_order ) ); + $this->assertEquals( $payment_method_id, $order_helper->get_stripe_source_id( $final_order ) ); $this->assertEquals( $customer_id, $order_helper->get_stripe_customer_id( $final_order ) ); $this->assertTrue( (bool) $order_helper->get_stripe_upe_redirect_processed( $final_order ) ); } @@ -2686,7 +2686,7 @@ public function test_process_payment_creates_new_intent_when_existing_intent_fai ]; // Save the failed intent ID to the order - WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $mock_failed_intent->id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent_id( $order, $mock_failed_intent->id ); $order->save(); // Mock that we find an existing failed intent on the order @@ -2823,7 +2823,7 @@ function ( $passed_order ) use ( $order ) { )[0]; $this->assertEquals( 'success', $response['result'] ); - $this->assertEquals( $payment_method_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_source( $final_order ) ); + $this->assertEquals( $payment_method_id, WC_Stripe_Order_Helper::get_instance()->get_stripe_source_id( $final_order ) ); $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } diff --git a/tests/phpunit/WC_Stripe_Helper_Test.php b/tests/phpunit/WC_Stripe_Helper_Test.php index 991115a845..37e7db4394 100644 --- a/tests/phpunit/WC_Stripe_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Helper_Test.php @@ -217,7 +217,7 @@ public function test_get_order_by_intent_id( $status, $success ) { $order->set_status( $status ); $intent_id = 'pi_mock'; - WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, $intent_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent_id( $order, $intent_id ); $order->save_meta_data(); $order = WC_Stripe_Helper::get_order_by_intent_id( $intent_id ); diff --git a/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php b/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php index 1d838cdcc8..53824b539f 100644 --- a/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php +++ b/tests/phpunit/WC_Stripe_Payment_Gateway_Test.php @@ -97,7 +97,7 @@ public function test_default_get_payment_intent_from_order() { public function test_success_get_payment_intent_from_order() { $order = WC_Helper_Order::create_order(); - WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, 'pi_123' ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent_id( $order, 'pi_123' ); $expected_intent = (object) [ 'id' => 'pi_123' ]; $callback = function ( $preempt, $request_args, $url ) use ( $expected_intent ) { @@ -130,7 +130,7 @@ public function test_success_get_payment_intent_from_order() { public function test_error_get_payment_intent_from_order() { $order = WC_Helper_Order::create_order(); - WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, 'pi_123' ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent_id( $order, 'pi_123' ); $response_error = (object) [ 'error' => [ @@ -851,7 +851,7 @@ public function test_process_refund_voids_pre_auth_on_cancel() { $order->set_transaction_id( 'ch_123' ); $this->updateOrderMeta( $order, '_stripe_charge_captured', 'no' ); - WC_Stripe_Order_Helper::get_instance()->update_stripe_intent( $order, 'pi_123' ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_intent_id( $order, 'pi_123' ); $order->save(); $order_id = $order->get_id(); diff --git a/tests/phpunit/WC_Stripe_Subscription_Initial_Test.php b/tests/phpunit/WC_Stripe_Subscription_Initial_Test.php index 96eea684c3..189f4d54be 100644 --- a/tests/phpunit/WC_Stripe_Subscription_Initial_Test.php +++ b/tests/phpunit/WC_Stripe_Subscription_Initial_Test.php @@ -213,7 +213,7 @@ public function test_initial_intent_parameters() { $this->assertArrayHasKey( 'redirect', $result ); $order = wc_get_order( $order_id ); - $order_data = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ); + $order_data = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent_id( $order ); $this->assertEquals( $order_data, 'pi_123abc' ); diff --git a/tests/phpunit/WC_Stripe_Subscription_Renewal_Test.php b/tests/phpunit/WC_Stripe_Subscription_Renewal_Test.php index ce8c0c82d6..23db5e158d 100644 --- a/tests/phpunit/WC_Stripe_Subscription_Renewal_Test.php +++ b/tests/phpunit/WC_Stripe_Subscription_Renewal_Test.php @@ -247,7 +247,7 @@ public function test_renewal_successful() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); $order = wc_get_order( $order_id ); - $order_data = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ); + $order_data = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent_id( $order ); $this->assertEquals( $order_data, 'pi_123abc' ); @@ -367,7 +367,7 @@ public function test_renewal_authorization_required() { // Assert that we saved the payment intent to the order. $order_id = $renewal_order->get_id(); $order = wc_get_order( $order_id ); - $order_data = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent( $order ); + $order_data = WC_Stripe_Order_Helper::get_instance()->get_stripe_intent_id( $order ); $order_transaction_id = $order->get_transaction_id(); // Intent was saved to order even though there was an error in the response body. diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index 179b533c32..39d09573c6 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -804,7 +804,7 @@ public function test_process_webhook_refund_updated( $notification_status, $emai $order->set_transaction_id( $charge_id ); $order->save(); - WC_Stripe_Order_Helper::get_instance()->update_stripe_refund( $order, $refund_id ); + WC_Stripe_Order_Helper::get_instance()->update_stripe_refund_id( $order, $refund_id ); $order->save_meta_data(); $refund_order = WC_Helper_Order::create_order(); From 1b18de3c97702368100cc457235cac5ff6464f09 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Wed, 8 Oct 2025 15:02:28 -0300 Subject: [PATCH 71/71] Changelog and readme entries --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index c7b9e6de22..e1958b510a 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 10.0.0 - xxxx-xx-xx = +* Dev - Renames previous Order Helper class methods to use the `_id` suffix * Dev - Expands the Stripe Order Helper class to handle customer ID, card ID, UPE payment type, and UPE redirect status metas * Update - Removes frontend code related to Payment Request Buttons in the checkout page * Update - Disable Payment Request Buttons and ensure Express Checkout is used when express checkout buttons are enabled diff --git a/readme.txt b/readme.txt index 9b7e917e56..654df08612 100644 --- a/readme.txt +++ b/readme.txt @@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o == Changelog == = 10.0.0 - xxxx-xx-xx = +* Dev - Renames previous Order Helper class methods to use the `_id` suffix * Dev - Expands the Stripe Order Helper class to handle customer ID, card ID, UPE payment type, and UPE redirect status metas * Update - Removes frontend code related to Payment Request Buttons in the checkout page * Update - Disable Payment Request Buttons and ensure Express Checkout is used when express checkout buttons are enabled