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:
@@ -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.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user