Adjusting the timestamp so Stripe auto-cancels subscriptions 1 year in the future [RESOLVED]

Hi everyone! I’m hoping someone could help me figure this out :slightly_smiling_face:

The premise:

I am using an embedded Stripe CC field (Elements) to create a subscription form. This is a non-recurring subscription, with 1 single payment at the beginning, and the subscription expires 1 year later. When the user hits “submit” on the sign-up form, I need to send an adjusted timestamp over to Stripe, along with the customer data, which says “Hey Stripe, set this subscription up to automatically cancel at the 1 year mark.” I’m thinking I can leverage this filter: https://docs.gravityforms.com/gform_stripe_subscription_params_pre_update_customer/#cancel-at-specified-date-time

Here’s what I’ve done so far, and what I need help with:

I grabbed the timestamp via regular PHP, and added 1 year to it. How do I take this adjusted timestamp and send it off to Stripe with all the other subscription entry info that the user has just submitted, to ensure that Stripe uses that timestamp as the cancelation date? This is where I am so far, but feeling unsure:

$date_one_year_later = gmdate("Y-m-d", strtotime("+1 year"));
add_filter( 'gform_stripe_subscription_params_pre_update_customer', 'stripe_subscription_cancel_at', 10, 7 );
function stripe_subscription_cancel_at( $subscription_params, $customer, $plan, $feed, $entry, $form, $trial_period_days ) {
	// cancel subscription one year after current timestamp
	$subscription_params['cancel_at'] = strtotime( $date_one_year_later );
	return $subscription_params;
}

Another idea that might work, but I’m not sure:
If I set the “Billing Cycle” to 1 year in my Stripe feed, could I use this filter as-is, and wham-bam, problem solved? The subscription would just be canceled 1 year later? https://docs.gravityforms.com/gform_stripe_subscription_cancel_at_period_end/

For anyone interested, I figured out how to pass the cancelation date to Stripe - the key is that Stripe requires the date in unix timestamp.

$date_one_year_later_unix = strtotime( gmdate("Y-m-d", strtotime("+1 year")) );
add_filter( 'gform_stripe_subscription_params_pre_update_customer', 'stripe_subscription_cancel_at', 10, 7 );
function stripe_subscription_cancel_at( $subscription_params, $customer, $plan, $feed, $entry, $form, $trial_period_days ) {
	// cancel subscription one year after current timestamp
	// $subscription_params['cancel_at'] = strtotime( '2020-12-31' );
	$subscription_params['cancel_at'] = $date_one_year_later_unix;
	return $subscription_params;
}
1 Like

Thank you for sharing that. I think the $date_one_year_later_unix variable should be defined inside your function, like this:

add_filter( 'gform_stripe_subscription_params_pre_update_customer', 'stripe_subscription_cancel_at', 10, 7 );
function stripe_subscription_cancel_at( $subscription_params, $customer, $plan, $feed, $entry, $form, $trial_period_days ) {
	// cancel subscription one year after current timestamp
	// $subscription_params['cancel_at'] = strtotime( '2020-12-31' );
	$date_one_year_later_unix = strtotime( gmdate("Y-m-d", strtotime("+1 year")) );
	$subscription_params['cancel_at'] = $date_one_year_later_unix;
	return $subscription_params;
}

But if it’s already working for you, you should be all set. If you need anything else, please let us know.

Hi Chris, thank you so much for your response. You are absolutely right, it should be inside the filter. I decided to use the GF local timestamp instead of gmdate, as well. Here is my final code for anyone interested:

add_filter( 'gform_stripe_subscription_params_pre_update_customer', 'stripe_subscription_cancel_at', 10, 7 );
function stripe_subscription_cancel_at( $subscription_params, $customer, $plan, $feed, $entry, $form, $trial_period_days ) {
	$date_one_year_later_unix = strtotime('+1 year', GFCommon::get_local_timestamp( time() ) );
	$subscription_params['cancel_at'] = $date_one_year_later_unix;
	return $subscription_params;
}
1 Like

Snoop

Good job!

1 Like