2

Basic question that I'm stuck on:

If I receive a POST request with the following JSON:

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

How do I parse that to access the paramater "id": "in_15PLg6oL0IiYYj"

Thank you!!

NOTE: This is coming from a webhook via Stripe Payment Processing.

I've tried a few variations of this, but seem to be going in circles:

# Parse JSON
event_data = JSON.parse(json)

# Get Invoice id
invoice_id = Stripe::Invoice.retrieve(event_data.data.object.id)
nathan
  • 514
  • 1
  • 7
  • 20
  • updated to include what i've been trying – nathan Jan 11 '13 at 21:49
  • possible duplicate of [How do I parse JSON with Ruby on Rails?](http://stackoverflow.com/questions/1826727/how-do-i-parse-json-with-ruby-on-rails) – Justin Jan 14 '13 at 23:24

3 Answers3

9

Instead of event_data.data.object.id

You should try:

event_data["data"]["object"]["id"]
hrr
  • 1,676
  • 1
  • 13
  • 15
0

If you receive the data in the params hash, then:

logger.debug params["id"]

should work.

Roger
  • 7,535
  • 5
  • 41
  • 63
0

That is RAW POST DATA, not URL Parameters

You can cross check if you are getting raw data on http://requestb.in/

Raw data will not be accessible with params['id'] as it does not have a Key

The data you want is accessible through

request.env["rack.input"].read
user706001
  • 980
  • 7
  • 8