簡體   English   中英

如何在初始化程序中調用處理程序方法? 創建網絡掛鈎

[英]How to call handler methods in an initializer? creating webhooks

我想實時接收帶狀Web鈎子,為此,我必須使用初始化程序。 使用Stripe_events gem。 我不太熟悉初始化程序,但是我在這里學習!

-我希望通過handler_method調用event (webhook)。

我的初始值設定項/stripe.rb

Rails.configuration.stripe = {
  :publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
  :secret_key => ENV['STRIPE_SECRET_KEY']

}

Stripe.api_key = Rails.configuration.stripe[:secret_key]


StripeEvent.configure do |events|
    events.subscribe 'charge.succeeded', ReservationsController.new
    events.all = AllEvents.new



end

如您所見,我設置了events.all = AllEvents.new
我想將所有事件都調用到這個stripe_handler中。 基於event.type是Ex。 'charge.succeeded'

if event.type == 'charge.succeeded' 

etc........

end

在app / stripe_handlers / all_events.rb中

class AllEvents
  def call(event)

    if event.type == 'charge.succeeded'

        reservation = Reservation.find_by_transaction_id(event.object.id)
        reservation.update_attributes pay_completed: true

        # reservation = Reservation.find_by_transaction_id
    elsif event.type == 'customer.created'


    elsif event.type == 'account.application.deauthorized'

            # look out for account.updated and check if the account ID is unknown

  end
end
end

總而言之,我想將events.all值發送到handler_methods,在其中我可以為每個Webhook進行操作。

我想在評論中添加這個內容,但是太大了。 在快速閱讀此處的文檔之后,我在下面寫下了我對如何使用stripe_event gem的理解。

因此, initializers/stripe.rb需要類似下面的代碼塊。 您所需要做的就是調用events.subscribe在configure塊中,帶有事件的名稱和將處理該事件的類的實例。 您不需要僅使用一個對象來處理所有事件。

StripeEvent.configure do |events|
    events.subscribe 'charge.succeeded', ChargeSucceeded.new
    event.subscribe 'customer.created', CustomerCreated.new
    event.subscribe 'account.application.deauthorized', Deauthorised.new
end

然后,處理事件的類如下所示:

class ChargeSucceeded
  def call(event)
    #Code to handle event 'charge.succeeded'
  end
end

class CustomerCreated
  def call(event)
    #Code to handle event 'customer.created'
  end
end

class Deauthorised
  def call(event)
    #Code to handle event 'account.application.deauthorized'
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM