Ask A Question

Notifications

You’re not receiving notifications from this thread.

Stripe - Not cancelling access until the end of subscription period

Tony asked in General

Chris great job with the Stripe videos. I've just been through and implemented them. :)

The next bit I need to add is to stop my users access from disappearing the minute that they cancel. If they cancel the plan - I'd like for them to retain access to the subscription features until the subscription period has finished and then their status to change back to a regular user.

Any tips on how best to implement this?

Reply

Just noticed to cancel at the end of the subscription period you just needed to add (:at_period_end => true) to the delete command.

So if I updated my destroy method to look like this:

customer = Stripe::Customer.retrieve(current_user.stripe_id)
customer.subscriptions.retrieve(current_user.stripe_subscription_id).delete(:at_period_end => true)
current_user.update(stripe_subscription_id: nil, subscription_end_date: Time.at(subscription.current_period_end))

Where I've created a subscription_end_date and then just display to the customer that the subscription will be cancelled on this date. This should work ok right?

Reply

Yo! Sorry I got behind on answering forum questions over the holiday weekend.

So I actually just do almost exactly that. The one thing is you probably won't want to actually remove the stripe_subscription_id just yet because they still have a subscription until the end of the period. It's good to keep it on the user until it's actually removed.

You can have a webhook that listens to the actual cancel subscription event in Stripe which will happen at that end_date. That webhook response can actually remove the stripe_subscription_id from the user effectively canceling their account. You can also remove the subscription_end_date so that doesn't display in their account section as well.

The Stripe Webhooks episode can help on setting up the webhook listener for this event. https://gorails.com/episodes/stripe-webhooks

Reply

Thanks for your help.

So I've tried to implement what you said but I think there must be something wrong with my stripe.rb file as it doesn't seem to be updating my users information when the customer.subscription.deleted webhook is being sent.

Does this look correct for the event? As neither the subscription_end_date or stripe_subscription_id are being updated to nil on the users profile.

events.subscribe "customer.subscription.deleted" do |event|
if @user = User.find_by_stripe_id(event.data.object.customer)
  @user.update_attribute(subscription_end_date: nil, stripe_subscription_id: nil)
  @user.save!
end

end

Am I finding the user correctly?

Reply

Yes that does look correct. Remember that this only fires at real period end, not when they cancel.

Are you sending over some test events for it that aren't finding the user?

Reply

Yes. I'm setting it to cancel at the period end and then manually visiting the customer on stripe and deleting the subscription so that I don't have to wait for the period to elapse. It is still generating a subscription.deleted webhook so I'd assume that wouldn't have an effect on it, would it?

Edit: After fiddling about with a few things it seems to be working now. Not quite sure what change did it though! Must have been a small typo or something along the way.

Next to get the hang of coupons and free trials.

Thanks once again Chris. I really appreciate you taking the time to talk to the community.

Reply

Glad you got it working! I wondered if that might be the case. It looked like everything would work from here. :)

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,400+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.