我正在使用Koudoku进行订阅。收到Stripe Webhook之后,我想做其他事情。
In the docs,它表明您可以像这样添加回调:
Koudoku.setup do |config|
config.subscriptions_owned_by = :user
config.stripe_publishable_key = ENV['STRIPE_PUBLISHABLE_KEY']
config.stripe_secret_key = ENV['STRIPE_SECRET_KEY']
# add webhooks
config.subscribe 'charge.failed', YourChargeFailed
end
我不知道如何编写YourChargeFailed
部分。我尝试过类似的事情:
config.subscribe 'order.payment_succeeded', ActiveRecord::Subscription.after_successful_payment
但是我得到undefined method after_successful_payment for #<Class:0x007fb845849b30>
如何成功订阅Stripe事件,捕获返回数据并启动回调函数?
谢谢!
更新
这是我尝试过的,以及收到的相应错误:
purchases_helper.rb
module PurchasesHelper
require 'stripe'
def stripe_webhook(event)
puts 'Purchases Helper'
puts 'invoice.payment_succeeded'
@customer = Stripe::Customer.retrieve(event[:data][:object][:customer])
@user = User.find_by(email: @customer[:email])
@badge = Badge.find_by(condition: '2019Purchase')
@badges_user = BadgesUser.find_by(user_id: @user.id, badge_id: @badge.id)
# if @badges_user === nil
# BadgesUser.create(user_id: user.id, badge_id: badge.id)
# end
puts 'badge created'
end
end
initializers / koudoku.rb
Koudoku.setup do |config|
include ::PurchasesHelper
config.subscribe 'charge.succeeded' do |event|
puts 'charge created'
::PurchasesHelper.stripe_webhook(event)
end
end
错误:
undefined method `stripe_webhook' for PurchasesHelper:Module excluded from capture: Not configured to send/capture in environment 'development'
NoMethodError (undefined method `stripe_webhook' for PurchasesHelper:Module):
另一种尝试:
Koudoku.setup do |config|
config.subscribe 'charge.succeeded' do |event|
puts 'charge created'
PurchasesHelper.stripe_webhook(event)
end
end
错误:
undefined method `stripe_webhook' for PurchasesHelper:Module excluded from capture: Not configured to send/capture in environment 'development'
NoMethodError (undefined method `stripe_webhook' for PurchasesHelper:Module):
第三次尝试:
Koudoku.setup do |config|
include PurchasesHelper
config.subscribe 'charge.succeeded' do |event|
puts 'charge created'
stripe_webhook(event)
end
end
错误:
A copy of PurchasesHelper has been removed from the module tree but is still active! excluded from capture: Not configured to send/capture in environment 'development'
ArgumentError (A copy of PurchasesHelper has been removed from the module tree but is still active!):
答案 0 :(得分:1)
我发现您的代码只有一个问题。
module PurchasesHelper
require 'stripe'
def self.stripe_webhook(event) # NB self.
puts 'Purchases Helper'
puts 'invoice.payment_succeeded'
@customer = Stripe::Customer.retrieve(event[:data][:object][:customer])
@user = User.find_by(email: @customer[:email])
@badge = Badge.find_by(condition: '2019Purchase')
@badges_user = BadgesUser.find_by(user_id: @user.id, badge_id: @badge.id)
# if @badges_user === nil
# BadgesUser.create(user_id: user.id, badge_id: badge.id)
# end
puts 'badge created'
end
end
然后您说出来
Koudoku.setup do |config|
config.subscribe 'charge.succeeded' do |event|
puts 'charge created'
PurchasesHelper.stripe_webhook(event)
end
end
这应该有效
等等,为什么?!
Modules
是将方法,类和常量分组在一起的一种方式。 Modules
会给您带来两个主要好处。
include
时)您已经在Module
上定义了一个实例方法,当包含该实例方法时,该实例方法将出现在对象的每个实例中。
,但是在这种情况下您不这样做。您想在stripe_webhook
本身上调用Module
。
在这种情况下添加self. stripe_webhook
= PurchasesHelper. stripe_webhook
,这是在类/模块上定义方法的方式。
您甚至可以做更多奇怪的事情,例如:
class Animal
def self.all
%w[dog cat bird]
end
end
def Animal.include?(a)
self.all.include?(a)
end
Animal.include?('cat') # true
Animal.include?('virus') # false
因此您甚至可以在动物类的类范围之外定义方法,它将起作用。
总结:
在此示例中:
module PurchasesHelper
def self.stripe_webhook(event)
#...
end
end
等于
module PurchasesHelper
def PurchasesHelper.stripe_webhook(event)
#...
end
end
这就是为什么仅添加self
就能调用PurchasesHelper.stripe_webhook
答案 1 :(得分:0)
在Koudoku文档中,它表示实际上使用stripe_event处理https://github.com/integrallis/stripe_event
因此,查看strip_event示例,您可以传递一个块并执行所需的任何操作,或者传递对call
方法https://github.com/integrallis/stripe_event#usage做出响应的操作