1. Go to this page and download the library: Download lemonsqueezy/laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lemonsqueezy / laravel example snippets
use LemonSqueezy\Laravel\Billable;
class User extends Authenticatable
{
use Billable;
}
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
use Illuminate\Http\Request;
Route::get('/buy', function (Request $request) {
return $request->user()->checkout('variant-id')
->withName('John Doe')
->withEmail('[email protected]')
->withBillingAddress('US', '10038') // Country & Zip Code
->withTaxNumber('123456679')
->withDiscountCode('PROMO');
});
use Illuminate\Http\Request;
Route::get('/buy', function (Request $request) {
return $request->user()->checkout('variant-id', custom: ['foo' => 'bar']);
});
use Illuminate\Http\Request;
Route::get('/customer-portal', function (Request $request) {
return $request->user()->redirectToCustomerPortal();
});
$url = $user->customerPortalUrl();
if ($order->paid()) {
// ...
}
if ($order->hasProduct('your-product-id')) {
// ...
}
if ($order->hasVariant('your-variant-id')) {
// ...
}
if ($user->hasPurchasedProduct('your-product-id')) {
// ...
}
if ($user->hasPurchasedVariant('your-variant-id')) {
// ...
}
use Illuminate\Http\Request;
Route::get('/subscribe', function (Request $request) {
return $request->user()->subscribe('variant-id');
});
$subscription = $user->subscription();
if ($user->subscribed()) {
// ...
}
if ($user->subscription()->valid()) {
// ...
}
if ($user->subscription()->hasProduct('your-product-id')) {
// ...
}
if ($user->subscription()->hasVariant('your-variant-id')) {
// ...
}
if ($user->subscribedToVariant('your-variant-id')) {
// ...
}
if ($user->subscribed('swimming')) {
// ...
}
if ($user->subscribedToVariant('your-variant-id', 'swimming')) {
// ...
}
if ($user->subscription()->cancelled()) {
// ...
}
if ($user->subscription()->onGracePeriod()) {
// ...
}
if ($user->subscription()->expired()) {
// ...
}
if ($user->subscription()->pastDue()) {
// ...
}
// Get all active subscriptions...
$subscriptions = Subscription::query()->active()->get();
// Get all of the cancelled subscriptions for a specific user...
$subscriptions = $user->subscriptions()->cancelled()->get();
namespace App\Listeners;
use LemonSqueezy\Laravel\Events\WebhookHandled;
class LemonSqueezyEventListener
{
/**
* Handle received Lemon Squeezy webhooks.
*/
public function handle(WebhookHandled $event): void
{
if ($event->payload['meta']['event_name'] === 'subscription_updated') {
// Handle the incoming event...
}
}
}
namespace App\Providers;
use App\Listeners\LemonSqueezyEventListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use LemonSqueezy\Laravel\Events\WebhookHandled;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
WebhookHandled::class => [
LemonSqueezyEventListener::class,
],
];
}
ini
LEMON_SQUEEZY_API_KEY=your-lemon-squeezy-api-key