Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I integrate Laravel Pennant with Laravel Cashier


How can I integrate Laravel Pennant with Laravel Cashier


To integrate Laravel Pennant (feature flags) with Laravel Cashier (subscription billing), you can use Pennant to conditionally enable or disable billing-related features or subscription plans based on feature flags. Here is a detailed approach on how to combine both in a Laravel application:

1. Setup Laravel Cashier

- Add the `Billable` trait to your billable model, usually `User`:

php
use Laravel\Cashier\Billable;

class User extends Authenticatable
{
    use Billable;
}

- Configure your billing provider (Stripe or Paddle) as per Laravel Cashier documentation, including API keys and migrations[1][5].

2. Setup Laravel Pennant

- Install and configure Pennant for feature flags.

- Define your features in a service provider or using feature classes. For example, define a feature flag controlling access to a premium subscription:

php
use Laravel\Pennant\Feature;

Feature::define('premium-subscription', fn (User $user) => $user->is_premium_eligible);

Or create a feature class with a `resolve()` method that returns true/false based on custom logic[2][6][8].

3. Integrate Pennant Feature Flags with Cashier Billing Logic

- Use Pennant's feature checks to conditionally enable subscription plans or billing-related UI elements.

- For example, in your controller or subscription logic, check if the feature is enabled before allowing the user to subscribe:

php
use Laravel\Pennant\Feature;

if (Feature::active('premium-subscription', $user)) {
    // Allow subscribing to premium plan via Cashier
    $user->newSubscription('default', 'premium-plan')->create($paymentMethod);
} else {
    // Show alternative or deny access
}

- In Blade views, you can also conditionally show subscription buttons or pricing based on feature flags:

blade
@feature('premium-subscription', $user)
    Subscribe to Premium
@endfeature

4. Advanced: Use Pennant Scopes for Granular Control

- Pennant supports scoping feature flags by user or other models. You can define features that only enable billing features for certain user segments or roles.

- This allows you to roll out subscription plans gradually or only to specific customers.

5. Optional: Custom Pennant Driver for Billing Data

- If you want to store feature flags related to billing in a custom storage (e.g., Redis or database), you can implement a custom Pennant driver by implementing `Laravel\Pennant\Contracts\Driver` interface[2][6].

- This can be useful if you want to dynamically enable/disable features based on billing events or subscription status.

Summary

- Add `Billable` trait to your user model for Cashier.

- Define feature flags in Pennant to control access to subscription features.

- Check feature flags before creating subscriptions or showing billing UI.

- Optionally, use Pennant scopes or custom drivers for advanced scenarios.

This approach allows you to leverage Laravel Pennant’s feature flagging to incrementally roll out or control subscription billing features managed by Laravel Cashier in a clean and maintainable way. Both packages integrate seamlessly within Laravel’s ecosystem and can be combined by simply checking Pennant features around Cashier billing logic[1][2][5][8].

Citations:
[1] https://laravel.com/docs/11.x/billing
[2] https://laravel.com/docs/12.x/pennant
[3] https://laravel-news.com/feature-flags-with-laravel-pennant
[4] https://apiacademy.treblle.com/laravel-api-course/feature-flags
[5] https://laravel.com/docs/12.x/cashier-paddle
[6] https://laravel.com/docs/11.x/pennant
[7] https://laravel.su/docs/10.x/cashier-paddle
[8] https://hackernoon.com/how-to-manage-feature-flags-with-laravel-pennant
[9] https://swapnil.dev/how-to-configure-laravel-cashier-with-multiple-models
[10] https://stackoverflow.com/questions/41130446/laravel-5-and-cashier-on-company-table