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.
Usage
add_filter( 'gform_stripe_subscription_params_pre_update_customer', 'your_function_name', 10, 7 );
Parameters
- $subscription_params array
The subscription parameters. Check out Stripe’s documentation to see what parameters subscriptions can accept.
-
$customer object
The Stripe customer object.
-
$plan object
The Stripe plan object.
-
$feed Feed Object
The feed currently being processed.
-
$entry Entry Object
The entry currently being processed.
-
$form Form Object
The form which created the current entry.
-
$trial_period_days int
The 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 should be placed in the functions.php file of your active theme.
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.