Add Queue Pro billing, renewal, and scheduler.
Deploy Ladill Queue / deploy (push) Successful in 42s
Deploy Ladill Queue / deploy (push) Successful in 42s
Organizations can upgrade from wallet with plan expiry enforcement and nightly qms:pro-renew charges. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,27 +3,42 @@
|
||||
namespace App\Services\Qms;
|
||||
|
||||
use App\Models\Organization;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class PlanService
|
||||
{
|
||||
public function planKey(Organization $organization): string
|
||||
{
|
||||
$settings = $organization->settings ?? [];
|
||||
$plan = (string) ($settings['plan'] ?? 'free');
|
||||
|
||||
if ($plan === 'pro' && ! empty($settings['plan_expires_at'])) {
|
||||
if (Carbon::parse($settings['plan_expires_at'])->isPast()) {
|
||||
return 'free';
|
||||
}
|
||||
}
|
||||
|
||||
return array_key_exists($plan, config('qms.plans', [])) ? $plan : 'free';
|
||||
}
|
||||
|
||||
public function plan(Organization $organization): string
|
||||
{
|
||||
return (string) data_get($organization->settings, 'plan', 'free');
|
||||
return $this->planKey($organization);
|
||||
}
|
||||
|
||||
public function isPro(Organization $organization): bool
|
||||
{
|
||||
return $this->plan($organization) === 'pro';
|
||||
return $this->planKey($organization) === 'pro';
|
||||
}
|
||||
|
||||
public function maxBranches(Organization $organization): ?int
|
||||
{
|
||||
return config('qms.plans.'.$this->plan($organization).'.max_branches');
|
||||
return config('qms.plans.'.$this->planKey($organization).'.max_branches');
|
||||
}
|
||||
|
||||
public function maxQueues(Organization $organization): ?int
|
||||
{
|
||||
return config('qms.plans.'.$this->plan($organization).'.max_queues');
|
||||
return config('qms.plans.'.$this->planKey($organization).'.max_queues');
|
||||
}
|
||||
|
||||
public function canAddBranch(Organization $organization, int $currentCount): bool
|
||||
@@ -39,4 +54,9 @@ class PlanService
|
||||
|
||||
return $max === null || $currentCount < $max;
|
||||
}
|
||||
|
||||
public function proPriceMinor(): int
|
||||
{
|
||||
return (int) config('qms.plans.pro.price_minor', 9900);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Qms;
|
||||
|
||||
use App\Models\Organization;
|
||||
use App\Services\Billing\BillingClient;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class ProRenewalService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BillingClient $billing,
|
||||
private readonly PlanService $plans,
|
||||
) {}
|
||||
|
||||
/** @return Collection<int, Organization> */
|
||||
public function dueOrganizations(): Collection
|
||||
{
|
||||
return Organization::query()
|
||||
->where('settings->plan', 'pro')
|
||||
->whereNotNull('settings->plan_expires_at')
|
||||
->get()
|
||||
->filter(fn (Organization $organization) => $this->isDue($organization));
|
||||
}
|
||||
|
||||
public function isDue(Organization $organization): bool
|
||||
{
|
||||
$settings = $organization->settings ?? [];
|
||||
if (($settings['plan'] ?? '') !== 'pro') {
|
||||
return false;
|
||||
}
|
||||
if (array_key_exists('auto_renew', $settings) && $settings['auto_renew'] === false) {
|
||||
return false;
|
||||
}
|
||||
if (empty($settings['plan_expires_at'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Carbon::parse($settings['plan_expires_at'])->lte(now());
|
||||
}
|
||||
|
||||
public function renewIfDue(Organization $organization): void
|
||||
{
|
||||
if (! $this->isDue($organization)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$settings = $organization->settings ?? [];
|
||||
$price = $this->plans->proPriceMinor();
|
||||
$reference = 'queue-pro-'.$organization->id.'-'.now()->format('YmdHis');
|
||||
|
||||
try {
|
||||
$charged = $this->billing->debit(
|
||||
$organization->owner_ref,
|
||||
$price,
|
||||
'queue_pro_renewal',
|
||||
$reference,
|
||||
'Ladill Queue Pro — monthly renewal',
|
||||
);
|
||||
} catch (\Throwable) {
|
||||
$charged = false;
|
||||
}
|
||||
|
||||
if ($charged) {
|
||||
$settings['plan'] = 'pro';
|
||||
$settings['auto_renew'] = true;
|
||||
$settings['plan_expires_at'] = now()
|
||||
->addDays((int) config('qms.pro.period_days', 30))
|
||||
->toIso8601String();
|
||||
unset($settings['plan_renewal_error']);
|
||||
$organization->update(['settings' => $settings]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$expires = Carbon::parse($settings['plan_expires_at']);
|
||||
$graceEnd = $expires->copy()->addDays((int) config('qms.pro.grace_days', 3));
|
||||
if ($graceEnd->isPast()) {
|
||||
$settings['plan'] = 'free';
|
||||
unset($settings['plan_expires_at']);
|
||||
$settings['plan_renewal_error'] = 'Suspended after failed renewal.';
|
||||
} else {
|
||||
$settings['plan_renewal_error'] = 'Renewal failed — top up your wallet.';
|
||||
}
|
||||
|
||||
$organization->update(['settings' => $settings]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user