使用 laravel Cashier 从 stripe 检索产品价格

问题描述 投票:0回答:3

假设我在 stripe 上创建了一个产品,其 API ID“price_1IYwYtA4vM4p6MlHCuiAZx9X”的成本为 25.00USD

那么我们如何使用 Laravel Cashier 使用 API id 检索该产品的费用?

laravel laravel-5 stripe-payments laravel-cashier
3个回答
4
投票

正如 @Nolan H 已经解释的那样,这是两个不同的实体,您必须进行两个 api 调用才能将项目合并在一起。

因此,记住这一点,您可以自己用一些小技巧来做到这一点。
您可以在下面找到我的解决方案,我是如何做到的。我有不同付款间隔和价格产品。 稍后在前端,我向用户显示层级,并可以使用 Price_id 继续付款流程。

解决方案


    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
        ]);
    }

完整回复

这里有趣的部分是带有我们自己架构的 items 键。
您可以将键更改为产品名称而不是 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"
    }
}

3
投票

您正在混淆产品和价格,这是两个独立的概念/对象。产品代表您所销售的商品或服务的描述。价格决定了您为特定产品支付的金额和频率。每个价格都引用一个产品,但同一产品可以有多个价格。

虽然似乎不支持直接通过收银台检索产品和价格,但您始终可以使用 Stripe API /

stripe-php
执行此操作,如下所示。

您可以使用 API 检索给定

product
的价格,并通过过滤器列出它们(API 参考):

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

您可以通过检索给定 Price 对象并查看

unit_amount
currency
recurring.interval
API 参考)来查看该对象的价格设置:

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

0
投票

您可以在

views
中显示当前发出请求和显示请求的方法。

© www.soinside.com 2019 - 2024. All rights reserved.