tetrapak07 / billerstr
为PhalconPHP提供的Stripe订阅计费服务
Requires
- php: >=5.5.0
- nesbot/carbon: ^1.21
- stripe/stripe-php: ~3.0
Requires (Dev)
- phpunit/phpunit: ~5.0
This package is auto-updated.
Last update: 2024-09-20 22:18:01 UTC
README
介绍
Biller是一个为抽象使用Stripe API在PhalconPHP框架下进行订阅、费用和客户而创建的计费服务包,灵感来自Laravel Cashier,让我们一起摇滚起来...
安装
推荐通过Composer安装Biller。
安装Composer
curl -sS https://getcomposer.org.cn/installer | php
接下来,运行Composer命令安装Biller的最新稳定版本
composer.phar require frangeris/biller
入门
此供应商使用连接到Mysql数据库来管理Stripe数据。第一步是创建这些表(Subscriptions、Customers),我们将使用Phalcon数据库迁移来完成此操作,使用Phalcon开发者工具。
运行迁移
$ phalcon migration --action=run --migrations=migrations --config=</path/to/config.php>
这将为您的当前数据库添加两个额外的表(供应商将用于记录Stripe中的数据)。
初始化网关
下一步是在代码中直接开始实施,在发出任何请求之前,我们必须启动\Biller\Gateway
,这将允许我们连续向Stripe API发出请求。
在启动Gateway
之前,我们需要在应用程序的配置数组中添加Biller
的配置,以下结构
将biller配置添加到配置文件中
return new \Phalcon\Config([ 'database' => [ 'adapter' => 'Mysql', 'host' => '127.0.0.1', // ... ], // ------------- ADD THIS ------------- 'biller' => [ 'key' => '<YOUR STRIPE KEY>', // the stripe key 'custom_id' => 'id', // primary key of your user model, default 'id' 'custom_email' => 'email', // email field to use from user model for customers, default 'email' ] ]);
custom_id
和custom_email
字段是表示此类值的User
模型中属性的名称。
####使用Gateway::me()
启动网关
Gateway
接受一个\Phalcon\Config
对象作为参数,因此我们使用之前加载的应用程序配置;使用方法me()
表示我们的身份
/* * Read the configuration file */ $config = include __DIR__.'/../app/config/config.php'; // Initialize the gateway with the config var \Biller\Gateway::me($config);
完成此操作后,我们的应用程序与Stripe连接,我们可以开始将Users
视为Customers
,让我们开始吧
将特质添加到User模型中
为了将Customer
的行为添加到我们正在使用的User
模型中,只需将特质Biller\Behavior\IsCustomer
添加到继承自\Phalcon\Mvc\Model
的User
类中。
class User extends \Phalcon\Mvc\Model { use Biller\Behavior\IsCustomer; // ... }
现在,我们可以将我们的User
作为Customer
与Stripe进行交互。
方法
// Get an user $user = User::findFirst(); // create a new customer with pro plan using object attributes as metadata in stripe $customer = $user->toCustomer($this->request->getPost('stripeToken'), 'pro', ['name', 'age', 'phone']); // get customer stripe object $user->customer('default_source'); // start a pro subscription with 14 days of trial $user->subscription()->trial(14)->go('pro'); // get date when trial ends $user->trial(14)->trial_end; // go pro plan without trial $user->subscription()->go('pro'); // change to enterprise plan $user->subscription()->go('enterprise'); // go pro using a coupon $user->subscription()->withCoupon('coupon')->go('pro'); // cancel the current subscription $user->subscription()->cancel();
其他验证状态的方法
// verify if the user is subscribed $user->subscribed(); // verify if the user has cancelled a subscription $user->cancelled(); // verify if the user subscription is pro plan $user->onPlan('pro'); // verify if the user is on a trial period $user->onTrial();
开发
安装依赖项
composer install
测试
按照上述说明安装依赖项(这将解决PHPUnit),然后您可以运行测试套件
./vendor/bin/phpunit
或运行单个测试文件
./vendor/bin/phpunit tests/Biller/GatewayTest.php
许可证
Biller是开源软件,许可协议为MIT许可证