organization($request); $canManage = app(FrontdeskPermissions::class)->isAdmin($this->member($request)); $settings = $organization->settings ?? []; $expiresAt = ! empty($settings['plan_expires_at']) ? Carbon::parse($settings['plan_expires_at']) : null; $planKey = $plans->planKey($organization); return view('frontdesk.pro.index', [ 'organization' => $organization, 'planKey' => $planKey, 'isPro' => $planKey === 'pro', 'isEnterprise' => $planKey === 'enterprise', 'hasPaidPlan' => $plans->hasPaidPlan($organization), 'canManage' => $canManage, 'proPriceMinor' => $plans->proPriceMinor(), 'prepaidMonths' => (array) config('frontdesk.prepaid_months', [6, 12, 24]), 'currency' => (string) config('billing.currency', 'GHS'), 'planExpiresAt' => $expiresAt, 'salesUrl' => ladill_platform_url('contact-sales'), ]); } public function subscribe(Request $request, PlanService $plans, BillingClient $billing): RedirectResponse { $this->authorizeAbility($request, 'settings.manage'); $organization = $this->organization($request); if ($plans->hasPaidPlan($organization)) { return redirect()->route('frontdesk.pro.index') ->with('success', 'Your organization already has an active paid plan.'); } if ($plans->isEnterprise($organization)) { return redirect()->route('frontdesk.pro.index') ->with('success', 'Your organization is on Frontdesk Enterprise.'); } return $this->chargeMonthlyWallet( $organization, $billing, $plans->proPriceMinor(), 'pro', 'frontdesk_pro', 'frontdesk-pro-'.$organization->id.'-'.now()->format('Y-m-d-His'), 'Ladill Frontdesk Pro — monthly subscription', 'Welcome to Frontdesk Pro — active for one month.', ); } public function subscribePrepaid(Request $request, PlanService $plans, BillingClient $billing): RedirectResponse { $this->authorizeAbility($request, 'settings.manage'); $validated = $request->validate([ 'plan' => ['required', 'in:pro'], 'months' => ['required', 'integer', 'in:6,12,24'], ]); $organization = $this->organization($request); if ($plans->hasPaidPlan($organization) || $plans->isEnterprise($organization)) { return redirect()->route('frontdesk.pro.index') ->with('success', 'Your organization already has an active plan.'); } $months = (int) $validated['months']; $amountMinor = $plans->proPriceMinor() * $months; try { $checkout = $billing->initiatePlanCheckout( $organization->owner_ref, 'pro', $months, $amountMinor, route('frontdesk.pro.paystack.callback'), ['organization_id' => $organization->id], ); } catch (\Throwable) { return redirect()->route('frontdesk.pro.index') ->with('error', 'Could not start Paystack checkout. Please try again.'); } $url = trim((string) ($checkout['checkout_url'] ?? '')); if ($url === '') { return redirect()->route('frontdesk.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('frontdesk.pro.index')->with('error', 'Missing payment reference.'); } try { $result = $billing->verifyPlanCheckout($reference); } catch (\Throwable) { return redirect()->route('frontdesk.pro.index') ->with('error', 'Could not verify payment. Contact support with reference: '.$reference); } if (! ($result['paid'] ?? false)) { return redirect()->route('frontdesk.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('frontdesk.pro.index') ->with('error', 'Organization not found for this payment.'); } $this->authorizeAbility($request, 'settings.manage'); if ($this->organization($request)->id !== $organization->id) { abort(403); } $months = (int) ($result['months'] ?? 0); $this->activatePrepaidPlan($organization, 'pro', $months); return redirect()->route('frontdesk.pro.index') ->with('success', "Frontdesk Pro is active for {$months} months."); } private function chargeMonthlyWallet( Organization $organization, BillingClient $billing, int $priceMinor, string $plan, string $billingSource, string $reference, string $description, string $successMessage, ): RedirectResponse { try { if (! $billing->canAfford($organization->owner_ref, $priceMinor)) { return redirect()->route('frontdesk.pro.index') ->with('error', 'Insufficient wallet balance. Top up your Ladill wallet to upgrade.'); } $charged = $billing->debit( $organization->owner_ref, $priceMinor, $billingSource, $reference, $description, ); } catch (\Throwable) { return redirect()->route('frontdesk.pro.index') ->with('error', 'Could not process upgrade. Please try again.'); } if (! $charged) { return redirect()->route('frontdesk.pro.index') ->with('error', 'Insufficient wallet balance. Top up your Ladill wallet to upgrade.'); } $settings = $organization->settings ?? []; $settings['plan'] = $plan; $settings['auto_renew'] = true; $settings['billing_method'] = 'wallet_monthly'; $settings['plan_expires_at'] = now() ->addDays((int) config('frontdesk.pro.period_days', 30)) ->toIso8601String(); unset($settings['plan_renewal_error']); $organization->update(['settings' => $settings]); return redirect()->route('frontdesk.pro.index')->with('success', $successMessage); } private function activatePrepaidPlan(Organization $organization, string $plan, int $months): void { $settings = $organization->settings ?? []; $settings['plan'] = $plan; $settings['auto_renew'] = false; $settings['billing_method'] = 'paystack_prepaid'; $settings['plan_expires_at'] = now()->addMonths($months)->toIso8601String(); unset($settings['plan_renewal_error']); $organization->update(['settings' => $settings]); } }