P0lska
P0lska

Reputation: 471

Adding a new Card in Stripe using Rails always says Missing required param: source

I'm really stuck as to why I can't get this error to leave me be!

I'm trying to add a new card to an existing user through the Stripe API, the Stripe Gem and stripe.js

Server Log: (i've truncated values since the hashes are quite long):

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 method:

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

returned customer (from the puts customer)

{
 "id": "cus_7gCrNNYh3QWYyB",
 "object": "customer",
 "account_balance": 0,
 "created": 1452221885,
 "currency": "usd",
 "default_source": "card_17QqS0Hrpi1EikTtuQ24pf",
 "delinquent": false,
 "description": null,
 "discount": null,
 "email": "[email protected]",
 "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"
 }
}

Some notes that I've checked:

I get the same error before and after I've updated the STRIPE API I'm on the most recent Stripe API version (2015-10-16) The stripe gem is version 1.3.1 (October 29, 2015) Rails 4.2.2

Upvotes: 2

Views: 4772

Answers (1)

Ywain
Ywain

Reputation: 17505

This error message means that in your code:

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

stripe_card_token is nil.

Remember that you can view all requests sent with your API key in your dashboard. For example, to view all failed non-GET requests sent with your test API key: https://dashboard.stripe.com/test/logs?success=false&method=not_get

Upvotes: 2

Related Questions