我試圖讓我的應用程序通過Webhook從條紋成功收費。這是基於克里斯奧利弗的GoRails對條紋Webhooks的討論。我正在使用Koudoku創建條紋功能。當我嘗試將JSON對象加載到Charges表中時,我遇到了一個錯誤。條紋NoMethodError:未定義的方法'收費'的零:NilClass在Rails控制檯
這裏是當我試圖記錄事件發生的錯誤:
RecordCharges.new.call(event)
Subscription Load (0.3ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."stripe_id" = ? LIMIT 1 [["stripe_id", "cus_withheldforexample"]]
NoMethodError: undefined method `charges' for nil:NilClass
在我的應用程序,用戶有一個訂閱。訂閱有很多費用。費用屬於訂閱。
這裏的模式:
create_table "charges", force: :cascade do |t|
t.string "stripe_id"
t.string "amount"
t.string "card_last4"
t.string "card_type"
t.string "card_exp_month"
t.string "card_exp_year"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "subscription_id"
end
create_table "subscriptions", force: :cascade do |t|
t.string "stripe_id"
t.integer "plan_id"
t.string "last_four"
t.integer "coupon_id"
t.string "card_type"
t.float "current_price"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
我Stripe.rb文件
class RecordCharges
def call(event)
charge = event.data.object
subscription = Subscription.find_by(stripe_id: charge.customer)
subscription.charges.create(
stripe_id: charge.id,
amount: charge.amount,
card_last4: charge.source.last4,
card_type: charge.source.brand,
card_exp_month: charge.source.exp_month,
card_exp_year: charge.source.exp_year
)
end
end
StripeEvent.configure do |events|
events.subscribe 'charge.succeeded', RecordCharges.new
end
有什麼建議?謝謝!
'subscription'返回nil。所以我可能會讓它成爲'find_by!'並引發一個'ActiveRecord :: RecordNotFound'錯誤,然後解救它。收費是否有客戶? –