spackleso / spackle-php
Spackle 是将您的 PHP 应用程序与 Stripe 计费集成最简单的方法。有关详细信息,请参阅 https://www.spackle.so。
Requires
- stripe/stripe-php: ^10.12
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is not auto-updated.
Last update: 2024-09-19 17:56:43 UTC
README
Spackle PHP 库提供了对在 Spackle 平台上创建的计费感知标志的优化访问。
文档
请参阅 PHP API 文档。
设置
安装 Spackle 库
composer require spackleso/spackle-php
使用绑定时,使用 Composer 的自动加载
require_once('vendor/autoload.php');
配置您的环境
为了使用 Spackle,您需要在 Spackle
单例上配置您的密钥。您可以在 Spackle 应用程序的 设置页面 中找到您的密钥。
\Spackle\Spackle::setApiKey('<api key>');
使用方法
价格表
获取价格表
\Spackle\PricingTable::retrieve("abcde123");
价格表对象
{ id: string name: string intervals: string[] products: { id: string name: string description: string features: { id: string name: string key: string type: number value_flag: boolean value_limit: number | null }[] prices: { month?: { id: string unit_amount: number currency: string } year?: { id: string unit_amount: number currency: string } } }[] }
权限
获取客户
Spackle 使用 stripe ids 作为客户功能的引用。
$customer = \Spackle\Customer::retrieve("cus_000000000");
验证功能访问
$customer->enabled("feature_key");
获取功能限制
$customer->limit("feature_key");
检查客户的订阅
客户的当前订阅在 subscriptions
方法中可用。这些是定义在 Stripe PHP 库 中的有效 \Stripe\Subscription
对象。
$customer->subscriptions();
等待者
从 Stripe 中发生操作到它在 Spackle 中反映出来之间有一段短暂的延迟。为了解决这个问题,Spackle 提供了一个具有静态方法的 Waiters
类,可用于等待 Stripe 对象更新和复制。
- 等待创建客户
\Spackle\Waiters::waitForCustomer("cus_00000000");
- 等待创建订阅
\Spackle\Waiters::waitForSubscription("cus_000000000", "sub_00000000");
- 等待更新订阅
\Spackle\Waiters::waitForSubscription("cus_000000000", "sub_00000000", array("status" => "active"));
这些将阻塞,直到 Spackle 使用来自 Stripe 的最新信息更新,或者直到发生超时。
开发环境中的使用
在生产中,Spackle 需要一个有效的 Stripe 客户端。然而,在开发环境中,状态需要被控制,这并不是最佳选择。作为替代方案,您可以使用文件存储来使用种子数据测试您的应用程序。
/app/spackle.json { "cus_000000000": { "features": [ { "type": 0, "key": "flag_feature", "value_flag": true }, { "type": 1, "key": "limit_feature", "value_limit": 100 } ], "subscriptions": [ { "id": "sub_000000000", "status": "trialing", "quantity": 1 } ] } }
然后在您的应用程序中配置文件存储
\Spackle\Spackle::setStore(new \Spackle\Stores\FileStore("/app/spackle.json"));
测试环境中的使用
在生产中,Spackle 需要一个有效的 Stripe 客户端。然而,在测试或某些开发环境中,这并不理想。作为替代方案,您可以使用内存存储来使用种子数据测试您的应用程序。
\Spackle\Spackle::setStore(new \Spackle\Stores\MemoryStore()); \Spackle\Spackle::getStore()->set_customer_data("cus_000000000", array( "features" => array( array( "type" => 0, "key" => "flag_feature", "value_flag" => true ), array( "type" => 1, "key" => "limit_feature", "value_limit" => 100 ) ), "subscriptions" => array( array( "id" => "sub_000000000", "status" => "trialing", "quantity" => 1 ) ) );
注意:内存存储不是线程安全的,并且状态将在每次应用程序重启时重置。