Add Paystack prepaid billing and fix sidebar footer spacing.
Deploy Ladill Queue / deploy (push) Successful in 2m27s

Monthly Pro/Enterprise stays on wallet; 6/12/24 month plans checkout via Paystack.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-06-30 01:55:38 +00:00
co-authored by Cursor
parent 165c7238fe
commit 07998b29b9
7 changed files with 237 additions and 21 deletions
+127 -7
View File
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Qms;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Qms\Concerns\ScopesToAccount;
use App\Models\Organization;
use App\Services\Billing\BillingClient;
use App\Services\Qms\PlanService;
use App\Services\Qms\QmsPermissions;
@@ -41,6 +42,7 @@ class ProController extends Controller
'branchCount' => $branchCount,
'canSubscribeEnterprise' => $plans->canSubscribeEnterprise($organization),
'enterpriseMinBranches' => (int) config('qms.enterprise.min_branches', 2),
'prepaidMonths' => (array) config('qms.prepaid_months', [6, 12, 24]),
'currency' => (string) config('billing.currency', 'GHS'),
'planExpiresAt' => $expiresAt,
'enterpriseBilledBranches' => (int) ($settings['enterprise_billed_branches'] ?? $branchCount),
@@ -58,7 +60,7 @@ class ProController extends Controller
->with('success', 'Your organization already has an active paid plan.');
}
return $this->chargePlan(
return $this->chargeMonthlyWallet(
$organization,
$billing,
$plans->proPriceMinor(),
@@ -87,12 +89,10 @@ class ProController extends Controller
->with('error', "Enterprise billing requires at least {$min} active branches. Add another branch or choose Pro for a single site.");
}
$priceMinor = $plans->enterprisePriceMinor($organization);
return $this->chargePlan(
return $this->chargeMonthlyWallet(
$organization,
$billing,
$priceMinor,
$plans->enterprisePriceMinor($organization),
'enterprise',
'queue_enterprise',
'queue-enterprise-'.$organization->id.'-'.now()->format('Y-m-d-His'),
@@ -102,8 +102,109 @@ class ProController extends Controller
);
}
private function chargePlan(
$organization,
public function subscribePrepaid(Request $request, PlanService $plans, BillingClient $billing): RedirectResponse
{
$this->authorizeAbility($request, 'settings.manage');
$validated = $request->validate([
'plan' => ['required', 'in:pro,enterprise'],
'months' => ['required', 'integer', 'in:6,12,24'],
]);
$organization = $this->organization($request);
if ($plans->hasPaidPlan($organization)) {
return redirect()->route('qms.pro.index')
->with('success', 'Your organization already has an active paid plan.');
}
$plan = $validated['plan'];
$months = (int) $validated['months'];
if ($plan === 'enterprise' && ! $plans->canSubscribeEnterprise($organization)) {
$min = (int) config('qms.enterprise.min_branches', 2);
return redirect()->route('qms.pro.index')
->with('error', "Enterprise billing requires at least {$min} active branches.");
}
$monthlyMinor = $plan === 'enterprise'
? $plans->enterprisePriceMinor($organization)
: $plans->proPriceMinor();
$amountMinor = $monthlyMinor * $months;
$branches = $plan === 'enterprise' ? $plans->activeBranchCount($organization) : null;
try {
$checkout = $billing->initiatePlanCheckout(
$organization->owner_ref,
$plan,
$months,
$amountMinor,
route('qms.pro.paystack.callback'),
array_filter([
'organization_id' => $organization->id,
'enterprise_billed_branches' => $branches,
], static fn ($v) => $v !== null),
);
} catch (\Throwable) {
return redirect()->route('qms.pro.index')
->with('error', 'Could not start Paystack checkout. Please try again.');
}
$url = trim((string) ($checkout['checkout_url'] ?? ''));
if ($url === '') {
return redirect()->route('qms.pro.index')
->with('error', 'Paystack did not return a checkout URL.');
}
return redirect()->away($url);
}
public function paystackCallback(Request $request, BillingClient $billing, PlanService $plans): RedirectResponse
{
$reference = (string) $request->query('reference', '');
if ($reference === '') {
return redirect()->route('qms.pro.index')->with('error', 'Missing payment reference.');
}
try {
$result = $billing->verifyPlanCheckout($reference);
} catch (\Throwable) {
return redirect()->route('qms.pro.index')
->with('error', 'Could not verify payment. Contact support with reference: '.$reference);
}
if (! ($result['paid'] ?? false)) {
return redirect()->route('qms.pro.index')
->with('error', 'Payment was not completed.');
}
$metadata = (array) ($result['metadata'] ?? []);
$organization = Organization::query()->find((int) ($metadata['organization_id'] ?? 0));
if (! $organization) {
return redirect()->route('qms.pro.index')
->with('error', 'Organization not found for this payment.');
}
$this->authorizeAbility($request, 'settings.manage');
if ($this->organization($request)->id !== $organization->id) {
abort(403);
}
$plan = (string) ($result['plan'] ?? 'pro');
$months = (int) ($result['months'] ?? 0);
$branches = isset($metadata['enterprise_billed_branches'])
? (int) $metadata['enterprise_billed_branches']
: null;
$this->activatePrepaidPlan($organization, $plan, $months, $branches);
$label = $plan === 'enterprise' ? 'Queue Enterprise' : 'Queue Pro';
return redirect()->route('qms.pro.index')
->with('success', "{$label} is active for {$months} months.");
}
private function chargeMonthlyWallet(
Organization $organization,
BillingClient $billing,
int $priceMinor,
string $plan,
@@ -139,6 +240,7 @@ class ProController extends Controller
$settings = $organization->settings ?? [];
$settings['plan'] = $plan;
$settings['auto_renew'] = true;
$settings['billing_method'] = 'wallet_monthly';
$settings['plan_expires_at'] = now()
->addDays((int) config('qms.pro.period_days', 30))
->toIso8601String();
@@ -150,4 +252,22 @@ class ProController extends Controller
return redirect()->route('qms.pro.index')->with('success', $successMessage);
}
private function activatePrepaidPlan(
Organization $organization,
string $plan,
int $months,
?int $enterpriseBranches = null,
): void {
$settings = $organization->settings ?? [];
$settings['plan'] = $plan;
$settings['auto_renew'] = false;
$settings['billing_method'] = 'paystack_prepaid';
$settings['plan_expires_at'] = now()->addMonths($months)->toIso8601String();
if ($enterpriseBranches !== null) {
$settings['enterprise_billed_branches'] = $enterpriseBranches;
}
unset($settings['plan_renewal_error']);
$organization->update(['settings' => $settings]);
}
}