Home PHP Framework Laravel Laravel development: How to use Laravel Cashier to implement subscription functionality?

Laravel development: How to use Laravel Cashier to implement subscription functionality?

Jun 13, 2023 pm 01:34 PM
laravel Subscription function cashier

Laravel开发:如何使用Laravel Cashier实现订阅功能?

Laravel Cashier是一个易于使用的Stripe付款处理包,可以帮助我们在Laravel应用程序中实现订阅功能。在此教程中,我们将学习如何使用Laravel Cashier实现订阅功能。

步骤1:安装Laravel Cashier

在我们开始使用Laravel Cashier之前,需要安装它。在Laravel项目中运行以下命令来安装Laravel Cashier:

composer require laravel/cashier
Copy after login

步骤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
Copy after login

步骤3:创建订阅计划

在我们实现订阅功能之前,我们需要在Stripe Dashboard中创建订阅计划。转到您的Stripe Dashboard并创建一个新的订阅计划。

在创建订阅计划时,请确保设置适当的金额,计费周期和试用期(如有需要)。

步骤4:创建Subscriber模型

Subscriber模型将继承Laravel Cashier的Billable Trait,并定义有关订阅的所有信息。通过执行以下命令在Laravel项目中创建Subscriber模型:

php artisan make:model Subscriber -m
Copy after login

此命令将创建一个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');
    }
}
Copy after login

我们在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';
    }
}
Copy after login

在上面的代码中,我们添加了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');
});
Copy after login

现在,我们需要添加一个SubscriptionController来处理我们的订阅功能。使用以下命令创建SubscriptionController:

php artisan make:controller SubscriptionController
Copy after login

在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,
        ]);
    }
}
Copy after login

在上面的代码中,我们创建了一个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
Copy after login

在上面的代码中,我们使用了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,
        ]);
    }
}
Copy after login

在上面的代码中,我们创建了一个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,
    ]);
}
Copy after login

在上面的代码中,我们处理了创建新的订阅,取消/恢复用户订阅,并显示订阅的最新发票。

现在,我们已经成功地使用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!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP vs. Flutter: The best choice for mobile development PHP vs. Flutter: The best choice for mobile development May 06, 2024 pm 10:45 PM

PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

How to use object-relational mapping (ORM) in PHP to simplify database operations? How to use object-relational mapping (ORM) in PHP to simplify database operations? May 07, 2024 am 08:39 AM

Database operations in PHP are simplified using ORM, which maps objects into relational databases. EloquentORM in Laravel allows you to interact with the database using object-oriented syntax. You can use ORM by defining model classes, using Eloquent methods, or building a blog system in practice.

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

Analysis of the advantages and disadvantages of PHP unit testing tools Analysis of the advantages and disadvantages of PHP unit testing tools May 06, 2024 pm 10:51 PM

PHP unit testing tool analysis: PHPUnit: suitable for large projects, provides comprehensive functionality and is easy to install, but may be verbose and slow. PHPUnitWrapper: suitable for small projects, easy to use, optimized for Lumen/Laravel, but has limited functionality, does not provide code coverage analysis, and has limited community support.

Comparison of the latest versions of Laravel and CodeIgniter Comparison of the latest versions of Laravel and CodeIgniter Jun 05, 2024 pm 05:29 PM

The latest versions of Laravel 9 and CodeIgniter 4 provide updated features and improvements. Laravel9 adopts MVC architecture and provides functions such as database migration, authentication and template engine. CodeIgniter4 uses HMVC architecture to provide routing, ORM and caching. In terms of performance, Laravel9's service provider-based design pattern and CodeIgniter4's lightweight framework give it excellent performance. In practical applications, Laravel9 is suitable for complex projects that require flexibility and powerful functions, while CodeIgniter4 is suitable for rapid development and small applications.

How do the data processing capabilities in Laravel and CodeIgniter compare? How do the data processing capabilities in Laravel and CodeIgniter compare? Jun 01, 2024 pm 01:34 PM

Compare the data processing capabilities of Laravel and CodeIgniter: ORM: Laravel uses EloquentORM, which provides class-object relational mapping, while CodeIgniter uses ActiveRecord to represent the database model as a subclass of PHP classes. Query builder: Laravel has a flexible chained query API, while CodeIgniter’s query builder is simpler and array-based. Data validation: Laravel provides a Validator class that supports custom validation rules, while CodeIgniter has less built-in validation functions and requires manual coding of custom rules. Practical case: User registration example shows Lar

PHP code unit testing and integration testing PHP code unit testing and integration testing May 07, 2024 am 08:00 AM

PHP Unit and Integration Testing Guide Unit Testing: Focus on a single unit of code or function and use PHPUnit to create test case classes for verification. Integration testing: Pay attention to how multiple code units work together, and use PHPUnit's setUp() and tearDown() methods to set up and clean up the test environment. Practical case: Use PHPUnit to perform unit and integration testing in Laravel applications, including creating databases, starting servers, and writing test code.

Laravel vs CodeIgniter: Which framework is better for large projects? Laravel vs CodeIgniter: Which framework is better for large projects? Jun 04, 2024 am 09:09 AM

When choosing a framework for large projects, Laravel and CodeIgniter each have their own advantages. Laravel is designed for enterprise-level applications, offering modular design, dependency injection, and a powerful feature set. CodeIgniter is a lightweight framework more suitable for small to medium-sized projects, emphasizing speed and ease of use. For large projects with complex requirements and a large number of users, Laravel's power and scalability are more suitable. For simple projects or situations with limited resources, CodeIgniter's lightweight and rapid development capabilities are more ideal.

See all articles