Retrieve the price for Product from stripe using laravel Cashier

Viewed 2373

Suppose i have created a product on stripe with API ID "price_1IYwYtA4vM4p6MlHCuiAZx9X" of cost $25.00USD

So how can we retrieve the cost of this product using API id using laravel cashier?

2 Answers

You are conflating the Product and the Price, which are two separate concepts/objects. A Product represents a description of the good or service you are selling. A Price determines how much and how often you will pay for a given Product. Each Price references a single Product, but you can have many Prices for the same Product.

While it does not appear that retrieve products & prices is support directly via Cashier, you can always do this with the Stripe API / stripe-php as below.

You can retrieve Prices for a given product using the API to list them with a filter (API ref):

$stripe = new \Stripe\StripeClient(
  'sk_test_123'
);
$stripe->prices->all(['product' => `prod_123`]);

And you can view the price set for a given Price object by retrieving it and looking at the unit_amount, currency and recurring.interval (API ref):

price = $stripe->prices->retrieve('price_456', []);

As @Nolan H has explained already thoose are two different Entities and you have to make two api calls to merge the items together.

So having this in mind you can do this with a little trick on your own.
Below you can find my solution how I did it. I have products with different payment interval and prices. In the frontend later I display the tiers to the user and can continue the payment process with the price_id.

The Solution


    public function products(){
        $stripe = Cashier::stripe();
        $products = $stripe->products->all(); // here you get all products
        $prices   = $stripe->prices->all();   // here you get all prices

        $items = []; // init empty array 
        $pkgs = (new PackageController)->getPackages(); // I have my own conditions saved here in my Controller ( see below ) 

        // Loop though each product and create your own schema
        foreach($products->data as $product):
          $key = $product->id;
          $tier = strtolower($product->name);
          $items[$key] = [];
          $items[$key]['id']   = $product->id;
          $items[$key]['tier'] = $tier;
          $items[$key]['name'] = $product->name;
          $items[$key]['metakey'] = $product->metadata;

          // I have only month and year prices, so I prefill them here
          $items[$key]['month'] = ['id' => null, 'price' => null];
          $items[$key]['year']  = ['id' => null, 'price' => null];

          // here I am binding my conditions from my package to the product (nothing to do with stripe, but my app, like having limits on stuff x)
          if(array_key_exists($tier, $pkgs))
            $items[$key]['condition'] = $pkgs[$tier];
          else
            $items[$key]['condition'] = [];

        endforeach;

        // now we need to add the existing prices for the products
        foreach($prices->data as $price):
          if($price->active == false) continue; // skip all archived prices 

          $key = $price->product;
          $items[$key][$price->recurring->interval]['id'] = $price->id;
          $items[$key][$price->recurring->interval]['price'] = $price->unit_amount;
        endforeach;


        return response()->json([
            'items' => $items,          // I work with my products <> price bindings here
            'products'  => $products,   // stripe response for products
            'prices'    => $prices,     // stripe response for prices
        ]);
    }

The full response

The interesting part here is the items key with our own schema.
You can change the keys to be the product name instead of the prod_id.

{
    "items": {
        "prod_L7Ay2QFGIFGWd7": {
            "id": "prod_L7Ay2QFGIFGWd7",
            "tier": "business",
            "name": "Business",
            "metakey": [],
            "month": {
                "id": "price_1KQwc6Ha5WPIKK5IM2tdjzCY",
                "price": 2490
            },
            "year": {
                "id": "price_1KQwc6Ha5WPIKK5I27jjkNd8",
                "price": 24900
            },
            "condition": {
                "users": "unlimited",
                "objects": "unlimited",
                "customer_management": true,
                "object_management": true,
                "employee_management": true,
                "task_management": true,
                "chat": true,
                "check_in": true,
                "storage": "individual",
                "task_extension": true,
                "multilanguage": true,
                "support_default": true,
                "support_extended": true,
                "emergency": true,
                "formular_documents": true,
                "time_tracking": true,
                "calendar": true,
                "location_tracking": true,
                "warehouse_management": true
            }
        },
        "prod_L7AxRKWZcChK9F": {
            "id": "prod_L7AxRKWZcChK9F",
            "tier": "premium",
            "name": "Premium",
            "metakey": [],
            "month": {
                "id": "price_1KnjlbHa5WPIKK5IjVleb0hR",
                "price": 2490
            },
            "year": {
                "id": "price_1KnjlzHa5WPIKK5InPCHJHZ1",
                "price": 26880
            },
            "condition": {
                "users": 15,
                "objects": "unlimited",
                "customer_management": true,
                "object_management": true,
                "employee_management": true,
                "task_management": true,
                "chat": true,
                "check_in": true,
                "storage": "15 GB",
                "task_extension": true,
                "multilanguage": true,
                "support_default": true,
                "support_extended": true,
                "emergency": true,
                "formular_documents": true,
                "time_tracking": true,
                "calendar": "optional",
                "location_tracking": "optional",
                "warehouse_management": "optional"
            }
        },
        "prod_L6ugqTDvGjaptE": {
            "id": "prod_L6ugqTDvGjaptE",
            "tier": "startup",
            "name": "StartUp",
            "metakey": {
                "popular": "true"
            },
            "month": {
                "id": "price_1KnjZAHa5WPIKK5I1MgPKICU",
                "price": 1749
            },
            "year": {
                "id": "price_1KnjZsHa5WPIKK5IKzYgCusH",
                "price": 18900
            },
            "condition": {
                "users": 5,
                "objects": 10,
                "customer_management": true,
                "object_management": true,
                "employee_management": true,
                "task_management": true,
                "chat": true,
                "check_in": true,
                "storage": "5 GB",
                "task_extension": true,
                "multilanguage": true,
                "support_default": true,
                "support_extended": true,
                "emergency": true,
                "formular_documents": "optional",
                "time_tracking": "optional",
                "calendar": "optional",
                "location_tracking": "optional",
                "warehouse_management": "optional"
            }
        },
        "prod_L6odxlRhol6BLN": {
            "id": "prod_L6odxlRhol6BLN",
            "tier": "basic",
            "name": "Basic",
            "metakey": [],
            "month": {
                "id": "price_1KnkAGHa5WPIKK5IhewMTNFP",
                "price": 0
            },
            "year": {
                "id": "price_1KnkAPHa5WPIKK5IWvZgqD66",
                "price": 0
            },
            "condition": {
                "users": 1,
                "objects": 1,
                "customer_management": true,
                "object_management": true,
                "employee_management": true,
                "task_management": true,
                "chat": true,
                "check_in": true,
                "storage": "100 MB",
                "task_extension": true,
                "multilanguage": true,
                "support_default": false,
                "support_extended": false,
                "emergency": false,
                "formular_documents": false,
                "time_tracking": false,
                "calendar": false,
                "location_tracking": false,
                "warehouse_management": false
            }
        }
    },
    "products": {
        "object": "list",
        "data": [
            {
                "id": "prod_L7Ay2QFGIFGWd7",
                "object": "product",
                "active": true,
                "attributes": [],
                "created": 1644337942,
                "description": null,
                "images": [],
                "livemode": false,
                "metadata": [],
                "name": "Business",
                "package_dimensions": null,
                "shippable": null,
                "statement_descriptor": null,
                "tax_code": "txcd_10103000",
                "type": "service",
                "unit_label": null,
                "updated": 1644337942,
                "url": null
            },
            {
                "id": "prod_L7AxRKWZcChK9F",
                "object": "product",
                "active": true,
                "attributes": [],
                "created": 1644337883,
                "description": null,
                "images": [],
                "livemode": false,
                "metadata": [],
                "name": "Premium",
                "package_dimensions": null,
                "shippable": null,
                "statement_descriptor": null,
                "tax_code": "txcd_10103000",
                "type": "service",
                "unit_label": null,
                "updated": 1649770127,
                "url": null
            },
            {
                "id": "prod_L6ugqTDvGjaptE",
                "object": "product",
                "active": true,
                "attributes": [],
                "created": 1644277375,
                "description": null,
                "images": [],
                "livemode": false,
                "metadata": {
                    "popular": "true"
                },
                "name": "StartUp",
                "package_dimensions": null,
                "shippable": null,
                "statement_descriptor": null,
                "tax_code": "txcd_10103000",
                "type": "service",
                "unit_label": null,
                "updated": 1649769377,
                "url": null
            },
            {
                "id": "prod_L6odxlRhol6BLN",
                "object": "product",
                "active": true,
                "attributes": [],
                "created": 1644254861,
                "description": null,
                "images": [],
                "livemode": false,
                "metadata": [],
                "name": "Basic",
                "package_dimensions": null,
                "shippable": null,
                "statement_descriptor": null,
                "tax_code": null,
                "type": "service",
                "unit_label": null,
                "updated": 1649771641,
                "url": null
            }
        ],
        "has_more": false,
        "url": "/v1/products"
    },
    "prices": {
        "object": "list",
        "data": [
            {
                "id": "price_1KnkAPHa5WPIKK5IWvZgqD66",
                "object": "price",
                "active": true,
                "billing_scheme": "per_unit",
                "created": 1649771641,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L6odxlRhol6BLN",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "year",
                    "interval_count": 1,
                    "trial_period_days": null,
                    "usage_type": "licensed"
                },
                "tax_behavior": "inclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 0,
                "unit_amount_decimal": "0"
            },
            {
                "id": "price_1KnkAGHa5WPIKK5IhewMTNFP",
                "object": "price",
                "active": true,
                "billing_scheme": "per_unit",
                "created": 1649771632,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L6odxlRhol6BLN",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "month",
                    "interval_count": 1,
                    "trial_period_days": null,
                    "usage_type": "licensed"
                },
                "tax_behavior": "inclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 0,
                "unit_amount_decimal": "0"
            },
            {
                "id": "price_1KnjlzHa5WPIKK5InPCHJHZ1",
                "object": "price",
                "active": true,
                "billing_scheme": "per_unit",
                "created": 1649770127,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L7AxRKWZcChK9F",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "year",
                    "interval_count": 1,
                    "trial_period_days": null,
                    "usage_type": "licensed"
                },
                "tax_behavior": "exclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 26880,
                "unit_amount_decimal": "26880"
            },
            {
                "id": "price_1KnjlbHa5WPIKK5IjVleb0hR",
                "object": "price",
                "active": true,
                "billing_scheme": "per_unit",
                "created": 1649770103,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L7AxRKWZcChK9F",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "month",
                    "interval_count": 1,
                    "trial_period_days": null,
                    "usage_type": "licensed"
                },
                "tax_behavior": "exclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 2490,
                "unit_amount_decimal": "2490"
            },
            {
                "id": "price_1KnjZsHa5WPIKK5IKzYgCusH",
                "object": "price",
                "active": true,
                "billing_scheme": "per_unit",
                "created": 1649769376,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L6ugqTDvGjaptE",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "year",
                    "interval_count": 1,
                    "trial_period_days": null,
                    "usage_type": "licensed"
                },
                "tax_behavior": "exclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 18900,
                "unit_amount_decimal": "18900"
            },
            {
                "id": "price_1KnjZAHa5WPIKK5I1MgPKICU",
                "object": "price",
                "active": true,
                "billing_scheme": "per_unit",
                "created": 1649769332,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L6ugqTDvGjaptE",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "month",
                    "interval_count": 1,
                    "trial_period_days": null,
                    "usage_type": "licensed"
                },
                "tax_behavior": "exclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 1749,
                "unit_amount_decimal": "1749"
            },
            {
                "id": "price_1KQwc6Ha5WPIKK5I27jjkNd8",
                "object": "price",
                "active": true,
                "billing_scheme": "per_unit",
                "created": 1644337942,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L7Ay2QFGIFGWd7",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "year",
                    "interval_count": 1,
                    "trial_period_days": 30,
                    "usage_type": "licensed"
                },
                "tax_behavior": "inclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 24900,
                "unit_amount_decimal": "24900"
            },
            {
                "id": "price_1KQwc6Ha5WPIKK5IM2tdjzCY",
                "object": "price",
                "active": true,
                "billing_scheme": "per_unit",
                "created": 1644337942,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L7Ay2QFGIFGWd7",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "month",
                    "interval_count": 1,
                    "trial_period_days": 30,
                    "usage_type": "licensed"
                },
                "tax_behavior": "inclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 2490,
                "unit_amount_decimal": "2490"
            },
            {
                "id": "price_1KQwb9Ha5WPIKK5IPXgHG5pc",
                "object": "price",
                "active": false,
                "billing_scheme": "per_unit",
                "created": 1644337883,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L7AxRKWZcChK9F",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "year",
                    "interval_count": 1,
                    "trial_period_days": 30,
                    "usage_type": "licensed"
                },
                "tax_behavior": "inclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 10900,
                "unit_amount_decimal": "10900"
            },
            {
                "id": "price_1KQwb9Ha5WPIKK5IJKkdL1LG",
                "object": "price",
                "active": false,
                "billing_scheme": "per_unit",
                "created": 1644337883,
                "currency": "eur",
                "livemode": false,
                "lookup_key": null,
                "metadata": [],
                "nickname": null,
                "product": "prod_L7AxRKWZcChK9F",
                "recurring": {
                    "aggregate_usage": null,
                    "interval": "month",
                    "interval_count": 1,
                    "trial_period_days": 30,
                    "usage_type": "licensed"
                },
                "tax_behavior": "inclusive",
                "tiers_mode": null,
                "transform_quantity": null,
                "type": "recurring",
                "unit_amount": 1090,
                "unit_amount_decimal": "1090"
            }
        ],
        "has_more": true,
        "url": "/v1/prices"
    }
}
Related