gform_stripe_subscription_params_pre_update_customer

Description

The gform_stripe_subscription_params_pre_update_customer filter in the Gravity Forms Stripe Add-On allows the subscription parameters to be overridden before the customer is subscribed to the plan.

For versions of the add-on older than 5.2, this filter is called before the subscription creation; for newer versions where the setting “Use Stripe’s Payment Element” has been enabled in the Stripe field, this filter is called to update the already created subscription.

Usage

add_filter( 'gform_stripe_subscription_params_pre_update_customer', 'your_function_name', 10, 7 );

Parameter

ParameterTypeDescription
$subscription_paramsarrayThe subscription parameters. Check out Stripe’s documentation for subscription creation or update to see what parameters subscriptions can accept.
$customerobjectThe Stripe customer object.
$planobjectThe Stripe plan object.
$feedFeed ObjectThe feed currently being processed.
$entryEntry ObjectThe entry currently being processed.
$formForm ObjectThe form which created the current entry.
$trial_period_daysintThe number of days the trial should last.

Examples

Cancel at period end.

The example below sets the subscription to be cancelled at the end of the current period.

add_filter( 'gform_stripe_subscription_params_pre_update_customer', 'change_details', 10, 7 );
function change_details( $subscription_params, $customer, $plan, $feed, $entry, $form, $trial_period_days ) {
	$subscription_params['cancel_at_period_end'] = true;

	return $subscription_params;
}

Cancel at specified date/time.

This example shows how you can use Stripe’s cancel_at parameter to provide a timestamp at which the subscription should cancel automatically. This is useful if you want to cancel the subscription automatically after a number of recurring payments. The example below sets the expiration time to one month and one day after the subscription creation, which would allow the subscription to be canceled after collecting two payments when the billing cycle is set to one month. Read the snippet comments to know which parts you need to customize.

add_filter( 'gform_stripe_subscription_params_pre_update_customer',  function ( $subscription_params, $customer, $plan, $feed, $entry, $form, $trial_period_days ) {
	gf_stripe()->log_debug( 'Running custom code for cancel_at parameter for form ID: ' . $entry['form_id'] );

	// Get the feed that processed the entry.
	$feed_name = rgars( $feed, 'meta/feedName' );
	gf_stripe()->log_debug( '$feed_name: ' . $feed_name );

	// Define the names of the feeds you want this code to run.
	$feed_names = array( 'My Subscription Feed', 'Another Feed', 'Stripe Feed 36' );
	gf_stripe()->log_debug( '$feed_names: ' . print_r( $feed_names, true ) );

	if ( ! in_array( $feed_name, $feed_names ) ) {
		// Abort if the entry was processed by a different feed.
		return $subscription_params;
	}

	// Get current date.
	$today = date_i18n( 'Y-m-d' );

	// Set date for cancel_at to 1 month and 1 day after the subscription creation.
	$subscription_params['cancel_at'] = strtotime( $today . ' +1 month +1 day' );
	gf_stripe()->log_debug( 'cancel_at set to: ' . $subscription_params['cancel_at'] . ' for feed ' . $feed_name );

	return $subscription_params;
}, 10, 7 );

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 version 2.3.4

Source Code

This filter is located in GFStripe::update_subscription() in gravityformsstripe/class-gf-stripe.php.