一个用于轻松将您的 Laravel 应用程序与 Lemon Squeezy 集成的包。
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.
We also recommend to read the Lemon Squeezy docs and developer guide.
The below features are not yet in this package but are planned to be added in the future:
There are a few steps you'll need to take to install the package:
We'll go over each of these below.
Install the package with composer:
composer require lemonsqueezy/laravelNext, 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:
LEMON_SQUEEZY_API_KEY=your-lemon-squeezy-api-keyWhen you're deploying your app to production, you'll have to create a new key in production mode to work with live data.
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:
LEMON_SQUEEZY_STORE=your-lemon-squeezy-store-idTo 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:
use LemonSqueezy\Laravel\Billable;
class User extends Authenticatable
{
use Billable;
}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.
[!NOTE] Every action on the library like charging, creating checkouts or subscribing will automatically create a customer in Lemon Squeezy for you and connect it to your billable. It should never be necessary to manually create a customer with the Lemon Squeezy API.
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:
php artisan migrateIf you want to customize these migrations, you can overwrite them.
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 `` tag.
...
@lemonJS
Finally, make sure to set up incoming webhooks. This is both needed in development as in production.
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.
php artisan lmsqueezy:listenFor ngrok, if your app is not running on port 8000 pass in the port you want to tunnel using:
php artisan lmsqueezy:listen ngrok --port=80Although this command should always cleanup the webhook after itself, you may wish to cleanup any lingering webhooks with the --cleanup flag:
php artisan lmsqueezy:listen --cleanupCurrently, this command supports Ngrok and Expose.
[!WARNING]
Thelmsqueezy:listencommand 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.
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.
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:
protected $except = [
'lemon-squeezy/*',
];Or if you're using Laravel v11 and up, you should exclude lemon-squeezy/* in your application's bootstrap/app.php file:
->withMiddleware(function (Middleware $middleware) {
$middleware->validateCsrfTokens(except: [
'lemon-squeezy/*',
]);
})Please review our upgrade guide when upgrading to a new version.
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:
php artisan vendor:publish --tag="lemon-squeezy-config"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:
LEMON_SQUEEZY_SIGNING_SECRET=your-webhook-signing-secretAny incoming webhook will now first be verified before being executed.
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:
php artisan vendor:publish --tag="lemon-squeezy-migrations"Then, ignore the package's migrations in your AppServiceProvider's register method:
use LemonSqueezy\Laravel\LemonSqueezy;
public function register(): void
{
LemonSqueezy::ignoreMigrations();
}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.
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 |
php artisan lmsqueezy:licenses 12345 |
List licenses generated for a given product ID |
php artisan lmsqueezy:licenses -p 3 -s 20 |
List the paginated result of all generated licenses |
php artisan lmsqueezy:licenses --order=1234 --status=active |
List active licenses for a given order ID |
With this package, you can easily create checkouts for your customers.
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:
use Illuminate\Http\Request;
Route::get('/buy', function (Request $request) {
return $request->user()->checkout('variant-id');
});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
urlon 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.
You can also overwrite the amount of a product variant by calling the charge method on a customer:
use Illuminate\Http\Request;
Route::get('/buy', function (Request $request) {
return $request->user()->charge(2500, 'variant-id');
});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.
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:
use Illuminate\Http\Request;
Route::get('/buy', function (Request $request) {
$checkout = $request->user()->checkout('variant-id');
return view('billing', ['checkout' => $checkout]);
});Now, create the button using the shipped Laravel Blade component from the package:
Buy Product
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:
Buy Product
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:
$request->user()->subscribe('variant-id')
->withoutSubscriptionPreview();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:
$request->user()->checkout('variant-id')
->withButtonColor('#FF2E1F');You can easily prefill user data for checkouts by overwriting the following methods on your billable model:
public function lemonSqueezyName(): ?string; // name
public function lemonSqueezyEmail(): ?string; // email
public function lemonSqueezyCountry(): ?string; // country
public function lemonSqueezyZip(): ?string; // zip
public function lemonSqueezyTaxNumber(): ?string; // tax_numberBy 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:
use Illuminate\Http\Request;
Route::get('/buy', function (Request $暂无开放 Issues,或尚未同步最近议题。