Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get values from a Stripe\Collection JSON output

I’m working with Stripe in a PHP app. I’ve created a product and set prices for it via code. I added 'lookup_key' => value to each price. That all worked. When I use stripes lookup_keys command to get prices by that key, I do not know how to parse the output returned.

Example price:

$stripe->prices->create([
   'product' => $product['id'],
   'unit_amount' => 35000,
   'currency' => 'usd',
   'lookup_key' => 'double'
 ]);

To get the price for the lookup_key => double, I use:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$double = $stripe->prices->all(['lookup_keys' => ['double'] ]);

var_dump($double );

The output for that is:

Stripe\Collection JSON: {
  "object": "list",
  "data": [
    {
      "id": "price_1OeHBECDQQBHRZz868I26QUy",
      "object": "price",
      "active": true,
      "billing_scheme": "per_unit",
      "created": 1706620608,
      "currency": "usd",
      "custom_unit_amount": null,
      "livemode": false,
      "lookup_key": "double",
      "metadata": [],
      "nickname": null,
      "product": "prod_PTDcNZpXM6NJMd",
      "recurring": null,
      "tax_behavior": "unspecified",
      "tiers_mode": null,
      "transform_quantity": null,
      "type": "one_time",
      "unit_amount": 35000,
      "unit_amount_decimal": "35000"
    }
  ],
  "has_more": false,
  "url": "/v1/prices"
}" 

How do I parse this to get the "unit_amount": 35000, or other items? OR, is there an easier way to get a price or prices from a product.

Thanks for any assistance.

>Solution :

$unitAmount = $double->data[0]->unit_amount;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading