try to fix stripe webhook

This commit is contained in:
Kyle Drake 2015-05-11 10:02:50 -07:00
parent 496385438a
commit f687b7becd
2 changed files with 24 additions and 14 deletions

View file

@ -1,3 +1,23 @@
def stripe_get_site_from_event(event)
customer_id = event['data']['object']['customer']
customer = Stripe::Customer.retrieve customer_id
# Some old accounts only have a username for the desc
desc_split = customer.description.split(' - ')
if desc_split.length == 1
site_where = {username: desc_split.first}
end
if desc_split.last.to_i == 0
site_where = {username: desc_split.first}
else
site_where = {id: desc_split.last}
end
site.where(site_where).first
end
post '/stripe_webhook' do
event = JSON.parse request.body.read
if event['type'] == 'customer.created'
@ -14,16 +34,7 @@ post '/stripe_webhook' do
end
if event['type'] == 'charge.failed'
EmailWorker.perform_async({
from: 'web@neocities.org',
to: 'kyle@neocities.org',
subject: "[Neocities] charge.failed debug",
body: event.inspect
})
site_id = event['data']['object']['description'].split(' - ').last
site = Site[site_id]
site = stripe_get_site_from_event event
EmailWorker.perform_async({
from: 'web@neocities.org',
@ -34,8 +45,7 @@ post '/stripe_webhook' do
end
if event['type'] == 'customer.subscription.deleted'
site_id = event['data']['object']['description'].split(' - ').last
site = Site[site_id]
site = stripe_get_site_from_event event
site.stripe_subscription_id = nil
site.plan_type = nil
site.save_changes validate: false