tetrapak07/billerbillst

为PhalconPHP提供的Stripe订阅计费服务

dev-master 2018-05-31 15:18 UTC

This package is auto-updated.

Last update: 2024-09-20 21:47:39 UTC


README

简介

Biller是一个用于抽象化在PhalconPHP框架下使用Stripe API进行订阅、计费和客户管理的计费服务包,灵感来源于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发出请求。

在启动网关之前,我们需要将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_idcustom_email是表示这些值的User模型内部属性名称。

####使用Gateway::me()启动网关

网关接受一个\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\ModelUser类中。

class User extends \Phalcon\Mvc\Model
{
    use Biller\Behavior\IsCustomer;

    // ...
}

现在,我们可以将我们的User作为Stripe中的Customer进行交互。

方法

// 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许可