Integrate Paddle (Subscription Billing Service) In Your Next Laravel Project

UI-Lib
7 min readApr 19, 2022

Payment gateway integration has become a necessary requirement for any business or enterprise website. There are various companies that offer payment transactions by implementing their payment gateway infrastructure in your project. Some of the well-known subscription billing services are Stripe, Paddle, Braintree, and many more. Today we will see how we can integrate the Paddle payment gateway into our Laravel project using Laravel Cashier.

I’ve been struggling for many days to implement Paddle in my Laravel project but I was not able to find a good demo project that was solely based on raw HTML and PHP so that I can learn. After implementing Paddle I thought to write a blog that consists of a demo project that implements Paddle so that you guys can have a clear outlook on how to build a Paddle payment gateway in your Laravel Project.

But first, we should understand why we should choose Paddle over other billing services. The main reason can be that Paddle is supported all over the world whereas Stripe and other billing services are not. With Paddle, you can also be able to accept payment not only with Card information but also with a PayPal account, Apple Pay account, and many more.

Laravel Cashier is there to help us easily implement payment gateways without writing a bunch of dreadful codes from scratch. It gives a fluent and expressive interface to the billing service we want to implement in our project and provides us with all the boilerplate code needed for integration. Laravel Cashier can handle coupons, switching subscriptions, subscription “quantities”, grace period cancellation, and even generate invoice PDFs, in addition to basic subscription management.

Note: The blog contains affiliate links.

Implementing Paddle

Creating Laravel Project

First of all, if we are starting from scratch we should start by creating our Laravel project by entering the following command,

composer create-project laravel/laravel <project-name>

I am creating a new Laravel project with Composer. If you don’t have composer installed please visit Composer Download.

The command above will create a Laravel project folder with all the necessary files.

Now we should update the .env file and setup the database,

Scaffolding Starter Kit

Now we should implement authentication functionality as we want an authorized user to subscribe to any of the plans we defined. Fortunately, with Laravel, we have authentication packages that do all the work for us. Laravel UI, Breeze, JetStream, etc all are Laravel packages that implement authentication features for us in our project. In this blog, we will use Laravel UI for authentication.

We need to insert the following command to download and install the Laravel UI package,

composer require laravel/ui

After that, for authentication scaffolding, we need to enter any of the following commands,

php artisan ui bootstrap --auth
php artisan ui vue --auth //I am using vue in my case
php artisan ui react --auth

Now we need to run,

npm install && npm run dev

If you run into any errors please update your vue-loader by entering,

npm update vue-loader

And again run,

npm run dev

This will automatically create a login and registration page with all necessary backend functionalities for your project.

Now to complete register and login functionality we need to migrate our database.

php artisan migrate

This will migrate all the tables and any changes that we did into our database.

For the time being my database looks like the following image,

Installing Cashier

Before installing the Laravel Cashier Paddle package we need to have a Paddle account or a Paddle Sandbox account which will provide us with the necessary API keys. Here we will work with a sandbox account which basically provides us with a demo paddle account for testing purposes. You can create your Paddle Sandbox account from here.

Laravel Cashier package for Paddle can be installed with the following composer command,

composer require laravel/cashier-paddle

As we are using a sandbox account, in our .env file we should declare the Paddle_Sandbox variable to true .

Now we should migrate our database.

php artisan migrate

After migration, we will notice some significant changes in our database.

We can see from the above figure that installing the cashier package and then migrating the database added some new tables called plans, receipts, subscriptions, etc. These are automatically generated by the Laravel Cashier package.

Configuring Cashier

Now we need to configure our Paddle API keys in the .env file.

Also, we need to add the Billable trait to our User model. This Billable trait lets us perform common billing tasks.

use Laravel\Paddle\Billable;class User extends Authenticatable{use Billable;}

To initiate the Paddle checkout widget we need to add the @paddleJS javaScript library inside the blade directive right before our application layout’s closing </head> tag.

<head>...@paddleJS</head>

Declaring View and Controller Files

Now that all our configuration is ready we should get started with our view and controller files.

First of all, we should add a button that will generate the subscription page from our homepage. So, in the home.blade.php file add the following code.

@extends('layouts.app')@section('content')<div class="container"><div class="row justify-content-center"><div class="col-md-8"><div class="card"><div class="card-header">{{ __('Dashboard') }}</div><div class="card-body">@if (session('status'))<div class="alert alert-success" role="alert">{{ session('status') }}</div>@endif{{ __('You are logged in!') }}<a href="/pay"><button>Subscribe To Premium</button></a></div></div></div></div></div>@endsection

In our web.php file, define the route for the given button.

Route::get('pay', [PaymentController::class, 'pay'])->middleware('auth');

Now create a controller called PaymentController and in its pay() function write the following code,

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Support\Facades\Auth;class PaymentController extends Controller{public function pay(){$paylink = Auth::user()->charge(50.0, "Premium");return view('billing', ['paylink' => $paylink]);}}

In the PaymentController.php file, we define a $paylink variable that will charge the authorized user. The charge() function, requires two parameters, the cost, and the plan name. We now pass the $paylink variable to the billing page.

We should create a billing.blade.php file and write the following code,

@extends('layouts.app')@section('content')<div class="container"><div class="row justify-content-center"><div class="col-md-8"><div class="card"><div class="card-header">{{ __('Billing') }}</div><div class="card-body">@if (session('status'))<div class="alert alert-success" role="alert">{{ session('status') }}</div>@endif<x-paddle-button :url="$paylink" class="px-8 py-4">Buy</x-paddle-button></div></div></div></div></div>@endsection

Now the Buy button, that we declared in the code will invoke the Paddle checkout. For testing, if your payment gateway is working or not you can use test card numbers available here. If you have your API keys right you will be able to see all transaction-related information in your Paddle account. Thus our Paddle payment gateway is implemented in our Laravel project with the help of Laravel Cashier.

This is just the beginning with Laravel Cashier, you can also do a bunch of stuff when you implemented Paddle with Laravel Cashier. For an in-depth analysis please visit Laravel Cashier (Paddle).

Wrapping Up

Integrating a payment gateway is a common thing in almost every website. Now for payment gateways, we are able to take our business in a global range. Paddle is a billing service that has a worldwide reach. So implementing Paddle on our websites should be encouraged.

Laravel Cashier for this purpose is very much helpful for us. It gives us complete ease implementing and working with Paddle. Not only that, the renowned Stripe payment gateway can also be easily integrated with Cashier which we will talk about on another day.

Our Products Implemented With Laravel

Gull — TailwindCss Admin Dashboard Template

Aatrox — TailwindCss Admin Dashboard Template

Stocky — Ultimate Inventory Management System with Pos

WorkTick — Ultimate HRM & Project Management

Similar Articles

--

--

UI-Lib

At UI-Lib you can download hand coded free and premium Admin Dashboards, Landing pages, UI Kits & Design Systems built with Angular, React & Bootstrap, etc.