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
|
# File 'app/controllers/effective/webhooks_controller.rb', line 6
def stripe
@event = (Stripe::Webhook.construct_event(request.body.read, request.env['HTTP_STRIPE_SIGNATURE'], EffectiveOrders.subscription[:webhook_secret]) rescue nil)
(head(:bad_request) and return) if !@event || (params[:livemode] == false && Rails.env.production?)
Rails.logger.info "STRIPE WEBHOOK: #{@event.type}"
Effective::Customer.transaction do
case @event.type
when 'invoice.payment_succeeded'
customer = Effective::Customer.where(stripe_customer_id: @event.data.object.customer).first!
customer.update_attributes!(status: 'active')
send_email(:subscription_payment_succeeded, customer)
when 'invoice.payment_failed'
customer = Effective::Customer.where(stripe_customer_id: @event.data.object.customer).first!
customer.update_attributes!(status: 'past_due')
send_email(:subscription_payment_failed, customer)
when 'customer.subscription.deleted'
customer = Effective::Customer.where(stripe_customer_id: @event.data.object.customer).first!
Effective::Subscription.where(customer: customer).destroy_all
customer.update_attributes!(stripe_subscription_id: nil, status: nil, active_card: nil)
send_email(:subscription_canceled, customer)
else
Rails.logger.info "[STRIPE WEBHOOK] Unhandled event type #{@event.type}"
end
end
head(:ok)
end
|