NoMethodError:未定义的方法`source'使用条纹时

时间:2016-05-17 21:40:03

标签: ruby-on-rails json stripe-payments

我试图制作它,以便我的用户可以使用网络连接从条纹中查看其结算历史记录。我从每个客户的条带中获取数据,但我没有获得卡源数据。

这是我的初始化程序/ 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。

这是我在控制台中查找购买时得到的结果。

run:event = Stripe :: Event.retrieve(&#34; event_id&#34;)

=> #<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方法错误...这是有道理的,因为它不存在。

这是我第一次整合条纹,所以我不确定我是否遗漏了设置中的内容。谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

并非所有Stripe事件都包含相同的数据。对于customer.subscription.created事件,您将获得上述JSON对象。它只是表示此客户现在正在使用此订阅。

我认为你要找的是一个收费。查看charge.succeeded webhook。它包括源对象,它将是用于支付的卡。