Compare commits

..

14 Commits

38 changed files with 718 additions and 33 deletions

View File

@@ -21,7 +21,7 @@ WORKDIR /var/www/payment
COPY . .
RUN chmod -R 665 /var/www/payment/storage /var/www/payment/bootstrap/cache
RUN chmod -R 775 /var/www/payment/storage /var/www/payment/bootstrap/cache
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

19
backend/app/Models/User.php Normal file → Executable file
View File

@@ -5,29 +5,18 @@ namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
#[Fillable(['name', 'email', 'password'])]
#[Hidden(['password', 'remember_token'])]
#[Guarded(['id'])]
#[Hidden(['password'])]
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable, HasApiTokens;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
protected $guarded = ['id'];
}

2
backend/app/Providers/AppServiceProvider.php Normal file → Executable file
View File

@@ -4,6 +4,7 @@ namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Passport;
use Carbon\CarbonInterval;
class AppServiceProvider extends ServiceProvider
{
@@ -21,5 +22,6 @@ class AppServiceProvider extends ServiceProvider
public function boot(): void
{
Passport::enablePasswordGrant();
Passport::personalAccessTokensExpireIn(CarbonInterval::minutes(1));
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Traits;
use Illuminate\Http\JsonResponse;
trait ApiResponse
{
public function successResponse(mixed $data = null, string $message = null, int $statusCode = 200): JsonResponse
{
if (!is_null($data)) {
return response()->json([
'data' => $data
], $statusCode);
}
$message = $message ?? __('messages.successful');
return response()->json([
'message' => $message
], $statusCode);
}
public function errorResponse(string $message, string $type = 'logical_exception', int $statusCode = 422): JsonResponse
{
return response()->json([
'type' => $type,
'message' => $message
], $statusCode);
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\User\Controller\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Traits\ApiResponse;
use App\User\Requests\Auth\LoginRequest;
use App\User\Requests\Auth\RegisterRequest;
use App\User\Resources\UserResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class AuthController extends Controller
{
use ApiResponse;
public function register(RegisterRequest $request): JsonResponse
{
User::query()->create([
'username' => $request->username,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$request->session()->regenerate();
return $this->successResponse();
}
public function login(LoginRequest $request): JsonResponse
{
$credentials = ['password' => $request->password,];
if (filter_var($request->login, FILTER_VALIDATE_EMAIL)) {
$credentials['email'] = $request->login;
} else {
$credentials['username'] = $request->login;
}
if (Auth::attempt($credentials))
{
$request->session()->regenerate();
$user = Auth::user();
return $this->successResponse(new UserResource($user));
}
return $this->errorResponse(__('messages.incorrect_or_username_password'));
}
public function logout(Request $request): JsonResponse
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return $this->successResponse();
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\User\Controller;
use App\Http\Controllers\Controller;
use App\Traits\ApiResponse;
use App\User\Requests\Profile\ChangePasswordRequest;
use App\User\Requests\Profile\EditRequest;
use App\User\Resources\UserResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class ProfileController extends Controller
{
use ApiResponse;
public function info(): JsonResponse
{
return $this->successResponse(new UserResource(Auth::user()));
}
public function edit(EditRequest $request): JsonResponse
{
$user = Auth::user();
$user->update([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'national_id' => $request->national_id,
'address' => $request->address,
'postal_code' => $request->postal_code,
'account_number' => $request->account_number,
'phone_number' => $request->phone_number,
'province_id' => $request->province_id,
'city_id' => $request->city_id,
]);
return $this->successResponse();
}
public function changePassword(ChangePasswordRequest $request): JsonResponse
{
$user = Auth::user();
if (! Hash::check($request->current_password, $user->password)) {
return $this->errorResponse(__('messages.incorrect_current_password'));
}
$user->update([
'password' => Hash::make($request->new_password),
]);
return $this->successResponse();
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\User\Requests\Auth;
use Illuminate\Foundation\Http\FormRequest;
class LoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'login' => 'required|string',
'password' => 'required|string',
];
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\User\Requests\Auth;
use Illuminate\Foundation\Http\FormRequest;
class RegisterRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'username' => 'required|unique:users,username',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|regex:/^(?=.*[a-zA-Z])(?=.*\d).+$/|min:6|confirmed:',
];
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\User\Requests\Profile;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
class ChangePasswordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'current_password' => 'required|string|regex:/^(?=.*[a-zA-Z])(?=.*\d).+$/|min:6',
'new_password' => 'required|string|regex:/^(?=.*[a-zA-Z])(?=.*\d).+$/|min:6',
];
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\User\Requests\Profile;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
class EditRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'first_name' => 'required|string',
'last_name' => 'required|string',
'national_id' => 'required',
'address' => 'required',
'postal_code' => 'required',
'account_number' => 'required',
'phone_number' => 'required|regex:/^964\d{9}$/|numeric|digits:11',
'province_id' => 'required',
'city_id' => 'required',
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\User\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'username' => $this->username,
'email' => $this->email,
];
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\User\Services\FIB;
use Exception;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class AuthorizationService
{
protected string $url;
protected string $channelName;
protected array $inputParameters;
public function __construct()
{
$this->url = config('fib.authorization.url');
$this->channelName = '';
}
public function setInputParameters(): void
{
$this->inputParameters = [
'client_id' => config('fib.authorization.client_id'),
'client_secret' => config('fib.authorization.client_secret'),
'grant_type' => 'client_credentials',
];
}
/**
* @throws Exception
*/
public function run(): array
{
try {
return $this->sendRequest();
}
catch (Exception $e) {
Log::channel($this->channelName)
->error(get_class($this),
[
'message' => $e->getMessage(),
]
);
throw $e;
}
}
/**
* @throws ConnectionException
*/
public function sendRequest(): array
{
return Http::withHeaders(['Content-Type' => 'application/json'])
->throw()
->withoutVerifying()
->post($this->url, $this->inputParameters)
->json();
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\User\Services\FIB;
use Exception;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class CancelPaymentService
{
protected string $url;
protected string $channelName;
protected string $accessToken;
protected array $headers;
public function __construct()
{
$this->url = config('fib.cancelPayment.url');
$this->channelName = '';
}
public function setHeaders(): void
{
$this->headers = [
'Authorization' => 'Bearer ' . $this->accessToken,
'Content-Type' => 'application/json',
];
}
/**
* @throws Exception
*/
public function run(): array
{
try {
return $this->sendRequest();
}
catch (Exception $e) {
Log::channel($this->channelName)
->error(get_class($this),
[
'message' => $e->getMessage(),
]
);
throw $e;
}
}
/**
* @throws ConnectionException
*/
public function sendRequest(): array
{
return Http::withHeaders($this->headers)
->throw()
->withoutVerifying()
->post($this->url)
->json();
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\User\Services\FIB;
use Exception;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class CheckPaymentStatusService
{
protected string $url;
protected string $channelName;
public function __construct()
{
$this->url = config('fib.checkPaymentStatus.url');
$this->channelName = '';
}
/**
* @throws Exception
*/
public function run(): array
{
try {
return $this->sendRequest();
}
catch (Exception $e) {
Log::channel($this->channelName)
->error(get_class($this),
[
'message' => $e->getMessage(),
]
);
throw $e;
}
}
/**
* @throws ConnectionException
*/
public function sendRequest(): array
{
return Http::throw()
->withoutVerifying()
->post($this->url)
->json();
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace App\User\Services\FIB;
use Exception;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class CreatePaymentService
{
protected string $url;
protected string $channelName;
protected string $accessToken;
protected array $headers;
protected array $inputParameters;
public function __construct()
{
$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',
];
}
public function setInputParameters(array $inputParameters): void
{
$this->inputParameters = $inputParameters;
}
/**
* @throws Exception
*/
public function run(): array
{
try {
return $this->sendRequest();
}
catch (Exception $e) {
Log::channel($this->channelName)
->error(get_class($this),
[
'message' => $e->getMessage(),
]
);
throw $e;
}
}
/**
* @throws ConnectionException
*/
public function sendRequest(): array
{
return Http::withHeaders($this->headers)
->throw()
->withoutVerifying()
->post($this->url, $this->inputParameters)
->json();
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\User\Services\FIB;
use Exception;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class RefundService
{
protected string $url;
protected string $channelName;
protected string $accessToken;
protected array $headers;
public function __construct()
{
$this->url = config('fib.refund.url');
$this->channelName = '';
}
public function setHeaders(): void
{
$this->headers = [
'Authorization' => 'Bearer ' . $this->accessToken,
'Content-Type' => 'application/json',
];
}
/**
* @throws Exception
*/
public function run(): array
{
try {
return $this->sendRequest();
}
catch (Exception $e) {
Log::channel($this->channelName)
->error(get_class($this),
[
'message' => $e->getMessage(),
]
);
throw $e;
}
}
/**
* @throws ConnectionException
*/
public function sendRequest(): array
{
return Http::withHeaders($this->headers)
->throw()
->withoutVerifying()
->post($this->url)
->json();
}
}

2
backend/bootstrap/app.php Normal file → Executable file
View File

@@ -12,7 +12,7 @@ return Application::configure(basePath: dirname(__DIR__))
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
//
$middleware->web()->validateCsrfTokens(['*']);
})
->withExceptions(function (Exceptions $exceptions): void {
//

0
backend/bootstrap/cache/.gitignore vendored Normal file → Executable file
View File

0
backend/config/auth.php Normal file → Executable file
View File

34
backend/config/cors.php Executable file
View File

@@ -0,0 +1,34 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Cross-Origin Resource Sharing (CORS) Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your settings for cross-origin resource sharing
| or "CORS". This determines what cross-origin operations may execute
| in web browsers. You are free to adjust these settings as needed.
|
| To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
*/
'paths' => ['*'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
];

25
backend/config/fib.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
return [
'authorization' => [
'url' => env('FIB_AUTHORIZATION_URL'),
'client_id' => env('FIB_CLIENT_ID'),
'client_secret' => env('FIB_CLIENT_SECRET'),
],
'createPayment' => [
'url' => env('FIB_CREATE_PAYMENT_URL'),
],
'checkPaymentStatus' => [
'url' => env('FIB_CHECK_PAYMENT_STATUS_URL'),
],
'cancelPayment' => [
'url' => env('FIB_CANCEL_PAYMENT_URL'),
],
'refund' => [
'url' => env('FIB_REFUND_URL'),
],
];

0
backend/config/session.php Normal file → Executable file
View File

2
backend/database/factories/UserFactory.php Normal file → Executable file
View File

@@ -25,7 +25,7 @@ class UserFactory extends Factory
public function definition(): array
{
return [
'name' => fake()->name(),
'username' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),

View File

@@ -13,18 +13,23 @@ return new class extends Migration
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username');
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
$table->tinyInteger('status')->default(0);
$table->string('national_id')->nullable();
$table->string('address')->nullable();
$table->string('postal_code')->nullable();
$table->string('account_number')->nullable();
$table->string('phone_number')->nullable();
$table->string('province_id')->nullable();
$table->string('province_name')->nullable();
$table->string('city_id')->nullable();
$table->string('city_name')->nullable();
Schema::create('password_reset_tokens', function (Blueprint $table) {
$table->string('email')->primary();
$table->string('token');
$table->timestamp('created_at')->nullable();
$table->timestamps();
});
Schema::create('sessions', function (Blueprint $table) {
@@ -43,7 +48,6 @@ return new class extends Migration
public function down(): void
{
Schema::dropIfExists('users');
Schema::dropIfExists('password_reset_tokens');
Schema::dropIfExists('sessions');
}
};

3
backend/database/seeders/DatabaseSeeder.php Normal file → Executable file
View File

@@ -18,8 +18,9 @@ class DatabaseSeeder extends Seeder
// User::factory(10)->create();
User::factory()->create([
'name' => 'Test User',
'username' => 'Test User',
'email' => 'test@example.com',
'password' => 'password123@',
]);
}
}

2
backend/routes/api.php Normal file → Executable file
View File

@@ -1,8 +1,10 @@
<?php
use App\User\Controller\Auth\AuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:api');

23
backend/routes/web.php Normal file → Executable file
View File

@@ -1,7 +1,24 @@
<?php
use App\User\Controller\Auth\AuthController;
use App\User\Controller\ProfileController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::prefix('auth')
->name('Auth.')
->controller(AuthController::class)
->group(function () {
Route::post('register', 'register')->name('register');
Route::post('login', 'login')->name('login');
Route::post('logout', 'logout')->name('logout');
});
Route::prefix('profile')
->name('profile.')
->middleware('auth')
->controller(ProfileController::class)
->group(function () {
Route::get('info', 'info')->name('info');
Route::post('edit', 'edit')->name('edit');
Route::post('change_password', 'changePassword')->name('changePassword');
});

0
backend/storage/app/.gitignore vendored Normal file → Executable file
View File

0
backend/storage/app/private/.gitignore vendored Normal file → Executable file
View File

0
backend/storage/app/public/.gitignore vendored Normal file → Executable file
View File

0
backend/storage/framework/.gitignore vendored Normal file → Executable file
View File

0
backend/storage/framework/cache/.gitignore vendored Normal file → Executable file
View File

0
backend/storage/framework/cache/data/.gitignore vendored Normal file → Executable file
View File

0
backend/storage/framework/sessions/.gitignore vendored Normal file → Executable file
View File

0
backend/storage/framework/testing/.gitignore vendored Normal file → Executable file
View File

0
backend/storage/framework/views/.gitignore vendored Normal file → Executable file
View File

0
backend/storage/logs/.gitignore vendored Normal file → Executable file
View File

View File

@@ -12,7 +12,7 @@ server {
fastcgi_pass laravel:9000;
fastcgi_index index.php;
fastcgi_hide_header X-Powered-By;
fastcgi_param SCRIPT_FILENAME /var/www/payment/backend/public/index.php;
fastcgi_param SCRIPT_FILENAME /var/www/payment/public/index.php;
}
location ~ /\.(?!well-known).* {