app/controllers/stripe_controller.rb
# TODO: This has mostly been taken from stripe documentation and it's not very robust right now.
# Rewrite this with strong params etc
# And move it to Background jobs
This has mostly been taken from stripe documentation and it's not very robust right now.
Rewrite this with strong params etc
And move it to Background jobs
This has mostly been taken from stripe documentation and it ' s not very robust right now . Rewrite this with strong params etc And move it to Background jobs
def payment_webhook
    webhook_secret = ENV['STRIPE_WEBHOOK_SECRET']
    payload = request.body.read
    if !webhook_secret.empty?
      sig_header = request.env['HTTP_STRIPE_SIGNATURE']
      event = nil
      begin
        event = Stripe::Webhook.construct_event(
          payload, sig_header, webhook_secret
        )
      rescue JSON::ParserError => e
        render body: nil, status: 200
        return
      rescue Stripe::SignatureVerificationError => e
        puts '⚠️  Webhook signature verification failed.'
        render body: nil, status: 200
        return
      end
    else
      data = JSON.parse(payload, symbolize_names: true)
      event = Stripe::Event.construct_from(data)
    end
    event_type = event['type']
    data = event['data']
    data_object = data['object']
    case event.type
    when 'customer.subscription.created'
      StripeUpdateLocalSubscriptionDetailsWorker.perform_async(data_object)
      render body: nil, status: 201
      return
    when 'customer.subscription.deleted'
      StripeCancelLocalSubscriptionWorker.perform_async(data_object)
      render body: nil, status: 201
      return
    when 'customer.subscription.updated'
      StripeUpdateLocalSubscriptionDetailsWorker.perform_async(data_object)
      render body: nil, status: 201
      return
    when 'invoice.paid'
      render body: nil, status: 201
      return
    when 'invoice.payment_failed'
    else
      puts "Unhandled event type: #{event.type}"
    end
    render body: nil, status: 200
  end
def payment_webhook webhook_secret = ENV [ 'STRIPE_WEBHOOK_SECRET' ] payload = request . body . read if ! webhook_secret . empty? sig_header = request . env [ 'HTTP_STRIPE_SIGNATURE' ] event = nil begin event = Stripe :: Webhook . construct_event ( payload , sig_header , webhook_secret ) rescue JSON :: ParserError => e render body : nil , status : 200 return rescue Stripe :: SignatureVerificationError => e puts '⚠️  Webhook signature verification failed.' render body : nil , status : 200 return end else data = JSON . parse ( payload , symbolize_names : true ) event = Stripe :: Event . construct_from ( data ) end event_type = event [ 'type' ] data = event [ 'data' ] data_object = data [ 'object' ] case event . type when 'customer.subscription.created' StripeUpdateLocalSubscriptionDetailsWorker . perform_async ( data_object ) render body : nil , status : 201 return when 'customer.subscription.deleted' StripeCancelLocalSubscriptionWorker . perform_async ( data_object ) render body : nil , status : 201 return when 'customer.subscription.updated' StripeUpdateLocalSubscriptionDetailsWorker . perform_async ( data_object ) render body : nil , status : 201 return when 'invoice.paid' render body : nil , status : 201 return when 'invoice.payment_failed' else puts "Unhandled event type: #{event.type}" end render body : nil , status : 200 end
TODO: This has mostly been taken from stripe documentation and it's not very robust right now.
TODO : This has mostly been taken from stripe documentation and it ' s not very robust right now .
# Retrieve the event by verifying the signature using the raw body and secret if webhook signing is configured. # Invalid payload # Invalid signature # Get the type of webhook event sent # 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. # 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.