Laravel Cashier (Stripe) is the base package used to enable Stripe payments with extra customization.

Intro

Due the limitations of Laravel Cashier for Stripe and Paddle to work with each other by default, there have been some adjustments and modifications:

  • updated table names:
    • stripe_subscriptions
    • stripe_subscription_items
    • stripe_orders
  • custom models:
    • App\Models\Payments\Stripe\Subscription
    • App\Models\Payments\Stripe\SubscriptionItem
    • App\Models\Payments\Stripe\Order
  • custom trait: App\Traits\Billable
  • custom URL for webhooks /payment/webhook named cashier.webhook

Create Stripe account

Create Stripe account and add keys…

Docs coming soon…

Migrations

Docs coming soon…

Generate products

Go to config/payments.php and setup your products and plans.

You can update product name, description and features.

It is not recommended to update product IDs of existing products and prices.

Stripe does not allow updating the price amount, currency or interval for existing prices.

If you want to update any of those fields (price, currency, interval) then just update them in the payments file and run the sync command. Command will create a new price (or use the archived one if parameters match), set it as default (if specified) and deactivate the old price. Upon update, it’s recommended to manually delete any unused old prices. Sync command can’t delete them due the Stripe API restrictions.

Regardless of all the automatic tasks the sync command is doing, you should always check the Stripe Dashboard to make sure everything is setup correctly.

Sync products

To sync Stripe products with the payments file go to your terminal and run this command:

php artisan payments:sync-products

This will create or update products defined in payments file with your Stripe Product catalog.

If you want to handle your products and prices manually, then don’t run this command.

If you set the data manually, make sure to clear products cache when you update them in Stripe.

Get products

To get products from Stripe, call this method:

use App\Services\PaymentService;

$products = PaymentService::getProducts();

All fetched products are cached to avoid unnecessary connections to the Stripe API. If you are using sync command to handle your products, it will automatically clear the cache on update. If you are creating your products manually then you should also clear the cache manually.

Webhooks

Stripe can notify your application of a variety of events via webhooks to sync data from Stripe with your database.

It’s highly recommended to enable webhooks.

You can extend it as you wish following the offical docs.

Local

  1. To make local webhook URL publicly accessible install Stripe CLI
  2. Follow the provided steps to login to stripe via stripe-cli
  3. Once you’re logged in, run this in your terminal: stripe listen --forward-to localhost:8000/payment/webhook or stripe listen --forward-to https://butchr.test/payment/webhook
  4. Copy webhook secret provided by stripe-cli listener to STRIPE_WEBHOOK_SECRET in .env file.
  5. CSRF should be disabled for webhooks (this is already included with boilerplate)

Resend event for testing

If you forgot to run webhooks locally or want to make some updates and additional testing, you can resend events from terminal.

You can find event IDs in Stripe dashboard and then run this command.

stripe events resend evt_1Q2YfaRujsEDTyysDc3I3DdS

For subscriptions you should resend customer.subscription.updated event.

For one-time payments you should resend checkout.session.completed event.

Production

Once you setup the live keys STRIPE_KEY and STRIPE_SECRET on your server, run this command:

php artisan cashier:webhook

Then go to your Stripe dashboard, click on your new webhook endpoint and then click Reveal Signing secret.

Copy the value to your environment variable STRIPE_WEBHOOK_SECRET.

Now you are ready to use webhooks in production.

Don’t forget to optimize (cache) your application files on .env update!

Update webhooks

If you need to add new event handlers go to app/Listeners/StripeEventListener.php and update it accordingly.

Billing settings

Go to Billing settings in your Stripe Dashboard and setup the settings according to your requirements.

If your application is using recurring payments (subscriptions) you can use Stripe invoicing system. Go to Invoicing settings and setup the settings according to your requirements.

For Single payments Stripe is not creating automatic invoices as for subscriptions. You can use their Invoicing service (which could trigger some additional fees), custom solution via webhooks or external service. More details here.

Billing Portal

Stripe offers an easy way to set up a billing portal so that your customer can manage their subscription, payment methods and view their billing history.

This is only included if you are using subscriptions since it has no specific use for single orders.