No description
  • PHP 99.9%
  • Blade 0.1%
Find a file
Troy Siedsma 7057c6eaa2 Security N6: catch the real Stripe signature exception
hasValidSignature caught Stripe\Error\SignatureVerification, a class that does
not exist in stripe-php v17, so a bad signature would have propagated uncaught
instead of returning false (it fails closed, but as a 500). Catch the real
Stripe\Exception\SignatureVerificationException, which was already imported.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 03:25:19 +00:00
config Re-platform onto LithiumHosting/laravel-stripe-billing Forgejo origin 2026-05-22 13:14:19 +00:00
database/migrations Re-platform onto LithiumHosting/laravel-stripe-billing Forgejo origin 2026-05-22 13:14:19 +00:00
src Security N6: catch the real Stripe signature exception 2026-06-23 03:25:19 +00:00
tests Re-platform onto LithiumHosting/laravel-stripe-billing Forgejo origin 2026-05-22 13:14:19 +00:00
.gitignore Re-platform onto LithiumHosting/laravel-stripe-billing Forgejo origin 2026-05-22 13:14:19 +00:00
composer.json Modernize composer constraints + normalize formatting for Laravel 11/12 2026-05-22 16:06:02 +00:00
LICENSE.md Standardize composer.json + README + LICENSE 2026-05-22 15:03:52 +00:00
phpunit.xml.dist Re-platform onto LithiumHosting/laravel-stripe-billing Forgejo origin 2026-05-22 13:14:19 +00:00
README.md Modernize composer constraints + normalize formatting for Laravel 11/12 2026-05-22 16:06:02 +00:00

Laravel Stripe Billing

Subscription + charge management for Laravel apps built on Stripe. Forked from tmyers273/laravel-stripe-billing and modernized for PHP 8.2 and Laravel 11/12.

Installation

Via composer:

composer require lithiumhosting/laravel-stripe-billing

After package is installed via composer, run the following command: php artisan vendor:publish and then pick Provider: LithiumHosting\StripeBilling\StripeBillingServiceProvider from the displayed list. This will copy migrations files and config into your app.

Afterwards run the migrations: php artisan migrate

Usage

Add Billable trait to the User model.

Stripe Secret

In order to use this package you must posses a Stripe Secret Key. It must be stored as an environment variable STRIPE_SECRET and/or set in config/services.php under stripe.secret

Models

  • Product (this is the parent product used to control access rights - e.g. Pro, Gold, Basic, Team etc.)
  • Price (this defines price and trial period e.g. pro_monthly_10, gold_yearly_9999 etc.)
  • Subscription
  • Card

Product model represents product access/privileges parameters Price model optionally belongs to Product model and represents price parameters

Public API

StripeBilling static helper

StripeBilling::createTestToken();
StripeBilling::setApiKey($apiKey);
StripeBilling::setCurrency($currency);

Stripe customer

// Create a customer from token (new default card will be created)
$user->retrieveOrCreateStripeCustomer($token);
$user->retrieveStripeCustomer($token);

Subscriptions

By default users can have multiple subscriptions. But this can be changed by setting unique_active_subscription to true in config/stripe-billing.php

Check subscription
// Check if user is already subscribed to product
// Accepts Price object, Product object, string (name of Product or Price) e.g. basic, basic_yearly_90
$user->isSubscribedTo($product);

// Check if user is subscribed to a specific $price
$user->isSubscribedStrictlyTo($price);

// true or false
$user->hasActiveSubscriptions();

// or for subscription
// accepts Price|Product|string (name of Product or Price)
$subscription->isFor($product);
Retrieve subscriptions and chain methods
$user->getSubscriptionFor($product)->isActive();
$user->getSubscriptionFor('basic-monthly-10')->cancelNow();

// in the vast majority of cases your users will be only allowed
// to have one active subscription, so use this method when applicable
$user->getFirstActiveSubscription();
Create products and subscriptions
// Create the plans
$bronzePlan= Plan::create([
    'description' => 'Bronze Plan',
    'name' => 'bronze',
]);

// Create the Price
$bronzeMonthly = Price::create([
    'product_id' => $bronzePlan->id, // parent plan id
    'description' => 'Monthly Bronze Plan',
    'name' => 'bronze_monthly_50.00',
    'interval' => 'month',
    'stripe_product_id' => 'bronze_monthly', // this needs to be created in Stripe first
    'price' => 5000,
    'active' => true,
]);

// Accepts Plan object or string representing Plan name e.g. bronze_monthly_50.00
$user->subscribeTo($bronzeMonthly); // for already existing stripe customer
$user->subscribeTo($bronzeMonthly, $token); // for user without created customer
List subscriptions
$user->subscriptions;
$user->activeSubscriptions;
Cancel subscriptions
$subscription->cancelAtPeriodEnd();
$subscription->cancelNow();
Resuming subscription

Resuming subscription is possible only as long as it is on grace period

$subscription->resume();
Extend trial

Extending the trial is done in two ways.

$subscription->trialEndAt($unixTimestamp);
$days = 10;
$subscription->addDaysToTrial($days);

This will default prorate to false

Changing plan

// Accepts Plan object
$subscription->changeTo($basicMonthlyPlan);

Subscription scopes

Subscription::active()->get(); // get all active subscriptions
Subscription::canceledAndArchived()->get(); // get all canceled and non active subscriptions

Charges and credit cards

Default card

$user->defaultCard;

Add card from token

$card = $user->addCardFromToken($token);

This will create a stripe customer if user does not have one assigned yet. The default source of the new Stripe customer will be used to create the default credit card for the user, which will be used by default for future transactions.

If Stripe customer already exists for the user and the user already has a default card, the new card will be added as a new source for the users's Stripe Customer and a new Card record will be created in the local database.

Set another card as default

$user->setCardAsDefault($card);

That method by default takes \LithiumHosting\StripeBilling\Models\Card::class as an argument and makes the card the default card for that user. User's corresponding Stripe Customer's default source also gets updated.

Check if user already has a default card assigned

$user->hasDefaultCard(); //true or false

Or you can pass an instance of \LithiumHosting\StripeBilling\Models\Card::class to verify if that particular card is the default one for the user:

$user->hasDefaultCard($card); //true or false

Remove card

$user->removeCard($card);

Card helper methods

$card->isOwnedBy($user); // true or false
$card->isDefault(); // true or false

Single charges

When user already has a default card assigned:

$user->charge(2500); // Charge 25$

Charge by token

$user->chargeByToken(1799, 'some token from stripe.js'); // Charge 17.99$

Charge via non-default card

/**
* @param int $amount
* @param Card $card
* @param array $params
* @return mixed
*/
$user->chargeCard(1799, $card); // Charge 17.99$

Coupons

Coupon can be either a Stripe/Coupon or a string coupon ID of an existing coupon

$user->applyCoupon($coupon);

Middleware

Register in HTTP Kernel.php

'subscription' => \LithiumHosting\StripeBilling\Middleware\SubscriptionMiddleware::class,

The middleware can take parameters like so: subscription:basic,pro - that means that users with any of these subscriptions can pass the middleware. When used without parameters it will just look for any active including onTrial or OnGracePeriod subscriptions

Blade directives

subscribed directive determines if user is logged in and subscribed

@subscribed
// do something
@endsubscribed

unless_subscribed directive determines if user is logged in and not subscribed

@unless_subscribed
// do something
@endunless_subscribed

Config

'models' => [
        'owner' => 'App\User',
        'subscription' => \LithiumHosting\StripeBilling\Models\Subscription::class,
        'pricing_plan' => \LithiumHosting\StripeBilling\Models\Price::class,
        'plan' => \LithiumHosting\StripeBilling\Models\Plan::class,
        'card' => \LithiumHosting\StripeBilling\Models\Card::class,
    ],
    
    'tables' => [
        'owner' => 'users',
        'subscriptions' => 'subscriptions',
        'pricing_plans' => 'pricing_plans',
        'plans' => 'plans',
        'cards' => 'cards',
    ],
    
    'unique_active_subscription' => false,
];

License

This package, laravel-stripe-billing is licensed under The MIT License (MIT). Please see License File for more information.

Is it any good?

Yes.

When people first hear about a new product, they frequently ask if it is any good. A Hacker News user remarked:

Note to self: Starting immediately, all raganwald projects will have a "Is it any good?" section in the readme, and the answer shall be "yes.".