2016-10-04 5 views
0

ライブストライプのウェブフックを受け取りたいのですが、そのためにはイニシャライザを使用する必要があります。 Stripe_events gemを使用しています。イニシャライザには慣れていませんが、私はここで学びます!イニシャライザでハンドラメソッドを呼び出す方法は?

- 私のevent(webhook)がhandler_methodで呼び出されるようにします。あなたが見ることができるように

マイ初期化子/ 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 

は、私はこの1 stripe_handlerにすべてのイベントを呼び出したいevents.all = AllEvents.new
設定します。 event.typeがExであるものに基づいています。すべてのすべてでアプリ/ 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 

'charge.succeeded'

if event.type == 'charge.succeeded' 

etc........ 

end 

、私は、各ウェブフックのためのアクションを行うことができますhandler_methodsにevents.all値を送信します。

+0

うーん、あなたは「handle_methodへのすべての値が」と言う、あなたは様々なイベントを処理するために、個々のメソッドのような意味ですか:

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

は、その後のイベントを処理するクラスは次のようになりますか?そうであれば、私はあなたが個々の "handler_methods"を作るのを止めていることを理解していません。 – Rashmirathi

+0

はい!この 'def call(event)'のようにイベントを受け取ったとすると、内部では、ちょうどwebhookとして送られた特定のイベントであるイベント値を使用します –

答えて

1

私はこれをコメントに入れたいと思っていましたが、コメントするには大きすぎます。ドキュメントhereをすぐに読んだ後、私はstripe_event gemの使い方について私が理解していることを書き留めました。

だから、initializers/stripe.rbあなたは以下のコードブロックのようなものが必要です。 configureのブロックで、イベントの名前とそのイベントを処理するクラスのインスタンスを指定して、events.subscribeを呼び出してください。 1つのオブジェクトだけを使用してすべてのイベントを処理する必要はありません。

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 
+0

それは、すべてのイベントを1つのイベントに呼び出すのは良い考えですが、ちょうどこのようにしています。それぞれ別々のコントローラを作成していますか?それはコントローラの多くですか?またはコントローラ内に複数のクラスを持つことはできますか? –

+0

@MichaelLombardo申し訳ありませんが、これらはレールコントローラではありません。これらは単純なルビクラスであり、 'call'メソッドと' event'メソッドを引数として取ります。私はちょっと混乱しているので、ワードコントローラを使わないように私の答えを編集しました。コントローラーのすべての操作はgemで行われ、gemは1つのコントローラーしか使用しません[こちら](https://github.com/integrallis/stripe_event/blob/master/app/controllers/stripe_event/webhook_controller.rb) – Rashmirathi

+0

Imエラーが発生しました/Users/michael/Desktop/projects/ScribeOwl/config/initializers/stripe.rb:13:in'ブロック内<必須(必須)> ':初期化されていない定数ChargeSucceeded –

関連する問題