[larveral]Laravel Cashier安装配置篇教程详解

时间:2020-09-22  来源:php安装  阅读:

0、开始之前

在开始之前我们先理清几个概念。

Stripe是一个为个人和公司提供在线支付解决方案的工具,支持Web和移动App。

Laravel Cashier为Stripe提供了相应接口以便在Laravel中实现订购支付功能,从而避免让我们编写重复的样板化的代码,真正专注于业务逻辑处理,提高编程效率。

那么,Laravel Cashier到底是怎样的一把利器,下面让我们一一揭晓。

1、安装配置

使用composer安装

我们使用Composer安装Laravel Cashier依赖包,首先将如下依赖添加到composer.json:

"laravel/cashier": "~5.0"

然后运行composer update。

安装完成后,注册服务提供者Laravel\Cashier\CashierServiceProvider::class到config/app.php。

新增数据表字段

我们还要对users表添加相关字段,这可以通过以下命令生成迁移文件:

php artisan cashier:table users

然后运行

php artisan migrate

执行成功后去查看users表,可见新增了与stripe相关的字段:

users表新增与Cashier相关字段

修改User模型

此外还需要修改User模型类如下:

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

use Laravel\Cashier\Billable;
use Laravel\Cashier\Contracts\Billable as BillableContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract,BillableContract
{
    use Authenticatable, CanResetPassword, Billable;

    protected $dates = ["trial_ends_at", "subscription_ends_at"];

    ... //其他代码

}

配置Stripe选项

最后还要去config/services.php中配置stripe选项。

"stripe" => [
    "model" => App\User::class,
    "key" => env("STRIPE_KEY"),
    "secret" => env("STRIPE_SECRET"),
],
model不用修改,key和secret需要我们去Stripe官网注册一个账户,然后查看账户设置,通过https://dashboard.stripe.com/account/apikeys获取API Keys:

注册Stripe获取API Keys

这里我们使用使用Test Publishable Key作为配置文件中stripe选项的key,Test Secret Key作为stripe选项的secret。当然我们需要将相应值配置到.env中。

本节我们先讲到这里,下一节我们将重点探讨订购相关实现。

[larveral]Laravel Cashier安装配置篇教程详解

http://m.bbyears.com/jiaocheng/100276.html

推荐访问:蜡染
相关阅读 猜你喜欢
本类排行 本类最新