From 89766ab37e0b51af22497d8f14825a8b176528da Mon Sep 17 00:00:00 2001 From: James Allan Date: Mon, 25 Oct 2021 16:43:30 +1000 Subject: [PATCH] Don't process payment intent success webhooks for WCPay subscriptions related intents (#3199) * Dont process payment intent success webhooks for wcpay subscription related intents * Get the invoiceID from the event['data']['object'] * Dont assume the invoice param will always be set in the request Co-authored-by: mattallan --- .../class-wc-rest-payments-webhook-controller.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/includes/admin/class-wc-rest-payments-webhook-controller.php b/includes/admin/class-wc-rest-payments-webhook-controller.php index 790c98246dc..48682e3c8f7 100644 --- a/includes/admin/class-wc-rest-payments-webhook-controller.php +++ b/includes/admin/class-wc-rest-payments-webhook-controller.php @@ -257,8 +257,7 @@ private function process_webhook_expired_authorization( $event_body ) { private function process_webhook_payment_intent_succeeded( $event_body ) { $event_data = $this->read_rest_property( $event_body, 'data' ); $event_object = $this->read_rest_property( $event_data, 'object' ); - - $intent_id = $this->read_rest_property( $event_object, 'id' ); + $intent_id = $this->read_rest_property( $event_object, 'id' ); // Look up the order related to this charge. $order = $this->wcpay_db->order_from_intent_id( $intent_id ); @@ -267,8 +266,14 @@ private function process_webhook_payment_intent_succeeded( $event_body ) { // Retrieving order with order_id in case intent_id was not properly set. Logger::debug( 'intent_id not found, using order_id to retrieve order' ); $metadata = $this->read_rest_property( $event_object, 'metadata' ); - $order_id = $metadata['order_id']; - $order = $this->wcpay_db->order_from_order_id( $order_id ); + + if ( isset( $metadata['order_id'] ) ) { + $order_id = $metadata['order_id']; + $order = $this->wcpay_db->order_from_order_id( $order_id ); + } elseif ( ! empty( $event_object['invoice'] ) ) { + // If the payment intent contains an invoice it is a WCPay Subscription-related intent and will be handled by the `invoice.paid` event. + return; + } } if ( ! $order ) {