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]);
}
}
+44
View File
@@ -61,4 +61,48 @@ class BillingClient
return true;
}
/**
* @param array<string, mixed> $metadata
* @return array{checkout_url: string, reference: string}
*/
public function initiatePlanCheckout(
string $publicId,
string $plan,
int $months,
int $amountMinor,
string $returnUrl,
array $metadata = [],
): array {
$res = Http::withToken($this->token())->acceptJson()->timeout(15)->post($this->base().'/plan-checkout', [
'user' => $publicId,
'app' => (string) config('billing.service', 'queue'),
'plan' => $plan,
'months' => $months,
'amount_minor' => $amountMinor,
'return_url' => $returnUrl,
'metadata' => $metadata,
]);
$res->throw();
return [
'checkout_url' => (string) $res->json('checkout_url'),
'reference' => (string) $res->json('reference'),
];
}
/** @return array<string, mixed> */
public function verifyPlanCheckout(string $reference): array
{
$res = Http::withToken($this->token())->acceptJson()->timeout(15)->post($this->base().'/plan-checkout/verify', [
'reference' => $reference,
]);
if ($res->status() === 422) {
return ['paid' => false, 'error' => $res->json('error')];
}
$res->throw();
return $res->json();
}
}
+2
View File
@@ -203,6 +203,8 @@ return [
'min_branches' => (int) env('QUEUE_ENTERPRISE_MIN_BRANCHES', 2),
],
'prepaid_months' => [6, 12, 24],
'rule_types' => [
'overflow' => 'Overflow routing',
'priority_boost' => 'Priority boost',
+3 -5
View File
@@ -89,8 +89,8 @@
@endif
</nav>
@if ($permissions->can($member, 'settings.view'))
<div class="shrink-0 border-t border-slate-200 px-3 py-3">
@if ($permissions->can($member, 'settings.view'))
<a href="{{ route('qms.settings.edit') }}"
class="group flex items-center gap-3 rounded-lg px-3 py-2 text-[13px] transition {{ $settingsActive ? 'bg-indigo-50 text-indigo-700 font-semibold' : 'text-slate-600 hover:bg-slate-50 hover:text-slate-900' }}">
<svg class="h-[18px] w-[18px] shrink-0 {{ $settingsActive ? 'text-indigo-600' : 'text-slate-400' }}" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
@@ -98,18 +98,16 @@
</svg>
<span>Settings</span>
</a>
</div>
@endif
<div class="shrink-0 {{ $permissions->can($member, 'settings.view') ? '' : 'border-t border-slate-200' }} px-3 py-3">
@php $proActive = request()->routeIs('qms.pro.*'); @endphp
@if (! empty($hasPaidPlan))
<a href="{{ route('qms.pro.index') }}" class="group flex items-center gap-3 rounded-lg px-3 py-2 text-[13px] transition {{ $proActive ? 'bg-indigo-50 text-indigo-700 font-semibold' : 'text-slate-600 hover:bg-slate-50 hover:text-slate-900' }}">
<a href="{{ route('qms.pro.index') }}" class="group {{ $permissions->can($member, 'settings.view') ? 'mt-0.5' : '' }} flex items-center gap-3 rounded-lg px-3 py-2 text-[13px] transition {{ $proActive ? 'bg-indigo-50 text-indigo-700 font-semibold' : 'text-slate-600 hover:bg-slate-50 hover:text-slate-900' }}">
<svg class="h-[18px] w-[18px] shrink-0 text-amber-500" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2.25c.3 0 .58.18.7.46l2.36 5.5 5.96.5a.75.75 0 0 1 .43 1.31l-4.53 3.9 1.36 5.83a.75.75 0 0 1-1.12.81L12 17.77l-5.16 3a.75.75 0 0 1-1.12-.81l1.36-5.83-4.53-3.9a.75.75 0 0 1 .43-1.31l5.96-.5 2.36-5.5c.12-.28.4-.46.7-.46Z"/></svg>
<span>{{ ! empty($isEnterprise) ? 'Queue Enterprise' : 'Queue Pro' }}</span>
</a>
@else
<a href="{{ route('qms.pro.index') }}" class="group flex items-center gap-3 rounded-lg bg-gradient-to-r from-indigo-600 to-emerald-500 px-3 py-2 text-[13px] font-semibold text-white shadow-sm transition hover:opacity-95">
<a href="{{ route('qms.pro.index') }}" class="group {{ $permissions->can($member, 'settings.view') ? 'mt-0.5' : '' }} flex items-center gap-3 rounded-lg bg-gradient-to-r from-indigo-600 to-emerald-500 px-3 py-2 text-[13px] font-semibold text-white shadow-sm transition hover:opacity-95">
<svg class="h-[18px] w-[18px] shrink-0" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2.25c.3 0 .58.18.7.46l2.36 5.5 5.96.5a.75.75 0 0 1 .43 1.31l-4.53 3.9 1.36 5.83a.75.75 0 0 1-1.12.81L12 17.77l-5.16 3a.75.75 0 0 1-1.12-.81l1.36-5.83-4.53-3.9a.75.75 0 0 1 .43-1.31l5.96-.5 2.36-5.5c.12-.28.4-.46.7-.46Z"/></svg>
<span>Upgrade to Pro</span>
</a>
+40 -7
View File
@@ -5,7 +5,7 @@
$enterpriseTotal = number_format($enterprisePriceMinor / 100, 0);
@endphp
<div class="mx-auto max-w-5xl space-y-6">
<div class="mx-auto max-w-5xl space-y-6" x-data="{ billing: 'monthly' }">
@if (session('success'))
<div class="rounded-xl border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm text-emerald-800">{{ session('success') }}</div>
@endif
@@ -15,7 +15,20 @@
<div class="rounded-2xl border border-slate-200 bg-white px-6 py-5">
<h1 class="text-2xl font-bold text-slate-900">Choose your Queue plan</h1>
<p class="mt-1 text-sm text-slate-600">Billed monthly from your Ladill wallet. Enterprise includes a one-time setup fee <a href="{{ $salesUrl }}" class="font-medium text-indigo-600 hover:text-indigo-800" target="_blank" rel="noopener">contact sales</a> for onboarding.</p>
<p class="mt-1 text-sm text-slate-600">
Pay monthly from your Ladill wallet, or prepay 6/12/24 months via Paystack.
Enterprise includes a one-time setup fee <a href="{{ $salesUrl }}" class="font-medium text-indigo-600 hover:text-indigo-800" target="_blank" rel="noopener">contact sales</a> for onboarding.
</p>
<div class="mt-4 flex flex-wrap gap-2">
<button type="button" @click="billing = 'monthly'"
:class="billing === 'monthly' ? 'bg-indigo-600 text-white' : 'bg-slate-100 text-slate-700 hover:bg-slate-200'"
class="rounded-lg px-3 py-1.5 text-sm font-medium transition">Monthly · wallet</button>
@foreach ($prepaidMonths as $months)
<button type="button" @click="billing = '{{ $months }}'"
:class="billing === '{{ $months }}' ? 'bg-indigo-600 text-white' : 'bg-slate-100 text-slate-700 hover:bg-slate-200'"
class="rounded-lg px-3 py-1.5 text-sm font-medium transition">{{ $months }} months · Paystack</button>
@endforeach
</div>
</div>
<div class="grid gap-5 lg:grid-cols-3">
@@ -38,7 +51,10 @@
<div class="flex flex-col rounded-2xl border border-slate-200 bg-white p-6 {{ $planKey === 'pro' ? 'ring-2 ring-indigo-500' : '' }}">
<p class="text-xs font-semibold uppercase tracking-wide text-indigo-600">Pro</p>
<p class="mt-2 text-3xl font-bold text-slate-900">{{ $currency }} {{ $proPrice }}<span class="text-base font-medium text-slate-500">/mo</span></p>
<p class="mt-1 text-sm text-slate-500">Unlimited branches & queues</p>
<p class="mt-1 text-sm text-slate-500" x-show="billing !== 'monthly'" x-cloak>
<span x-text="billing === 'monthly' ? '' : '{{ $currency }} ' + ({{ $proPriceMinor }} * billing / 100).toLocaleString() + ' total'"></span>
</p>
<p class="mt-1 text-sm text-slate-500" x-show="billing === 'monthly'">Unlimited branches & queues</p>
<ul class="mt-5 flex-1 space-y-2 text-sm text-slate-700">
<li>Advanced routing rules</li>
<li>Unlimited kiosks & displays</li>
@@ -55,10 +71,18 @@
.
</p>
@elseif ($canManage)
<form method="post" action="{{ route('qms.pro.subscribe') }}">
<form x-show="billing === 'monthly'" method="post" action="{{ route('qms.pro.subscribe') }}">
@csrf
<button class="btn-primary w-full">Upgrade to Pro</button>
<button class="btn-primary w-full">Upgrade to Pro monthly wallet</button>
</form>
<template x-for="months in @js($prepaidMonths)" :key="months">
<form x-show="billing == months" method="post" action="{{ route('qms.pro.subscribe-prepaid') }}">
@csrf
<input type="hidden" name="plan" value="pro">
<input type="hidden" name="months" :value="months">
<button type="submit" class="btn-primary w-full" x-text="'Pay ' + months + ' months via Paystack'"></button>
</form>
</template>
@else
<p class="text-sm text-slate-500">Ask an admin to upgrade.</p>
@endif
@@ -96,12 +120,21 @@
</p>
@elseif ($canManage)
@if ($canSubscribeEnterprise)
<form method="post" action="{{ route('qms.pro.subscribe-enterprise') }}">
<form x-show="billing === 'monthly'" method="post" action="{{ route('qms.pro.subscribe-enterprise') }}">
@csrf
<button class="w-full rounded-xl bg-amber-400 px-4 py-2.5 text-sm font-semibold text-slate-900 hover:bg-amber-300">
Upgrade to Enterprise {{ $currency }} {{ $enterpriseTotal }}/mo
Upgrade {{ $currency }} {{ $enterpriseTotal }}/mo wallet
</button>
</form>
<template x-for="months in @js($prepaidMonths)" :key="'ent-' + months">
<form x-show="billing == months" method="post" action="{{ route('qms.pro.subscribe-prepaid') }}">
@csrf
<input type="hidden" name="plan" value="enterprise">
<input type="hidden" name="months" :value="months">
<button type="submit" class="w-full rounded-xl bg-amber-400 px-4 py-2.5 text-sm font-semibold text-slate-900 hover:bg-amber-300"
x-text="'Pay ' + months + ' months via Paystack — {{ $currency }} ' + ({{ $enterprisePriceMinor }} * months / 100).toLocaleString()"></button>
</form>
</template>
@else
<p class="text-sm text-slate-300">Add at least {{ $enterpriseMinBranches }} active branches, or choose Pro for a single site.</p>
@endif
+2
View File
@@ -173,5 +173,7 @@ Route::middleware(['auth', 'platform.session'])->group(function () {
Route::get('/pro', [ProController::class, 'index'])->name('qms.pro.index');
Route::post('/pro/subscribe', [ProController::class, 'subscribe'])->name('qms.pro.subscribe');
Route::post('/pro/subscribe-enterprise', [ProController::class, 'subscribeEnterprise'])->name('qms.pro.subscribe-enterprise');
Route::post('/pro/subscribe-prepaid', [ProController::class, 'subscribePrepaid'])->name('qms.pro.subscribe-prepaid');
Route::get('/pro/paystack/callback', [ProController::class, 'paystackCallback'])->name('qms.pro.paystack.callback');
});
});
+17
View File
@@ -182,4 +182,21 @@ class QmsProTest extends TestCase
$this->assertSame('enterprise', $settings['plan']);
$this->assertSame(2, $settings['enterprise_billed_branches']);
}
public function test_prepaid_checkout_redirects_to_paystack(): void
{
Http::fake([
'billing.test/plan-checkout' => Http::response([
'checkout_url' => 'https://checkout.paystack.test/pay',
'reference' => 'SAP-QUEUE-TEST',
], 201),
]);
$this->actingAs($this->owner)
->post(route('qms.pro.subscribe-prepaid'), [
'plan' => 'pro',
'months' => 12,
])
->assertRedirect('https://checkout.paystack.test/pay');
}
}