DEV Community

Cover image for Laravel LemonSqueezy for Non-Auth Users
Serg
Serg

Posted on

Laravel LemonSqueezy for Non-Auth Users

Extending LemonSqueezy to Non-Registered Users: A Practical Guide

When it comes to online shopping, making the checkout process as smooth as possible is crucial. Many Laravel developers rely on LemonSqueezy, a handy package for managing transactions. However, Laravel LemonSqueezy is primarily designed for registered users, which can be a problem for guests.

But fear not, there's a simple solution to make LemonSqueezy work for everyone, whether they're logged in or not.

By default, Laravel LemonSqueezy's checkout features are geared towards registered users. But what about those who are just browsing without an account? That's where the LemonSqueezyService comes into play. It's a nifty tool that allows you to extend LemonSqueezy's capabilities to non-registered users hassle-free.

To get started, make sure you've added the LemonSqueezyServiceProvider to your Laravel app. Once that's done, you can use the LemonSqueezyService to interact with LemonSqueezy's API and give non-registered users access to product payment links.

Add LemonSqueezy configs into services.php


    'lemonsqueezy' => [
        'base_url' => 'https://api.lemonsqueezy.com/v1',
        'key' => env('LEMON_SQUEEZY_API_KEY'),
        'store' => env('LEMON_SQUEEZY_STORE'),
    ],
Enter fullscreen mode Exit fullscreen mode

Copy Service and ServiceProvider from the following Gist

https://gist.github.com/karakhanyans/95703b69322479c1968f41aeb8069aba

Or use Larafast Laravel Boilerplate which comes with LemonSqueezy and Stripe integrated.

Getting Product Info

The first step is fetching product details to display on your site. You can do this using the products() method:

app(LemonSqueezyService::class)->products();
Enter fullscreen mode Exit fullscreen mode

Dealing with Variants

For products with different options or variants, or even those with just one price, you'll need to handle things a bit differently. The variants() method helps you fetch relevant information based on a specific product ID:

public function variants($productId)
{
    return $this->client->get('variants?filter[product_id]='.$productId)->json();
}
Enter fullscreen mode Exit fullscreen mode

Generating Checkout URLs

For products with a single variant, you can use the buy_now_url attribute to create a direct checkout link. To include a discount code, simply append ?checkout%5Bdiscount_code%5D=YOUR_DISCOUNT_CODE to the URL.

$attributes['buy_now_url'].'?checkout%5Bdiscount_code%5D=YOUR_DISCOUNT_CODE'
Enter fullscreen mode Exit fullscreen mode

For products with multiple variants, you'll need to generate checkout URLs dynamically using the checkout() method:

public function checkout($variant)
{
    // Implementation details
}
Enter fullscreen mode Exit fullscreen mode

Example Implementation

Here's how you can use the LemonSqueezyService to fetch and display product information:

// Fetch products (cached for performance)
$products = Cache::rememberForever('lemonsqueezy-products', static fn () => app(LemonSqueezyService::class)->products());

// Process product data
$products = collect($products['data'])
    ->filter(fn ($product) => $product['attributes']['status'] === 'published')
    ->sortByDesc(fn ($product) => $product['attributes']['price'])
    ->map(function ($product) {
        // Processing logic
    });

// Use $products variable in your view files for display
Enter fullscreen mode Exit fullscreen mode

By following these steps, you can provide a seamless shopping experience for both registered and non-registered users, leveraging the power of LemonSqueezy in your Laravel application.

Top comments (0)