diff --git a/src/Concerns/HasQuotas.php b/src/Concerns/HasQuotas.php index b6e3e43..3fd5a0b 100644 --- a/src/Concerns/HasQuotas.php +++ b/src/Concerns/HasQuotas.php @@ -85,7 +85,6 @@ public function recordFeatureUsage($feature, $value = 1, bool $incremental = tru // to billing using metering instead of calculating the difference. $usage->fill([ 'used' => $this->getFeatureQuota($feature, $plan), - 'used_total' => $incremental ? $usage->used_total + $value : $value, ]); if ($exceedHandler) { @@ -93,7 +92,9 @@ public function recordFeatureUsage($feature, $value = 1, bool $incremental = tru } } - return tap($usage)->save(); + $usage->save(); + + return $usage; } /** @@ -126,7 +127,9 @@ public function reduceFeatureUsage($feature, $uses = 1, bool $incremental = true 'used_total' => $used, ]); - return tap($usage)->save(); + $usage->save(); + + return $usage; } /** @@ -169,6 +172,22 @@ public function getUsedQuota($feature) return $usage ? $usage->used : 0; } + /** + * Get the feature used total quota. + * + * @param \RenokiCo\CashierRegister\Feature|string|int $feature + * @return int|float + */ + public function getTotalUsedQuota($feature) + { + /** @var \RenokiCo\CashierRegister\Models\Usage|null $usage */ + $usage = $this->usage() + ->whereFeatureId($feature) + ->first(); + + return $usage ? $usage->used_total : 0; + } + /** * Get the feature quota remaining. * diff --git a/tests/StripeFeatureTest.php b/tests/StripeFeatureTest.php index 2261e3f..9aee073 100644 --- a/tests/StripeFeatureTest.php +++ b/tests/StripeFeatureTest.php @@ -95,6 +95,7 @@ public function setUp(): void Saas::meteredFeature('Metered Build Minutes', 'metered.build.minutes', 3000) ->meteredPrice(static::$stripeMeteredPriceId, 0.1, 'minute'), Saas::feature('Seats', 'teams', 10)->notResettable(), + Saas::feature('Mails', 'mails', 300), ]); Saas::plan('Yearly $100', static::$stripeYearlyPlanId) @@ -354,6 +355,27 @@ public function test_feature_usage_over_the_amount() $this->assertEquals(1, $overQuota); } + public function test_feature_usage_over_the_amount_increments_total_usage_correctly() + { + $user = factory(User::class)->create(); + + $plan = Saas::getPlan(static::$stripeMonthlyPlanId); + + $subscription = $this->createSubscription($user, $plan); + + $subscription->recordFeatureUsage('mails', 100); + $subscription->recordFeatureUsage('mails', 100); + $subscription->recordFeatureUsage('mails', 100); + + $this->assertEquals(300, $subscription->getUsedQuota('mails')); + $this->assertEquals(300, $subscription->getTotalUsedQuota('mails')); + + $subscription->recordFeatureUsage('mails', 100); + + $this->assertEquals(300, $subscription->getUsedQuota('mails')); + $this->assertEquals(400, $subscription->getTotalUsedQuota('mails')); + } + public function test_feature_usage_over_the_amount_with_metering() { $user = factory(User::class)->create(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 998acc1..b2c328a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -45,7 +45,7 @@ protected function getPackageProviders($app) { return [ \Laravel\Cashier\CashierServiceProvider::class, - \Laravel\Paddle\CashierServiceProvider ::class, + \Laravel\Paddle\CashierServiceProvider::class, \RenokiCo\CashierRegister\CashierRegisterServiceProvider::class, ]; }