Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #50 from renoki-co/fix/double-increment
Browse files Browse the repository at this point in the history
[fix] Fixed incorrect double increment for `used_total`
  • Loading branch information
rennokki authored Oct 18, 2021
2 parents b9ba9af + a8a59c5 commit ffe383d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/Concerns/HasQuotas.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ 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) {
$exceedHandler($feature, $valueOverQuota, $this);
}
}

return tap($usage)->save();
$usage->save();

return $usage;
}

/**
Expand Down Expand Up @@ -126,7 +127,9 @@ public function reduceFeatureUsage($feature, $uses = 1, bool $incremental = true
'used_total' => $used,
]);

return tap($usage)->save();
$usage->save();

return $usage;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/StripeFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
Expand Down

0 comments on commit ffe383d

Please sign in to comment.