bookmark_bordergform_stripe_webhook

Description

The gform_stripe_webhook filter can be used to add support for performing actions on additional Stripe events. The following event types are supported by default:

  • charge.refunded
  • customer.subscription.deleted
  • invoice.payment_succeeded
  • invoice.payment_failed

See the Stripe API Reference for a full list of event types for which webhooks are sent.

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

  • $action array

    An associative array containing the event details or an empty array for unsupported event types.

        $action = array(
            'type'             => '',
            'amount'           => '',
            'transaction_type' => '',
            'transaction_id'   => '',
            'subscription_id'  => '',
            'entry_id'         => '',
            'payment_status'   => '',
            'note'             => '',
        );
    

  • $event \Stripe\Event

    The Stripe event object for the webhook which was received.

Examples

1. 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

Your code snippet should be placed in the functions.php file of your active theme.

Source Code

$action = apply_filters( 'gform_stripe_webhook', $action, $event );

This hook is located in GFStripe::callback() in class-gf-stripe.php