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.'); } }