Skip to content

Add hook to allow for custom webhook post-processing #4466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions includes/class-wc-stripe-webhook-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,17 @@ public function process_webhook( $request_body ) {
$this->process_setup_intent( $notification );

}

/**
* Fires after a webhook has been processed, but before we respond to Stripe.
* This allows for custom processing of the webhook after it has been processed.
*
* @since 9.7.0
*
* @param string $webhook_type The type of webhook that was processed.
* @param object $notification The webhook data sent from Stripe.
*/
do_action( 'wc_stripe_webhook_processed', (string) $notification->type, $notification );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like we should also include an order reference where possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

payment_intent.succeeded and payment_intent.amount_capturable_updated events might be processed async

Without a major refactor, what we can do is exclude those events from this action, and do something similar to what was done for the do_action( 'wc_gateway_stripe_process_payment', $charge, $order ) call, which is called in both cases with the same params (the charge and the order):

do_action( 'wc_gateway_stripe_process_payment', $charge, $order );

do_action( 'wc_gateway_stripe_process_payment', $charge, $order );

Specifically for the request in STRIPE-564, the existing action might give them the info they want (it's called with the latest charge)

Copy link
Contributor

@malithsen malithsen Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to wrap the hook in a try/catch (and log the error on failure) so that it doesn't interrupt rest of the flow if something fails within the hook.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like we should also include an order reference where possible.

I attempted to add the $order to the action, but some events have a different way to retrieve the order information. We would have to either:

  • Move the order retrieval logic for each event type to the process_webhook and pass it as an additional parameter for every method handling an event inside of it
  • Move the action call inside every method handling an event (duplicating it multiple times)

So, unless that's necessary for some reason, it's not worth the refactoring.

Without a major refactor, what we can do is exclude those events from this action, and do something similar to what was done for the do_action( 'wc_gateway_stripe_process_payment', $charge, $order ) call, which is called in both cases with the same params (the charge and the order):

Do you mean something like this, Diego?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to wrap the hook in a try/catch (and log the error on failure) so that it doesn't interrupt rest of the flow if something fails within the hook.

Agreed! Added it in 5866fb2

}

/**
Expand Down
Loading