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)
2
  • updated to include what i've been trying Commented Jan 11, 2013 at 21:49
  • possible duplicate of How do I parse JSON with Ruby on Rails? Commented Jan 14, 2013 at 23:24

3 Answers 3

9

Instead of event_data.data.object.id

You should try:

event_data["data"]["object"]["id"]
Sign up to request clarification or add additional context in comments.

Comments

0

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

logger.debug params["id"]

should work.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.