lucasdotvin / laravel-soulbscription

A straightforward interface to handle subscriptions and feature consumption.
https://soulbscription.lucas.vin
MIT License
601 stars 84 forks source link

Switching subscription plans can results in negative balance #29

Closed martin-ro closed 2 years ago

martin-ro commented 2 years ago

Hi, great package! I'm using it together with cashier and noticed an issue with feature consumption when switching plans.

Imagine a scenario where you have Plan A (100 min.) and Plan B (25 min.)

The user subscribes to Plan A, uses 50 minutes, and switches then to Plan B. The user has now (negative) -25 min. balance.

Maybe I missed something in the documentation?

Cheers

lucasdotvin commented 2 years ago

Hello, Martin! Thanks for reporting this! Could you elaborate and explain what you're trying to achieve? I believe some applications would benefit from this behavior, so maybe we need to create a configuration entry to define how the package should act in this case. Is your idea to erase the consumption after a plan switch? 🤘

martin-ro commented 2 years ago

Hi Lucas, probably not erasing the consumption but not allowing negative consumption for display? It could be done like this:

public function getRemainingCharges($featureName): float
    {
        if (empty($this->getFeature($featureName))) {
            return 0;
        }

        $currentConsumption = $this->getCurrentConsumption($featureName);
        $totalCharges = $this->getTotalCharges($featureName);

        return max($totalCharges - $currentConsumption, 0);
    }

I think it's just a better UX experience.

lucasdotvin commented 2 years ago

Oh, I got it! I'm gonna work on a setting entry for this behavior, then. Thanks for suggesting it! 🚀