diff --git a/changelog.txt b/changelog.txt index d78fd619f7..42b89e1aa4 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 9.9.0 - xxxx-xx-xx = +* 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 * Fix - Payment method test instructions not showing up for the Optimized Checkout payment element diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index e65ed4c553..5ed51f9e87 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -2003,4 +2003,14 @@ public static function is_connected( $mode = null ) { return isset( $options['publishable_key'], $options['secret_key'] ) && trim( $options['publishable_key'] ) && trim( $options['secret_key'] ); } } + + /** + * Checks if the order is using a Stripe payment method. + * + * @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 ); + } } diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index b06a0632ca..4760060003 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 ( substr( (string) $order->get_payment_method(), 0, 6 ) !== 'stripe' || ! $this->get_intent_from_order( $order ) ) { + if ( ! WC_Stripe_Helper::is_stripe_gateway_order( $order ) || ! $this->get_intent_from_order( $order ) ) { return $cancel_order; } diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index b0a3505e01..83f27360cc 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -735,7 +735,7 @@ public function process_webhook_refund( $notification ) { $order_id = $order->get_id(); - if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) { + if ( WC_Stripe_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' ); @@ -821,7 +821,7 @@ public function process_webhook_refund_updated( $notification ) { $order_id = $order->get_id(); - if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) { + if ( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ) { $charge = $order->get_transaction_id(); $refund_id = $order->get_meta( '_stripe_refund_id' ); $currency = $order->get_currency(); diff --git a/includes/compat/class-wc-stripe-subscriptions-helper.php b/includes/compat/class-wc-stripe-subscriptions-helper.php index 4a82e5a4ed..e30a879b86 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 ( 'stripe' !== substr( (string) $subscription->get_payment_method(), 0, 6 ) ) { + if ( ! WC_Stripe_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/readme.txt b/readme.txt index 7a7742bd82..c7fc12d60b 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 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 * Fix - Payment method test instructions not showing up for the Optimized Checkout payment element diff --git a/tests/phpunit/WC_Stripe_Helper_Test.php b/tests/phpunit/WC_Stripe_Helper_Test.php index 7206db3be4..4d09f87802 100644 --- a/tests/phpunit/WC_Stripe_Helper_Test.php +++ b/tests/phpunit/WC_Stripe_Helper_Test.php @@ -895,4 +895,25 @@ 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 ) ); + } }