Files
famous-ly4-ev/app-modules/payment/src/Http/Controllers/PaymentController.php
T

44 lines
1.2 KiB
PHP
Raw Normal View History

<?php
namespace Modules\Payment\Http\Controllers;
use Illuminate\Http\JsonResponse;
2026-08-16 23:50:39 +07:00
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Gate;
use Modules\Booking\Models\Booking;
use Modules\Payment\Actions\InitiatePaymentAction;
2026-08-18 11:54:34 +07:00
use Modules\Payment\Enums\PaymentStatus;
use Modules\Payment\Http\Resources\PaymentResource;
class PaymentController extends Controller
{
public function __construct(
private InitiatePaymentAction $initiatePaymentAction,
) {}
2026-08-16 23:50:39 +07:00
public function initiate(Request $request, Booking $booking): JsonResponse
{
2026-08-16 23:50:39 +07:00
$openid = $request->attributes->get('fastapi_openid');
if ($openid === null) {
2026-08-20 00:01:28 +07:00
Gate::authorize('pay', $booking);
2026-08-16 23:50:39 +07:00
}
$payment = $this->initiatePaymentAction->handle($booking);
2026-08-18 11:54:34 +07:00
if ($payment->status === PaymentStatus::Failed) {
return response()->json([
'message' => 'Payment initiation failed',
'errors' => [
'payment' => ['Payment initiation failed'],
],
], 422);
}
return (new PaymentResource($payment))
->response()
->setStatusCode(201);
}
}