Class: StripeController

Inherits:
ApplicationController show all
Defined in:
app/controllers/stripe_controller.rb

Instance Method Summary collapse

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details

#checkout_sessionObject



7
8
9
10
11
12
13
14
# File 'app/controllers/stripe_controller.rb', line 7

def checkout_session
  session_id = params[:session_id]
  session = Stripe::Checkout::Session.retrieve({
                                                 id: session_id,
                                                 expand: ['customer']
                                               })
  render json: { session: session }
end

#webhooksObject



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
65
66
67
# File 'app/controllers/stripe_controller.rb', line 16

def webhooks
  webhook_secret = '{{STRIPE_WEBHOOK_SECRET}}'
  payload = request.body.read
  if webhook_secret.empty?
    data = JSON.parse(payload, symbolize_names: true)
    event = Stripe::Event.construct_from(data)
  else
    # Retrieve the event by verifying the signature using the raw body and secret if webhook signing is configured.
    sig_header = request.env['HTTP_STRIPE_SIGNATURE']
    event = nil

    begin
      event = Stripe::Webhook.construct_event(
        payload, sig_header, webhook_secret
      )
    rescue JSON::ParserError
      # Invalid payload
      status 400
      return
    rescue Stripe::SignatureVerificationError
      # Invalid signature
      puts '⚠️  Webhook signature verification failed.'
      status 400
      return
    end
  end
  # Get the type of webhook event sent
  event_type = event['type']
  data = event['data']
  data['object']

  case event_type
  when 'checkout.session.completed'
    # Payment is successful and the subscription is created.
    # You should provision the subscription and save the customer ID to your database.
  when 'invoice.paid'
    # Continue to provision the subscription as payments continue to be made.
    # Store the status in your database and check when a user accesses your service.
    # This approach helps you avoid hitting rate limits.
  when 'invoice.payment_failed'
    # The payment failed or the customer does not have a valid payment method.
    # The subscription becomes past_due. Notify your customer and send them to the
    # customer portal to update their payment information.
  when 'customer.subscription.deleted'
    # The subscription has been canceled.
    # Record customer as not in good standing.
  else
    puts "Unhandled event type: \#{event.type}"
  end

  status 200
end