Table of Contents
1、简介
1.1 安装&配置
2、订阅实现
2.1 创建订阅
2.2 检查订阅状态
2.3 修改订阅
2.4 订阅数量
2.5 订阅税金
2.6 取消订阅
2.7 恢复订阅
3、处理 StripeWebhook
3.1 订阅失败处理
3.2 其它Webhooks
4、一次性付款
5、发票
生成PDF发票
Home Backend Development PHP Tutorial [ Laravel 5.2 文档 ] 服务 -- 订阅支付实现:Laravel Cashier

[ Laravel 5.2 文档 ] 服务 -- 订阅支付实现:Laravel Cashier

Jun 20, 2016 pm 12:38 PM

1、简介

LaravelCashier 为通过  Stripe实现订阅支付服务提供了一个优雅平滑的接口。它封装了几乎所有你恐惧编写的样板化的订阅支付代码。除了基本的订阅管理外,Cashier还支持处理优惠券、订阅升级/替换、订阅“数量”、取消宽限期,甚至生成PDF 发票。

1.1 安装&配置

Composer

首先,添加 Cashier 包到 composer.json文件并运行 composer update命令:

"laravel/cashier": "~6.0"
Copy after login

服务提供者

接下来,在 config/app.php配置文件中注册服务提供者: Laravel\Cashier\CashierServiceProvider。

迁移

使用 Cashier 之前,我们需要准备好数据库。我们需要添加一个字段到 users表,还要创建新的 subscriptions表来处理所有用户订阅:

Schema::table('users', function ($table) {    $table->string('stripe_id')->nullable();    $table->string('card_brand')->nullable();    $table->string('card_last_four')->nullable();});Schema::create('subscriptions', function ($table) {    $table->increments('id');    $table->integer('user_id');    $table->string('name');    $table->string('stripe_id');    $table->string('stripe_plan');    $table->integer('quantity');    $table->timestamp('trial_ends_at')->nullable();    $table->timestamp('ends_at')->nullable();    $table->timestamps();});
Copy after login

创建好迁移后,只需简单运行 migrate命令,相应修改就会更新到数据库。

设置模型

接下来,添加 Billabletrait 到 User模型类:

use Laravel\Cashier\Billable;class User extends Authenticatable{    use Billable;}
Copy after login

Stripe键

最后,在配置文件 config/services.php中设置 Stripe 键:

'stripe' => [    'model'  => 'User',    'secret' => env('STRIPE_API_SECRET'),],
Copy after login

2、订阅实现

2.1 创建订阅

要创建一个订阅,首先要获取一个账单模型的实例,通常是 App\User的实例。获取到该模型实例之后,你可以使用 newSubscription方法来创建该模型的订阅:

$user = User::find(1);$user->newSubscription('main', 'monthly')->create($creditCardToken);
Copy after login

第一个传递给 newSubscription方法的参数是该订阅的名字,如果应用只有一个订阅,可以将其称作 main或 primary,第二个参数用于指定用户订阅的 Stripe计划,该值对应 Stripe 中相应计划的 id。

create方法会自动创建这个 Stripe 订阅,同时更新数据库中 Stripe 的客户 ID(即 users表中的 stripe_id)和其它相关的账单信息。如果你的订阅计划有试用期,试用期结束时间也会自动被设置到数据库相应字段。

额外的用户信息

如果你想要指定额外的客户信息,你可以将其作为第二个参数传递给 create方法:

$user->newSubscription('main', 'monthly')->create($creditCardToken, [    'email' => $email,     'description' => 'Our First Customer']);
Copy after login

要了解更多 Stripe 支持的字段,可以查看 Stripe 关于 创建消费者的文档。

优惠券

如果你想要在创建订阅的时候使用优惠券,可以使用 withCoupon方法:

$user->newSubscription('main', 'monthly')     ->withCoupon('code')     ->create($creditCardToken);
Copy after login

2.2 检查订阅状态

用户订阅你的应用后,你可以使用各种便利的方法来简单检查订阅状态。首先,如果用户有一个有效的订阅,则 subscribed方法返回 true,即使订阅现在出于试用期:

if ($user->subscribed('main')) {    //}
Copy after login

subscribed方法还可以用于路由中间件,基于用户订阅状态允许你对路由和控制器的访问进行过滤:

public function handle($request, Closure $next){    if ($request->user() && ! $request->user()->subscribed('main')) {        // This user is not a paying customer...        return redirect('billing');    }    return $next($request);}
Copy after login

如果你想要判断一个用户是否还在试用期,可以使用 onTrial方法,该方法在为还处于试用期的用户显示警告信息很有用:

if ($user->->subscription('main')->onTrial()) {    //}
Copy after login

onPlan方法可用于判断用户是否基于 Stripe ID 订阅了给定的计划:

if ($user->onPlan('monthly')) {    //}
Copy after login

已取消的订阅状态

要判断用户是否曾经是有效的订阅者,但现在取消了订阅,可以使用 cancelled方法:

if ($user->subscription('main')->cancelled()) {    //}
Copy after login

你还可以判断用户是否曾经取消过订阅,但现在仍然在“宽限期”直到完全失效。例如,如果一个用户在3月5号取消了一个实际有效期到3月10号的订阅,该用户处于“宽限期”直到3月10号。注意 subscribed方法在此期间仍然返回 true。

if ($user->subscription('main')->onGracePeriod()) {    //}
Copy after login
Copy after login

2.3 修改订阅

用户订阅应用后,偶尔想要改变到新的订阅计划,要将用户切换到新的订阅,使用 swap方法。例如,我们可以轻松切换用户到 premium订阅:

$user = App\User::find(1);$user->subscription('main')->swap('stripe-plan-id');
Copy after login

如果用户在试用,试用期将会被维护。还有,如果订阅存在数量,数量也可以被维护。切换订阅计划后,

可以使用 invoice方法立即给用户开发票:

$user->subscription('main')->swap('stripe-plan-id');$user->invoice();
Copy after login

2.4 订阅数量

有时候订阅也会被数量影响,例如,应用中每个账户每月需要付费$10,要简单增加或减少订阅数量,使用 incrementQuantity和 decrementQuantity方法:

$user = User::find(1);$user->subscription('main')->incrementQuantity();// Add five to the subscription's current quantity...$user->subscription('main')->incrementQuantity(5);$user->subscription('main')->decrementQuantity();// Subtract five to the subscription's current quantity...$user->subscription('main')->decrementQuantity(5);
Copy after login

你也可以使用 updateQuantity方法指定数量:

$user->subscription('main')->updateQuantity(10);
Copy after login

想要了解更多订阅数量信息,查阅相关 Stripe文档。

2.5 订阅税金

在 Cashier 中,提供 tax_percent值发送给 Stripe 很简单。要指定用户支付订阅的税率,实现账单模型的 getTaxPercent方法,并返回一个在0到100之间的数值,不要超过两位小数:

public function getTaxPercent() {    return 20;}
Copy after login

这将使你可以在模型基础上使用税率,对跨越不同国家的用户很有用。

2.6 取消订阅

要取消订阅,可以调用用户订阅上的 cancel方法:

$user->subscription('main')->cancel();
Copy after login

当订阅被取消时,Cashier 将会自动设置数据库中的 subscription_ends_at字段。该字段用于了解 subscribed方法什么时候开始返回 false。例如,如果客户3月1号份取消订阅,但订阅直到3月5号才会结束,那么 subscribed方法继续返回 true直到3月5号。

你可以使用 onGracePeriod方法判断用户是否已经取消订阅但仍然在“宽限期”:

if ($user->subscription('main')->onGracePeriod()) {    //}
Copy after login
Copy after login

2.7 恢复订阅

如果用户已经取消订阅但想要恢复该订阅,可以使用 resume方法,前提是该用户必须在宽限期内:

$user->subscription('main')->resume();
Copy after login

如果该用户取消了一个订阅然后在订阅失效之前恢复了这个订阅,则不会立即支付该账单,取而代之的,他们的订阅只是被重新激活,并回到正常的支付周期。

3、处理 StripeWebhook

3.1 订阅失败处理

如果客户的信用卡失效怎么办?不用担心—— Cashier 自带了 Webhook 控制器,该控制器可以很方便地为你取消客户订阅。只需要定义如下控制器路由:

Route::post('stripe/webhook', 'Laravel\Cashier\WebhookController@handleWebhook');
Copy after login

就是这样!失败的支付将会被该控制器捕获和处理。当 Stripe 判断订阅失败(正常情况下尝试支付失败三次后)时该控制器将会取消客户的订阅。不要忘了:你需要在 Stripe 控制面板设置中配置相应的 webhook URI,否则不能正常工作。

由于 Stripe webhooks 需要通过 Laravel 的CSRF验证,所以我们将该 URI 置于 VerifyCsrfToken中间件排除列表中:

protected $except = [    'stripe/*',];
Copy after login

3.2 其它Webhooks

如果你有额外想要处理的 Stripe webhook 事件,只需简单继承 Webhook 控制器, 你的方法名应该和 Cashier 期望的约定一致,尤其是方法应该以“handle”开头并以驼峰命名法命名。例如,如果你想要处理 invoice.payment_succeededwebhook,你应该添加 handleInvoicePaymentSucceeded方法到控制器:

<?phpnamespace App\Http\Controller;use Laravel\Cashier\WebhookController as BaseController;class WebhookController extends BaseController{    /**     * 处理 stripe webhook.     *     * @param  array  $payload     * @return Response     */    public function handleInvoicePaymentSucceeded($payload)    {        // 处理该事件    }}
Copy after login

4、一次性付款

如果你想要使用订阅客户的信用卡一次性结清账单,可以使用账单模型实例上的 charge方法,该方法接收付款金额(应用使用的货币的最小单位对应的金额数值)作为参数,例如,下面的例子使用信用卡支付100美分,或1美元:

$user->charge(100);
Copy after login

charge方法接收一个数组作为第二个参数,允许你传递任何你想要传递的底层 Stripe 账单创建参数:

$user->charge(100, [    'source' => $token,    'receipt_email' => $user->email,]);
Copy after login

如果支付失败 charge方法将返回 false,这通常表明付款被拒绝:

if ( ! $user->charge(100)) {    // The charge was denied...}
Copy after login

如果支付成功,该方法将会返回一个完整的 Stripe 响应。

5、发票

你可以使用 invoices方法轻松获取账单模型的发票数组:

$invoices = $user->invoices();
Copy after login

当列出客户发票时,你可以使用发票的辅助函数来显示相关的发票信息。例如,你可能想要在表格中列出每张发票,从而方便用户下载它们:

<table>    @foreach ($invoices as $invoice)        <tr>            <td>{{ $invoice->dateString() }}</td>            <td>{{ $invoice->dollars() }}</td>            <td><a href="/user/invoice/{{ $invoice->id }}">Download</a></td>        </tr>    @endforeach</table>
Copy after login

生成PDF发票

在生成PDF分票之前,需要安装 PHP 库 dompdf:

composer require dompdf/dompdf
Copy after login

在路由或控制器中,使用 downloadInvoice方法生成发票的 PDF 下载,该方法将会自动生成相应的 HTTP 响应发送下载到浏览器:

Route::get('user/invoice/{invoice}', function ($invoiceId) {    return Auth::user()->downloadInvoice($invoiceId, [        'vendor'  => 'Your Company',        'product' => 'Your Product',    ]);});
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles