Home >PHP Framework >Laravel >Laravel development: How to use Laravel Cashier to implement subscription functionality?
Laravel开发:如何使用Laravel Cashier实现订阅功能?
Laravel Cashier是一个易于使用的Stripe付款处理包,可以帮助我们在Laravel应用程序中实现订阅功能。在此教程中,我们将学习如何使用Laravel Cashier实现订阅功能。
步骤1:安装Laravel Cashier
在我们开始使用Laravel Cashier之前,需要安装它。在Laravel项目中运行以下命令来安装Laravel Cashier:
composer require laravel/cashier
步骤2:配置Stripe凭据
Laravel Cashier需要您配置Stripe凭据才能正常工作。如果您还没有Stripe账户,可以在https://stripe.com/上注册一个新账户。然后,登录到Stripe Dashboard,点击API选项卡,复制您的私钥并将其添加到.env文件中。
STRIPE_SECRET_KEY=your_stripe_secret_key STRIPE_PUBLIC_KEY=your_stripe_public_key
步骤3:创建订阅计划
在我们实现订阅功能之前,我们需要在Stripe Dashboard中创建订阅计划。转到您的Stripe Dashboard并创建一个新的订阅计划。
在创建订阅计划时,请确保设置适当的金额,计费周期和试用期(如有需要)。
步骤4:创建Subscriber模型
Subscriber模型将继承Laravel Cashier的Billable Trait,并定义有关订阅的所有信息。通过执行以下命令在Laravel项目中创建Subscriber模型:
php artisan make:model Subscriber -m
此命令将创建一个Subscriber模型文件和一个迁移文件。
在迁移文件中,我们需要将Billable Trait添加到模型中。修改Subscriber模型迁移文件,如下所示:
<?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateSubscribersTable extends Migration { public function up() { Schema::create('subscribers', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->string('stripe_id')->nullable(); $table->string('card_brand')->nullable(); $table->string('card_last_four')->nullable(); $table->timestamp('trial_ends_at')->nullable(); $table->timestamps(); }); Schema::table('subscribers', function (Blueprint $table) { $table->softDeletes(); }); } public function down() { Schema::table('subscribers', function (Blueprint $table) { $table->dropSoftDeletes(); }); Schema::dropIfExists('subscribers'); } }
我们在Subscriber模型中添加了订阅所需的所有字段以及软删除支持。
步骤5:配置Laravel Cashier
在Subscriber模型中添加Laravel Cashier Trait,如下所示:
<?php namespace App; use IlluminateFoundationAuthUser as Authenticatable; use IlluminateNotificationsNotifiable; use IlluminateDatabaseEloquentSoftDeletes; use LaravelCashierBillable; class Subscriber extends Authenticatable { use Notifiable, SoftDeletes, Billable; protected $dates = ['deleted_at']; protected $fillable = [ 'name', 'email', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; public function getStripeKeyName() { return 'stripe_id'; } }
在上面的代码中,我们添加了Billable Trait并指定了Stripe中的“stripe_id”。
步骤6:实现订阅功能
现在,我们已经配置好了Laravel Cashier并创建了Subscriber模型,我们可以开始使用Laravel Cashier来实现订阅功能。
在我们开始之前,我们需要确保用户已登录。我们将使用Laravel的Authentication功能来处理此步骤。
在web.php中添加以下路由:
Route::group(['middleware' => ['auth']], function () { Route::get('/subscribe', 'SubscriptionController@index')->name('subscribe.index'); Route::post('/subscribe', 'SubscriptionController@store')->name('subscribe.store'); Route::get('/subscribe/cancel', 'SubscriptionController@cancel')->name('subscribe.cancel'); Route::get('/subscribe/resume', 'SubscriptionController@resume')->name('subscribe.resume'); Route::get('/subscribe/invoice', 'SubscriptionController@invoice')->name('subscribe.invoice'); });
现在,我们需要添加一个SubscriptionController来处理我们的订阅功能。使用以下命令创建SubscriptionController:
php artisan make:controller SubscriptionController
在SubscriptionController中添加index方法,
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use LaravelCashierExceptionsIncompletePayment; use Stripe{EphemeralKey, PaymentIntent, PaymentMethod}; class SubscriptionController extends Controller { public function index(Request $request) { $intent = $request->user()->createSetupIntent(); return view('subscription.index', [ 'intent' => $intent, ]); } }
在上面的代码中,我们创建了一个Setup Intent用于收集用户的付款信息。然后,我们将此Setup Intent传递到视图中,并在用于订阅的表单中使用它。
在订阅表单中,我们可以使用Stripe.js来收集用户的付款信息。以下是一个基本的订阅表单示例:
@extends('layouts.app') @section('content') {!! Form::open(['route' => 'subscribe.store', 'class' => 'form-horizontal']) !!} <div class="form-group row"> <label for="name">{{ __('Plan') }}</label> <select name="plan" id="plan" class="form-control"> <option value="price_123456">Basic Plan - $20/month</option> <option value="price_123457">Standard Plan - $50/month</option> <option value="price_123458">Premium Plan - $100/month</option> </select> <div id="card-element"></div> </div> <div class="form-group row"> <div id="card-errors" class="mx-auto" role="alert"></div> </div> <div class="form-group row mb-0"> <button type="submit" class="btn btn-primary"> {{ __('Subscribe') }} </button> </div> {!! Form::close() !!} <script src="https://js.stripe.com/v3/"></script> <script> let stripe = Stripe('{{ env('STRIPE_PUBLIC_KEY') }}'); let elements = stripe.elements(); let card = elements.create('card', { hidePostalCode: true, style: { base: { iconColor: '#666EE8', color: '#31325F', lineHeight: '40px', fontWeight: 300, fontFamily: '"Helvetica Neue", Helvetica, sans-serif', fontSize: '15px', '::placeholder': { color: '#CFD7E0', }, }, }, } ); card.mount('#card-element'); let cardErrors = document.getElementById('card-errors'); card.addEventListener('change', function(event) { if (event.error) { cardErrors.textContent = event.error.message; } else { cardErrors.textContent = ''; } }); let submitButton = document.querySelector('button[type=submit]'); let form = document.querySelector('form'); let clientSecret = null; submitButton.addEventListener('click', async function(ev) { ev.preventDefault(); submitButton.disabled = true; let {setupIntent, error} = await stripe.confirmCardSetup( clientSecret, { payment_method: { card: card, } }); if (error) { cardErrors.textContent = error.message; submitButton.disabled = false; return; } let paymentMethodId = setupIntent.payment_method; axios.post('{{ route('subscribe.store') }}', { plan: document.querySelector('#plan').value, payment_method: paymentMethodId, _token: '{{ csrf_token() }}' }).then(function(response) { window.location.href = response.data.success_url; }).catch(function(error) { submitButton.disabled = false; }); }); (async () => { let {setupIntent} = await axios.get('/api/create-setup-intent', { headers: { 'X-CSRF-Token': document.head.querySelector('meta[name="csrf-token"]').content } }).then(response => response.data) clientSecret = setupIntent.client_secret; })(); </script> @endsection
在上面的代码中,我们使用了Stripe.js来构建一个简单的订阅表单。我们通过使用Stripe.js中的create()方法创建一个card元素,然后将其挂载到#card-element div中。这里我们还使用了axios来处理POST请求,其中包含订阅计划信息和付款信息。如果付款被成功处理,我们将重定向用户到成功页面。
现在,我们需要添加存储用户付款信息和创建订阅逻辑的存储库。
<?php namespace AppRepositories; use AppSubscriber; use IlluminateSupportFacadesConfig; use LaravelCashierExceptionsPaymentActionRequired; use LaravelCashierExceptionsPaymentFailure; use LaravelCashierSubscription; use StripeStripe; class SubscriberRepository { public function createFromRequest(array $data) { $subscriber = Subscriber::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); $subscriber->createAsStripeCustomer([ 'name' => $subscriber->name, 'email' => $subscriber->email, ]); return $subscriber; } public function subscribe(Subscriber $subscriber, string $plan, string $paymentMethod) { $subscriber->newSubscription('main', $plan)->create($paymentMethod, [ 'email' => $subscriber->email, ]); } }
在上面的代码中,我们创建了一个SubscriberRepository类,用于存储用户信息和创建订阅。在我们的createFromRequest()方法中,我们将创建一个新的Stripe Customer,然后将其相关信息存储到我们的Subscriber模型中。在subscribe()方法中,我们使用Laravel Cashier的newSubscription()函数来为用户创建新的订阅。
步骤7:处理订阅回调
在订阅回调中,我们需要更新用户订阅的当前状态。如果订阅被取消或期满,我们需要根据需要取消或恢复用户订阅。
在SubscriptionController中添加以下方法:
public function store(Request $request, SubscriberRepository $subscriberRepository) { $paymentMethod = $request->input('payment_method'); $plan = $request->input('plan'); $subscriberRepository->subscribe( $request->user(), $plan, $paymentMethod ); return response()->json([ 'success_url' => route('subscribe.invoice'), ]); } public function cancel(Request $request) { $request->user()->subscription('main')->cancel(); return redirect()->route('subscribe.index') ->with('success', 'Your subscription has been cancelled.'); } public function resume(Request $request) { $request->user()->subscription('main')->resume(); return redirect()->route('subscribe.index') ->with('success', 'Your subscription has been resumed.'); } public function invoice(Request $request) { $invoice = $request->user()->invoices()->latest()->first(); return view('subscription.invoice', [ 'invoice' => $invoice, ]); }
在上面的代码中,我们处理了创建新的订阅,取消/恢复用户订阅,并显示订阅的最新发票。
现在,我们已经成功地使用Laravel Cashier实现了订阅功能,并可以让用户创建和管理其订阅了!
The above is the detailed content of Laravel development: How to use Laravel Cashier to implement subscription functionality?. For more information, please follow other related articles on the PHP Chinese website!