在Customer.create之后如何解析Stripe JSON响应? [英] How to parse Stripe JSON response after Customer.create?

查看:146
本文介绍了在Customer.create之后如何解析Stripe JSON响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了使用此版本的Stripe API的其他线程,并且从未解析实际解析嵌套JSON的问题(例如:如何读取条纹webhook响应 ),尽管副本的原件"非常不同,并且没有给我任何提示.

I've read other threads which use this version of the Stripe API, and the question of actually parsing the nested JSON was never answered (ex: How to parse Stripe JSON response after credit card creation?); or marked as duplicate (ex: How to read the stripe webhook response), though the 'orig of the duplicate' is very different, and doesn't give me a clue.

这就是我所拥有的.这部分有效

So here is what I've got. This part works

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

...并得到一个类似于以下内容的JSON响应:

... and gets me a JSON response which 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 to a hash with:

stripe_customer_params = JSON.parse customer.to_s

哪个得到了:

{"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 these are nil when inspected:

stripe_customer_params[:id]
stripe_customer_params[:cards]

第一个应该从JSON获取我的ID ,而第二个应该具有完整哈希的子集.我要去哪里错了?

The first should get me the id from the JSON, and the latter should have a subset of the full-hash. Where am I going wrong?

------编辑 好吧,我太近了.这是在将json解析为哈希时,您必须使用这样的引号:

------EDIT Ok, I was so close. It is that when parsing json to hash, you have to use quotes like this:

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

现在,我在此获得没有将String隐式转换为Integer的信息":

So now I get "no implicit conversion of String into Integer" on this:

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

DB字段是一个字符串,而哈希值被引用为一个字符串,因此,有些嵌套的哈希数组仍然可以计算出来.

The DB field is a string, and the hash value is quoted as a string, so, some bit with the nested array of hashes to figure out still.

--------编辑2

--------Edit 2

好的,答案是:

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

...因为数据"是一个哈希数组(尽管其中只有1个哈希).

... because 'data' is an array of hashes (though only 1 hash is in it).

------编辑3 为了澄清为什么我要返回JSON而不是返回对象;我正在使用javascript生成的弹出式表单.表单是通过以下方式内联生成的:

-------Edit 3 For clarification as to why I get JSON back, rather than an object; I am using the javascript-generated pop-up form. The form is generated inline with:

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

这将创建一个带有文本"Submit"的按钮(上面的数据标签).单击后,将显示弹出表单.

This creates a button, with text "Submit" (data-label, above). When clicked, the pop-up form appears.

我已经弄清了Ruby-JSON-parsing语法,所以现在可以使用了.但是,如果可以添加一些其他的"data-foo"来代替JSON,则可以方便地知道该对象,而不是JSON.

I have the Ruby-JSON-parsing syntax figured out, so it is working now. But if there is some other 'data-foo' I can add to get an object back, instead of JSON, that would be good to know for simplicity's sake.

推荐答案

解析JSON的新手-我在值周围缺少引号-应该是这样:

Newbie to parsing JSON - I had missing Quotes around values - should be this:

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

数据"元素是一个数组,因此您可以使用该数组:

And the 'data' element is an array, so for that one you use:

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

首先尝试上述Bongs的回复.取决于您实现Stripe的方式,这可能会起作用.

Try the response by Bongs above, first. Depending on how you implement Stripe, that may work.

这篇关于在Customer.create之后如何解析Stripe JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆