From 3f2e1a1d93495fd389acbdac5b4e03d04d397ec4 Mon Sep 17 00:00:00 2001 From: Dale du Preez Date: Fri, 1 Aug 2025 15:14:29 +0200 Subject: [PATCH 1/2] First pass vibe-coded refactor --- includes/class-wc-stripe-webhook-handler.php | 174 +++++++++++++----- .../WC_Stripe_Webhook_Handler_Test.php | 1 + 2 files changed, 124 insertions(+), 51 deletions(-) diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index f2f8befd6f..101eee23c3 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -262,7 +262,7 @@ public function process_webhook_payment( $notification, $retry = true ) { return; } - $order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id ); + $order = $this->get_order_from_notification( $notification ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via source ID: ' . $notification->data->object->id ); @@ -395,7 +395,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 = $this->get_order_from_notification( $notification ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge ); @@ -436,7 +436,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 = $this->get_order_from_notification( $notification ); $status = $notification->data->object->status; if ( ! $order ) { @@ -488,7 +488,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 = $this->get_order_from_notification( $notification ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); @@ -560,7 +560,7 @@ public function process_webhook_charge_succeeded( $notification ) { return; } - $order = WC_Stripe_Helper::get_order_by_charge_id( $charge->id ); + $order = $this->get_order_from_notification( $notification ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $charge->id ); @@ -618,7 +618,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 = $this->get_order_from_notification( $notification ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); @@ -653,16 +653,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 = $this->get_order_from_notification( $notification ); - // 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 ); - - if ( ! $order ) { - WC_Stripe_Logger::log( 'Could not find order via charge/source ID: ' . $notification->data->object->id ); - return; - } + WC_Stripe_Logger::log( 'Could not find order via charge/source ID: ' . $notification->data->object->id ); + return; } // Don't proceed if payment method isn't Stripe. @@ -689,19 +684,15 @@ public function process_webhook_source_canceled( $notification ) { * @param object $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 = $this->get_order_from_notification( $notification ); 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 ); - } - - if ( ! $order ) { - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); + WC_Stripe_Logger::log( 'Could not find order via refund ID: ' . $notification->data->object->id ); return; } + $refund_object = $this->get_refund_object( $notification ); + $order_id = $order->get_id(); if ( 'stripe' === substr( (string) $order->get_payment_method(), 0, 6 ) ) { @@ -777,8 +768,9 @@ public function process_webhook_refund( $notification ) { * @param object $notification */ public function process_webhook_refund_updated( $notification ) { + // Note that we avoid calling `get_refund_object()` as that code makes a call to Stripe. $refund_object = $notification->data->object; - $order = WC_Stripe_Helper::get_order_by_charge_id( $refund_object->charge ); + $order = $this->get_order_from_notification( $notification ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order to update refund via charge ID: ' . $refund_object->charge ); @@ -872,20 +864,13 @@ public function process_webhook_refund_updated( $notification ) { * @param object $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 ); - - 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 = $this->get_order_from_notification( $notification ); - if ( ! $order ) { - WC_Stripe_Logger::log( '[Review Opened] Could not find order via charge ID: ' . $notification->data->object->charge ); - return; - } + if ( ! $order ) { + $reference_type = isset( $notification->data->object->payment_intent ) ? 'payment_intent' : 'charge'; + $reference = 'payment_intent' === $reference_type ? $notification->data->object->payment_intent : $notification->data->object->charge ?? 'unknown'; + WC_Stripe_Logger::log( '[Review Opened] Could not find order via ' . $reference_type . ': ' . $reference ); + return; } $this->set_stripe_order_status_before_hold( $order, $order->get_status() ); @@ -913,20 +898,13 @@ public function process_review_opened( $notification ) { * @param object $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 ); - - 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 = $this->get_order_from_notification( $notification ); - if ( ! $order ) { - WC_Stripe_Logger::log( '[Review Closed] Could not find order via charge ID: ' . $notification->data->object->charge ); - return; - } + if ( ! $order ) { + $reference_type = isset( $notification->data->object->payment_intent ) ? 'payment_intent' : 'charge'; + $reference = 'payment_intent' === $reference_type ? $notification->data->object->payment_intent : $notification->data->object->charge ?? 'unknown'; + WC_Stripe_Logger::log( '[Review Closed] Could not find order via ' . $reference_type . ': ' . $reference ); + return; } /* translators: 1) The reason type. */ @@ -1030,7 +1008,7 @@ public function get_partial_amount_to_charge( $notification ) { */ public function process_payment_intent( $notification ) { $intent = $notification->data->object; - $order = $this->get_order_from_intent( $intent ); + $order = $this->get_order_from_notification( $notification ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via intent ID: ' . $intent->id ); @@ -1146,7 +1124,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 = $this->get_order_from_notification( $notification ); if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via setup intent ID: ' . $intent->id ); @@ -1334,6 +1312,8 @@ public function process_account_updated( $notification ) { public function process_webhook( $request_body ) { $notification = json_decode( $request_body ); + $order = $this->get_order_from_notification( $notification ); + switch ( $notification->type ) { case 'account.updated': $this->process_account_updated( $notification ); @@ -1399,6 +1379,98 @@ public function process_webhook( $request_body ) { } } + protected function get_order_from_notification( $notification ) { + $object = $notification->data->object ?? new stdClass(); + $order = false; + + // Handle different notification types based on their structure + switch ( $notification->type ) { + case 'source.chargeable': + // Source-based notifications + $order = WC_Stripe_Helper::get_order_by_source_id( $object->id ); + break; + + case 'source.canceled': + // Try charge ID first, then source ID as fallback + $order = WC_Stripe_Helper::get_order_by_charge_id( $object->id ); + if ( ! $order ) { + $order = WC_Stripe_Helper::get_order_by_source_id( $object->id ); + } + break; + + case 'charge.succeeded': + case 'charge.failed': + case 'charge.expired': + case 'charge.captured': + // Charge-based notifications + $order = WC_Stripe_Helper::get_order_by_charge_id( $object->id ); + break; + + case 'charge.dispute.created': + case 'charge.dispute.closed': + // Dispute notifications - use charge ID from dispute object + $order = WC_Stripe_Helper::get_order_by_charge_id( $object->charge ); + break; + + case 'charge.refunded': + // Refund notifications - try refund ID first, then charge ID + $refund_object = $this->get_refund_object( $notification ); + $order = WC_Stripe_Helper::get_order_by_refund_id( $refund_object->id ); + if ( ! $order ) { + $order = WC_Stripe_Helper::get_order_by_charge_id( $object->id ); + } + break; + + case 'charge.refund.updated': + // Refund update notifications - use charge ID from refund object + $order = WC_Stripe_Helper::get_order_by_charge_id( $object->charge ); + break; + + case 'review.opened': + case 'review.closed': + // Review notifications - try payment intent first, then charge + if ( isset( $object->payment_intent ) ) { + $order = WC_Stripe_Helper::get_order_by_intent_id( $object->payment_intent ); + } else { + $order = WC_Stripe_Helper::get_order_by_charge_id( $object->charge ); + } + break; + + case 'payment_intent.processing': + case 'payment_intent.succeeded': + case 'payment_intent.payment_failed': + case 'payment_intent.amount_capturable_updated': + case 'payment_intent.requires_action': + // Payment intent notifications - rely on get_order_from_intent() + $order = $this->get_order_from_intent( $object ); + break; + + case 'setup_intent.succeeded': + case 'setup_intent.setup_failed': + // Setup intent notifications + $order = WC_Stripe_Helper::get_order_by_setup_intent_id( $object->id ); + break; + + default: + // For unknown notification types, try common patterns + if ( isset( $object->payment_intent ) ) { + $order = WC_Stripe_Helper::get_order_by_intent_id( $object->payment_intent ); + } elseif ( isset( $object->charge ) ) { + $order = WC_Stripe_Helper::get_order_by_charge_id( $object->charge ); + } elseif ( isset( $object->id ) ) { + // Try as charge ID first + $order = WC_Stripe_Helper::get_order_by_charge_id( $object->id ); + if ( ! $order ) { + // Try as source ID + $order = WC_Stripe_Helper::get_order_by_source_id( $object->id ); + } + } + break; + } + + return $order; + } + /** * Fetches an order from a payment intent. * diff --git a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php index f5a04cef77..15d340469b 100644 --- a/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php +++ b/tests/phpunit/WC_Stripe_Webhook_Handler_Test.php @@ -741,6 +741,7 @@ public function test_process_webhook_refund_updated( $notification_status, $emai $refund_order->save(); $notification = (object) [ + 'type' => 'charge.refund.updated', 'data' => (object) [ 'object' => (object) [ 'id' => $refund_id, From f1c5dc0e81e9b4872a7bad8ceb848aa2cd98ce96 Mon Sep 17 00:00:00 2001 From: Dale du Preez Date: Fri, 1 Aug 2025 15:57:12 +0200 Subject: [PATCH 2/2] Try to compute the order data once --- includes/class-wc-stripe-webhook-handler.php | 158 ++++++++++++------- 1 file changed, 104 insertions(+), 54 deletions(-) diff --git a/includes/class-wc-stripe-webhook-handler.php b/includes/class-wc-stripe-webhook-handler.php index 101eee23c3..08102d2c4b 100644 --- a/includes/class-wc-stripe-webhook-handler.php +++ b/includes/class-wc-stripe-webhook-handler.php @@ -253,16 +253,19 @@ public function get_request_headers() { * * @since 4.0.0 * @version 4.0.0 - * @param object $notification - * @param bool $retry + * @param object $notification The notification data from Stripe. + * @param bool $retry Whether to retry the payment. + * @param WC_Order|false|null $order The related order. */ - public function process_webhook_payment( $notification, $retry = true ) { + public function process_webhook_payment( $notification, $retry = true, $order = null ) { // The following 3 payment methods are synchronous so does not need to be handle via webhook. if ( WC_Stripe_Payment_Methods::CARD === $notification->data->object->type || WC_Stripe_Payment_Methods::SEPA_DEBIT === $notification->data->object->type || 'three_d_secure' === $notification->data->object->type ) { return; } - $order = $this->get_order_from_notification( $notification ); + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via source ID: ' . $notification->data->object->id ); @@ -328,13 +331,13 @@ public function process_webhook_payment( $notification, $retry = true ) { // Don't do anymore retries after this. if ( 5 <= $this->retry_interval ) { - return $this->process_webhook_payment( $notification, false ); + return $this->process_webhook_payment( $notification, false, $order ); } sleep( $this->retry_interval ); $this->retry_interval++; - return $this->process_webhook_payment( $notification, true ); + return $this->process_webhook_payment( $notification, true, $order ); } else { $localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' ); $order->add_order_note( $localized_message ); @@ -392,10 +395,13 @@ public function process_webhook_payment( $notification, $retry = true ) { * We want to put the order into on-hold and add an order note. * * @since 4.0.0 - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order|false|null $order The related order. */ - public function process_webhook_dispute( $notification ) { - $order = $this->get_order_from_notification( $notification ); + public function process_webhook_dispute( $notification, $order = null ) { + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge ); @@ -433,10 +439,13 @@ public function process_webhook_dispute( $notification ) { * Process webhook dispute that is closed. * * @since 4.4.1 - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order|false|null $order The related order. */ - public function process_webhook_dispute_closed( $notification ) { - $order = $this->get_order_from_notification( $notification ); + public function process_webhook_dispute_closed( $notification, $order = null ) { + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } $status = $notification->data->object->status; if ( ! $order ) { @@ -485,10 +494,13 @@ public function process_webhook_dispute_closed( $notification ) { * * @since 4.0.0 * @version 4.0.0 - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order|false|null $order The related order. */ - public function process_webhook_capture( $notification ) { - $order = $this->get_order_from_notification( $notification ); + public function process_webhook_capture( $notification, $order = null ) { + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); @@ -537,9 +549,10 @@ public function process_webhook_capture( $notification ) { * * @since 4.0.0 * @version 4.0.0 - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order|false|null $order The related order. */ - public function process_webhook_charge_succeeded( $notification ) { + public function process_webhook_charge_succeeded( $notification, $order = null ) { if ( empty( $notification->data->object ) ) { WC_Stripe_Logger::log( 'Missing charge object in charge.succeeded webhook, Event ID: %s', $notification->id ?? 'unknown' ); return; @@ -560,7 +573,9 @@ public function process_webhook_charge_succeeded( $notification ) { return; } - $order = $this->get_order_from_notification( $notification ); + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $charge->id ); @@ -615,10 +630,13 @@ public function process_webhook_charge_succeeded( $notification ) { * @since 4.0.0 * @since 4.1.5 Can handle any fail payments from any methods. * @since 9.0.0 Can handle payment expiration. - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order|false|null $order The related order. */ - public function process_webhook_charge_failed( $notification ) { - $order = $this->get_order_from_notification( $notification ); + public function process_webhook_charge_failed( $notification, $order = null ) { + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); @@ -650,10 +668,13 @@ public function process_webhook_charge_failed( $notification ) { * * @since 4.0.0 * @since 4.1.15 Add check to make sure order is processed by Stripe. - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order|false|null $order The related order. */ - public function process_webhook_source_canceled( $notification ) { - $order = $this->get_order_from_notification( $notification ); + public function process_webhook_source_canceled( $notification, $order = null ) { + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via charge/source ID: ' . $notification->data->object->id ); @@ -681,10 +702,13 @@ public function process_webhook_source_canceled( $notification ) { * * @since 4.0.0 * @version 4.9.0 - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order|false|null $order The related order. */ - public function process_webhook_refund( $notification ) { - $order = $this->get_order_from_notification( $notification ); + public function process_webhook_refund( $notification, $order = null ) { + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via refund ID: ' . $notification->data->object->id ); @@ -765,12 +789,15 @@ public function process_webhook_refund( $notification ) { /** * Process a refund update. * - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order_Refund|false|null $refund The refund object. */ - public function process_webhook_refund_updated( $notification ) { + public function process_webhook_refund_updated( $notification, $order = null ) { // Note that we avoid calling `get_refund_object()` as that code makes a call to Stripe. $refund_object = $notification->data->object; - $order = $this->get_order_from_notification( $notification ); + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order to update refund via charge ID: ' . $refund_object->charge ); @@ -861,10 +888,13 @@ public function process_webhook_refund_updated( $notification ) { * Process webhook reviews that are opened. i.e Radar. * * @since 4.0.6 - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order|false|null $refund The related order. */ - public function process_review_opened( $notification ) { - $order = $this->get_order_from_notification( $notification ); + public function process_review_opened( $notification, $order = null ) { + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { $reference_type = isset( $notification->data->object->payment_intent ) ? 'payment_intent' : 'charge'; @@ -895,10 +925,13 @@ public function process_review_opened( $notification ) { * Process webhook reviews that are closed. i.e Radar. * * @since 4.0.6 - * @param object $notification + * @param object $notification The notification data from Stripe. + * @param WC_Order|false|null $order The related order. */ - public function process_review_closed( $notification ) { - $order = $this->get_order_from_notification( $notification ); + public function process_review_closed( $notification, $order = null ) { + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { $reference_type = isset( $notification->data->object->payment_intent ) ? 'payment_intent' : 'charge'; @@ -1004,11 +1037,14 @@ public function get_partial_amount_to_charge( $notification ) { /** * Handles the processing of a payment intent webhook. * - * @param stdClass $notification The webhook notification from Stripe. + * @param object $notification The webhook notification from Stripe. + * @param WC_Order|false|null $order The related order. */ - public function process_payment_intent( $notification ) { + public function process_payment_intent( $notification, $order = null ) { $intent = $notification->data->object; - $order = $this->get_order_from_notification( $notification ); + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via intent ID: ' . $intent->id ); @@ -1122,9 +1158,17 @@ public function process_payment_intent( $notification ) { $this->unlock_order_payment( $order ); } - public function process_setup_intent( $notification ) { + /** + * Handles the processing of a setup intent webhook. + * + * @param object $notification The webhook notification from Stripe. + * @param WC_Order|false|null $order The related order. + */ + public function process_setup_intent( $notification, $order = null ) { $intent = $notification->data->object; - $order = $this->get_order_from_notification( $notification ); + if ( null === $order ) { + $order = $this->get_order_from_notification( $notification ); + } if ( ! $order ) { WC_Stripe_Logger::log( 'Could not find order via setup intent ID: ' . $intent->id ); @@ -1320,48 +1364,48 @@ public function process_webhook( $request_body ) { break; case 'source.chargeable': - $this->process_webhook_payment( $notification ); + $this->process_webhook_payment( $notification, true, $order ); break; case 'source.canceled': - $this->process_webhook_source_canceled( $notification ); + $this->process_webhook_source_canceled( $notification, $order ); break; case 'charge.succeeded': - $this->process_webhook_charge_succeeded( $notification ); + $this->process_webhook_charge_succeeded( $notification, $order ); break; case 'charge.failed': case 'charge.expired': - $this->process_webhook_charge_failed( $notification ); + $this->process_webhook_charge_failed( $notification, $order ); break; case 'charge.captured': - $this->process_webhook_capture( $notification ); + $this->process_webhook_capture( $notification, $order ); break; case 'charge.dispute.created': - $this->process_webhook_dispute( $notification ); + $this->process_webhook_dispute( $notification, $order ); break; case 'charge.dispute.closed': - $this->process_webhook_dispute_closed( $notification ); + $this->process_webhook_dispute_closed( $notification, $order ); break; case 'charge.refunded': - $this->process_webhook_refund( $notification ); + $this->process_webhook_refund( $notification, $order ); break; case 'charge.refund.updated': - $this->process_webhook_refund_updated( $notification ); + $this->process_webhook_refund_updated( $notification, $order ); break; case 'review.opened': - $this->process_review_opened( $notification ); + $this->process_review_opened( $notification, $order ); break; case 'review.closed': - $this->process_review_closed( $notification ); + $this->process_review_closed( $notification, $order ); break; case 'payment_intent.processing': @@ -1369,16 +1413,22 @@ public function process_webhook( $request_body ) { case 'payment_intent.payment_failed': case 'payment_intent.amount_capturable_updated': case 'payment_intent.requires_action': - $this->process_payment_intent( $notification ); + $this->process_payment_intent( $notification, $order ); break; case 'setup_intent.succeeded': case 'setup_intent.setup_failed': - $this->process_setup_intent( $notification ); + $this->process_setup_intent( $notification, $order ); } } + /** + * Helper method to centralize the logic for getting the order from a webhook notification. + * + * @param object $notification The webhook notification from Stripe. + * @return WC_Order|WC_Order_Refund|false The order or refund object, or false if not found. + */ protected function get_order_from_notification( $notification ) { $object = $notification->data->object ?? new stdClass(); $order = false;