Compare commits

..

5 Commits

12 changed files with 125 additions and 122 deletions

View File

@@ -3,8 +3,10 @@
namespace App\Models;
use Database\Factories\UserGatewayFactory;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
class UserGateway extends Model
{
@@ -12,4 +14,11 @@ class UserGateway extends Model
use HasFactory;
protected $guarded = ['id'];
protected function logo(): Attribute
{
return Attribute::make(
get: fn($value) => $value == null ? null : Storage::disk('public')->url($value)
);
}
}

View File

@@ -11,13 +11,21 @@ class CancelPaymentService
{
protected string $url;
protected string $channelName;
protected AuthorizationService $authorizationService;
protected string $accessToken;
protected array $headers;
public function __construct(AuthorizationService $authorizationService)
public function __construct()
{
$this->url = config('fib.cancelPayment.url');
$this->channelName = '';
$this->authorizationService = $authorizationService;
}
public function setHeaders(): void
{
$this->headers = [
'Authorization' => 'Bearer ' . $this->accessToken,
'Content-Type' => 'application/json',
];
}
/**
@@ -42,26 +50,13 @@ class CancelPaymentService
/**
* @throws ConnectionException
* @throws Exception
*/
public function sendRequest(): array
{
return Http::withHeaders([
'Authorization' => 'Bearer ' . $this->getToken(),
'Content-Type' => 'application/json',
])
return Http::withHeaders($this->headers)
->throw()
->withoutVerifying()
->post($this->url)
->json();
}
/**
* @throws Exception
*/
public function getToken(): string
{
$data = $this->authorizationService->run();
return $data['access_token'];
}
}

View File

@@ -11,13 +11,11 @@ class CheckPaymentStatusService
{
protected string $url;
protected string $channelName;
protected AuthorizationService $authorizationService;
public function __construct(AuthorizationService $authorizationService)
public function __construct()
{
$this->url = config('fib.checkPaymentStatus.url');
$this->channelName = 'fib_status';
$this->authorizationService = $authorizationService;
$this->channelName = '';
}
/**
@@ -42,26 +40,12 @@ class CheckPaymentStatusService
/**
* @throws ConnectionException
* @throws Exception
*/
public function sendRequest(): array
{
return Http::withHeaders([
'Authorization' => 'Bearer ' . $this->getToken(),
'Content-Type' => 'application/json',
])
->throw()
return Http::throw()
->withoutVerifying()
->post($this->url)
->json();
}
/**
* @throws Exception
*/
public function getToken(): string
{
$data = $this->authorizationService->run();
return $data['access_token'];
}
}

View File

@@ -11,14 +11,27 @@ class CreatePaymentService
{
protected string $url;
protected string $channelName;
protected string $accessToken;
protected array $headers;
protected array $inputParameters;
protected AuthorizationService $authorizationService;
public function __construct(AuthorizationService $authorizationService)
public function __construct()
{
$this->url = config('fib.createPayment.url');
$this->channelName = 'fib_create_payment';
$this->authorizationService = $authorizationService;
$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',
];
}
public function setInputParameters(array $inputParameters): void
@@ -48,26 +61,13 @@ class CreatePaymentService
/**
* @throws ConnectionException
* @throws Exception
*/
public function sendRequest(): array
{
return Http::withHeaders([
'Authorization' => 'Bearer ' . $this->getToken(),
'Content-Type' => 'application/json',
])
return Http::withHeaders($this->headers)
->throw()
->withoutVerifying()
->post($this->url, $this->inputParameters)
->json();
}
/**
* @throws Exception
*/
public function getToken(): string
{
$data = $this->authorizationService->run();
return $data['access_token'];
}
}

View File

@@ -11,13 +11,21 @@ class RefundService
{
protected string $url;
protected string $channelName;
protected AuthorizationService $authorizationService;
protected string $accessToken;
protected array $headers;
public function __construct(AuthorizationService $authorizationService)
public function __construct()
{
$this->url = config('fib.refund.url');
$this->channelName = 'fib_refund';
$this->authorizationService = $authorizationService;
$this->channelName = '';
}
public function setHeaders(): void
{
$this->headers = [
'Authorization' => 'Bearer ' . $this->accessToken,
'Content-Type' => 'application/json',
];
}
/**
@@ -42,26 +50,13 @@ class RefundService
/**
* @throws ConnectionException
* @throws Exception
*/
public function sendRequest(): array
{
return Http::withHeaders([
'Authorization' => 'Bearer ' . $this->getToken(),
'Content-Type' => 'application/json',
])
return Http::withHeaders($this->headers)
->throw()
->withoutVerifying()
->post($this->url)
->json();
}
/**
* @throws Exception
*/
public function getToken(): string
{
$data = $this->authorizationService->run();
return $data['access_token'];
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Services\FIB\DTO;
namespace App\Services\Payments\DTO;
class FIBPaymentCreateDTO
{

View File

@@ -6,30 +6,22 @@ use App\Enums\PaymentStatusEnum;
use App\Models\FibTransaction;
use App\Models\Payment;
use App\Models\Transaction;
use App\Services\FIB\CreatePaymentService;
use App\Services\FIB\DTO\FIBPaymentCreateDTO;
use App\Services\FIB\FIBClient;
use App\Services\Payments\DTO\FIBPaymentCreateDTO;
use App\Services\Payments\DTO\PaymentCreateDTO;
use Exception;
use Illuminate\Support\Facades\DB;
use Throwable;
class PaymentService
{
protected CreatePaymentService $create_payment_service;
public $fib_client;
public function __construct(CreatePaymentService $create_payment_service)
public function __construct()
{
$this->create_payment_service = $create_payment_service;
$this->fib_client = new FibClient();
}
/**
* @throws Exception
* @throws Throwable
*/
public function createPayment(PaymentCreateDTO $dto)
{
return DB::transaction(function () use ($dto) {
try {
$payment = Payment::query()->create([
'user_id' => $dto->user_id,
'username' => $dto->username,
@@ -43,7 +35,7 @@ class PaymentService
'user_gateway_id' => $dto->user_gateway_id,
]);
$response = $this->create_payment_service->run();
$response = $this->fib_client->createPaymentService();
$fib_response_dto = FIBPaymentCreateDTO::fromResponse($response);
$transaction = Transaction::query()->create([
@@ -51,15 +43,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' => '',
]);
FibTransaction::query()->create([
$fib_transactions = FibTransaction::query()->create([
'transaction_id' => $transaction->id,
'qrcode' => $fib_response_dto->qrCode,
'readable_code' => $fib_response_dto->readableCode,
@@ -72,8 +64,9 @@ class PaymentService
'status_id' => PaymentStatusEnum::Unpaid->value,
'status_name' => PaymentStatusEnum::Unpaid->name,
]);
return $payment;
});
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
}
}

View File

@@ -14,6 +14,7 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Laravel\Passport\ClientRepository;
use Throwable;
class GatewayManagementController extends Controller
@@ -23,11 +24,11 @@ class GatewayManagementController extends Controller
public function index(Request $request)
{
return DataTableFacade::run(
UserGateway::query()->where('user_id', '=', Auth::guard('web')->user()->id),
UserGateway::query()->where('user_id', '=', Auth::guard('web')->id()),
$request,
allowedFilters: ['*'],
allowedSortings: ['*'],
allowedSelects: ['id', 'name', 'domain', 'tax_id', 'bank_name', 'account_holder', 'iban', 'business_type_id', 'business_type_name'],
allowedSelects: ['*'],
);
}
@@ -38,9 +39,19 @@ class GatewayManagementController extends Controller
{
$user = Auth::guard('web')->user();
$logo = $request->has('logo') ? FileFacade::save($request->file('logo'), "{$user->id}") : null;
$logo = $request->hasFile('logo')
? FileFacade::save($request->file('logo'), "user_gateway/{$user->id}")
: null;
$passportClient = DB::transaction(function () use ($user, $logo, $request) {
$clientName = "Gateway {$request->name} - User {$user->id}";
$client = app(ClientRepository::class)->createPasswordGrantClient($clientName, 'users', true);
$client->Fill([
'owner_type' => get_class($user),
'owner_id' => $user->id,
])->save();
DB::transaction(function () use ($user, $logo, $request) {
UserGateway::query()->create([
'user_id' => $user->id,
'username' => $user->username,
@@ -53,15 +64,20 @@ class GatewayManagementController extends Controller
'business_type_id' => $request->business_type_id,
'business_type_name' => BusinessTypeEnum::name($request->business_type_id),
'logo' => $logo,
'client_id' => $client->id,
'client_secret' => $client->secret,
]);
$user->update(['authority_level' => 1]);
return $client;
});
return $this->successResponse();
return $this->successResponse([
'client_id' => $passportClient->id,
'client_secret' => $passportClient->secret,
]);
}
public function show(UserGateway $gateway): JsonResponse
{
return $this->successResponse($gateway);

View File

@@ -13,8 +13,7 @@ class StoreRequest extends FormRequest
*/
public function authorize(): bool
{
return Auth::guard('web')->user()->kyc_status;
}
return true; }
/**
* Get the validation rules that apply to the request.

View File

@@ -133,24 +133,6 @@ 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',
],
],
];

View File

@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('user_gateways', function (Blueprint $table) {
$table->string('client_id')->nullable();
$table->string('client_secret')->nullable();
$table->integer('status')->default(0);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('user_gateways', function (Blueprint $table) {
$table->dropColumn(['client_id', 'client_secret','status']);
});
}
};

View File

@@ -77,7 +77,7 @@ Route::prefix('expert')
Route::prefix('profile')
->name('profile.')
->middleware('auth')
->middleware('auth:expert')
->controller(ProfileController::class)
->group(function () {
Route::get('info', 'info')->name('info');