Description
The gform_stripe_webhook filter can be used to perform custom actions when Stripe sends a webhook event selected in your Stripe webhook configuration.
The following event types are processed by the Stripe Add-On by default, starting with version 6.0.
| Event Type |
|---|
| charge.captured |
| charge.expired |
| charge.failed |
| charge.refunded |
| charge.succeeded |
| checkout.session.completed |
| checkout.session.async_payment_succeeded |
| checkout.session.async_payment_failed |
| customer.subscription.deleted |
| customer.subscription.updated |
| invoice.payment_succeeded |
| invoice.payment_failed |
| payment_intent.succeeded |
| payment_intent.payment_failed |
| setup_intent.succeeded |
| setup_intent.setup_failed |
See the Stripe API Reference for a full list of event types for which webhooks are sent.
Note: The Stripe Add-On already processes the events listed above. In most cases, you should use one of the payment hooks (listed below) instead of gform_stripe_webhook when you need to run custom actions for these events. Once the legacy element support is removed, the required event list may be reduced.
Gravity Forms Hooks
- gform_post_payment_completed – Triggered after a payment is completed.
- gform_post_payment_refunded – Triggered after a payment is refunded.
- gform_post_payment_transaction – Triggered after a payment transaction is added to a Gravity Forms entry.
- gform_post_subscription_started – Triggered after a subscription is successfully created.
- gform_subscription_canceled – Triggered after a subscription is canceled.
- gform_post_add_subscription_payment – Triggered after a subscription/recurring payment is completed.
- gform_post_fail_subscription_payment – Triggered after a subscription/recurring payment fails.
- gform_post_payment_action – Generic hook that is triggered after any payment and subscription related action is performed.
Usage
The hook which would run for all Stripe feeds can be used like so:
add_filter( 'gform_stripe_webhook', 'your_function_name', 10, 2 );
Parameters
| Parameter | Type | Description |
|---|---|---|
| $action | array | An associative array containing the event details, or an empty array for unsupported event types. |
| $event | \Stripe\Event | The Stripe event object received from the webhook. |
$action = array(
'type' => '',
'amount' => '',
'transaction_type' => '',
'transaction_id' => '',
'subscription_id' => '',
'entry_id' => '',
'payment_status' => '',
'note' => '',
);
Examples
Add a new event type.
The following shows how you can add support for processing webhooks for other event types than the defaults listed above.
add_filter( 'gform_stripe_webhook', 'stripe_webhook_custom_action', 10, 2 );
function stripe_webhook_custom_action( $action, $event ) {
$type = rgar( $event, 'type' );
switch ( $type ) {
case 'charge.failed':
$action['transaction_id'] = rgars( $event, 'data/object/id' );
$entry_id = gf_stripe()->get_entry_by_transaction_id( $action['transaction_id'] );
if ( ! $entry_id ) {
return new WP_Error( 'entry_not_found', sprintf( 'Entry for transaction id: %s was not found. Webhook cannot be processed.', $action['transaction_id'] ) );
}
$entry = GFAPI::get_entry( $entry_id );
$action['entry_id'] = $entry_id;
$action['type'] = 'fail_payment';
$action['amount'] = gf_stripe()->get_amount_import( rgars( $event, 'data/object/amount' ), $entry['currency'] );
break;
}
return $action;
}
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Since
This filter was added in Stripe v1.0.
Source Code
$action = apply_filters( 'gform_stripe_webhook', $action, $event );
This hook is located in GFStripe::callback() in class-gf-stripe.php.