integrallis / stripe_event

Stripe webhook integration for Rails applications.
https://rubygems.org/gems/stripe_event
MIT License
840 stars 107 forks source link

Friendly docs for beginners #135

Open zsid opened 3 years ago

zsid commented 3 years ago

Hi. Thank you very much for creating the gem. It really solves the problem of setup and verifications for Stripe.

I have just started picking up Rails again and really struggling to set up the gem. I have copied and pasted the examples and tried to make something work but to no vail. I was wondering what your thoughts are on creating an example folder and a rails project which people can clone and see it working.

I am doing webhooks by following the setup mentioned in Stripe's docs with stripe-cli. I can see that things are happening in my rails terminal when the app is running but when I tried to create a new order for example it failed. I have also tried using a logger but that didnt work either

config/stripe.rb

Stripe.api_key = Rails.application.credentials.dig(:stripe, :secrect_key) # this is not a type for the secret
StripeEvent.signing_secret = 'whsec_7KN2VtuaSbwZHf2KvgokYmhIqmN6CXA9'

StripeEvent.configure do |events|
  # events.subscribe 'checkout.session.completed', Stripe::CheckoutEventHandler.new
  # events.subscribe 'checkout.session.completed' do |event|

  # end
  events.all Testing::BillingEventLogger.new(Rails.logger)
  # events.subscribe 'checkout.session', Testing::CheckoutEventHandler.new

end

These are the different things that I have tried

app/services/testing/checkout_event_handler.rb
# module Stripe
#   class CheckoutEventHandler
#     def call(event)
#       begin
#         self.send handle_checkout_session_completed, event
#       rescue JSON::ParserError => e
#         # handle the json parsing error here
#         raise # re-raise the exception to return a 500 error to stripe
#       rescue NoMethodError => e
#         #code to run when handling an unknown event
#       end
#     end

#     def handle_checkout_session_completed(event)
#       Test.create(name: 'hi')
#     end
#   end

#   def Testing
#     def call(event)
#       Test.create(name: 'Testing')
#     end
#   end

# end

module Testing
  # class CheckoutEventHandler
  #   def call(event)
  #     begin
  #       self.send handle_checkout_session_completed, event
  #     rescue JSON::ParserError => e
  #       # handle the json parsing error here
  #       raise # re-raise the exception to return a 500 error to stripe
  #     rescue NoMethodError => e
  #       #code to run when handling an unknown event
  #     end
  #   end

  #   def handle_checkout_session_completed(event)
  #     Test.create(name: 'hi')
  #   end
  # end

  class BillingEventLogger
    def initialize(logger)
      @logger = logger
    end

    def call(event)
      @logger.info "I AM CALLED ******** ZHIVKO"
      @logger.info "BILLING:#{event.type}:#{event.id}"
    end
  end
end

Looking forward to your thoughts and any feedback welcome. Thanks.

tannerhallman commented 1 year ago

have you subscribed to those events in the stripe dashboard?