Files
device-management/app/Http/Controllers/CheckoutController.php
T
Wian Drs 60946ddf37
tests / ci (push) Has been cancelled
update lisensi prosess
2026-08-25 13:43:55 +07:00

71 lines
2.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Billing\PaymentGatewayManager;
use App\Models\BillingOrder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use Throwable;
class CheckoutController extends Controller
{
public function show(string $order): Response
{
return $this->render($order);
}
public function returned(string $order): Response
{
return $this->render($order);
}
public function retry(Request $request, string $order, PaymentGatewayManager $gateways): RedirectResponse
{
$billingOrder = BillingOrder::withoutGlobalScope('tenant')->where('uuid', $order)->where('status', 'pending')->firstOrFail();
try {
$driver = $gateways->driver($billingOrder->gateway);
$methods = collect($driver->paymentMethods($billingOrder));
if ($methods->isNotEmpty()) {
$data = $request->validate(['payment_method' => ['required', 'string']]);
abort_unless($methods->contains('code', $data['payment_method']), 422, 'Kanal pembayaran tidak tersedia.');
$billingOrder->update(['payment_method' => $data['payment_method']]);
}
$billingOrder->update($driver->createCheckout($billingOrder->fresh()));
$billingOrder->update(['metadata' => collect($billingOrder->metadata ?? [])->except('gateway_error')->all()]);
} catch (Throwable $exception) {
report($exception);
return back()->withErrors(['payment' => str($exception->getMessage())->after(':')->trim()->toString() ?: 'Gateway pembayaran belum siap.']);
}
return redirect()->away($billingOrder->checkout_url);
}
private function render(string $uuid): Response
{
$order = BillingOrder::withoutGlobalScope('tenant')->where('uuid', $uuid)->firstOrFail();
$plan = config("billing.plans.{$order->plan_code}");
$methods = [];
$gatewayError = null;
if ($order->status === 'pending' && ! $order->checkout_url) {
try {
$methods = app(PaymentGatewayManager::class)->driver($order->gateway)->paymentMethods($order);
} catch (Throwable $exception) {
report($exception);
$gatewayError = str($exception->getMessage())->after(':')->trim()->toString();
}
}
return Inertia::render('checkout/show', ['order' => [
'uuid' => $order->uuid, 'plan' => $plan['name'], 'status' => $order->status,
'total' => $order->total, 'currency' => $order->currency, 'checkout_url' => $order->checkout_url,
'expires_at' => $order->expires_at?->toIso8601String(), 'gateway' => $order->gateway,
'payment_method' => $order->payment_method, 'payment_methods' => $methods,
'gateway_error' => $gatewayError,
]]);
}
}