Billing

Utilizing Laravel's Cashier (v16) Scaffold comes with a fully setup billing system for use with Stripe.

Requires

Some familiarity and an account with Stripe.

Outline

The billing system handles the Stripe integration with Cashier as mentioned. Subscribing goes through Stripe Checkout, and managing a subscription goes through the Stripe billing portal, so Scaffold never touches card details. It contains the domain.com/stripe/webhook endpoint for Stripe feedback (excluded from CSRF in bootstrap/app.php) as well as a simple config file for handling the various "plans" your app may offer.

Routes

GET   user/billing                       // the billing page
POST  user/billing/subscribe             // redirects to Stripe Checkout
GET   user/billing/subscribe/success     // Checkout success landing
GET   user/billing/subscribe/cancelled   // Checkout cancel landing

The subscribe action creates a Checkout session for the chosen plan, requiring a billing address. The success and cancel routes clear the user's subscription cache, log an activity, and return the user to the billing page.

Config

config/billing.php

Within the billing config you can set the subscription name, the details for your invoices, and the various plans you offer. Each plan is keyed by its Stripe price, which you set in your .env. These plans are what is listed to the end user when they view the billing screen.

'plans' => [
    env('PLAN_MONTHLY', 'plan_monthly') => [
        'price' => 9.99,
        'name' => 'Monthly',
        'frequency' => 'monthly',
        'key' => env('PLAN_MONTHLY', 'plan_monthly'),
        'label' => 'Monthly ($9.99)',
        'currency' => 'USD',
        'encouraged' => false,
        'pricing' => '$9.99/month',
        'features' => [],
    ],
    // ...
],

Gates and Middleware

The subscribed gate passes when the user has an active subscription, is on a trial, or is an admin. The Subscription middleware uses it to protect routes, aborting with a 401 otherwise. There are also subscription-not-canceled and subscription-canceled gates for the UI.

Gate::allows('subscribed', $user);

User Data

As with all Stripe integrations including Cashier all you receive is the last 4 digits of a credit card when the subscription is generated. The billing UI provides the user with which card their subscription is on, the name of the plan they are on, and their upcoming payment.

Webhooks and testing

The webhook setup is key to having everything function well with the portal system. Ideally, we don't want to touch any customer data so its best to be able to direct accordingly. Webhooks will provide us with the state changes that happen in the Stripe UI.

You can review how to test your webhooks here:

https://stripe.com/docs/stripe-cli

Some commands you will need to run locally:

stripe login
stripe listen --forward-to https://{local-domain}/stripe/webhook

Change Plan

This should be handled in the customer portal configuration.

Coupons / Promotional Codes

This should be configured within the customer portal configuration. These are added when a person subscribes or updates a subscription.

Billing Portal

Rather than maintain a complex set of forms and views for payment methods, cancelling, renewing, and invoices, Scaffold by default lets Stripe handle all that with the billing portal.

In order to ensure you have everything set up correctly you need to have a Stripe account and some ENVs set.

PLAN_MONTHLY={price_token}
PLAN_YEARLY={price_token}

STRIPE_KEY={dev_public}
STRIPE_SECRET={dev_secret}
STRIPE_WEBHOOK_SECRET={webhook_secret}

CASHIER_CURRENCY=USD

Stripe Dashboard

Within Stripe's dashboard you need to set up your products, some pricing the billing config handles the two cases of monthly and yearly. Within products you create prices to ensure you do that properly. You then need to configure the webhook which can be done either directly in the UI: please follow Laravel's Cashier docs for those details OR you can use their command php artisan cashier:webhook. Lastly, you need to set up your "Customer Billing Portal" and ensure its branding matches what you want for your product.

Tax

These links are to the test endpoints, you will need to do the same on live/prod mode.

If you enable tax you need to set an origin location for your business:

https://dashboard.stripe.com/test/settings/tax

You need to also ensure that your billing portal is configured or "enabled"

https://dashboard.stripe.com/test/settings/billing/portal