-
Notifications
You must be signed in to change notification settings - Fork 6
/
stripe_webhooks_controller.rb
64 lines (54 loc) · 1.71 KB
/
stripe_webhooks_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
class StripeWebhooksController < ApplicationController
skip_forgery_protection
def account
begin
event = construct_event(ENV['STRIPE_ACCOUNT_ENDPOINT_SECRET'])
rescue JSON::ParserError => e
Rollbar.warn(e)
head :not_acceptable
return
end
# case event.type
# This is where handlers would go if we had any yet
# else
Rollbar.warn("Unhandled event type for account webhook listener: #{event.type}")
# end
head :ok
end
def connect
begin
event = construct_event(ENV['STRIPE_CONNECT_ENDPOINT_SECRET'])
rescue JSON::ParserError => e
Rollbar.warn(e)
head :not_acceptable
return
end
handle_event(event)
head :ok
end
private
def construct_event(endpoint_secret)
payload = request.body.read
sig_header = request.headers['Stripe-Signature']
Stripe::Webhook.construct_event(payload, sig_header, endpoint_secret)
end
def handle_event(event)
case event.type
when 'account.application.deauthorized'
account = event.data.object
conventions = Convention.where(stripe_account_id: account.id)
conventions.update_all(stripe_account_id: nil, stripe_account_ready_to_charge: false)
when 'account.application.authorized', 'account.updated'
account = event.data.object
conventions = Convention.where(stripe_account_id: account.id)
conventions.each do |convention|
if convention && !convention.stripe_account_ready_to_charge
ConnectStripeAccountService.new(convention: convention, account: account).call!
end
end
else
Rollbar.warn("Unhandled event type for Connect webhook listener: #{event.type}")
end
end
end