百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

laravel

> 后端框架
开源

一个用于轻松将您的 Laravel 应用程序与 Lemon Squeezy 集成的包。

590 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个用于轻松将您的 Laravel 应用程序与 Lemon Squeezy 集成的包。

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.

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
  • 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:

  1. Requiring the package through Composer
  2. Creating an API Key
  3. Connecting your store
  4. Configuring the Billable Model
  5. Running Migrations
  6. Connecting to Lemon JS
  7. Setting up webhooks

We'll go over each of these below.

Composer

Install the package with composer:

bash
composer require lemonsqueezy/laravel

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:

ini
LEMON_SQUEEZY_API_KEY=your-lemon-squeezy-api-key

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:

ini
LEMON_SQUEEZY_STORE=your-lemon-squeezy-store-id

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:

php
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.

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:

bash
php 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 `` tag.

blade

    ...
 
    @lemonJS

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.

bash
php artisan lmsqueezy:listen

For ngrok, if your app is not running on port 8000 pass in the port you want to tunnel using:

aiignore
php artisan lmsqueezy:listen ngrok --port=80

Although this command should always cleanup the webhook after itself, you may wish to cleanup any lingering webhooks with the --cleanup flag:

bash
php artisan lmsqueezy:listen --cleanup

Currently, this command supports Ngrok and Expose.

[!WARNING]
The lmsqueezy: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:

php
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:

php
->withMiddleware(function (Middleware $middleware) {
    $middleware->validateCsrfTokens(except: [
        'lemon-squeezy/*',
    ]);
})

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:

bash
php artisan vendor:publish --tag="lemon-squeezy-config"

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:

ini
LEMON_SQUEEZY_SIGNING_SECRET=your-webhook-signing-secret

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:

bash
php artisan vendor:publish --tag="lemon-squeezy-migrations"

Then, ignore the package's migrations in your AppServiceProvider's register method:

php
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.

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
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

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:

php
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 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:

php
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.

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:

php
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:

blade

    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:

blade

    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:

php
$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:

php
$request->user()->checkout('variant-id')
    ->withButtonColor('#FF2E1F');

Prefill User Data

You can easily prefill user data for checkouts by overwriting the following methods on your billable model:

php
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_number

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:

php
use Illuminate\Http\Request;
 
Route::get('/buy', function (Request $

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPbillinglaravellemonsqueezyphp

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架