user(); $planKey = $this->subscriptions->planKey($user); return view('events.pro.index', [ 'subscription' => $this->subscriptions->subscriptionFor($user), 'planKey' => $planKey, 'isPro' => $planKey === ProSubscription::PLAN_PRO, 'isEnterprise' => $planKey === ProSubscription::PLAN_ENTERPRISE, 'hasPaidPlan' => $this->subscriptions->hasPaidPlan($user), 'gatingActive' => $this->subscriptions->gatingActive(), 'proPriceMinor' => $this->subscriptions->priceMinor(), 'enterprisePriceMinor' => $this->subscriptions->enterprisePriceMinor(), 'prepaidMonths' => (array) config('events.prepaid_months', [6, 12, 24]), 'currency' => (string) config('events.pro.currency', 'GHS'), ]); } public function subscribe(Request $request): RedirectResponse { [$ok, $message] = $this->subscriptions->subscribe(ladill_account() ?? $request->user()); return redirect()->route('events.pro.index')->with($ok ? 'success' : 'error', $message); } public function subscribeEnterprise(Request $request): RedirectResponse { [$ok, $message] = $this->subscriptions->subscribeEnterprise(ladill_account() ?? $request->user()); return redirect()->route('events.pro.index')->with($ok ? 'success' : 'error', $message); } public function subscribePrepaid(Request $request, BillingClient $billing): RedirectResponse { $validated = $request->validate([ 'plan' => ['required', 'in:pro,enterprise'], 'months' => ['required', 'integer', 'in:6,12,24'], ]); $user = ladill_account() ?? $request->user(); if ($this->subscriptions->hasPaidPlan($user)) { return redirect()->route('events.pro.index') ->with('success', 'You already have an active subscription.'); } $plan = $validated['plan']; $months = (int) $validated['months']; $monthlyMinor = $plan === 'enterprise' ? $this->subscriptions->enterprisePriceMinor() : $this->subscriptions->priceMinor(); try { $checkout = $billing->initiatePlanCheckout( $user, $plan, $months, $monthlyMinor * $months, route('events.pro.paystack.callback'), ['user_id' => $user->id], ); } catch (\Throwable) { return redirect()->route('events.pro.index') ->with('error', 'Could not start Paystack checkout. Please try again.'); } $url = trim((string) ($checkout['checkout_url'] ?? '')); if ($url === '') { return redirect()->route('events.pro.index') ->with('error', 'Paystack did not return a checkout URL.'); } return redirect()->away($url); } public function paystackCallback(Request $request, BillingClient $billing): RedirectResponse { $reference = (string) $request->query('reference', ''); if ($reference === '') { return redirect()->route('events.pro.index')->with('error', 'Missing payment reference.'); } try { $result = $billing->verifyPlanCheckout($reference); } catch (\Throwable) { return redirect()->route('events.pro.index') ->with('error', 'Could not verify payment. Contact support with reference: '.$reference); } if (! ($result['paid'] ?? false)) { return redirect()->route('events.pro.index') ->with('error', 'Payment was not completed.'); } $metadata = (array) ($result['metadata'] ?? []); $user = User::query()->find((int) ($metadata['user_id'] ?? 0)); $account = ladill_account() ?? $request->user(); if (! $user || ! $account || $user->id !== $account->id) { return redirect()->route('events.pro.index') ->with('error', 'Account not found for this payment.'); } $plan = (string) ($result['plan'] ?? 'pro'); $months = (int) ($result['months'] ?? 0); $this->subscriptions->activatePrepaid($user, $plan, $months); $label = $plan === 'enterprise' ? 'Events Business' : 'Events Pro'; return redirect()->route('events.pro.index') ->with('success', "{$label} is active for {$months} months."); } public function cancel(Request $request): RedirectResponse { [$ok, $message] = $this->subscriptions->cancel(ladill_account() ?? $request->user()); return redirect()->route('events.pro.index')->with($ok ? 'success' : 'error', $message); } }