Deploy Ladill Frontdesk / deploy (push) Successful in 47s
Pro is GHS 990/branch/mo and Enterprise is GHS 1990/branch/mo, with the Care-style branch selector UI, self-serve Enterprise checkout, and renewal charges scaled to billed branches.
98 lines
3.2 KiB
PHP
98 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Frontdesk;
|
|
|
|
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()
|
|
->whereIn('settings->plan', ['pro', 'enterprise'])
|
|
->whereNotNull('settings->plan_expires_at')
|
|
->get()
|
|
->filter(fn (Organization $organization) => $this->isDue($organization));
|
|
}
|
|
|
|
public function isDue(Organization $organization): bool
|
|
{
|
|
$settings = $organization->settings ?? [];
|
|
if (! in_array($settings['plan'] ?? '', ['pro', 'enterprise'], true)) {
|
|
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 ?? [];
|
|
$plan = (string) ($settings['plan'] ?? 'pro');
|
|
$branches = max(
|
|
1,
|
|
(int) ($settings['billed_branches'] ?? 0),
|
|
$this->plans->activeBranchCount($organization),
|
|
);
|
|
$price = $this->plans->priceForBranches($plan, $branches);
|
|
$reference = 'frontdesk-'.$plan.'-'.$organization->id.'-'.now()->format('YmdHis');
|
|
$label = $plan === 'enterprise' ? 'Enterprise' : 'Pro';
|
|
|
|
try {
|
|
$charged = $this->billing->debit(
|
|
$organization->owner_ref,
|
|
$price,
|
|
'frontdesk_'.$plan.'_renewal',
|
|
$reference,
|
|
'Ladill Frontdesk '.$label.' — '.$branches.' branch(es) monthly renewal',
|
|
);
|
|
} catch (\Throwable) {
|
|
$charged = false;
|
|
}
|
|
|
|
if ($charged) {
|
|
$settings['plan'] = $plan;
|
|
$settings['auto_renew'] = true;
|
|
$settings['billed_branches'] = $branches;
|
|
$settings['plan_expires_at'] = now()
|
|
->addDays((int) config('frontdesk.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('frontdesk.pro.grace_days', 3));
|
|
if ($graceEnd->isPast()) {
|
|
$settings['plan'] = 'free';
|
|
unset($settings['plan_expires_at'], $settings['billed_branches']);
|
|
$settings['plan_renewal_error'] = 'Suspended after failed renewal.';
|
|
} else {
|
|
$settings['plan_renewal_error'] = 'Renewal failed — top up your wallet.';
|
|
}
|
|
|
|
$organization->update(['settings' => $settings]);
|
|
}
|
|
}
|