0

I am trying to access some metadata from a Stripe Invoice. I am running into an issue on this particular query even though I don't have trouble with any others, so I'm hoping a fresh set of eyes can see a mistake I'm making.

Here is an example response from Stripe:

{
  "object": {
    "id": "in_1DYkdY2Y54K5YG39D4uaDtnj",
    "object": "invoice",
    "amount_due": 1600,
    "amount_paid": 1600,
    "amount_remaining": 0,
    "application_fee": null,
    "attempt_count": 1,
    "attempted": true,
    "auto_advance": false,
    "billing": "charge_automatically",
    "billing_reason": "subscription_update",
    "charge": "ch_1DYkdZ2Y54K5YG39rqyAJ1Ca",
    "closed": true,
    "currency": "usd",
    "customer": "cus_DnCcf39Nqp4RwF",
    "date": 1542764176,
    "default_source": null,
    "description": null,
    "discount": null,
    "due_date": null,
    "ending_balance": 0,
    "finalized_at": 1542764176,
    "forgiven": false,
    "hosted_invoice_url": "https://pay.stripe.com/invoice/invst_aqIp3rAKOfGvBgeaTNpGMO9iLj",
    "invoice_pdf": "https://pay.stripe.com/invoice/invst_aqIp3rAKOfGvBgeaTNpGMO9iLj/pdf",
    "lines": {
      "object": "list",
      "data": [
        {
          "id": "sli_f14f8ad4d41b05",
          "object": "line_item",
          "amount": 1600,
          "currency": "usd",
          "description": "1 × Weekly-IbcrdPR83CjRKx (at $16.00 / week)",
          "discountable": true,
          "livemode": false,
          "metadata": {
          },
          "period": {
            "end": 1543368976,
            "start": 1542764176
          },
          "plan": {
            "id": "Weekly-IbcrdPR83CjRKx",
            "object": "plan",
            "active": true,
            "aggregate_usage": null,
            "amount": 1600,
            "billing_scheme": "per_unit",
            "created": 1542764176,
            "currency": "usd",
            "interval": "week",
            "interval_count": 1,
            "livemode": false,
            "metadata": {
              "customer_name": "",
              "customer_email": "",
              "customer_phone": "",
              "billing_address_line": "",
              "billing_address_state": "",
              "billing_address_city": "",
              "billing_address_zip": "",
              "billing_address_country": "",
              "customer_notes": "testing notes"
            },
            "nickname": null,
            "product": "prod_E0mBKUu2oscBpr",
            "tiers": null,
            "tiers_mode": null,
            "transform_usage": null,
            "trial_period_days": null,
            "usage_type": "licensed"
          },
          "proration": false,
          "quantity": 1,
          "subscription": "sub_E0mBjQnLQj7Gb3",
          "subscription_item": "si_E0mBf46EMlWF3F",
          "type": "subscription"
        }
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/invoices/in_1DYkdY2Y54K5YG39D4uaDtnj/lines"
    },
    "livemode": false,
    "metadata": {
    },
    "next_payment_attempt": null,
    "number": "9CBEAF8-0029",
    "paid": true,
    "payment_intent": null,
    "period_end": 1542764176,
    "period_start": 1542764176,
    "receipt_number": null,
    "starting_balance": 0,
    "statement_descriptor": null,
    "status": "paid",
    "subscription": "sub_E0mBjQnLQj7Gb3",
    "subtotal": 1600,
    "tax": 0,
    "tax_percent": null,
    "total": 1600,
    "webhooks_delivered_at": null
  }
}

Now here is my php code to first retrieve the invoice, and then to query for a specific line:

** Note: in this case, $invoice is dynamic and is returning correctly.

$charge = \Stripe\Invoice::retrieve( $invoice );
$notes = $charge->lines->data->plan->metadata->customer_notes;

This is returning blank. I must be missing a level somewhere for the JSON query. Any help is appreciated.

UPDATE

As requested, here is a bit of the response for var_dump($charge). Stripe doesn't display the entire response, but this is a copy of the amount of info they do return:

object(Stripe\Invoice)#708 (44) { ["id"]=> string(27) "in_1DYl032Y54K5YG39yiSWVthU" ["object"]=> string(7) "invoice" ["amount_due"]=> int(1700) ["amount_paid"]=> int(1700) ["amount_remaining"]=> int(0) ["application_fee"]=> NULL ["attempt_count"]=> int(1) ["attempted"]=> bool(true) ["auto_advance"]=> bool(false) ["billing"]=> string(20) "charge_automatically" ["billing_reason"]=> string(19) "subscription_update" ["charge"]=> string(27) "ch_1DYl032Y54K5YG394aoxZfbd" ["closed"]=> bool(true) ["currency"]=> string(3) "usd" ["customer"]=> string(18) "cus_DnCcf39Nqp4RwF" ["date"]=> int(1542765571) ["default_source"]=> NULL ["description"]=> NULL ["discount"]=> NULL ["due_date"]=> NULL ["ending_balance"]=> int(0) ["finalized_at"]=> int(1542765571) ["forgiven"]=> bool(false) ["hosted_invoice_url"]=> string(63) "https://pay.stripe.com/invoice/invst_SahhlLyii55mPqzmEDMFJWJeLt" ["invoice_pdf"]=> string(67) "https://pay.st...
RiotAct
  • 743
  • 9
  • 33
  • Perhaps you could give us the output of `var_dump($charge);` after retrieving it? – Caleb H. Nov 21 '18 at 01:58
  • I guess you are using some kind of Stripe library? – bart Nov 21 '18 at 02:05
  • 2
    `data` is an array. You have to access the element of the array. `$charge->lines->data[0]->plan->metadata->customer_notes` – HPierce Nov 21 '18 at 02:08
  • Yes, I am using the Stripe library. – RiotAct Nov 21 '18 at 02:08
  • 2
    See the square bracket after data? That's where it's going wrong. "data": [ { – Difster Nov 21 '18 at 02:08
  • I think it should be `$notes = $charge->lines->data[0]->plan->metadata->customer_notes;` Try inserting your data into http://jsonviewer.stack.hu/. Also something strange is that no errors are appearing. Have you disabled errors? – Chris Happy Nov 21 '18 at 02:09
  • @ChrisHappy ding ding ding! we have a winner! Totally overlooked the array for data so now it works fine. Also, in my listener webhook, I am using an `if` statement, so thats why it isnt throwing errors. – RiotAct Nov 21 '18 at 02:13
  • @HPierce I don't care. I got some assistance, not sure if this will helpful to anyone else. Thanks for the fresh eyes! I knew it would be something simple. – RiotAct Nov 21 '18 at 02:15
  • Glad I was able to help @RiotAct! God bless :) – Chris Happy Nov 24 '18 at 00:10

0 Answers0