mafrasil / cashier-polar
Laravel Cashier integration for Polar.sh subscription billing services
Fund package maintenance!
mafrasil
Requires
- php: ^8.3
- guzzlehttp/guzzle: ^7.8
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
- spatie/laravel-webhook-client: ^3.4
- standard-webhooks/standard-webhooks: dev-main
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- spatie/laravel-ray: ^1.35
README
Disclaimer
Note: This is not an official Laravel package. This is a community-built package following Laravel Cashier principles and is currently a work in progress.
Introduction
Cashier Polar provides an expressive, fluent interface to Polar's subscription billing services. It handles almost all of the boilerplate subscription billing code you are dreading writing.
Requirements
- PHP 8.3+
- Laravel 10.0+ / 11.0+
- Polar account and API credentials (https://polar.sh)
Installation
composer require mafrasil/cashier-polar php artisan vendor:publish --tag="cashier-polar-migrations" php artisan migrate php artisan vendor:publish --tag="cashier-polar-config"
Configuration
POLAR_API_KEY=your-api-key POLAR_ORGANIZATION_ID=your-organization-id POLAR_WEBHOOK_SECRET=your-webhook-secret POLAR_SANDBOX=true # Set to false for production
Generate a webhook secret:
php artisan cashier-polar:webhook-secret
Basic Usage
Setup Billable Model
use Mafrasil\CashierPolar\Concerns\Billable; class User extends Authenticatable { use Billable; }
Create a Checkout Session
// Simple checkout $checkout = $user->checkout('price_id'); // With options $checkout = $user->checkout('price_id', [ 'success_url' => route('checkout.success'), 'cancel_url' => route('checkout.cancel'), ]); // Redirect to checkout return redirect($checkout['url']);
Access Subscriptions
// Get subscription details $subscription = $user->subscription; echo $subscription->name; // Get subscription name echo $subscription->price; // Get formatted price (e.g., "10.00 USD") echo $subscription->interval; // Get billing interval (e.g., "month") echo $subscription->description; // Get subscription description // Check status if ($subscription->valid()) { // Active, on trial, or on grace period } if ($subscription->active()) { // Currently active } // Trial handling if ($subscription->onTrial()) { echo $subscription->trialEndDate(); // "2024-01-21" echo $subscription->daysUntilTrialEnds(); // Days remaining } // Cancellation handling if ($subscription->cancelled()) { if ($subscription->onGracePeriod()) { echo $subscription->endDate(); // "2024-01-21" echo $subscription->daysUntilEnds(); // Days remaining } } // Check specific plan if ($user->subscribedToPlan('premium')) { // Subscribed to premium plan }
Manage Subscriptions
// Cancel $subscription->cancel(); // End of period // Resume (during grace period) $subscription->resume();
Webhook Events
The package automatically handles these webhook events:
checkout.created
checkout.updated
order.created
subscription.created
subscription.updated
subscription.active
subscription.revoked
subscription.canceled
Listen for Events
// EventServiceProvider.php use Mafrasil\CashierPolar\Events\SubscriptionCreated; Event::listen(function (SubscriptionCreated $event) { $subscription = $event->subscription; $user = $subscription->billable; // Handle event });
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.