andrewculver / koudoku

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

Don't explode if a subscription doesn't exist when being hit from Stripe webhook #177

Open jayelkaake opened 7 years ago

jayelkaake commented 7 years ago

The Problem

If you use your Stripe account for other things other than the stuff you are linking to Koudoku then Stripe still hits the Koudoku webhooks with events from the other stuff. The result is that the stripe_id that hits the Koudoku registered webhooks throws a 500 error to Stripe.

Now that would be fine, and maybe even a good thing, HOWEVER Stripe will keep retrying to hit the webhook until it gets a 200 response. Eventually Stripe will threaten to even kill your webhook.

The Solution

Instead of letting it throw errors like NoMethodError: undefined methodpayment_succeeded' for nil:NilClass, just log a warning if the subscription doesn't exist for thestripe_id` that was posted.

Also...

Using dynamic find_by's likefind_by_blehis deprecated in Rails 4 and you should usefind_by(stripe_id: strip_id)` instead, so I updated these lines. See http://edgeguides.rubyonrails.org/4_0_release_notes.html#active-record-deprecations for reference.

Great gem BTW! Very useful!!

donnfelker commented 6 years ago

Good stuff. I agree, this should be in the gem. I recently ran into this as well after manually migrating from a Wordpress Plugin to a custom rails site that uses Koudoku. Kudos for this.