Laravel Cashier安裝配置篇教程詳解

來源:互聯網
上載者:User

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相關的欄位:

修改User模型

此外還需要修改User模型類如下:

<?php

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:

這裡我們使用使用Test Publishable Key作為設定檔中stripe選項的key,Test Secret Key作為stripe選項的secret。當然我們需要將相應值配置到.env中。

本節我們先講到這裡,下一節我們將重點探討訂購相關實現。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.