search
HomePHP FrameworkLaravelLaravel Development: How to handle subscription payments using Laravel Cashier and Paddle?

As more and more companies begin to provide subscription services to consumers, subscription payments have become an important part of modern Internet commerce. If you want to build a subscription service, or want to add subscription payment functionality to an existing service, the Laravel Cashier and Paddle combination will provide you with a suitable solution.

Laravel Cashier is an extension package developed by Laravel that provides a simple and easy-to-use interface for subscription processing and payment gateway integration. Paddle is a global payments solution that allows you to process payments and subscriptions in a standards-compliant way.

In this article, we will explore how to use Laravel Cashier and Paddle to handle subscription payments, including how to set up a Paddle account and Laravel project environment, how to handle subscription payments, and how to test Paddle.

Set up a Paddle account

In order to use Paddle as a payment gateway, you need to first create a Paddle account. After creating a Paddle account, you need to create a product in Paddle. This product can be the main product of your subscription service, or it can be an add-on product or service.

Next, you need to set up shipping locations and tax rules. These rules will depend on the localization scope of your service and other important factors. Finally, you need to set up a price plan or subscription plan for your product in Paddle. This plan will define the price, payment cycle, and other relevant information for your subscription service.

Set up the Laravel environment

Before setting up the Laravel environment, you need to determine the requirements for the Laravel version and PHP version. Laravel Cashier needs to run on Laravel 5.1 and above, and Paddle needs to run on PHP 7.0 and above.

After you confirm the Laravel and PHP version requirements, you need to introduce Laravel Cashier into the Laravel project. You can install Laravel Cashier by running the following command:

composer require laravel/cashier

After successful installation, you need to add the service provider and alias in config/app.php. Add the following service provider in the providers array:

LaravelCashierCashierServiceProvider::class,

Add the following alias in the aliases array:

'Cashier' => LaravelCashierFacadesCashier:: class,

Next, you need to configure your Paddle information. Add the following information to config/services.php:

'paddle' => [

'vendor_id' => env('PADDLE_VENDOR_ID'),
'vendor_auth_code' => env('PADDLE_VENDOR_AUTH_CODE'),

],

You need to replace the above information with your Paddle account Information. You can find this information in the settings page of your Paddle account.

Processing Subscription Payments

Now that you have set up your Paddle account and Laravel environment, you can start using Laravel Cashier and Paddle to process subscription payments.

In your application, you can create a simple route to handle payment requests. For example:

Route::get('/subscribe', function () {

$plan = request('plan');

$user = auth()->user();

$user->newSubscription('main', $plan)->create(request()->query('email'), [
    'metadata' => [
        'name' => $user->name,
        'company' => $user->company,
    ]
]);

return redirect('/');

});

In the above code, we use Cashier to create a new of subscriptions. We first get the user's email address and then create a new subscription. When creating a subscription, we add some user information to the metadata array.

You can also use the defaultPaymentMethod method to specify the user's default payment method:

$user->defaultPaymentMethod('pm_1234567890');

When a payment is received , you can use Stripe events to automatically update the user's payment status, for example:

Route::post('/stripe/webhook', function (Request $request) {

$payload = $request->all();
$event = null;

if (isset($payload['type'])) {
    $event = StripeEvent::constructFrom($payload);
}

if ($event && $event->type === 'customer.subscription.updated') {
    $subscription = $event->data->object->id;
    $customer = $event->data->object->customer;

    $user = User::where('stripe_id', $customer)->first();

    $user->subscriptions()->where('stripe_id', $subscription)->update([
        'stripe_status' => $event->data->object->status,
    ]);
}

return response()->json(['success' => true]);

}) ;

In the above code, we use Stripe events to update the user's subscription status. For specific usage, please refer to Stripe documentation.

Testing

After you complete the above steps and implement subscription payment, you should test Paddle to ensure that all functions are working properly.

Before you test, you need to configure a Paddle account and a test product for the test environment. To ensure that your code works smoothly against the test environment, you need to use test amounts and test subscription loops when testing.

After you complete the test, you can view the test data in the Paddle dashboard to make sure everything is working properly. If you encounter any problems, you can find more information in the Laravel Cashier or Paddle documentation.

Conclusion

The integration of Laravel Cashier and Paddle provides you with a simple, easy-to-configure solution for implementing subscription payment functionality. Whether you are building a new subscription service or adding subscription payment functionality to an existing service, this combination will provide you with the right solution.

The above is the detailed content of Laravel Development: How to handle subscription payments using Laravel Cashier and Paddle?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How to Use Laravel Migrations: A Step-by-Step TutorialHow to Use Laravel Migrations: A Step-by-Step TutorialMay 13, 2025 am 12:15 AM

LaravelmigrationsstreamlinedatabasemanagementbyallowingschemachangestobedefinedinPHPcode,whichcanbeversion-controlledandshared.Here'showtousethem:1)Createmigrationclassestodefineoperationslikecreatingormodifyingtables.2)Usethe'phpartisanmigrate'comma

Finding the Latest Laravel Version: A Quick and Easy GuideFinding the Latest Laravel Version: A Quick and Easy GuideMay 13, 2025 am 12:13 AM

To find the latest version of Laravel, you can visit the official website laravel.com and click the "Docs" button in the upper right corner, or use the Composer command "composershowlaravel/framework|grepversions". Staying updated can help improve project security and performance, but the impact on existing projects needs to be considered.

Staying Updated with Laravel: Benefits of Using the Latest VersionStaying Updated with Laravel: Benefits of Using the Latest VersionMay 13, 2025 am 12:08 AM

YoushouldupdatetothelatestLaravelversionforperformanceimprovements,enhancedsecurity,newfeatures,bettercommunitysupport,andlong-termmaintenance.1)Performance:Laravel9'sEloquentORMoptimizationsenhanceapplicationspeed.2)Security:Laravel8introducedbetter

Laravel: I messed up my migration, what can I do?Laravel: I messed up my migration, what can I do?May 13, 2025 am 12:06 AM

WhenyoumessupamigrationinLaravel,youcan:1)Rollbackthemigrationusing'phpartisanmigrate:rollback'ifit'sthelastone,or'phpartisanmigrate:reset'forall;2)Createanewmigrationtocorrecterrorsifalreadyinproduction;3)Editthemigrationfiledirectly,butthisisrisky;

Last Laravel version: Performance GuideLast Laravel version: Performance GuideMay 13, 2025 am 12:04 AM

ToboostperformanceinthelatestLaravelversion,followthesesteps:1)UseRedisforcachingtoimproveresponsetimesandreducedatabaseload.2)OptimizedatabasequerieswitheagerloadingtopreventN 1queryissues.3)Implementroutecachinginproductiontospeeduprouteresolution.

The Most Recent Laravel Version: Discover What's NewThe Most Recent Laravel Version: Discover What's NewMay 12, 2025 am 12:15 AM

Laravel10introducesseveralkeyfeaturesthatenhancewebdevelopment.1)Lazycollectionsallowefficientprocessingoflargedatasetswithoutloadingallrecordsintomemory.2)The'make:model-and-migration'artisancommandsimplifiescreatingmodelsandmigrations.3)Integration

Laravel Migrations Explained: Create, Modify, and Manage Your DatabaseLaravel Migrations Explained: Create, Modify, and Manage Your DatabaseMay 12, 2025 am 12:11 AM

LaravelMigrationsshouldbeusedbecausetheystreamlinedevelopment,ensureconsistencyacrossenvironments,andsimplifycollaborationanddeployment.1)Theyallowprogrammaticmanagementofdatabaseschemachanges,reducingerrors.2)Migrationscanbeversioncontrolled,ensurin

Laravel Migration: is it worth using it?Laravel Migration: is it worth using it?May 12, 2025 am 12:10 AM

Yes,LaravelMigrationisworthusing.Itsimplifiesdatabaseschemamanagement,enhancescollaboration,andprovidesversioncontrol.Useitforstructured,efficientdevelopment.

See all articles

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 Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools