lmsqueezy / laravel

A package to easily integrate your Laravel application with Lemon Squeezy.
https://lemonsqueezy.com
MIT License
521 stars 51 forks source link

Add, update and remove credits to each subscription #73

Closed NicolasMelian closed 5 months ago

NicolasMelian commented 9 months ago

Lemon Squeezy for Laravel Version

1.4

Laravel Version

10.10

PHP Version

8.3

Description

Hi guys! I have a website where I charge per image since I pay for the API, it occurred to me that the best idea and the one most people use is to add credits to each subscription. Weekly 100- Monthly: 1000 - Annual: 10000.

Now, I'm not sure if the logic I applied is correct and recommended! I was reading this package like the Lemon Squeezy documentation to do my best, but I'm a newbie. Here I leave them.

Create 3 listeners:

Steps To Reproduce

  1. AddCreditsForNewSub with this function:
use LemonSqueezy\Laravel\Events\SubscriptionCreated;



    public function handle(SubscriptionCreated $event): void
    {
      $user = $event->billable;

      $creditsToAdd = 0;

       $variantId = $event->payload['data']['attributes']['variant_id'];

        if($variantId == 216428){
            $creditsToAdd = 100;
        } else if($variantId == 216419){
            $creditsToAdd = 1000;
        } else if($variantId == 216426){
            $creditsToAdd = 1000;
        }

        $user->credits += $creditsToAdd;
        $user->save();
    }

UpdateCredits (adding the credits when the subscription renews automatically and when they go from monthly to annual) is the same as above, except that I put:

use LemonSqueezy\Laravel\Events\SubscriptionUpdated;

DeleteCredits (delete the credits on the day the subscription ends, according to the documentation I understood that the SubscriptionExpired webhook is triggered on the day the subscription ends)

use LemonSqueezy\Laravel\Events\SubscriptionExpired;

    public function handle(SubscriptionUpdated $event): void
    {
      $user = $event->billable;
      $creditsToAdd = 0;

        $variantId = $event->payload['data']['attributes']['variant_id'];

        $cancelled = $event->payload['data']['attributes']['canceled'];

        if($variantId == 220254 && $cancelled == false){
            $creditsToAdd = 100;
        } else if($variantId == 220255 && $cancelled == false){
            $creditsToAdd = 1000;
        } else if($variantId == 220256 && $cancelled == false){
            $creditsToAdd = 1000;
        }

        $user->credits = $creditsToAdd;
        $user->save();
    }

EventServiceProvider:

 protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        SubscriptionCreated::class => [
            AddCreditsForSubscription::class,
        ],
        SubscriptionUpdated::class => [
            UpdateCredits::class,
        ],
        SubscriptionExpired::class => [
            DeleteCredits::class,
        ],
    ];


(I'm sorry, I marked it as a bug without realizing it.)

driesvints commented 5 months ago

Hey @NicolasMelian. This indeed seems more like a feature request and more something for usage based billing which this package doesn't support yet. Please see https://github.com/lmsqueezy/laravel/issues/55