我正在嘗試使用它,以便我的用戶可以使用Web鉤子從條帶中查看其計費歷史記錄。我從每個客戶的條紋中獲取數據,但我沒有獲取卡源數據。NoMethodError:使用條紋時未定義的方法'源'
這裏是我的初始化/ stripe.rb文件
Stripe.api_key = STRIPE_SECRET
class RecordCharges
def call(event)
charge = event.data.object
#Look up the user in our database
user = User.find_by(stripe_id: charge.customer)
#Record a charge in our database
c = user.charges.where(stripe_id: charge.id).first_or_create
c.update(
stripe_id: charge.id,
amount: charge.amount,
amount_refunded: charge.soruce.amount_refunded,
card_last4: charge.source.last4,
card_brand: 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
這裏的cards_controller.rb
class CardsController < ApplicationController
before_action :authenticate_user!
skip_after_action :verify_authorized
def update
customer = Stripe::Customer.retrieve(current_user.stripe_id)
subscription = customer.subscriptions.retrieve(current_user.stripe_subscription_id)
subscription.source = params[:stripeToken]
subscription.save
current_user.update(
card_last4: params[:card_last4],
card_exp_month: params[:card_exp_month],
card_exp_year: params[:card_exp_year],
card_exp_year: params[:card_exp_year],
card_brand: params[:card_brand]
)
redirect_to edit_user_registration_path, notice: "Successfully updated your card"
end
end
看來,控制器工作,因爲我能夠更新用戶。問題是,當我做card_brand,card_exp_year等...都被設置爲nill。
以下是我在控制檯中查找購買時所得到的結果。
運行:事件=條紋:: Event.retrieve( 「事項標識」)
=> #<Stripe::Event:0x3fc6dc914fa0 id=evt_18CFSRGgSrxZ3yXH1ktgY3M6> JSON: {
"id": "evt_18CFSRGgSrxZ3yXH1ktgY3M6",
"object": "event",
"api_version": "2016-03-07",
"created": 1463519427,
"data": {"object":{"id":"sub_8TBqDmWv6yFKOd","object":"subscription","application_fee_percent":null,"cancel_at_period_end":false,"canceled_at":null,"created":1463519427,"current_period_end":1466197827,"current_period_start":1463519427,"customer":"cus_8TBqPq3Y9npBz0","discount":null,"ended_at":null,"metadata":{},"plan":{"id":"pro-49","object":"plan","amount":4900,"created":1463398458,"currency":"usd","interval":"month","interval_count":1,"livemode":false,"metadata":{},"name":"Monthly pro-49","statement_descriptor":null,"trial_period_days":null},"quantity":1,"start":1463519427,"status":"active","tax_percent":null,"trial_end":null,"trial_start":null}},
"livemode": false,
"pending_webhooks": 1,
"request": "req_8TBqLJDEuitTfN",
"type": "customer.subscription.created"
}
然後我通過運行event.data.object
的JSON看起來像這樣看對象數據:
=> #<Stripe::Subscription:0x3fc6dc9243ec id=sub_8TBqDmWv6yFKOd> JSON: {
"id": "sub_8TBqDmWv6yFKOd",
"object": "subscription",
"application_fee_percent": null,
"cancel_at_period_end": false,
"canceled_at": null,
"created": 1463519427,
"current_period_end": 1466197827,
"current_period_start": 1463519427,
"customer": "cus_8TBqPq3Y9npBz0",
"discount": null,
"ended_at": null,
"metadata": {},
"plan": {"id":"pro-49","object":"plan","amount":4900,"created":1463398458,"currency":"usd","interval":"month","interval_count":1,"livemode":false,"metadata":{},"name":"Monthly pro-49","statement_descriptor":null,"trial_period_days":null},
"quantity": 1,
"start": 1463519427,
"status": "active",
"tax_percent": null,
"trial_end": null,
"trial_start": null
}
請注意,源不在那裏。它應該有一個源對象,它擁有我需要的所有數據,但由於某種原因它沒有顯示出來。此外,如果我調用event.data.object.source,我會得到no方法錯誤...這是有道理的,因爲它不存在。
這是我第一次集成條紋,所以我不確定是否在設置中遺漏了某些東西。謝謝您的幫助。