gform_stripe_webhook

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.

Note: The Stripe Add-On already processes the events listed above. In most cases, you should use one of the payment hooks (listed below the events) 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.

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.

Gravity Forms Hooks

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

ParameterTypeDescription
$actionarrayAn associative array containing the event details, or an empty array for unsupported event types.
$event\Stripe\EventThe 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.