2017-09-01 28 views
0

在rails中實現條帶webhook時,我成功獲取了JSON格式的事件對象。但問題是我無法獲取像嵌套JSON中的金額,subscription_id和屬性的細節。從類對象獲取這些值也是不可用的。你能告訴我如何提取這些值。如何在Rails中從條帶webhook事件中檢索條帶數據

invoice = Stripe::Invoice.retrieve(:id => "in_1AwVDUHb727hqFGZ0UF7B8tB") 

我也得到如下回應:

#<Stripe::Invoice:0xebc79c id=in_1AwVDUHb727hqFGZ0UF7B8tB> JSON: { 
    "id": "in_1AwVDUHb727hqFGZ0UF7B8tB", 
    "object": "invoice", 
    "amount_due": 2500, 
    "application_fee": null, 
    "attempt_count": 1, 
    "attempted": true, 
    "billing": "charge_automatically", 
    "charge": "ch_1AwVDUHb727hqFGZ702PYTrW", 
    "closed": true, 
    "currency": "aud", 
    "customer": "cus_BJ7S8vrsbh9Lyj", 
    "date": 1504095764, 
    "description": null, 
    "discount": null, 
    "ending_balance": 0, 
    "forgiven": false, 
    "lines": {"object":"list","data":[{"id":"sub_BJ7ScT0lf9yVGV","object":"line_item","amount":2500,"currency":"aud","description":null,"discountable":true,"livemode":false,"metadata":{},"period":{"start":1504095764,"end":1506774164},"plan":{"id":"c844d552154b","object":"plan","amount":2500,"created":1504095762,"currency":"aud","interval":"month","interval_count":1,"livemode":false,"metadata":{},"name":"Plan2","statement_descriptor":null,"trial_period_days":null},"proration":false,"quantity":1,"subscription":null,"subscription_item":"si_1AwVDUHb727hqFGZh2iulZFY","type":"subscription"}],"has_more":false,"total_count":1,"url":"/v1/invoices/in_1AwVDUHb727hqFGZ0UF7B8tB/lines"}, 
    "livemode": false, 
    "metadata": {}, 
    "next_payment_attempt": null, 
    "number": "7b726e08d6-0001", 
    "paid": true, 
    "period_end": 1504095764, 
    "period_start": 1504095764, 
    "receipt_number": null, 
    "starting_balance": 0, 
    "statement_descriptor": null, 
    "subscription": "sub_BJ7ScT0lf9yVGV", 
    "subtotal": 2500, 
    "tax": null, 
    "tax_percent": null, 
    "total": 2500, 
    "webhooks_delivered_at": 1504095765 
} 

我想得到這樣的CUSTOMER_ID,subscription_id,plan_id的數據類型等 我將如何提取數據的值。

預先感謝您

回答

1

一旦你retrieved the invoice,你可以簡單地訪問對象的屬性,如:

invoice = Stripe::Invoice.retrieve("in_...") 
customer_id = invoice.customer 
subscription_id = invoice.subscription 

發票不攜帶該計劃的ID,所以你需要到retrieve the subscription第一張:

subscription = Stripe::Subscription.retrieve(subscription_id) 
plan_id = subscription.plan 
+0

謝謝Ywain,它適合我 – Pankaj