Search code examples
stripe-payments

Cancel a stripe subscription after at a specific date


I integrated stripe using php and I now want to set a cancel_at date for every created subscription. This cancellation date depends on the plan that is selected. I followed the steps described in this tutorial https://phppot.com/php/manage-recurring-payments-using-stripe-billing-in-php/ , but here they don't choose a cancel_at date upon creating the subscription. So how can I do that ? I prefer to set this expiry date after the subscription is created so that I keep the exact same structure as described in the tutorial. But I wonder where can I do that ? and how ? should I set the date in one of the webhooks handling sections for example in customer.subscription.created or on the success.php page ?

and the code would we like this ? :

$stripe->subscriptions->update(
 'sub_xxxxx',
 ['cancel_at' =>'10/10/2021']
);

Solution

  • You can supply the cancel_at field when creating a Subscription. It doesn't make sense to update the Subscription with the field after you create it because that would result in unnecessary API calls.

    https://stripe.com/docs/api/subscriptions/create#create_subscription-cancel_at

    But if you wish to, you can. Yes, you can also listen to customer.susbcription.created webhook event to update the Subscription's parameters.

    https://stripe.com/docs/api/events/types#event_types-customer.subscription.created