使用Rails在Stripe中添加新卡总是说缺少必需的参数:source

时间:2016-01-09 06:12:56

标签: ruby-on-rails stripe-payments

我真的不知道为什么我不能得到这个错误让我离开!

我尝试通过Stripe API,Stripe Gem和stripe.js向现有用户添加新卡

服务器日志:(由于哈希很长,我已经截断了值):

Started PATCH "/users/11/billing" for xxx.xxx.xxx.xxx at 2016-01-09 05:47:50     +0000
Processing by UsersController#update_billing as HTML
Parameters: {"utf8"=>"✓",   "authenticity_token"=>"kKgMt...ItQ==", "user"=>  {"stripe_card_token"=>"tok_17RFZpHrpi1EikTto5nCO0", "plan_id"=>""}, "id"=>"11"}
Stripe error while updating card info: Missing required param: source.

User.rb方法:

def update_card(subscriber, stripe_card_token)
  # customer is created correctly
  customer = Stripe::Customer.retrieve(subscriber.stripeid)
  puts customer
  customer.sources.create(:source => stripe_card_token)
  customer.save
  rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while updating card info: #{e.message}"
    errors.add :base, "#{e.message}"
    false
end

退回客户(来自看跌客户)

{
 "id": "cus_7gCrNNYh3QWYyB",
 "object": "customer",
 "account_balance": 0,
 "created": 1452221885,
 "currency": "usd",
 "default_source": "card_17QqS0Hrpi1EikTtuQ24pf",
 "delinquent": false,
 "description": null,
 "discount": null,
 "email": "merp@merp.com",
 "livemode": false,
 "metadata": {},
 "shipping": null,
 "sources": {
    "object": "list",
    "data": [
       {
          "id": "card_17QqS0Hrpi1EikTtuQ24pf",
          "object": "card",
          "address_city": null,
          "address_country": null,
          "address_line1": null,
          "address_line1_check": null,
          "address_line2": null,
          "address_state": null,
          "address_zip": null,
          "address_zip_check": null,
          "brand": "Visa",
          "country": "US",
          "customer": "cus_7gCrNNYh3QWY",
          "cvc_check": "pass",
          "dynamic_last4": null,
          "exp_month": 1,
          "exp_year": 2016,
          "fingerprint": "W6mF0Trq946uDH",
          "funding": "credit",
          "last4": "4242",
          "metadata": {},
          "name": null,
          "tokenization_method": null
       }
    ],
    "has_more": false,
    "total_count": 1,
    "url": "/v1/customers/cus_7gCrNNYh3QWY/sources"
 },
 "subscriptions": {
    "object": "list",
    "data": [
       {
          "id": "sub_7gCrMFXaDTb6",
          "object": "subscription",
          "application_fee_percent": null,
          "cancel_at_period_end": false,
          "canceled_at": null,
          "current_period_end": 1483844285,
          "current_period_start": 1452221885,
          "customer": "cus_7gCrNNYh3QWY",
          "discount": null,
          "ended_at": null,
          "metadata": {},
          "plan": {
             "id": "2",
             "object": "plan",
             "amount": 12000,
             "created": 1451801067,
             "currency": "usd",
             "interval": "year",
             "interval_count": 1,
             "livemode": false,
             "metadata": {},
             "name": "Business Plan",
             "statement_descriptor": "Business Plan",
             "trial_period_days": null
          },
          "quantity": 1,
          "start": 1452221885,
          "status": "active",
          "tax_percent": null,
          "trial_end": null,
          "trial_start": null
       }
    ],
    "has_more": false,
    "total_count": 1,
    "url": "/v1/customers/cus_7gCrNNYh3QWY/subscriptions"
 }
}

我检查了一些注意事项:

我在更新STRIPE API之前和之后得到了同样的错误 我是最新的Stripe API版本(2015-10-16) 条纹宝石是1.3.1版(2015年10月29日) Rails 4.2.2

1 个答案:

答案 0 :(得分:2)

此错误消息表示您的代码:

customer.sources.create(:source => stripe_card_token)

stripe_card_tokennil

请记住,您可以在信息中心中查看使用API​​密钥发送的所有请求。例如,要查看使用您的测试API密钥发送的所有失败的非GET请求:https://dashboard.stripe.com/test/logs?success=false&method=not_get