Francis Ouellet
Francis Ouellet

Reputation: 505

How to parse Stripe JSON response after credit card creation?

I want to save a Stripe card_id to my database based on the JSON response. I'm playing with the examples from the Stripe documentation right now:

customer = Stripe::Customer.retrieve("cus_3Ek7h52yGbLpQo")
customer.cards.create(:card => {:number => "4242424242424242",
:exp_month => 10, :exp_year => 2014})

The JSON looks like this:

#<Stripe::Card:0x3ff2f0191540> JSON: {"id":"card_3GHjrJqMgoyTNy","object":"card","last4":"4242","type":"Visa","exp_month":10,"exp_year":2014,"fingerprint":"Ds0FdzrOSdYMkwC0","customer":"cus_3Ek7h52yGbLpQo","country":"US","name":null,"address_line1":null,"address_line2":null,"address_city":null,"address_state":null,"address_zip":null,"address_country":null,"cvc_check":null,"address_line1_check":null,"address_zip_check":null}

In my controller, after creating the credit card, how can I parse the JSON to get only the card id? Is it even possible?

Upvotes: 0

Views: 1050

Answers (1)

Dhaulagiri
Dhaulagiri

Reputation: 3291

You should be able to get the credit card from the response like this:

@card = customer.cards.create(:card => {:number => "4242424242424242",
:exp_month => 10, :exp_year => 2014})

@card.id  #this should have the card id in it

Upvotes: 1

Related Questions