基本的にリクエストを受け入れて処理するには、コントローラーを作成する必要があります。それはかなり簡単ですが、最初は頭を悩ませるほど簡単ではありません。これが私の hooks_controller.rb の例です:
class HooksController < ApplicationController
require 'json'
Stripe.api_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def receiver
data_json = JSON.parse request.body.read
p data_json['data']['object']['customer']
if data_json[:type] == "invoice.payment_succeeded"
make_active(data_event)
end
if data_json[:type] == "invoice.payment_failed"
make_inactive(data_event)
end
end
def make_active(data_event)
@profile = Profile.find(User.find_by_stripe_customer_token(data['data']['object']['customer']).profile)
if @profile.payment_received == false
@profile.payment_received = true
@profile.save!
end
end
def make_inactive(data_event)
@profile = Profile.find(User.find_by_stripe_customer_token(data['data']['object']['customer']).profile)
if @profile.payment_received == true
@profile.payment_received = false
@profile.save!
end
end
end
def レシーバーは、ストライプ インターフェイスで Webhook をポイントする必要があるビューです。ビューは json を受け取り、支払いが失敗または成功した場合にユーザーのプロファイルを更新するために使用しています。