Stripe and Billing setup for Laravel Spark

Scott Anderson
3 min readMay 31, 2020

--

Laravel Spark comes with a Stripe integration for processing subscription payments via Credit Card.
This is a quick guide for configuring Stripe and Spark to work together.
The official documentation is here https://spark.laravel.com/docs/10.0/billing

1. Create a Stripe account

Go to https://stripe.com and register an account

2. Get your test API keys

In your Stripe Dashboard you should see a link that says ‘Get your test API keys’.

If not, use the ‘Developer’ menu item on the left side, then select ‘API keys’.

This may show your live keys by default. If so, then toggle the Test / Live data view using the switch in the top right corner.

Copy your test API keys somewhere safe, to be added to your Spark configuration.

3. Add your API keys to your .env file

Edit your .env file and set the following config values using the keys you saved in the previous step:

STRIPE_KEY=
STRIPE_SECRET=

4. Set up Stripe webhooks

In your Stripe Dashboard, click on Developer->Webhooks in the left side menu.

Click ‘Add Endpoint’

Enter your ‘Endpoint URL’ as your site’s domain plus /webhook/stripe
E.g. https://your-domain.com/webhook/stripe

Under ‘Events to send’ add the following 6 events, one at a time:

  • customer.subscription.updated
  • customer.subscription.deleted
  • customer.updated
  • customer.deleted
  • invoice.payment_action_required
  • invoice.payment_succeeded

5. Configure Plans/Products in Stripe

We need to configure a Product in Stripe for each available subscription plan you intend to offer in your Spark application.

In your Stripe Dashboard click on ‘Products’ on the left side menu.

Click ‘Add Product’.

Add your product name and price details.

Note: You don’t need to add a Free Trial as this functionality is already built into Spark.

Click on ‘Save Product’, or ‘Save and add more’, if you are configuring multiple plans.

After saving, you’ll see a summary of the Product.

Click on the Copy to Clipboard button next to the ‘API ID’ field. You’ll need to copy the API ID for each Product you create.

6. Add your Product API ID(s) to your SparkServiceProvider.php

Edit app/Providers/SparkServiceProvider.php and within the booted() function, edit or add Stripe Plans to match each of the Products you added within Stripe.

The 2nd argument in the Stripe::Plan() function should be the API ID(s) you copied from each Product in the previous step.

E.g.

Spark::plan(‘Pro’, ‘price_HNQvdu4lQHTBVX’)
->price(99)
->features([
‘All the best features.’
]);

Also, make sure the price matches the price for the product set in Stripe.

Bingo! You’re done.

By default, Spark configures a free trial period of 10 days, which doesn’t require a credit card.

You can modify or remove this by changing the following line in SparkServiceProvider.php

Spark::noCardUpFront()->trialDays(10);

For more details on setting up Team Billing and for checking a user’s subscription status within the application, see the Spark documentation https://spark.laravel.com/docs/10.0/billing

More Spark tutorials

Configuring email verification in Laravel Spark

Setting up Laravel Spark for local development

--

--

Scott Anderson

Full stack engineer @ beamible.com | React / Nextjs, Typescript, GraphQL, Laravel, AWS, GCP | Author of https://buff.ly/2yZYw3Z