简体   繁体   中英

How to cancel a customers stripe subscription at the end of period?

The functionality I want is if a customer with a subscription cancels their subscription I want to let them use my service until the end of the current period that they have paid for.

My plan to implement this is to, when they cancel, update their subscription to be cancelling at the end of the current period. I am currently trying to implement code in python that will update the customers subscriptions cancel_at_period_end property but I can't seem to get it to work. This is what I have:

stripe_customer_obj = stripe.Customer.retrieve(current_user.stripe_customer_id)
stripe_customer_subscription = stripe_customer_obj.subscriptions['data'][0]

stripe.Subscription.modify(
        stripe_customer_subscription['id'],
        metadata={"cancel_at_period_end": True},
    )

This runs fine when I print out the cancel_at_period_end property via the following command:

print(stripe_customer_subscription['cancel_at_period_end'])

it is False before and after the code where I modify the subscription. It's like the line where I modify the subscription isn't actually updating it or saving even though it is running and not crashing.

If it is relevant (i do not think it is)- i am trying to update the cancel_at_period_end property to True so that when the subscription does get cancelled at the end of the period a webhook will fire off that I can use to update the customers entry in my DB so that their access is cut off from the application.

The issue with your code is that you've included the cancel_at_period_end in the metadata of the Subscription object. That's just a simple key -> value store for storing arbitrary data.

You just need to pass the cancel_at_period_end as a normal param. Like this:

stripe.Subscription.modify(
  "sub_xxx",
  cancel_at_period_end=True  
)

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM