Skip to content
saucebase-devPublic

About

Laravel Stripe billing module for Saucebase: checkout, subscriptions, free trials, customer portal, pricing page, invoices, plan entitlements and limits, promotions and webhooks, with a Filament revenue dashboard. Vue and React (Inertia).

Topics

Resources

Code of conduct

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

Saucebase Billing

Tests Release Saucebase Core License

Laravel Stripe billing for Saucebase: checkout, subscriptions, trials, a pricing page and invoices, on Vue or React.

A module for Saucebase, the AI-ready Laravel SaaS kit. Modules are Composer packages you add to a Saucebase app; once installed, the code is yours to change.

Features

  • Stripe checkout, straight to Stripe or through your own checkout page
  • Subscriptions that cancel at period end and can be resumed
  • Customer portal for cards, invoices and plan changes
  • Free trials, one per customer, with or without a card up front, optionally with their own entitlements (fewer API requests, a feature off)
  • Grace period after a failed payment, with emails at each step
  • Plan kinds: free, subscription, lifetime and one-off
  • Entitlements: each plan turns features on and sets limits your app checks
  • Pricing page at /pricing, built from your plans
  • Promotions: a launch or seasonal price on the pricing page, with what is left and until when, applied at checkout and ended on its own
  • Billing settings: current plan, invoices and saved card
  • Catalogue sync with Stripe, both ways
  • Webhooks that are safe to retry and handle events out of order
  • Emails for every subscription and payment change
  • Admin panel for products, subscriptions and customers, with a revenue dashboard
  • Vue and React

Install

New to Saucebase? Start a new app and pick billing when it asks for modules:

curl -fsSL https://install.saucebase.dev | bash

It installs PHP and Composer if they're missing. With the CLI already installed, run saucebase new my-app instead.

Already have a Saucebase app? Add it with:

php artisan saucebase:module-install billing
npm install && npm run build

Then create your plans in Stripe and import them with php artisan billing:sync-catalog. They arrive hidden: describe them in the admin panel, under Billing → Products, and turn on Visible.

Manual install

If a patch doesn't apply, because your file has changed, make its change by hand:

user.patch makes users billing owners, which subscriptions need:

// app/Models/User.php
use Modules\Billing\Contracts\BillingOwner;
use Modules\Billing\Traits\Billable;

class User extends Authenticatable implements BillingOwner
{
    use Billable;
}

Configuration

Add your Stripe keys to .env:

STRIPE_SECRET_KEY=
STRIPE_PUBLISHABLE_KEY=
STRIPE_WEBHOOK_SECRET=

In Stripe, under Developers → Webhooks, add https://<your-app>/billing/webhooks/stripe with these events. Its signing secret is STRIPE_WEBHOOK_SECRET.

checkout.session.completed
checkout.session.async_payment_succeeded
customer.subscription.updated
customer.subscription.deleted
customer.subscription.trial_will_end
invoice.paid
invoice.payment_succeeded
invoice.payment_failed
charge.refunded
payment_method.attached
payment_method.detached
customer.updated

A 503 answer in Stripe's webhook log is normal: an event arrived before the one it depends on (an invoice before its checkout), and Stripe's retry applies it.

Locally, forward them with stripe listen --all-snapshot --forward-to localhost/billing/webhooks/stripe.

The rest is in the admin panel, under Settings → Billing: currency, checkout flow, grace period and trials.

Promotions

Show a discount on the pricing page, such as "$149 for the first 50 buyers", and apply it at checkout without the buyer typing a code.

  1. In Stripe, create a coupon and a promotion code for it, with any cap (Limit the number of times this code can be redeemed), end date, First-time order only and product limits. Stripe enforces all of them.
  2. In the admin panel, under Billing → Promotions, add the code and tick the prices whose cards show it. Billing reads the discount, cap, end date and count from Stripe.

A promotion written in code or a seeder, with no Stripe ID yet, is created at Stripe by Push new to Stripe (billing:push-catalog), as plans are: a coupon limited to its prices' products, and the code with its cap and end date. A code Stripe already has is adopted instead, but only if it makes the same offer: codes are shared by every app on one Stripe account, so a LAUNCH with other terms is left alone and the push warns you to rename yours. A promotion shows on the page only once it is at Stripe. It also warns when a promotion takes nothing off one of its prices, such as an amount off in euros on a dollar price.

A price shows one promotion. Its card strikes the regular price through, shows the promotional one, and says how long it lasts on a subscription (the coupon's duration: first payment, a number of months, or forever), how many are left and until when. Checkout sends the code. The payment records the discount and the code, in the admin and in the customer's payments.

The page reads stored data and never calls Stripe. Each completed checkout counts one redemption, and billing:sync-catalog (daily, and the Sync button) sets the count back to Stripe's. Once the cap is reached or the date passes, the page shows the regular price again, with no deploy.

Stripe has the final word. If it has used up or expired the code before the page caught up, checkout opens at the regular price and says so above the pay button.

After a purchase

A buyer coming back from the payment page lands on the Billing section of their settings, with a confirmation. An app that sells something to install can send them there instead: in the admin panel, under Settings → Billing → Checkout, choose the section in After a purchase, open (Packages, for the Packages module). An install or a template sets it with --set=billing.after_checkout_section=modules. The confirmation shows on whichever section opens.

Show plans on the homepage (optional)

The plans live at /pricing. To also list them on the homepage, use core's home::content.after slot. Paths below are relative to the module's resources/js/. A flat install keeps one stack there; a checkout with both keeps them in vue/ and react/, so use the folder of the stack you run.

  1. Share the plans on the homepage only, from shareInertiaData() in src/Providers/BillingServiceProvider.php (core's hook for shared props, called from boot()). This is the same payload BillingPlansController builds:

    use Modules\Billing\Models\Product;
    use Modules\Billing\Services\BillingOwners;
    use Modules\Billing\Services\PlanActions;
    
    protected function shareInertiaData(): void
    {
        Inertia::share('billing.home', function () {
            if (! request()->routeIs('index')) {
                return null;
            }
    
            $products = Product::displayable()->get();
            $user = request()->user();
    
            return [
                'products' => $products,
                ...$this->app->make(PlanActions::class)->for($this->app->make(BillingOwners::class)->for($user), $products, $user),
            ];
        });
    }
  2. Add components/HomePricing, next to ProductSection:

    <!-- Vue: components/HomePricing.vue -->
    <script setup lang="ts">
    import { usePage } from '@inertiajs/vue3';
    import { computed } from 'vue';
    import ProductSection from './ProductSection.vue';
    
    const home = computed(() => usePage().props.billing?.home);
    </script>
    
    <template>
        <ProductSection v-if="home?.products.length" v-bind="home" />
    </template>
    // React: components/HomePricing.tsx
    import { usePage } from '@inertiajs/react';
    import ProductSection from './ProductSection';
    
    export function HomePricing() {
        const home = usePage().props.billing?.home;
    
        return home?.products.length ? <ProductSection {...home} /> : null;
    }
  3. Register it inside setup() in the module's app entry. A lower order renders first. Use your stack's alias, @saucebase/vue or @saucebase/react:

    // registerGlobalComponent is already imported there, from '@saucebase/<stack>/lib/globalComponents'
    import HomePricing from './components/HomePricing.vue'; // React: import { HomePricing } from './components/HomePricing';
    
    // inside setup()
    registerGlobalComponent('home::content.after', HomePricing, { order: 10 });

Run npm run build afterwards.

Demo data

php artisan modules:seed --module=billing --state=showcase

Adds sample plans and a launch promotion on Lifetime ($100 off for the first 50 buyers), pushed to Stripe when your keys are set, sample customers, and a staff account (billing@saucebase.dev) that only sees billing in the admin. All use the DEMO_PASSWORD from .env.

License

MIT. See LICENSE.

About

Laravel Stripe billing module for Saucebase: checkout, subscriptions, free trials, customer portal, pricing page, invoices, plan entitlements and limits, promotions and webhooks, with a Filament revenue dashboard. Vue and React (Inertia).

Topics

Resources

Code of conduct

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages