Download the PHP package lemonsqueezy/laravel without Composer
On this page you can find all versions of the php package lemonsqueezy/laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lemonsqueezy/laravel
More information about lemonsqueezy/laravel
Files in lemonsqueezy/laravel
Package laravel
Short Description A package to easily integrate your Laravel application with Lemon Squeezy.
License MIT
Informations about the package laravel
Lemon Squeezy for Laravel
A package to easily integrate your Laravel application with Lemon Squeezy. It takes the pain out of setting up a checkout experience. Easily set up payments for your products or let your customers subscribe to your product plans. Handle grace periods, pause subscriptions, or offer free trials.
This package drew inspiration from Cashier which was created by Taylor Otwell.
Lemon Squeezy for Laravel is maintained by Dries Vints. I maintain this in my free time so any sponsorship to help fund development is greatly appreciated ❤️
We also recommend to read the Lemon Squeezy docs and developer guide.
Roadmap
The below features are not yet in this package but are planned to be added in the future:
- Subscription invoices
- Usage Based Billing
- License keys
- Marketing emails check
- Create discount codes
- Nova integration
Requirements
- PHP 8.1 or higher
- Laravel 10.0 or higher
Installation
There are a few steps you'll need to take to install the package:
- Requiring the package through Composer
- Creating an API Key
- Connecting your store
- Configuring the Billable Model
- Running Migrations
- Connecting to Lemon JS
- Setting up webhooks
We'll go over each of these below.
Composer
Install the package with composer:
API Key
Next, configure your API key. Create a new key in testing mode in the Lemon Squeezy dashboard and paste them in your .env
file as shown below:
When you're deploying your app to production, you'll have to create a new key in production mode to work with live data.
Store Identifier
Your store identifier will be used when creating checkouts for your products. Go to your Lemon Squeezy stores settings and copy the Store ID (the part after the #
sign) into the env value below:
Billable Model
To make sure we can actually create checkouts for our customers, we'll need to configure a model to be our "billable" model. This is typical the User
model of your app. To do this, import and use the Billable
trait on your model:
Now your user model will have access to methods from our package to create checkouts in Lemon Squeezy for your products. Note that you can make any model type a billable as you wish. It's not required to use one specific model class.
Running Migrations
The package comes with some migrations to store data received from Lemon Squeezy by webhooks. It'll add a lemon_squeezy_customers
table which holds all info about a customer. This table is connected to a billable model of any model type you wish. It'll also add a lemon_squeezy_subscriptions
table which holds info about subscriptions. Install these migrations by simply running artisan migrate
:
If you want to customize these migrations, you can overwrite them.
Lemon JS
Lemon Squeezy uses its own JavaScript library to initiate its checkout widget. We can make use of it by loading it through the Blade directive in the head
section of our app, right before the closing </head>
tag.
Webhooks
Finally, make sure to set up incoming webhooks. This is both needed in development as in production.
Webhooks In Development
The easiest way to set this up while developing your app is with the php artisan lmsqueezy:listen
command that ships with this package. This command will setup a webhook through the Lemon Squeezy API, start listening for any events and remove the webhook when quitting the command.
Although this command should always cleanup the webhook after itself, you may wish to cleanup any lingering webhooks with the --cleanup
flag:
Currently, this command supports Ngrok and Expose.
[!WARNING]
Thelmsqueezy:listen
command is currently not supported in Windows due to the lack of signal handling. Instead you can take the manual approach from the webhooks in production docs below. You'll still need to use a service like Ngrok or Expose to expose a publically accessible url.
Webhooks In Production
For production, we'll need to setup things manually. The package already ships with a route so all that's left is to go to your Lemon Squeezy's webhook settings and point the url to your app's domain. The path you should point to is /lemon-squeezy/webhook
by default. Make sure to select all event types.
[!NOTE]
We also very much recommend to verify webhook signatures in production.
Webhooks & CSRF Protection
Incoming webhooks should not be affected by CSRF protection. To prevent this, add your webhook path to the except list of your App\Http\Middleware\VerifyCsrfToken
middleware:
Or if you're using Laravel v11 and up, you should exclude lemon-squeezy/*
in your application's bootstrap/app.php
file:
Upgrading
Please review our upgrade guide when upgrading to a new version.
Configuration
The package offers various way to configure your experience with integrating with Lemon Squeezy.
By default, we don't recommend publishing the config file as most things can be configured with environment variables. Should you still want to adjust the config file, you can publish it with the following command:
Verifying Webhook Signatures
In order to make sure that incoming webhooks are actually from Lemon Squeezy, we can configure a signing secret for them. Go to your webhook settings in the Lemon Squeezy dashboard, click on the webhook of your app and copy the signing secret into the environment variable below:
Any incoming webhook will now first be verified before being executed.
Overwriting Migrations
Lemon Squeezy for Laravel ships with some migrations to hold data sent over. If you're using something like a string based identifier for your billable model, like a UUID, or want to adjust something to the migrations you can overwrite them. First, publish these with the following command:
Then, ignore the package's migrations in your AppServiceProvider
's register
method:
Now you'll rely on your own migrations rather than the package one. Please note though that you're now responsible as well for keeping these in sync withe package one manually whenever you upgrade the package.
Commands
Below you'll find a list of commands you can run to retrieve info from Lemon Squeezy:
Command | Description |
---|---|
php artisan lmsqueezy:products |
List all available products with their variants and prices |
php artisan lmsqueezy:products 12345 |
List a specific product by its ID with its variants and prices |
Checkouts
With this package, you can easily create checkouts for your customers.
Single Payments
For example, to create a checkout for a single-payment, use a variant ID of a product variant you want to sell and create a checkout using the snippet below:
This will automatically redirect your customer to a Lemon Squeezy checkout where the customer can buy your product.
[!NOTE] When creating a checkout for your store, each time you redirect a checkout object or call
url
on the checkout object, an API call to Lemon Squeezy will be made. These calls are expensive and can be time and resource consuming for your app. If you are creating the same session over and over again you may want to cache these urls.
Custom Priced Charges
You can also overwrite the amount of a product variant by calling the charge
method on a customer:
The amount should be a positive integer in cents.
You'll still need to provide a variant ID but can overwrite the price as you see fit. One thing you can do is create a "generic" product with a specific currency which you can dynamically charge against.
Overlay Widget
Instead of redirecting your customer to a checkout screen, you can also create a checkout button which will render a checkout overlay on your page. To do this, pass the $checkout
object to a view:
Now, create the button using the shipped Laravel Blade component from the package:
When a user clicks this button, it'll trigger the Lemon Squeezy checkout overlay. You can also, optionally request it to be rendered in dark mode:
If you're checking out subscriptions, and you don't want to show the "You will be charged..." text, you may disable this by calling the withoutSubscriptionPreview
method on the checkout object:
If you want to set a different color for the checkout button you may pass a hex color code (with the leading #
sign) through withButtonColor
:
Prefill User Data
You can easily prefill user data for checkouts by overwriting the following methods on your billable model:
By default, the attributes displayed in a comment on the right of the methods will be used.
Additionally, you may also pass this data on the fly by using the following methods:
Product Details
You can overwrite additional data for product checkouts with the withProductName
and withDescription
methods:
Receipt Thank You
Additionally, you can customize the thank you note for the order receipt email.
Redirects After Purchase
To redirect customers back to your app after purchase, you may use the redirectTo
method:
You may also set a default url for this by configuring the lemon-squeezy.redirect_url
in your config file:
In order to do this you'll need to publish your config file.
Expire Checkouts
You can indicate how long a checkout session should stay active by calling the expiresAt
method on it:
Custom Data
You can also pass along custom data with your checkouts. To do this, send along key/value pairs with the checkout method:
These will then later be available in the related webhooks for you.
Reserved Keywords
When working with custom data there are a few reserved keywords for this library:
billable_id
billable_type
subscription_type
Attempting to use any of these will result in an exception being thrown.
Customers
Customer Portal
Customers may easily manage their personal data like their name, email address, etc by visiting their customer portal. Lemon Squeezy for Laravel makes it easy to redirect customers to this by calling redirectToCustomerPortal
on the billable:
In order to call this method your billable already needs to have a subscription or made a purchase through Lemon Squeezy. Also, this method will perform an underlying API call so make sure to place this redirect behind a route which you can link to in your app.
Optionally, you also get the signed customer portal url directly:
My Orders
Besides the customer portal for managing subscriptions, Lemon Squeezy also has a "My Orders" portal to manage all of your purchases for a customer account. This does involve a mixture of purchases across multiple vendors. If this is something you wish your customers can find, you can link to https://app.lemonsqueezy.com/my-orders
and tell them to login with the email address they performed the purchase with.
Orders
Lemon Squeezy allows you to retrieve a list of all orders made for your store. You can then use this list to present all orders to your customers.
Retrieving Orders
To retrieve a list of orders for a specific customer, simply call the saved models in the database:
Checking Order Status
To check if an individual order is paid, you may use the paid
method:
Besides that, you have three other checks you can do: pending
, failed
& refunded
. If the order is refunded
, you may also use the refunded_at
timestamp:
You can also check if an order was for a specific product:
Or for a specific variant:
Additionally, you may check if a customer has purchased a specific product:
Or a specific variant:
These two checks will both make sure the correct product or variant was purchased and paid for. This is useful as well if you're offering a feature in your app like lifetime access.
Subscriptions
Setting Up Subscription Products
Setting up subscription products with different plans and intervals needs to be done in a specific way. Lemon Squeezy has a good guide on how to do this.
Although you're free to choose how you set up products and plans, it's easier to go for option two and create a product for each plan type. So for example, when you have a "Basic" and "Pro" plan and both have monthly and yearly prices, it's wiser to create two separate products for these and then add two variants for each for their monthly and yearly prices.
This gives you the advantage later on to make use of the hasProduct
method on a subscription which allows you to just check if a subscription is on a specific plan type and don't worry if it's on a monthly or yearly schedule.
Creating Subscriptions
Starting subscriptions is easy. For this, we need the variant id from our product. Copy the variant id and initiate a new subscription checkout from your billable model:
When the customer has finished their checkout, the incoming SubscriptionCreated
webhook will couple it to your billable model in the database. You can then retrieve the subscription from your billable model:
Checking Subscription Status
Once a customer is subscribed to your services, you can use a variety of methods to check for various states on the subscription. The most basic example, is to check if a customer is subscribed to a valid subscription:
You may use this in various places in your app like middleware, policies, etc, to offer your services. To check if an individual subscription is valid, you may use the valid
method:
This method, as well as the subscribed
method, will return true if your subscription is active, on trial, past due, paused for free or on its cancelled grace period.
You can also check if a subscription is on a specific product:
Or on a specific variant:
If you want to check if a subscription is on a specific variant and at the same valid you can use:
Or if you're using multiple subscription types, you can pass a type as an extra parameter:
Cancelled Status
To check if a user has cancelled their subscription you may use the cancelled
method:
When they're on their grace period, you can use the onGracePeriod
check:
If a subscription is fully cancelled and no longer on its grace period, you may use the expired
check:
Past Due Status
If a recurring payment for a subscription fails, the subscription will transition in a past due state. This means it's still a valid subscription but your customer will have a 2 weeks period where their payments will be retried.
In this state, you should instruct your customer to the Lemon Squeezy documentation
Subscription Scopes
Various subscriptions scopes are available to query subscriptions in specific states:
Here's all available scopes:
Updating Payment Information
To allow your customer to update their payment details, like their credit card info, you can redirect them with the following method:
Alternatively, if you want the URL to open in a more seamless way on top of your app (similar to the checkout overlay), you may use Lemon.js to open the URL with the LemonSqueezy.Url.Open()
method. First, pass the url to a view:
Then trigger it through a button:
This requires you to have set up Lemon.js.
Changing Plans
When a customer is subscribed to a monthly plan, they might want to upgrade to a better plan, change their payments to a yearly plan or downgrade to a cheaper plan. For these situations, you can allow them to swap plans by passing a different variant id with its product id to the swap
method:
This will swap the customer to their new subscription plan but billing will only be done on the next billing cycle. If you'd like to immediately invoice the customer you may use the swapAndInvoice
method instead:
[!NOTE] You'll notice in the above methods that you both need to provide a product ID and variant ID and might wonder why that is. Can't you derive the product ID from the variant ID? Unfortuntately that's only possible when swapping to variants between the same product. When swapping to a different product alltogether you are required to also provide the product ID in the Lemon Squeezy API. Therefor, we've made the decision to make this uniform and just always require the product ID as well.
Prorations
By default, Lemon Squeezy will prorate amounts when changing plans. If you want to prevent this, you may use the noProrate
method before executing the swap:
Changing The Billing Date
To change the date of the month on which your customer gets billed for their subscription, you may use the anchorBillingCycleOn
method:
In the above example, the customer will now get billed on the 21st of each month going forward. For more info, see the Lemon Squeezy docs.
Multiple Subscriptions
In some situation you may find yourself wanting to allow your customer to subscribe to multiple subscription types. For example, a gym may offer a swimming and weight lifting subscription. You can allow your customer to subscribe to either or both.
To handle the different subscriptions you may provide a type
of subscription as the second argument to subscribe
when starting a new one:
Now you may always refer this specific subscription type by providing the type
argument when retrieving it:
Pausing Subscriptions
To pause subscriptions, call the pause
method on it:
Optionally, provide a date when the subscription can resume:
This will fill in the resumes_at
timestamp on your customer. To know if your subscription is within its paused period you can use the onPausedPeriod
method:
To unpause, simply call that method on the subscription:
Pause State
By default, pausing a subscription will void its usage for the remainder of the pause period. If you instead would like your customers to use your services for free, you may use the pauseForFree
method:
Cancelling Subscriptions
To cancel a subscription, call the cancel
method on it:
This will set your subscription to be cancelled. If your subscription is cancelled mid-cycle, it'll enter a grace period and the ends_at
column will be set. The customer will still have access to the services provided for the remainder of the period. You can check for its grace period by calling the onGracePeriod
method:
Immediate cancellation with Lemon Squeezy is not possible. To resume a subscription while it's still on its grace period, call the resume
method:
When a cancelled subscription reaches the end of its grace period it'll transition to a state of expired and won't be able to resume any longer.
Subscription Trials
For a thorough read on trialing in Lemon Squeezy, have a look at their guide.
No Payment Required
To allow people to signup for your product without having them to fill out their payment details, you may set the trial_ends_at
column when creating them as a customer:
This is what's called "a generic trial" because it's not attached to any subscription. You can use the onTrial
method to check if a customer is currently trialing your app:
Or if you specifically also want to make sure it's a generic trial, you can use the onGenericTrial
method:
You can also retrieve the ending date of the trial by calling the trialEndsAt
method:
As soon as your customer is ready, or after their trial has expired, they may start their subscription:
Please note that when a customer starts their subscription when they're still on their generic trial, their trial will be cancelled because they have started to pay for your product.
Payment required
Another option is to require payment details when people want to trial your products. This means that after the trial expires, they'll immediately be subscribed to your product. To get started with this, you'll need to configure a trial period in your product's settings. Then, let a customer start a subscription:
After your customer is subscribed, they'll enter their trial period which you configured and won't be charged until after this date. You'll need to give them the option to cancel their subscription before this time if they want.
To check if your customer is currently on their free trial, you may use the onTrial
method on both the billable or an individual subscription:
To determine if a trial has expired, you may use the hasExpiredTrial
method:
Ending Trials Early
To end a trial with payment upfront early you may use the endTrial
method on a subscription:
This method will move the billing achor to the current day and thus ending any trial period the customer had.
Handling Webhooks
Lemon Squeezy can send your app webhooks which you can react on. By default, this package already does the bulk of the work for you. If you've properly set up webhooks, it'll listen to any incoming events and update your database accordingly. We recommend enabling all event types so it's easy for you to upgrade in the future.
To listen to incoming webhooks, we have two events that will be fired:
LemonSqueezy\Laravel\Events\WebhookReceived
LemonSqueezy\Laravel\Events\WebhookHandled
The WebhookReceived
will be fired as soon as a webhook comes in but has not been handled by the package's WebhookController
. The WebhookHandled
event will be fired as soon as the webhook has been processed by the package. Both events will contain the full payload of the incoming webhook.
If you want to react to these events, you'll have to create listeners for them. For example, you may want to react to a subscription being updated:
For an example payload, take a look at the Lemon Squeezy API docs.
Laravel v11 and up will detect the listener automatically. If you're on Laravel v10 or lower, you should wire it up in your app's EventServiceProvider
:
Webhook Events
Instead of listening to the WebhookHandled
event, you may also subscribe to one of the following, dedicated package events that are fired after a webhook has been handled:
LemonSqueezy\Laravel\Events\OrderCreated
LemonSqueezy\Laravel\Events\OrderRefunded
LemonSqueezy\Laravel\Events\SubscriptionCreated
LemonSqueezy\Laravel\Events\SubscriptionUpdated
LemonSqueezy\Laravel\Events\SubscriptionCancelled
LemonSqueezy\Laravel\Events\SubscriptionResumed
LemonSqueezy\Laravel\Events\SubscriptionExpired
LemonSqueezy\Laravel\Events\SubscriptionPaused
LemonSqueezy\Laravel\Events\SubscriptionUnpaused
LemonSqueezy\Laravel\Events\SubscriptionPaymentSuccess
LemonSqueezy\Laravel\Events\SubscriptionPaymentFailed
LemonSqueezy\Laravel\Events\SubscriptionPaymentRecovered
LemonSqueezy\Laravel\Events\LicenseKeyCreated
LemonSqueezy\Laravel\Events\LicenseKeyUpdated
All of these events contain a billable $model
instance and the event $payload
. The subscription events also contain the $subscription
object. These can be accessed through their public properties.
Changelog
Check out the CHANGELOG in this repository for all the recent changes.
License
Lemon Squeezy for Laravel is open-sourced software licensed under the MIT license.
All versions of laravel with dependencies
guzzlehttp/guzzle Version ^7.0
laravel/framework Version ^10.0|^11.0
laravel/prompts Version ^0.1|^0.2|^0.3
moneyphp/money Version ^4.0
nesbot/carbon Version ^2.0|^3.0