Add Care Pro billing, renewal, and scheduler.
Deploy Ladill Care / deploy (push) Successful in 35s

Organizations can subscribe from wallet, expire correctly on free tier, and renew nightly via care:pro-renew.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-06-30 01:07:23 +00:00
co-authored by Cursor
parent cd415e918f
commit 4bc485dfea
10 changed files with 403 additions and 4 deletions
@@ -0,0 +1,22 @@
<?php
namespace App\Console\Commands;
use App\Services\Care\ProRenewalService;
use Illuminate\Console\Command;
class RenewProSubscriptionsCommand extends Command
{
protected $signature = 'care:pro-renew';
protected $description = 'Charge the Ladill wallet for due Care Pro subscriptions (and downgrade after the grace window).';
public function handle(ProRenewalService $renewals): int
{
$due = $renewals->dueOrganizations();
$this->info("Renewing {$due->count()} due organization(s).");
$due->each(fn ($organization) => $renewals->renewIfDue($organization));
return self::SUCCESS;
}
}
@@ -0,0 +1,92 @@
<?php
namespace App\Http\Controllers\Care;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
use App\Services\Billing\BillingClient;
use App\Services\Care\CarePermissions;
use App\Services\Care\PlanService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\View\View;
class ProController extends Controller
{
use ScopesToAccount;
public function index(Request $request, PlanService $plans): View
{
$organization = $this->organization($request);
$canManage = app(CarePermissions::class)->can($this->member($request), 'settings.manage');
$settings = $organization->settings ?? [];
$expiresAt = ! empty($settings['plan_expires_at'])
? Carbon::parse($settings['plan_expires_at'])
: null;
return view('care.pro.index', [
'organization' => $organization,
'isPro' => $plans->isPro($organization),
'canManage' => $canManage,
'priceMinor' => $plans->proPriceMinor(),
'currency' => (string) config('billing.currency', 'GHS'),
'planExpiresAt' => $expiresAt,
'proFeatures' => [
'Unlimited branches',
'Full clinical workflows',
'Laboratory & pharmacy modules',
'Finance reports & audit exports',
'Queue integration',
],
]);
}
public function subscribe(Request $request, PlanService $plans, BillingClient $billing): RedirectResponse
{
$this->authorizeAbility($request, 'settings.manage');
$organization = $this->organization($request);
if ($plans->isPro($organization)) {
return redirect()->route('care.pro.index')
->with('success', 'Your organization is already on Care Pro.');
}
$priceMinor = $plans->proPriceMinor();
try {
if (! $billing->canAfford($organization->owner_ref, $priceMinor)) {
return redirect()->route('care.pro.index')
->with('error', 'Insufficient wallet balance. Top up your Ladill wallet to upgrade to Pro.');
}
$charged = $billing->debit(
$organization->owner_ref,
$priceMinor,
'care_pro',
'care-pro-'.$organization->id.'-'.now()->format('Y-m-d-His'),
'Ladill Care Pro — monthly subscription',
);
} catch (\Throwable) {
return redirect()->route('care.pro.index')
->with('error', 'Could not process upgrade. Please try again.');
}
if (! $charged) {
return redirect()->route('care.pro.index')
->with('error', 'Insufficient wallet balance. Top up your Ladill wallet to upgrade to Pro.');
}
$settings = $organization->settings ?? [];
$settings['plan'] = 'pro';
$settings['auto_renew'] = true;
$settings['plan_expires_at'] = now()
->addDays((int) config('care.pro.period_days', 30))
->toIso8601String();
unset($settings['plan_renewal_error']);
$organization->update(['settings' => $settings]);
return redirect()->route('care.pro.index')
->with('success', 'Welcome to Care Pro — active for one month.');
}
}
+18 -3
View File
@@ -3,22 +3,37 @@
namespace App\Services\Care;
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('care.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
{
$plan = config('care.plans.'.$this->plan($organization));
$plan = config('care.plans.'.$this->planKey($organization));
return $plan['max_branches'] ?? null;
}
+89
View File
@@ -0,0 +1,89 @@
<?php
namespace App\Services\Care;
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 = 'care-pro-'.$organization->id.'-'.now()->format('YmdHis');
try {
$charged = $this->billing->debit(
$organization->owner_ref,
$price,
'care_pro_renewal',
$reference,
'Ladill Care Pro — monthly renewal',
);
} catch (\Throwable) {
$charged = false;
}
if ($charged) {
$settings['plan'] = 'pro';
$settings['auto_renew'] = true;
$settings['plan_expires_at'] = now()
->addDays((int) config('care.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('care.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]);
}
}