MeteredBillingRestrictionsEngine update with the new rule for dunning

This commit is contained in:
Čarodej
2022-06-12 14:53:58 +02:00
parent 119780317d
commit ebf1b16aa5
6 changed files with 30 additions and 1 deletions

View File

@@ -196,7 +196,7 @@ class User extends Authenticatable implements MustVerifyEmail
public function __call($method, $parameters)
{
if (str_starts_with($method, 'can')) {
if (str_starts_with($method, 'can') || str_starts_with($method, 'get')) {
return resolve(RestrictionsManager::class)
->driver()
->$method($this, ...$parameters);

View File

@@ -69,6 +69,7 @@ class UserResource extends JsonResource
'canCreateFolder' => $this->canCreateFolder(),
'canCreateTeamFolder' => $this->canCreateTeamFolder(),
'canInviteTeamMembers' => $this->canInviteTeamMembers(),
'reason' => $this->getRestrictionReason(),
],
$this->mergeWhen($isFixedSubscription, fn () => [
'limitations' => $this->limitations->summary(),

View File

@@ -47,4 +47,9 @@ class DefaultRestrictionsEngine implements RestrictionsEngine
{
return true;
}
public function getRestrictionReason(User $user): string|null
{
// TODO: Implement getRestrictionReason() method.
}
}

View File

@@ -43,4 +43,9 @@ class FixedBillingRestrictionsEngine implements RestrictionsEngine
{
return true;
}
public function getRestrictionReason(User $user): string|null
{
// TODO: Implement getRestrictionReason() method.
}
}

View File

@@ -66,6 +66,22 @@ class MeteredBillingRestrictionsEngine implements RestrictionsEngine
return $this->checkFailedPayments($user);
}
public function getRestrictionReason(User $user): string|null
{
if ($this->getDunningSequenceCount($user) === 3) {
return match ($user->dunning->type) {
'limit_usage_in_new_accounts' => 'Please make your first payment.',
'usage_bigger_than_balance' => 'Please increase your account balance.',
};
}
if (! $this->checkFailedPayments($user)) {
return 'Please update your credit card.';
}
return null;
}
private function getDunningSequenceCount(User $user): int
{
return cache()->remember("dunning-count.$user->id", 3600, fn () => $user?->dunning->sequence ?? 0);

View File

@@ -16,4 +16,6 @@ interface RestrictionsEngine
public function canInviteTeamMembers(User $user, array $newInvites = []): bool;
public function canVisitShared(User $user): bool;
public function getRestrictionReason(User $user): string|null;
}