From 6db74f557b35c47559a76c11bb157ae95eb1a3eb Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Tue, 2 Jun 2026 14:03:58 +0330 Subject: [PATCH] add authorization to all fib services --- .../app/Services/FIB/CancelPaymentService.php | 29 ++++++++------- .../FIB/CheckPaymentStatusService.php | 22 ++++++++++-- .../app/Services/FIB/CreatePaymentService.php | 36 +++++++++---------- .../DTO/FIBPaymentCreateDTO.php | 2 +- backend/app/Services/FIB/RefundService.php | 31 +++++++++------- .../app/Services/Payments/PaymentService.php | 35 ++++++++++-------- .../GatewayManagementController.php | 2 +- backend/config/logging.php | 18 ++++++++++ 8 files changed, 113 insertions(+), 62 deletions(-) rename backend/app/Services/{Payments => FIB}/DTO/FIBPaymentCreateDTO.php (96%) diff --git a/backend/app/Services/FIB/CancelPaymentService.php b/backend/app/Services/FIB/CancelPaymentService.php index 8cfdb72a..73d195aa 100755 --- a/backend/app/Services/FIB/CancelPaymentService.php +++ b/backend/app/Services/FIB/CancelPaymentService.php @@ -11,21 +11,13 @@ class CancelPaymentService { protected string $url; protected string $channelName; - protected string $accessToken; - protected array $headers; + protected AuthorizationService $authorizationService; - public function __construct() + public function __construct(AuthorizationService $authorizationService) { $this->url = config('fib.cancelPayment.url'); $this->channelName = ''; - } - - public function setHeaders(): void - { - $this->headers = [ - 'Authorization' => 'Bearer ' . $this->accessToken, - 'Content-Type' => 'application/json', - ]; + $this->authorizationService = $authorizationService; } /** @@ -50,13 +42,26 @@ class CancelPaymentService /** * @throws ConnectionException + * @throws Exception */ public function sendRequest(): array { - return Http::withHeaders($this->headers) + return Http::withHeaders([ + 'Authorization' => 'Bearer ' . $this->getToken(), + 'Content-Type' => 'application/json', + ]) ->throw() ->withoutVerifying() ->post($this->url) ->json(); } + + /** + * @throws Exception + */ + public function getToken(): string + { + $data = $this->authorizationService->run(); + return $data['access_token']; + } } diff --git a/backend/app/Services/FIB/CheckPaymentStatusService.php b/backend/app/Services/FIB/CheckPaymentStatusService.php index 6a5ff4e5..58218c1e 100755 --- a/backend/app/Services/FIB/CheckPaymentStatusService.php +++ b/backend/app/Services/FIB/CheckPaymentStatusService.php @@ -11,11 +11,13 @@ class CheckPaymentStatusService { protected string $url; protected string $channelName; + protected AuthorizationService $authorizationService; - public function __construct() + public function __construct(AuthorizationService $authorizationService) { $this->url = config('fib.checkPaymentStatus.url'); - $this->channelName = ''; + $this->channelName = 'fib_status'; + $this->authorizationService = $authorizationService; } /** @@ -40,12 +42,26 @@ class CheckPaymentStatusService /** * @throws ConnectionException + * @throws Exception */ public function sendRequest(): array { - return Http::throw() + return Http::withHeaders([ + 'Authorization' => 'Bearer ' . $this->getToken(), + 'Content-Type' => 'application/json', + ]) + ->throw() ->withoutVerifying() ->post($this->url) ->json(); } + + /** + * @throws Exception + */ + public function getToken(): string + { + $data = $this->authorizationService->run(); + return $data['access_token']; + } } diff --git a/backend/app/Services/FIB/CreatePaymentService.php b/backend/app/Services/FIB/CreatePaymentService.php index 9df973ad..8a18a23f 100755 --- a/backend/app/Services/FIB/CreatePaymentService.php +++ b/backend/app/Services/FIB/CreatePaymentService.php @@ -11,27 +11,14 @@ class CreatePaymentService { protected string $url; protected string $channelName; - protected string $accessToken; - protected array $headers; protected array $inputParameters; + protected AuthorizationService $authorizationService; - public function __construct() + public function __construct(AuthorizationService $authorizationService) { $this->url = config('fib.createPayment.url'); - $this->channelName = ''; - } - - public function setAccessToken(string $accessToken): void - { - $this->accessToken = $accessToken; - } - - public function setHeaders(): void - { - $this->headers = [ - 'Authorization' => 'Bearer ' . $this->accessToken, - 'Content-Type' => 'application/json', - ]; + $this->channelName = 'fib_create_payment'; + $this->authorizationService = $authorizationService; } public function setInputParameters(array $inputParameters): void @@ -61,13 +48,26 @@ class CreatePaymentService /** * @throws ConnectionException + * @throws Exception */ public function sendRequest(): array { - return Http::withHeaders($this->headers) + return Http::withHeaders([ + 'Authorization' => 'Bearer ' . $this->getToken(), + 'Content-Type' => 'application/json', + ]) ->throw() ->withoutVerifying() ->post($this->url, $this->inputParameters) ->json(); } + + /** + * @throws Exception + */ + public function getToken(): string + { + $data = $this->authorizationService->run(); + return $data['access_token']; + } } diff --git a/backend/app/Services/Payments/DTO/FIBPaymentCreateDTO.php b/backend/app/Services/FIB/DTO/FIBPaymentCreateDTO.php similarity index 96% rename from backend/app/Services/Payments/DTO/FIBPaymentCreateDTO.php rename to backend/app/Services/FIB/DTO/FIBPaymentCreateDTO.php index e09cab6b..13ab44c2 100644 --- a/backend/app/Services/Payments/DTO/FIBPaymentCreateDTO.php +++ b/backend/app/Services/FIB/DTO/FIBPaymentCreateDTO.php @@ -1,6 +1,6 @@ url = config('fib.refund.url'); - $this->channelName = ''; - } - - public function setHeaders(): void - { - $this->headers = [ - 'Authorization' => 'Bearer ' . $this->accessToken, - 'Content-Type' => 'application/json', - ]; + $this->channelName = 'fib_refund'; + $this->authorizationService = $authorizationService; } /** @@ -50,13 +42,26 @@ class RefundService /** * @throws ConnectionException + * @throws Exception */ public function sendRequest(): array { - return Http::withHeaders($this->headers) + return Http::withHeaders([ + 'Authorization' => 'Bearer ' . $this->getToken(), + 'Content-Type' => 'application/json', + ]) ->throw() ->withoutVerifying() ->post($this->url) ->json(); } + + /** + * @throws Exception + */ + public function getToken(): string + { + $data = $this->authorizationService->run(); + return $data['access_token']; + } } diff --git a/backend/app/Services/Payments/PaymentService.php b/backend/app/Services/Payments/PaymentService.php index d50d8b7f..ff4d0a21 100644 --- a/backend/app/Services/Payments/PaymentService.php +++ b/backend/app/Services/Payments/PaymentService.php @@ -6,22 +6,30 @@ use App\Enums\PaymentStatusEnum; use App\Models\FibTransaction; use App\Models\Payment; use App\Models\Transaction; -use App\Services\FIB\FIBClient; -use App\Services\Payments\DTO\FIBPaymentCreateDTO; +use App\Services\FIB\CreatePaymentService; +use App\Services\FIB\DTO\FIBPaymentCreateDTO; use App\Services\Payments\DTO\PaymentCreateDTO; +use Exception; +use Illuminate\Support\Facades\DB; +use Throwable; class PaymentService { - public $fib_client; + protected CreatePaymentService $create_payment_service; - public function __construct() + public function __construct(CreatePaymentService $create_payment_service) { - $this->fib_client = new FibClient(); + $this->create_payment_service = $create_payment_service; } + /** + * @throws Exception + * @throws Throwable + */ public function createPayment(PaymentCreateDTO $dto) { - try { + return DB::transaction(function () use ($dto) { + $payment = Payment::query()->create([ 'user_id' => $dto->user_id, 'username' => $dto->username, @@ -35,7 +43,7 @@ class PaymentService 'user_gateway_id' => $dto->user_gateway_id, ]); - $response = $this->fib_client->createPaymentService(); + $response = $this->create_payment_service->run(); $fib_response_dto = FIBPaymentCreateDTO::fromResponse($response); $transaction = Transaction::query()->create([ @@ -43,15 +51,15 @@ class PaymentService 'amount' => $payment->amount, 'currency' => $payment->currency, 'callback_url' => $payment->callback_url, -// 'description' => '', -// 'expires_in' => '', + // 'description' => '', + // 'expires_in' => '', 'payment_id' => $payment->id, 'payment_track_id' => $payment->track_id, 'user_gateway_id' => $payment->user_gateway_id, -// 'status' => '', + // 'status' => '', ]); - $fib_transactions = FibTransaction::query()->create([ + FibTransaction::query()->create([ 'transaction_id' => $transaction->id, 'qrcode' => $fib_response_dto->qrCode, 'readable_code' => $fib_response_dto->readableCode, @@ -64,9 +72,8 @@ class PaymentService 'status_id' => PaymentStatusEnum::Unpaid->value, 'status_name' => PaymentStatusEnum::Unpaid->name, ]); + return $payment; - } catch (\Exception $exception) { - throw new \Exception($exception->getMessage()); - } + }); } } diff --git a/backend/app/User/Controllers/GatewayManagementController.php b/backend/app/User/Controllers/GatewayManagementController.php index 690d364e..d0f62c29 100644 --- a/backend/app/User/Controllers/GatewayManagementController.php +++ b/backend/app/User/Controllers/GatewayManagementController.php @@ -23,7 +23,7 @@ class GatewayManagementController extends Controller public function index(Request $request) { return DataTableFacade::run( - UserGateway::query()->where('user_id', '=', Auth::guard('web')->id()), + UserGateway::query()->where('user_id', '=', Auth::guard('web')->user()->id), $request, allowedFilters: ['*'], allowedSortings: ['*'], diff --git a/backend/config/logging.php b/backend/config/logging.php index ee09ba9e..04aa32a4 100755 --- a/backend/config/logging.php +++ b/backend/config/logging.php @@ -133,6 +133,24 @@ return [ 'level' => 'error', ], + 'fib_create_payment' => [ + 'driver' => 'single', + 'path' => storage_path('logs/fib/createPayment.log'), + 'level' => 'error', + ], + + 'fib_status' => [ + 'driver' => 'single', + 'path' => storage_path('logs/fib/status.log'), + 'level' => 'error', + ], + + 'fib_refund' => [ + 'driver' => 'single', + 'path' => storage_path('logs/fib/refund.log'), + 'level' => 'error', + ], + ], ];