How to parse Stripe JSON response after Customer.create?

I read other topics that use this version of the Stripe API, and the question of actually analyzing the embedded JSON never answered (for example: How to parse the Stripe JSON response after creating a credit card? ); or designated as a duplicate (for example: How to read the response to a web entry in the strip ), although the "source of the duplicate" is very different and does not give me hints,

So here is what I have. This part works

customer = Stripe::Customer.create(
    :card  => params[:stripeToken],
    :plan => params[:stripe_plan_id],
    :email => current_user.email
  )

... and gets a JSON response that looks like this:

"id": "cus_4ab7wXjPrnoBvm",
"object": "customer",
"created": 1408029243,
"livemode": false,
"description": null,
"email": "edsmith@test.com",
"delinquent": false,
"metadata": {},
"subscriptions": {"object":"list","total_count":1,"has_more":false,"url":"/v1/customers/cus_4ab7wXjPrnoBvm/subscriptions","data":[{"id":"sub_4ab7tOTkirRQ8r","plan":{"id":"gold","interval":"month","name":"Gold Plan","created":1408013865,"amount":399,"currency":"usd","object":"plan","livemode":false,"interval_count":1,"trial_period_days":null,"metadata":{},"statement_description":null},"object":"subscription","start":1408029243,"status":"active","customer":"cus_4ab7wXjPrnoBvm","cancel_at_period_end":false,"current_period_start":1408029243,"current_period_end":1410707643,"ended_at":null,"trial_start":null,"trial_end":null,"canceled_at":null,"quantity":1,"application_fee_percent":null,"discount":null,"metadata":{}}]},
"discount": null,
"account_balance": 0,
"currency": "usd",
"cards": {"object":"list","total_count":1,"has_more":false,"url":"/v1/customers/cus_4ab7wXjPrnoBvm/cards","data":[{"id":"card_14RPvP4WgFgXeu1koPs2f9Zd","object":"card","last4":"5904","brand":"Diners Club","funding":"credit","exp_month":2,"exp_year":2015,"fingerprint":"XTb65AiBJVROg4FA","country":null,"name":"edsmith@test.com","address_line1":null,"address_line2":null,"address_city":null,"address_state":null,"address_zip":null,"address_country":null,"cvc_check":"pass","address_line1_check":null,"address_zip_check":null,"customer":"cus_4ab7wXjPrnoBvm"}]},
"default_card": "card_14RPvP4WgFgXeu1koPs2f9Zd"
}

So, I parse this with a hash with:

stripe_customer_params = JSON.parse customer.to_s

What happens:

{"id"=>"cus_4abKeDBM5EGfy6", "object"=>"customer", "created"=>1408030013, "livemode"=>false, "description"=>nil, "email"=>"georgesmith@test.com", "delinquent"=>false, "metadata"=>{}, "subscriptions"=>{
    "object"=>"list", "total_count"=>1, "has_more"=>false, "url"=>"/v1/customers/cus_4abKeDBM5EGfy6/subscriptions", "data"=>[{
        "id"=>"sub_4abKNsI4WqeZYS", "plan"=>{
            "id"=>"gold", "interval"=>"month", "name"=>"Gold Plan", "created"=>1408013865, "amount"=>399, "currency"=>"usd", "object"=>"plan", "livemode"=>false, "interval_count"=>1, "trial_period_days"=>nil, "metadata"=>{}, "statement_description"=>nil
            }, 
            "object"=>"subscription", "start"=>1408030013, "status"=>"active", "customer"=>"cus_4abKeDBM5EGfy6", "cancel_at_period_end"=>false, "current_period_start"=>1408030013, "current_period_end"=>1410708413, "ended_at"=>nil, "trial_start"=>nil, "trial_end"=>nil, "canceled_at"=>nil, "quantity"=>1, "application_fee_percent"=>nil, "discount"=>nil, "metadata"=>{}
        }
    ]}, 
"discount"=>nil, "account_balance"=>0, "currency"=>"usd", "cards"=>{
    "object"=>"list", "total_count"=>1, "has_more"=>false, "url"=>"/v1/customers/cus_4abKeDBM5EGfy6/cards", "data"=>[{
        "id"=>"card_14RQ7p4WgFgXeu1kOETsWaqN", "object"=>"card", "last4"=>"5556", "brand"=>"Visa", "funding"=>"debit", "exp_month"=>4, "exp_year"=>2015, "fingerprint"=>"PDbelzu2DLr2A1C3", "country"=>"US", "name"=>"georgesmith@test.com", "address_line1"=>nil, "address_line2"=>nil, "address_city"=>nil, "address_state"=>nil, "address_zip"=>nil, "address_country"=>nil, "cvc_check"=>"pass", "address_line1_check"=>nil, "address_zip_check"=>nil, "customer"=>"cus_4abKeDBM5EGfy6"
        }]
    }, 
"default_card"=>"card_14RQ7p4WgFgXeu1kOETsWaqN"}

But they are checked when checking:

stripe_customer_params[:id]
stripe_customer_params[:cards]

The former should get me an identifier from JSON, and the latter should have a subset of the full hash. Where am I mistaken?

------ EDIT , . , json :

stripe_customer_params['id']
stripe_customer_params['cards']

, " String Integer":

stripe_customer_params['cards']['data']['last4']

, - , .

-------- 2

, :

stripe_customer_params['cards']['data'].first['last4']

... "" - ( 1 ).

------- 3 , JSON, ; , javascript. :

  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
    data-key="pk_test_foo"
    data-label="Submit" 
    data-email="<%=current_user.email%>" 
    data-image="/square-image.png" 
    data-name="mysite.com" 
    data-description="Payment Plan 1" 
    data-amount="1000">
  </script>

"" ( , ). .

Ruby-JSON, . "data-foo", , JSON, .

+4
3

JSON - - :

stripe_customer_params['id']
stripe_customer_params['cards']

'data' , :

stripe_customer_params['cards']['data'].first['last4']

. , Stripe, .

0

. Stripe Checkout . , :

# Create a Customer
customer = Stripe::Customer.create(
  :source => token,
  :plan => "gold",
  :email => @user.email
)

# Grab the response data from Stripe:
card = customer.sources.first


# Save their Stripe ID:
@user.stripe_customer_id = customer.id

# Save their credit card details:
@user.card_id = card.id
@user.card_brand = card.brand
@user.card_last4 = card.last4
@user.card_expiration = Date.new(card.exp_year, card.exp_month, 1)

:

@user.save
+1

Pretty simple:

customer = Stripe::Customer.create(
  :card  => params[:stripeToken],
  :plan => params[:stripe_plan_id],
  :email => current_user.email
)

And then

customer.id # will return id and similarly you can access other attributes
0
source

All Articles