PHP code example of lemonsqueezy / laravel

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

protected $except = [
    'lemon-squeezy/*',
];

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

use LemonSqueezy\Laravel\LemonSqueezy;

public function register(): void
{
    LemonSqueezy::ignoreMigrations();
}

use Illuminate\Http\Request;
 
Route::get('/buy', function (Request $request) {
    return $request->user()->checkout('variant-id');
});

use Illuminate\Http\Request;
 
Route::get('/buy', function (Request $request) {
    return $request->user()->charge(2500, 'variant-id');
});

use Illuminate\Http\Request;
 
Route::get('/buy', function (Request $request) {
    $checkout = $request->user()->checkout('variant-id');

    return view('billing', ['checkout' => $checkout]);
});

$request->user()->subscribe('variant-id')
    ->withoutSubscriptionPreview();

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

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');
});

$request->user()->checkout('variant-id')
    ->withProductName('Ebook')
    ->withDescription('A thrilling novel!');

$request->user()->checkout('variant-id')
    ->withThankYouNote('Thanks for your purchase!');

$request->user()->checkout('variant-id')
    ->redirectTo(url('/'));

'redirect_url' => 'https://my-app.com/dashboard',

$request->user()->checkout('variant-id')
    ->expiresAt(now()->addDays(3));

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();

Subscription::query()->onTrial();
Subscription::query()->active();
Subscription::query()->paused();
Subscription::query()->pastDue();
Subscription::query()->unpaid();
Subscription::query()->cancelled();
Subscription::query()->expired();

use Illuminate\Http\Request;
 
Route::get('/update-payment-info', function (Request $request) {
    $subscription = $request->user()->subscription();

    return view('billing', [
        'paymentMethodUrl' => $subscription->updatePaymentMethodUrl(),
    ]);
});

use Illuminate\Http\Request;
 
Route::get('/update-payment-info', function (Request $request) {
    $subscription = $request->user()->subscription();

    return view('billing', [
        'paymentMethodUrl' => $subscription->updatePaymentMethodUrl(),
    ]);
});

use App\Models\User;

$user = User::find(1);

$user->subscription()->swap('product-id', 'variant-id');

$user = User::find(1);

$user->subscription()->swapAndInvoice('product-id', 'variant-id');

$user = User::find(1);

$user->subscription()->noProrate()->swap('product-id', 'variant-id');

$user = User::find(1);

$user->subscription()->anchorBillingCycleOn(21);

$user = User::find(1);

$checkout = $user->subscribe('variant-id', 'swimming');

$user = User::find(1);

// Retrieve the swimming subscription type...
$subscription = $user->subscription('swimming');

// Swap plans for the gym subscription type...
$user->subscription('gym')->swap('product-id', 'variant-id');

// Cancel the swimming subscription...
$user->subscription('swimming')->cancel();

$user = User::find(1);

$user->subscription()->pause();

$user = User::find(1);

$user->subscription()->pause(
    now()->addDays(5)
);

if ($user->subscription()->onPausedPeriod()) {
    // ...
}

$user->subscription()->unpause();

$user->subscription()->pauseForFree();

$user = User::find(1);

$user->subscription()->cancel();

if ($user->subscription()->onGracePeriod()) {
    // ...
}

$user->subscription()->resume();

use App\Models\User;
 
$user = User::create([
    // ...
]);
 
$user->createAsCustomer([
    'trial_ends_at' => now()->addDays(10)
]);

if ($user->onTrial()) {
    // User is within their trial period...
}

if ($user->onGenericTrial()) {
    // User is within their "generic" trial period...
}

if ($user->onTrial()) {
    $trialEndsAt = $user->trialEndsAt();
}

use Illuminate\Http\Request;

Route::get('/buy', function (Request $request) {
    return $request->user()->subscribe('variant-id');
});

use Illuminate\Http\Request;

Route::get('/buy', function (Request $request) {
    return $request->user()->subscribe('variant-id');
});

if ($user->onTrial()) {
    // ...
}
 
if ($user->subscription()->onTrial()) {
    // ...
}

if ($user->hasExpiredTrial()) {
    // ...
}
 
if ($user->subscription()->hasExpiredTrial()) {
    // ...
}

$user = User::find(1);

$user->subscription()->endTrial();


 
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
bash
php artisan migrate
bash
php artisan lmsqueezy:listen
bash
php artisan lmsqueezy:listen --cleanup
bash
php artisan vendor:publish --tag="lemon-squeezy-config"
ini
LEMON_SQUEEZY_SIGNING_SECRET=your-webhook-signing-secret
bash
php artisan vendor:publish --tag="lemon-squeezy-migrations"