如何从Rails

时间:2017-09-01 09:16:33

标签: ruby-on-rails json stripe-payments webhooks

在rails中实现条带webhook时,我成功地以JSON格式获取事件对象。但问题是我无法获得嵌套JSON中的amount,subscription_id,属性等详细信息。从类对象获取这些值也不可用。您能否告诉我如何提取这些值。

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 个答案:

答案 0 :(得分: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