2

我坚持的基本问题:

如果我收到带有以下 JSON 的 POST 请求:

JSON: {
  "type": "invoice.payment_succeeded",
  "pending_webhooks": 0,
  "created": 1357936579,
  "object": "event",
  "livemode": false,
  "id": "evt_15PLdo9JdZ2yN4",
  "data": {
    "object": {
      "subtotal": 3500,
      "attempted": true,
      "paid": true,
      "amount_due": 3500,
      "closed": true,
      "period_end": 1357936578,
      "lines": {
        "url": "/v1/invoices/in_15PLg6oL0IiYYj/lines",
        "count": 1,
        "object": "list",
        "data": [
          {
            "type": "subscription",
            "period": {
              "end": 1360614978,
              "start": 1357936578
            },
            "livemode": false,
            "object": "line_item",
            "proration": false,
            "amount": 3500,
            "quantity": 1,
            "plan": {
              "interval_count": 1,
              "livemode": false,
              "object": "plan",
              "amount": 3500,
              "trial_period_days": null,
              "name": "forecast3",
              "currency": "usd",
              "id": "forecast3",
              "interval": "month"
            },
            "id": "su_15PLVebFWecrdQ",
            "currency": "usd",
            "description": null
          }
        ]
      },
      "starting_balance": 0,
      "charge": "ch_15PLYLUiVusQDH",
      "object": "invoice",
      "total": 3500,
      "ending_balance": 0,
      "date": 1357936578,
      "period_start": 1357936087,
      "attempt_count": 0,
      "discount": null,
      "livemode": false,
      "id": "in_15PLg6oL0IiYYj",
      "next_payment_attempt": null,
      "customer": "cus_15OrEWGsLN3CkP",
      "currency": "usd"
    }
  }
}

我如何解析它以访问参数"id": "in_15PLg6oL0IiYYj"

谢谢!!

注意:这是来自通过 Stripe Payment Processing 的 webhook。

我已经尝试了一些变体,但似乎在绕圈子:

# Parse JSON
event_data = JSON.parse(json)

# Get Invoice id
invoice_id = Stripe::Invoice.retrieve(event_data.data.object.id)
4

3 回答 3

9

代替event_data.data.object.id

你应该试试:

event_data["data"]["object"]["id"]
于 2013-01-11T21:53:40.470 回答
0

如果您收到 params 哈希中的数据,则:

logger.debug params["id"]

应该管用。

于 2013-01-11T21:49:08.607 回答
0

那是RAW POST DATA,而不是 URL 参数

您可以交叉检查是否在http://requestb.in/上获取原始数据

原始数据将无法通过 params['id'] 访问,因为它没有密钥

你想要的数据可以通过

request.env["rack.input"].read
于 2014-07-26T11:57:26.453 回答