I stand up for children in need. Please join me in helping this family.
Laravel Cashier (Paddle): Simplifying Subscription Billing
Laravel Cashier Paddle provides an expressive, fluent interface to Paddle's subscription billing services. It handles most of the boilerplate subscription billing code, allowing developers to focus on building their applications. Let's explore how to use Cashier Paddle and some of its key features.
Getting Started
First, install Cashier Paddle via Composer:
composer require laravel/cashier-paddle
Next, add the Billable trait to your User model:
use Laravel\Paddle\Billable;
class User extends Authenticatable
{
use Billable;
}
Configuration
Configure your Paddle keys in your .env
file:
PADDLE_VENDOR_ID=your-paddle-vendor-id
PADDLE_VENDOR_AUTH_CODE=your-paddle-vendor-auth-code
PADDLE_PUBLIC_KEY="your-paddle-public-key"
PADDLE_SANDBOX=true
Creating Subscriptions
Creating a subscription is straightforward with Cashier Paddle:
$user = User::find(1);
$subscription = $user->newSubscription('default', $planId)->create($paymentMethod);
This creates a new subscription for the user with the 'default' name and the specified Paddle plan ID.
Checking Subscription Status
You can easily check a user's subscription status:
if ($user->subscribed('default')) {
// User has an active subscription
}
if ($user->subscription('default')->onTrial()) {
// User is on trial
}
if ($user->subscription('default')->cancelled()) {
// Subscription is cancelled
}
Handling Paddle Webhooks
Cashier Paddle automatically handles Paddle's webhooks. Just set up a route:
use Illuminate\Support\Facades\Route;
use Laravel\Paddle\Http\Controllers\WebhookController;
Route::post(
'/paddle/webhook',
[WebhookController::class, 'handleWebhook']
);
Single Charges
For one-time charges, use the charge method:
$user->charge(1000, 'Product Name');
Suggested Usages
SaaS Applications: Implement tiered pricing plans for your software service.
Digital Content Subscriptions: Offer premium content access through subscriptions.
Membership Sites: Manage member access and recurring payments.
E-learning Platforms: Charge for course access or premium features.
API Access: Implement usage-based billing for API access.
Subscription Modifiers
Cashier Paddle supports subscription modifiers for flexible pricing:
$user->subscription('default')->addModifier(500, 'Extra Feature');
Conclusion
Laravel Cashier Paddle simplifies the complex world of subscription billing with Paddle, allowing you to focus on building great products. With its intuitive API and seamless Paddle integration, implementing a robust billing system has never been easier.
Remember to always refer to the official Laravel Cashier Paddle documentation for the most up-to-date information and best practices.
More posts
Embracing Vulnerability: Brené Brown's Guide to Authentic Living
Brené Brown's TED Talk, "The Power of Vulnerability," explores how embracing vulnerability can foster deeper human connections, courage, and authenticity.
Embracing Optimism: The Power of Seeing the Good
Inspired by John Green's quote, this post explores the power of maintaining an optimistic outlook. It challenges readers to embrace the possibility of overwhelming good and provides practical tips for cultivating positivity in daily life.
Building and Using a Blockchain in PHP with Decentralized Applications (DApps)
Explore how to integrate PHP-based blockchain with Decentralized Applications (DApps). Learn about smart contract integration, decentralized storage, and user authentication, with a practical example of a decentralized marketplace.