Class: Effective::WebhooksController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/effective/webhooks_controller.rb

Instance Method Summary collapse

Instance Method Details

#stripeObject



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
# File 'app/controllers/effective/webhooks_controller.rb', line 18

def stripe
  @event = (Stripe::Webhook.construct_event(request.body.read, request.env['HTTP_STRIPE_SIGNATURE'], EffectiveOrders.subscriptions[:webhook_secret]) rescue nil)
  (head(:ok) and return) if request.get? && @event.blank?
  (head(:bad_request) and return) unless @event

  unless EffectiveOrders.subscriptions[:ignore_livemode]
    (head(:bad_request) and return) if (params[:livemode] == false && Rails.env.production?)
  end

  Rails.logger.info "[STRIPE] webhook received: #{@event.type} for #{customer || 'no customer'}"

  Effective::Customer.transaction do
    case @event.type
    # when 'customer.created'
    # when 'customer.updated'
    # when 'customer.source.created'
    # when 'customer.source.deleted'
    # when 'customer.subscription.created'
    # when 'customer.subscription.updated'
    # when 'invoice.created'
    # when 'invoice.payment_succeeded'
    # when 'invoiceitem.created'
    # when 'invoiceitem.updated'
    # when 'charge.succeeded'
    # when 'charge.failed' # Card declined. 4000 0000 0000 0341

    when 'invoice.payment_succeeded'
      subscriptions.each { |subscription| subscription.update!(status: EffectiveOrders::ACTIVE) }
      send_email(:subscription_payment_succeeded, customer)
    when 'invoice.payment_failed'
      subscriptions.each { |subscription| subscription.update!(status: EffectiveOrders::PAST_DUE) }
      send_email(:subscription_payment_failed, customer)
    when 'customer.subscription.deleted'
      subscriptions.each { |subscription| subscription.destroy! }
      send_email(:subscription_canceled, customer)
    when 'customer.subscription.created'
      send_email(:subscription_created, customer)
    when 'customer.subscription.updated'
      send_email(:subscription_updated, customer)
    else
      Rails.logger.info "[STRIPE] successful event: #{@event.type}. Nothing to do."
    end
  end

  head(:ok)
end