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'),
],
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();
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();
}
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'
For products with multiple variants, you'll need to generate checkout URLs dynamically using the checkout()
method:
public function checkout($variant)
{
// Implementation details
}
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
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)