Godzilla74 Godzilla74 - 5 months ago 19
Ruby Question

Extract Customer ID from Stripe JSON response in Ruby

Since Stripe doesn't have a way to lookup a customer based their email address, I have to pull all of them down and compare accordingly (from this SO question). This works fine, but now

customer_i_need
is:

[#<Stripe::Customer:0x3fc836b51efc id=cus_8BETSKddddnJD> JSON: {
"id": "cus_8BETSKddddnJD",
"object": "customer",
"account_balance": 0,
"business_vat_id": null,
"created": 1459377705,
"currency": "usd",
"default_source": "card_17us0SDcysUdddff5P8gqGmWm",
"delinquent": false,
"description": null,
"discount": null,
"email": "bobby@test.com",
"livemode": false,
"metadata": {},
"shipping": null,
"sources": {"object":"list","data":[{"id":"card_17us0SDcysUdddff5P8gqGmWm","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_8BETSKddddnJD","cvc_check":null,"dynamic_last4":null,"exp_month":1,"exp_year":2019,"fingerprint":"aHBJ0hDddffdaB9","funding":"credit","last4":"4242","metadata":{},"name":null,"tokenization_method":null}],"has_more":false,"total_count":1,"url":"/v1/customers/cus_8BETSKddddnJD/sources"},
"subscriptions": {"object":"list","data":[{"id":"sub_8BETB4CWTlBert","object":"subscription","application_fee_percent":null,"cancel_at_period_end":false,"canceled_at":null,"created":1459377705,"current_period_end":1490913705,"current_period_start":1459377705,"customer":"cus_8BETSKddddnJD","discount":null,"ended_at":null,"metadata":{},"plan":{"id":"annual_600","object":"plan","amount":60000,"created":1459172660,"currency":"usd","interval":"year","interval_count":1,"livemode":false,"metadata":{},"name":"annual_600","statement_descriptor":"Trial","trial_period_days":365},"quantity":1,"start":1459377705,"status":"trialing","tax_percent":null,"trial_end":1490913705,"trial_start":1459377705}],"has_more":false,"total_count":1,"url":"/v1/customers/cus_8BETSKddddnJD/subscriptions"}
}]


really, I just need to be able to add the
id
(cus_8BETSKddddnJD) to my users account if it's already created, but I'm having trouble accessing the value.

I've tried
customer_i_need["id"]
but that's not working. I've also tried to convert
customer_i_need.to_json
, then access the value the same way, but that's not working either... where am I going wrong?

Answer

It looks to me like you get some instance of Stripe::Customer returned. I would expect that you can just call id on that instance:

customer_i_need.id

Or since it looks like the instance is the only element in an array:

array.first.id
Comments