Skip to content

Commit acdb6e1

Browse files
wjrosaMayisha
andauthored
Helper method to check for an order gateway (#4566)
* Checking for the subscription detached payment method * Changelog and readme entries * Unit tests * Fix tests * Fix tests * Fix tests * Helper method to check for an order gateway * Replacing additional occurrence * Changelog and readme entries * Unit test * Update includes/class-wc-stripe-helper.php Co-authored-by: Mayisha <[email protected]> --------- Co-authored-by: Mayisha <[email protected]>
1 parent bddc0fc commit acdb6e1

7 files changed

+37
-4
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 9.9.0 - xxxx-xx-xx =
4+
* Dev - Introduces a new helper method to identify Stripe orders
45
* Add - Setting to allow merchants to control the layout of the Optimized Checkout payment element on the checkout page
56
* Fix - Removes the credit card payment method requirement for the Optimized Checkout feature
67
* Fix - Payment method test instructions not showing up for the Optimized Checkout payment element

includes/class-wc-stripe-helper.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,4 +2003,14 @@ public static function is_connected( $mode = null ) {
20032003
return isset( $options['publishable_key'], $options['secret_key'] ) && trim( $options['publishable_key'] ) && trim( $options['secret_key'] );
20042004
}
20052005
}
2006+
2007+
/**
2008+
* Checks if the order is using a Stripe payment method.
2009+
*
2010+
* @param $order WC_Order The order to check.
2011+
* @return bool
2012+
*/
2013+
public static function is_stripe_gateway_order( $order ) {
2014+
return WC_Gateway_Stripe::ID === substr( (string) $order->get_payment_method(), 0, 6 );
2015+
}
20062016
}

includes/class-wc-stripe-order-handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public function prevent_cancelling_orders_awaiting_action( $cancel_order, $order
471471
}
472472

473473
// Bail if payment method is not stripe or `stripe_{apm_method}` or doesn't have an intent yet.
474-
if ( substr( (string) $order->get_payment_method(), 0, 6 ) !== 'stripe' || ! $this->get_intent_from_order( $order ) ) {
474+
if ( ! WC_Stripe_Helper::is_stripe_gateway_order( $order ) || ! $this->get_intent_from_order( $order ) ) {
475475
return $cancel_order;
476476
}
477477

includes/class-wc-stripe-webhook-handler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ public function process_webhook_refund( $notification ) {
735735

736736
$order_id = $order->get_id();
737737

738-
if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) {
738+
if ( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ) {
739739
$charge = $order->get_transaction_id();
740740
$captured = $order->get_meta( '_stripe_charge_captured' );
741741
$refund_id = $order->get_meta( '_stripe_refund_id' );
@@ -821,7 +821,7 @@ public function process_webhook_refund_updated( $notification ) {
821821

822822
$order_id = $order->get_id();
823823

824-
if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) {
824+
if ( WC_Stripe_Helper::is_stripe_gateway_order( $order ) ) {
825825
$charge = $order->get_transaction_id();
826826
$refund_id = $order->get_meta( '_stripe_refund_id' );
827827
$currency = $order->get_currency();

includes/compat/class-wc-stripe-subscriptions-helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public static function is_subscription_payment_method_detached( $subscription )
151151
return false;
152152
}
153153

154-
if ( 'stripe' !== substr( (string) $subscription->get_payment_method(), 0, 6 ) ) {
154+
if ( ! WC_Stripe_Helper::is_stripe_gateway_order( $subscription ) ) {
155155
// If the payment method is not a Stripe method, we don't need to check further.
156156
return false;
157157
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
111111
== Changelog ==
112112

113113
= 9.9.0 - xxxx-xx-xx =
114+
* Dev - Introduces a new helper method to identify Stripe orders
114115
* Add - Setting to allow merchants to control the layout of the Optimized Checkout payment element on the checkout page
115116
* Fix - Removes the credit card payment method requirement for the Optimized Checkout feature
116117
* Fix - Payment method test instructions not showing up for the Optimized Checkout payment element

tests/phpunit/WC_Stripe_Helper_Test.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,4 +895,25 @@ public function is_webhook_url_provider() {
895895
'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 ],
896896
];
897897
}
898+
899+
/**
900+
* Tests for `is_stripe_gateway_order`.
901+
*
902+
* @return void
903+
*/
904+
public function test_is_stripe_gateway_order() {
905+
// Test with a Stripe order (Klarna).
906+
$order = WC_Helper_Order::create_order();
907+
$order->set_payment_method( 'stripe_klarna' );
908+
$this->assertTrue( WC_Stripe_Helper::is_stripe_gateway_order( $order ) );
909+
910+
// Test with a non-Stripe order.
911+
$order = WC_Helper_Order::create_order();
912+
$order->set_payment_method( 'cod' );
913+
$this->assertFalse( WC_Stripe_Helper::is_stripe_gateway_order( $order ) );
914+
915+
// Test with an empty order.
916+
$order = new WC_Order();
917+
$this->assertFalse( WC_Stripe_Helper::is_stripe_gateway_order( $order ) );
918+
}
898919
}

0 commit comments

Comments
 (0)