> ## Documentation Index
> Fetch the complete documentation index at: https://docs.larafast.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LemonSqueezy

> Documentation for Larafast

### Connect LemonSqueezy to Project

1. Create a [LemonSqueezy Account](https://www.lemonsqueezy.com/) and set up your store

### Setup API Keys

To connect LemonSqueezy to the Larafast project you need to set API keys.

1. Create an API token in your account from [https://app.lemonsqueezy.com/settings/api](https://app.lemonsqueezy.com/settings/api)
2. Add `LEMON_SQUEEZY_API_KEY=` variable to .env file with API key
3. Add  Store ID to .env `LEMON_SQUEEZY_STORE=`

<Tip>
  You can find your store ID in your [Settings->Stores](https://app.lemonsqueezy.com/settings/stores) page
</Tip>

<img src="https://mintcdn.com/larafast/p7p6xbnL6MINm0mR/images/features/payments/lemonsqueezy/store_id.jpg?fit=max&auto=format&n=p7p6xbnL6MINm0mR&q=85&s=f614f3fa51e6532a87b4328d171a25f5" alt="title" width="839" height="375" data-path="images/features/payments/lemonsqueezy/store_id.jpg" />

P.S. Use it without #, (made that mistake by myself :D )

### Migrations

To publish LemonSqueezy migration files, run the following command

```bash theme={null}
php artisan vendor:publish --tag="lemon-squeezy-migrations"
```

### Subscriptions

Create your products in LemonSqueezy -> [https://app.lemonsqueezy.com/products](https://app.lemonsqueezy.com/products) and choose "Subscription" for the pricing option.&#x20;

<img src="https://mintcdn.com/larafast/p7p6xbnL6MINm0mR/images/features/payments/lemonsqueezy/subscription.avif?fit=max&auto=format&n=p7p6xbnL6MINm0mR&q=85&s=f43b7738e45e22c59308f28d55f55868" alt="title" width="768" height="338" data-path="images/features/payments/lemonsqueezy/subscription.avif" />

After the product is created, copy the Product ID and Variant ID to use in the subscription checkout URL.\
To find a Product ID and Variant ID, go to your products page-> [https://app.lemonsqueezy.com/products](https://app.lemonsqueezy.com/products)&#x20;

<img src="https://mintcdn.com/larafast/p7p6xbnL6MINm0mR/images/features/payments/lemonsqueezy/copy.avif?fit=max&auto=format&n=p7p6xbnL6MINm0mR&q=85&s=1c2517c910d4aa9a78faa68af4df976b" alt="title" width="768" height="318" data-path="images/features/payments/lemonsqueezy/copy.avif" />

Unlike Stripe, LemonSqueezy requires and product and variant ID to be sent for checkout.&#x20;

Checkout URL:

```php web.php theme={null}
Route::get('subscription-checkout/{productId}/{variantId}', [LemonSqueezyController::class, 'subscriptionCheckout'])->name('subscription.checkout');
```

**That's it. You are ready to accept subscriptions 🙌**

### Single Payment Products

Create your products in LemonSqueezy -> [https://app.lemonsqueezy.com/products](https://app.lemonsqueezy.com/products) and choose "Single Payment" for the pricing option.&#x20;

<img src="https://mintcdn.com/larafast/p7p6xbnL6MINm0mR/images/features/payments/lemonsqueezy/single.avif?fit=max&auto=format&n=p7p6xbnL6MINm0mR&q=85&s=a3489b9799056ad5af8946f6735434a4" alt="title" width="768" height="353" data-path="images/features/payments/lemonsqueezy/single.avif" />

After the product is created, copy the Variant ID to use in the product checkout URL.\
To find a Variant ID, go to your products page-> [https://app.lemonsqueezy.com/products](https://app.lemonsqueezy.com/products)&#x20;

<img src="https://mintcdn.com/larafast/p7p6xbnL6MINm0mR/images/features/payments/lemonsqueezy/variant.avif?fit=max&auto=format&n=p7p6xbnL6MINm0mR&q=85&s=889bf9f7bad74eef9d97a66d985ebbef" alt="title" width="768" height="382" data-path="images/features/payments/lemonsqueezy/variant.avif" />

For Single Payment products, you need only variant ID

Checkout URL:

```php web.php theme={null}
Route::get('product-checkout/{variantId}', [LemonSqueezyController::class, 'productCheckout'])->name('product.checkout');
```

<Tip>
  If you want to have a product checkout without logged in users, use LemonSqueezyService.php to retrieve products and product buy now links.
</Tip>

#### LemonSqueezy URL's

Larafast comes with all URL's pre-defined, you can find them in **routes/web.php**

```php web.php theme={null}
Route::prefix('lemonsqueezy')->name('lemonsqueezy.')->group(function () {
    Route::get('subscription-checkout/{productId}/{variantId}', [LemonSqueezyController::class, 'subscriptionCheckout'])->name('subscription.checkout');
    // If your product checkout does not require auth user,
    // move this part outside "auth:sanctum" middleware and change the logic inside method
    Route::get('product-checkout/{variantId}', [LemonSqueezyController::class, 'productCheckout'])->name('product.checkout');
    Route::get('billing', [LemonSqueezyController::class, 'billing'])->name('billing'); // Redirects to Customer Portal
});
```

### Middleware

Use pre-defined `Http/Middleware/Subscribed` middleware for subscription-protected routes.

```php web.php theme={null}
Route::middleware([Subscribed::class])->group(function () {
    // Add endpoints that are only for subscribed users
});
```

### Customer Portal

To redirect users to their Customer Portal for Subscription and Orders management use /billing route

```php web.php theme={null}
Route::get('billing', [LemonSqueezyController::class, 'billing'])->name('billing'); // Redirects to Customer Portal
```

### Webhooks

Finally, make sure to set up incoming webhooks. This is both needed in development and 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 theme={null}
php artisan lmsqueezy:listen
```

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

```bash theme={null}
php artisan lmsqueezy:listen --cleanup
```

Currently, this command supports [Ngrok](https://ngrok.com/) and [Expose](https://github.com/beyondcode/expose).

**Webhooks In Production**

For production, we'll need to set things manually. Go to [your Lemon Squeezy's webhook settings](https://app.lemonsqueezy.com/settings/webhooks) 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.

<Tip>
  We also very much recommend to [verify webhook signatures](https://github.com/lmsqueezy/laravel#verifying-webhook-signatures) in production.
</Tip>

Add  `LEMON_SQUEEZY_SIGNING_SECRET=` in your .env file

<Tip>
  To read more about Laravel and LemonSqueezy integration check the LemonSqueezy Laravel package doc-> [https://github.com/lmsqueezy/laravel](https://github.com/lmsqueezy/laravel)
</Tip>
