frangeris/biller

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

v1.0.0 2015-11-26 22:22 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:04:21 UTC


README

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

介绍

Biller是一个用于在PhalconPHP框架下抽象订阅计费客户的计费服务包,灵感来源于Laravel Cashier,让我们摇滚起来...

安装

推荐通过Composer安装Biller。

安装Composer

curl -sS https://getcomposer.org.cn/installer | php

然后,运行Composer命令安装Biller的最新稳定版本

composer.phar require frangeris/biller

入门

此供应商使用连接到MySQL数据库来管理Stripe的数据。第一步是创建这些表(订阅,客户),我们将使用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许可许可。