【问题标题】:How to retrive stripe data from stripe webhook event in Rails如何从 Rails 中的条带 webhook 事件中检索条带数据
【发布时间】:2018-02-10 07:42:52
【问题描述】:

在 Rails 中实现条带 webhook 时,我成功地获取了 JSON 格式的事件对象。但问题是我无法获得嵌套 JSON 中的金额、订阅 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 等值。 我将如何提取数据。

提前谢谢你

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2017-01-13
      • 1970-01-01
      • 2018-02-14
      • 2022-10-18
      • 2012-09-09
      • 1970-01-01
      • 2022-07-29
      • 2021-05-17
      • 1970-01-01
      相关资源
      最近更新 更多