andrewculver / koudoku

Robust subscription support for Rails with Stripe.
MIT License
1.16k stars 187 forks source link

Issue canceling subscription on stripe's website - leaves plan_id in db to make the subscription appear active. #178

Open gwalshington opened 7 years ago

gwalshington commented 7 years ago

The issue:

When subscriptions are cancelled on Stripe's dashboard instead of in the rails app, Subscription in the database still has a plan_id present, which makes

Subscription.exists?(:user_id => current_user.id)
true

This makes it appear on the views as if the subscription is still active. So when the user clicks cancel, there is an error. Then, I tried setting plan_id to nil manually in the console, and got the same error:

irb(main):021:0>s = Subscription.find(1)
irb(main):022:0> s.plan_id = nil
=> nil
irb(main):023:0> s.save
   (6.5ms)  BEGIN
   (5.2ms)  ROLLBACK
Stripe::InvalidRequestError: (Status 404) (Request req_AtnzH4egKHi6Xt) No active subscriptions for customer: cus_9zB6PvzSrfqfWb
yas4891 commented 7 years ago

Hi @gwalshington

can you please point a webhook to /koudoku/webhooks on your app?

Koudoku should then handle this via this code: https://github.com/andrewculver/koudoku/blob/master/config/initializers/stripe_event.rb#L22

If not, please add a #cancel method to your subscription_owner class

amloelxer commented 7 years ago

@yas4891 any idea of what that webhook should look like? I have a similar problem and in my routes.rb file I have this, and it obviously isn't working. Any help is greatly appreciated! post '/koudoku/webhooks' => "controller#my_method"

yas4891 commented 7 years ago

@amloelxer You don't need to add a controller to handle this. You just point the webhook in to /koudoku/webhooks via the Stripe Dashboard.

Then you follow the instructions here: https://github.com/andrewculver/koudoku#webhooks

If you need further help, let me know!

amloelxer commented 7 years ago

@yas4891 ahh that makes a lot more sense. I added the webhook of https://www.mywebsite.com/koudoku/webhooks and got a success on the stripe side, but still have the active subscription in my database. Is there anyway to log that Koudoku has indeed received the hook?

yas4891 commented 7 years ago

You should see that from the logs of your webserver. On Heroku you can install Papertrail

Cheers, Christoph

On Tue, Jul 25, 2017 at 11:48 PM, Alexander Moller <notifications@github.com

wrote:

@yas4891 https://github.com/yas4891 ahh that makes a lot more sense. I added the webhook of 'https://www.mywebsite.com/koudoku/webhooks` and got a success on the stripe side, but still have the active subscription in my database. Is there anyway to log that Koudoku has indeed received the hook?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/andrewculver/koudoku/issues/178#issuecomment-317843697, or mute the thread https://github.com/notifications/unsubscribe-auth/ABOUaD4hwZtHyt55kPG-LVPKffg6u_zpks5sRj-jgaJpZM4OEHUS .

amloelxer commented 7 years ago

@yas4891 thanks so much for your help! Sorry for all the novice questions. I'm new to web dev

amloelxer commented 7 years ago

@yas4891 one more question. When I respond to the webhook that cancels the payment (which I canceled from the stripe dashboard), I try to set my local subscription plan_id to nil and save it. Unfortunately when I do this, it tries to send the update remotely as well and obviously errors out because there is no active subscription on the stripe servers/dashboard once I cancel it. Is there way to do an update to my local db of subscriptions and not affect the ones in stripe?

yas4891 commented 7 years ago

Use update_attribute :plan_id, nil instead

Cheers, Christoph

On Wed, Jul 26, 2017 at 11:09 PM, Alexander Moller <notifications@github.com

wrote:

@yas4891 https://github.com/yas4891 one more question. When I respond to the webhook that cancels the payment (which I canceled from the stripe dashboard), I try to set my local subscription plan_id to nil and save it. Unfortunately when I do this, it tries to send the update remotely as well and obviously errors out because there is no active subscription on the stripe servers/dashboard once I cancel it. Is there way to do an update to my local db of subscriptions and not affect the ones in stripe?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/andrewculver/koudoku/issues/178#issuecomment-318183405, or mute the thread https://github.com/notifications/unsubscribe-auth/ABOUaAKvtpaR4nVJiPtGDXYOZr_WFtdIks5sR6rugaJpZM4OEHUS .