Deploy Ladill POS / deploy (push) Successful in 35s
Co-authored-by: Cursor <cursoragent@cursor.com>
137 lines
5.1 KiB
PHP
137 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Pos;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Pos\ProSubscription;
|
|
use App\Models\User;
|
|
use App\Services\Pos\BillingClient;
|
|
use App\Services\Pos\SubscriptionService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class ProController extends Controller
|
|
{
|
|
public function __construct(private readonly SubscriptionService $subscriptions) {}
|
|
|
|
public function index(Request $request): View
|
|
{
|
|
$user = ladill_account() ?? $request->user();
|
|
$planKey = $this->subscriptions->planKey($user);
|
|
|
|
return view('pos.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('pos.prepaid_months', [6, 12, 24]),
|
|
'currency' => (string) config('pos.pro.currency', 'GHS'),
|
|
]);
|
|
}
|
|
|
|
public function subscribe(Request $request): RedirectResponse
|
|
{
|
|
[$ok, $message] = $this->subscriptions->subscribe(ladill_account() ?? $request->user());
|
|
|
|
return redirect()->route('pos.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('pos.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('pos.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('pos.pro.paystack.callback'),
|
|
['user_id' => $user->id],
|
|
);
|
|
} catch (\Throwable) {
|
|
return redirect()->route('pos.pro.index')
|
|
->with('error', 'Could not start Paystack checkout. Please try again.');
|
|
}
|
|
|
|
$url = trim((string) ($checkout['checkout_url'] ?? ''));
|
|
if ($url === '') {
|
|
return redirect()->route('pos.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('pos.pro.index')->with('error', 'Missing payment reference.');
|
|
}
|
|
|
|
try {
|
|
$result = $billing->verifyPlanCheckout($reference);
|
|
} catch (\Throwable) {
|
|
return redirect()->route('pos.pro.index')
|
|
->with('error', 'Could not verify payment. Contact support with reference: '.$reference);
|
|
}
|
|
|
|
if (! ($result['paid'] ?? false)) {
|
|
return redirect()->route('pos.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('pos.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' ? 'POS Business' : 'POS Pro';
|
|
|
|
return redirect()->route('pos.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('pos.pro.index')->with($ok ? 'success' : 'error', $message);
|
|
}
|
|
}
|