From b15069ef1661161dd4ce0c7aba100c9cc6ffb634 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 04:49:31 +0100 Subject: [PATCH 01/17] Add update_wallets_logs_table migration --- .../update_wallets_logs_table.php.stub | 38 +++++++++++++++++++ src/LaravelPayPocketServiceProvider.php | 10 +++-- 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 database/migrations/update_wallets_logs_table.php.stub diff --git a/database/migrations/update_wallets_logs_table.php.stub b/database/migrations/update_wallets_logs_table.php.stub new file mode 100644 index 0000000..5ae26a8 --- /dev/null +++ b/database/migrations/update_wallets_logs_table.php.stub @@ -0,0 +1,38 @@ +string('detail')->nullable()->after('status'); + } + if (!Schema::hasColumn('wallets_logs', 'reference')) { + $table->string('reference')->nullable()->after('ip'); + } + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('wallets_logs', function (Blueprint $table) { + if (Schema::hasColumn('wallets_logs', 'detail')) { + $table->dropColumn('detail'); + } + if (Schema::hasColumn('wallets_logs', 'reference')) { + $table->dropColumn('reference'); + } + }); + } +}; diff --git a/src/LaravelPayPocketServiceProvider.php b/src/LaravelPayPocketServiceProvider.php index f58c9ae..ef3c4b9 100644 --- a/src/LaravelPayPocketServiceProvider.php +++ b/src/LaravelPayPocketServiceProvider.php @@ -18,13 +18,17 @@ public function configurePackage(Package $package): void ->name('laravel-pay-pocket') ->hasConfigFile() ->hasViews() - ->hasMigrations('create_wallets_logs_table', 'create_wallets_table'); + ->hasMigrations( + 'create_wallets_logs_table', + 'create_wallets_table', + 'update_wallets_logs_table' + ); } public function bootingPackage() { $this->publishes([ - __DIR__.'/../Enums/' => app_path('Enums'), + __DIR__ . '/../Enums/' => app_path('Enums'), ], 'pay-pocket-wallets'); } -} +} \ No newline at end of file From 8110c2ecfe71a9aa3c7e8c7c512e3498e0a2e63d Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 05:34:34 +0100 Subject: [PATCH 02/17] Add Reference and Detail to Logs --- config/pay-pocket.php | 10 +++++++-- src/LaravelPayPocketServiceProvider.php | 11 +++++++++ src/Traits/BalanceOperation.php | 30 ++++++++++++++++--------- src/Traits/HandlesDeposit.php | 8 +++---- src/Traits/HandlesPayment.php | 8 +++---- tests/OperationsWithFacadeTest.php | 4 +--- 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/config/pay-pocket.php b/config/pay-pocket.php index 4cb9728..1b65891 100644 --- a/config/pay-pocket.php +++ b/config/pay-pocket.php @@ -2,5 +2,11 @@ // config for HPWebdeveloper/LaravelPayPocket return [ - -]; + 'log_reference_length' => 12, + 'log_reference_prefix' => null, + /** + * The log reference generator should be static + * The third array item should contain optional parameters to pass to the generator + */ + 'log_reference_generator' => [\Illuminate\Support\Str::class, 'random', [15]], +]; \ No newline at end of file diff --git a/src/LaravelPayPocketServiceProvider.php b/src/LaravelPayPocketServiceProvider.php index ef3c4b9..57638a6 100644 --- a/src/LaravelPayPocketServiceProvider.php +++ b/src/LaravelPayPocketServiceProvider.php @@ -30,5 +30,16 @@ public function bootingPackage() $this->publishes([ __DIR__ . '/../Enums/' => app_path('Enums'), ], 'pay-pocket-wallets'); + + + $this->publishes([ + __DIR__ . '/../config/pay-pocket.php' => config_path('pay-pocket.php'), + ], 'config'); + } + + public function registeringPackage() + { + // Automatically apply the package configuration + $this->mergeConfigFrom(__DIR__ . '/../config/pay-pocket.php', 'pay-pocket'); } } \ No newline at end of file diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index ae895ef..a459774 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -2,12 +2,14 @@ namespace HPWebdeveloper\LaravelPayPocket\Traits; +use Illuminate\Support\Str; + trait BalanceOperation { protected $createdLog; /** - * Check if Balance is more than zero. + * Check if Balance is more than zero. */ public function hasBalance(): bool { @@ -15,32 +17,38 @@ public function hasBalance(): bool } /** - * Decrement Balance and create a log entry. + * Decrement Balance and create a log entry. */ - public function decrementAndCreateLog($value): void + public function decrementAndCreateLog($value, $detail = null): void { - $this->createLog('dec', $value); + $this->createLog('dec', $value, $detail); $this->decrement('balance', $value); } /** - * Increment Balance and create a log entry. + * Increment Balance and create a log entry. */ - public function incrementAndCreateLog($value): void + public function incrementAndCreateLog($value, $detail = null): void { - $this->createLog('inc', $value); + $this->createLog('inc', $value, $detail); $this->increment('balance', $value); } /** - * Create a new log record + * Create a new log record */ - protected function createLog($logType, $value): void + protected function createLog($logType, $value, $detail = null): void { $currentBalance = $this->balance ?? 0; $newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value; + $refGen = config('pay-pocket.log_reference_generator', [Str::class, 'random', [12]]); + $reference = config('pay-pocket.reference_string_prefix', ''); + $reference .= isset($refGen[0], $refGen[1]) + ? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? []) + : Str::random(config('pay-pocket.log_reference_length', 12)); + $this->createdLog = $this->logs()->create([ 'wallet_name' => $this->type->value, 'from' => $currentBalance, @@ -48,8 +56,10 @@ protected function createLog($logType, $value): void 'type' => $logType, 'ip' => \Request::ip(), 'value' => $value, + 'detail' => $detail, + 'reference' => $reference ]); $this->createdLog->changeStatus('Done'); } -} +} \ No newline at end of file diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 108c1ac..3294741 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -15,7 +15,7 @@ trait HandlesDeposit /** * Deposit an amount to the user's wallet of a specific type. */ - public function deposit(string $type, int|float $amount): bool + public function deposit(string $type, int|float $amount, $detail = null): bool { $depositable = $this->getDepositableTypes(); @@ -27,10 +27,10 @@ public function deposit(string $type, int|float $amount): bool throw new InvalidValueException(); } - DB::transaction(function () use ($type, $amount) { + DB::transaction(function () use ($type, $amount, $detail) { $type = WalletEnums::tryFrom($type); $wallet = $this->wallets()->firstOrCreate(['type' => $type]); - $wallet->incrementAndCreateLog($amount); + $wallet->incrementAndCreateLog($amount, $detail); }); return true; @@ -63,4 +63,4 @@ private function isRequestValid($type, array $depositable) return true; } -} +} \ No newline at end of file diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 2e31745..b55eb1d 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,13 +13,13 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue): void + public function pay(int|float $orderValue, $detail = null): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); } - DB::transaction(function () use ($orderValue) { + DB::transaction(function () use ($orderValue, $detail) { $remainingOrderValue = $orderValue; $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); @@ -30,7 +30,7 @@ public function pay(int|float $orderValue): void } $amountToDeduct = min($wallet->balance, $remainingOrderValue); - $wallet->decrementAndCreateLog($amountToDeduct); + $wallet->decrementAndCreateLog($amountToDeduct, $detail); $remainingOrderValue -= $amountToDeduct; if ($remainingOrderValue <= 0) { @@ -43,4 +43,4 @@ public function pay(int|float $orderValue): void } }); } -} +} \ No newline at end of file diff --git a/tests/OperationsWithFacadeTest.php b/tests/OperationsWithFacadeTest.php index 24a5c5c..5e5fdf9 100644 --- a/tests/OperationsWithFacadeTest.php +++ b/tests/OperationsWithFacadeTest.php @@ -18,7 +18,6 @@ expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(234.56); expect(LaravelPayPocket::checkBalance($user))->toBeFloat(234.56); - }); test('user can deposit two times', function () { @@ -34,7 +33,6 @@ expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(1023.68); expect(LaravelPayPocket::checkBalance($user))->toBeFloat(1023.68); - }); test('user can pay order', function () { @@ -96,4 +94,4 @@ expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(0.12); expect(LaravelPayPocket::checkBalance($user))->toBeFloat(0.12); -}); +}); \ No newline at end of file From 317e5552512c30bf63306ca5b6d9db0bb812b2e3 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Mon, 8 Jan 2024 15:11:40 +0100 Subject: [PATCH 03/17] rebase and add notes/description feature --- .../migrations/update_wallets_logs_table.php.stub | 8 ++++---- src/Traits/BalanceOperation.php | 14 +++++++------- src/Traits/HandlesDeposit.php | 8 ++++---- src/Traits/HandlesPayment.php | 8 ++++---- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/database/migrations/update_wallets_logs_table.php.stub b/database/migrations/update_wallets_logs_table.php.stub index 5ae26a8..b9ac4ca 100644 --- a/database/migrations/update_wallets_logs_table.php.stub +++ b/database/migrations/update_wallets_logs_table.php.stub @@ -12,8 +12,8 @@ return new class extends Migration public function up(): void { Schema::table('wallets_logs', function (Blueprint $table) { - if (!Schema::hasColumn('wallets_logs', 'detail')) { - $table->string('detail')->nullable()->after('status'); + if (!Schema::hasColumn('wallets_logs', 'notes')) { + $table->string('notes')->nullable()->after('status'); } if (!Schema::hasColumn('wallets_logs', 'reference')) { $table->string('reference')->nullable()->after('ip'); @@ -27,8 +27,8 @@ return new class extends Migration public function down(): void { Schema::table('wallets_logs', function (Blueprint $table) { - if (Schema::hasColumn('wallets_logs', 'detail')) { - $table->dropColumn('detail'); + if (Schema::hasColumn('wallets_logs', 'notes')) { + $table->dropColumn('notes'); } if (Schema::hasColumn('wallets_logs', 'reference')) { $table->dropColumn('reference'); diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index a459774..a82a027 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -19,25 +19,25 @@ public function hasBalance(): bool /** * Decrement Balance and create a log entry. */ - public function decrementAndCreateLog($value, $detail = null): void + public function decrementAndCreateLog($value, $notes = null): void { - $this->createLog('dec', $value, $detail); + $this->createLog('dec', $value, $notes); $this->decrement('balance', $value); } /** * Increment Balance and create a log entry. */ - public function incrementAndCreateLog($value, $detail = null): void + public function incrementAndCreateLog($value, $notes = null): void { - $this->createLog('inc', $value, $detail); + $this->createLog('inc', $value, $notes); $this->increment('balance', $value); } /** * Create a new log record */ - protected function createLog($logType, $value, $detail = null): void + protected function createLog($logType, $value, $notes = null): void { $currentBalance = $this->balance ?? 0; @@ -56,10 +56,10 @@ protected function createLog($logType, $value, $detail = null): void 'type' => $logType, 'ip' => \Request::ip(), 'value' => $value, - 'detail' => $detail, + 'notes' => $notes, 'reference' => $reference ]); $this->createdLog->changeStatus('Done'); } -} \ No newline at end of file +} diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 3294741..73c33ff 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -15,7 +15,7 @@ trait HandlesDeposit /** * Deposit an amount to the user's wallet of a specific type. */ - public function deposit(string $type, int|float $amount, $detail = null): bool + public function deposit(string $type, int|float $amount, $notes = null): bool { $depositable = $this->getDepositableTypes(); @@ -27,10 +27,10 @@ public function deposit(string $type, int|float $amount, $detail = null): bool throw new InvalidValueException(); } - DB::transaction(function () use ($type, $amount, $detail) { + DB::transaction(function () use ($type, $amount, $notes) { $type = WalletEnums::tryFrom($type); $wallet = $this->wallets()->firstOrCreate(['type' => $type]); - $wallet->incrementAndCreateLog($amount, $detail); + $wallet->incrementAndCreateLog($amount, $notes); }); return true; @@ -63,4 +63,4 @@ private function isRequestValid($type, array $depositable) return true; } -} \ No newline at end of file +} diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index b55eb1d..6e534c6 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,13 +13,13 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue, $detail = null): void + public function pay(int|float $orderValue, $notes = null): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); } - DB::transaction(function () use ($orderValue, $detail) { + DB::transaction(function () use ($orderValue, $notes) { $remainingOrderValue = $orderValue; $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); @@ -30,7 +30,7 @@ public function pay(int|float $orderValue, $detail = null): void } $amountToDeduct = min($wallet->balance, $remainingOrderValue); - $wallet->decrementAndCreateLog($amountToDeduct, $detail); + $wallet->decrementAndCreateLog($amountToDeduct, $notes); $remainingOrderValue -= $amountToDeduct; if ($remainingOrderValue <= 0) { @@ -43,4 +43,4 @@ public function pay(int|float $orderValue, $detail = null): void } }); } -} \ No newline at end of file +} From 4d1b5dbbbe03bb706661d7edce06d5fb4e350e91 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Mon, 8 Jan 2024 15:14:27 +0100 Subject: [PATCH 04/17] Add test for payment description --- tests/OperationsWithFacadeTest.php | 14 +++++++++++++- tests/OperationsWithoutFacadeTest.php | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/tests/OperationsWithFacadeTest.php b/tests/OperationsWithFacadeTest.php index 5e5fdf9..5aaa2c0 100644 --- a/tests/OperationsWithFacadeTest.php +++ b/tests/OperationsWithFacadeTest.php @@ -94,4 +94,16 @@ expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(0.12); expect(LaravelPayPocket::checkBalance($user))->toBeFloat(0.12); -}); \ No newline at end of file +}); + +test('description can be added during payment', function () { + $user = User::factory()->create(); + + $type = 'wallet_2'; + + $description = \Illuminate\Support\Str::random(); + LaravelPayPocket::deposit($user, $type, 234.56); + LaravelPayPocket::pay($user, 234.56, $description); + + expect(WalletsLog::where('notes', $description)->exists())->toBe(true); +}); diff --git a/tests/OperationsWithoutFacadeTest.php b/tests/OperationsWithoutFacadeTest.php index 66e09db..4ed2d6f 100644 --- a/tests/OperationsWithoutFacadeTest.php +++ b/tests/OperationsWithoutFacadeTest.php @@ -17,7 +17,6 @@ expect($user->getWalletBalanceByType('wallet_2'))->toBeFloat(234.56); expect($user->walletBalance)->toBeFloat(234.56); - }); test('user can deposit two times', function () { @@ -33,7 +32,6 @@ expect($user->getWalletBalanceByType('wallet_2'))->toBeFloat(1023.68); expect($user->walletBalance)->toBeFloat(1023.68); - }); test('user can pay order', function () { @@ -98,3 +96,15 @@ expect($user->walletBalance)->toBeFloat(0.12); }); + +test('description can be added during payment', function () { + $user = User::factory()->create(); + + $type = 'wallet_2'; + + $description = \Illuminate\Support\Str::random(); + $user->deposit($type, 234.56); + $user->pay(234.56, $description); + + expect(WalletsLog::where('notes', $description)->exists())->toBe(true); +}); From c0df115c3846657baf6ffdfd90f3f3449b6b556f Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Mon, 8 Jan 2024 15:29:36 +0100 Subject: [PATCH 05/17] Pass tests payment description/notes --- src/Interfaces/WalletOperations.php | 4 ++-- src/Models/WalletsLog.php | 2 +- src/Services/PocketServices.php | 8 ++++---- src/Traits/HandlesDeposit.php | 2 +- src/Traits/HandlesPayment.php | 2 +- tests/OperationsWithFacadeTest.php | 14 +++++++++++++- tests/OperationsWithoutFacadeTest.php | 14 +++++++++++++- tests/TestCase.php | 13 ++++++++----- 8 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index 42742ef..47e132d 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -10,7 +10,7 @@ public function getWalletBalanceByType(string $walletType); public function hasSufficientBalance($value): bool; - public function pay(int|float $orderValue); + public function pay(int|float $orderValue, ?string $notes = null); - public function deposit(string $type, int|float $amount): bool; + public function deposit(string $type, int|float $amount, ?string $notes = null): bool; } diff --git a/src/Models/WalletsLog.php b/src/Models/WalletsLog.php index ca751e2..e278404 100644 --- a/src/Models/WalletsLog.php +++ b/src/Models/WalletsLog.php @@ -16,7 +16,7 @@ class WalletsLog extends Model use HasFactory; protected $fillable = [ - 'from', 'to', 'type', 'ip', 'value', 'wallet_name', + 'from', 'to', 'type', 'ip', 'value', 'wallet_name', 'notes', ]; public function loggable(): MorphTo diff --git a/src/Services/PocketServices.php b/src/Services/PocketServices.php index 1fe31cc..91db495 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -4,14 +4,14 @@ class PocketServices { - public function deposit($user, $type, $amount) + public function deposit($user, $type, $amount, $notes = null) { - return $user->deposit($type, $amount); + return $user->deposit($type, $amount, $notes); } - public function pay($user, $orderValue) + public function pay($user, $orderValue, $notes = null) { - return $user->pay($orderValue); + return $user->pay($orderValue, $notes); } public function checkBalance($user) diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 73c33ff..03cbaf8 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -15,7 +15,7 @@ trait HandlesDeposit /** * Deposit an amount to the user's wallet of a specific type. */ - public function deposit(string $type, int|float $amount, $notes = null): bool + public function deposit(string $type, int|float $amount, ?string $notes = null): bool { $depositable = $this->getDepositableTypes(); diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 6e534c6..4bfa835 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,7 +13,7 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue, $notes = null): void + public function pay(int|float $orderValue, ?string $notes = null): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); diff --git a/tests/OperationsWithFacadeTest.php b/tests/OperationsWithFacadeTest.php index 5aaa2c0..6a44c9e 100644 --- a/tests/OperationsWithFacadeTest.php +++ b/tests/OperationsWithFacadeTest.php @@ -1,6 +1,7 @@ toBeFloat(0.12); }); -test('description can be added during payment', function () { +test('notes can be added during deposit', function () { + $user = User::factory()->create(); + + $type = 'wallet_2'; + + $description = \Illuminate\Support\Str::random(); + LaravelPayPocket::deposit($user, $type, 234.56, $description); + + expect(WalletsLog::where('notes', $description)->exists())->toBe(true); +}); + +test('notes can be added during payment', function () { $user = User::factory()->create(); $type = 'wallet_2'; diff --git a/tests/OperationsWithoutFacadeTest.php b/tests/OperationsWithoutFacadeTest.php index 4ed2d6f..6a9f3d8 100644 --- a/tests/OperationsWithoutFacadeTest.php +++ b/tests/OperationsWithoutFacadeTest.php @@ -1,5 +1,6 @@ walletBalance)->toBeFloat(0.12); }); -test('description can be added during payment', function () { +test('notes can be added during deposit', function () { + $user = User::factory()->create(); + + $type = 'wallet_2'; + + $description = \Illuminate\Support\Str::random(); + $user->deposit($type, 234.56, $description); + + expect(WalletsLog::where('notes', $description)->exists())->toBe(true); +}); + +test('notes can be added during payment', function () { $user = User::factory()->create(); $type = 'wallet_2'; diff --git a/tests/TestCase.php b/tests/TestCase.php index 55ad4e2..6af0807 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,9 +22,9 @@ protected function setUp(): void */ Factory::guessFactoryNamesUsing( - fn (string $modelName) => 'HPWebdeveloper\\LaravelPayPocket\\Tests\\Database\\Factories\\'.class_basename( + fn (string $modelName) => 'HPWebdeveloper\\LaravelPayPocket\\Tests\\Database\\Factories\\' . class_basename( $modelName - ).'Factory' + ) . 'Factory' ); } @@ -46,13 +46,16 @@ public function getEnvironmentSetUp($app) $migration->up(); */ - $migration = include __DIR__.'/database/migrations/create_users_tables.php'; + $migration = include __DIR__ . '/database/migrations/create_users_tables.php'; $migration->up(); - $migration = include __DIR__.'/../database/migrations/create_wallets_logs_table.php.stub'; + $migration = include __DIR__ . '/../database/migrations/create_wallets_logs_table.php.stub'; $migration->up(); - $migration = include __DIR__.'/../database/migrations/create_wallets_table.php.stub'; + $migration = include __DIR__ . '/../database/migrations/create_wallets_table.php.stub'; + $migration->up(); + + $migration = include __DIR__ . '/../database/migrations/update_wallets_logs_table.php.stub'; $migration->up(); } } From 6c2fdbb8cb3d6671e65bf6297261df5c3f4cfd2b Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Mon, 8 Jan 2024 15:33:04 +0100 Subject: [PATCH 06/17] Updating README --- README.md | 72 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 30625af..102222a 100644 --- a/README.md +++ b/README.md @@ -7,48 +7,46 @@ [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/hpwebdeveloper/laravel-pay-pocket/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/hpwebdeveloper/laravel-pay-pocket/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) [![Imports](https://github.com/HPWebdeveloper/laravel-pay-pocket/actions/workflows/check_imports.yml/badge.svg?branch=main)](https://github.com/HPWebdeveloper/laravel-pay-pocket/actions/workflows/check_imports.yml) - **Laravel Pay Pocket** is a package designed for Laravel applications, offering the flexibility to manage multiple wallet types within two dedicated database tables, `wallets` and `wallets_logs`. **Demo** https://github.com/HPWebdeveloper/demo-pay-pocket **Note:** This package does not handle payments from payment platforms, but instead offers the concept of virtual money, deposit, and withdrawal. -* **Author**: Hamed Panjeh -* **Vendor**: hpwebdeveloper -* **Package**: laravel-pay-pocket -* **Alias name**: Laravel PPP (Laravel Pay Pocket Package) -* **Version**: `1.x` -* **PHP Version**: 8.1+ -* **Laravel Version**: `10.x` -* **[Composer](https://getcomposer.org/):** `composer require hpwebdeveloper/laravel-pay-pocket` - +- **Author**: Hamed Panjeh +- **Vendor**: hpwebdeveloper +- **Package**: laravel-pay-pocket +- **Alias name**: Laravel PPP (Laravel Pay Pocket Package) +- **Version**: `1.x` +- **PHP Version**: 8.1+ +- **Laravel Version**: `10.x` +- **[Composer](https://getcomposer.org/):** `composer require hpwebdeveloper/laravel-pay-pocket` ### Support Policy -| Version | Laravel | PHP | Release date | End of improvements | End of support | -|---------|----------------|---------------|--------------|---------------------|----------------| -| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | | -| x.x | | | | | | | - +| Version | Laravel | PHP | Release date | End of improvements | End of support | +| ------- | ------- | ------------- | ------------ | ------------------- | -------------- | --- | +| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | | +| x.x | | | | | | | ## Installation: -- **Step 1:** You can install the package via composer: +- **Step 1:** You can install the package via composer: ```bash composer require hpwebdeveloper/laravel-pay-pocket ``` -- **Step 2:** Publish and run the migrations with: +- **Step 2:** Publish and run the migrations with: ```bash php artisan vendor:publish --tag="pay-pocket-migrations" php artisan migrate ``` + You have successfully added two dedicated database tables, `wallets` and `wallets_logs`, without making any modifications to the `users` table. -- **Step 3:** Publish the wallet types using +- **Step 3:** Publish the wallet types using ```bash php artisan vendor:publish --tag="pay-pocket-wallets" @@ -60,7 +58,7 @@ This command will automatically publish the `WalletEnums.php` file into your app ### Prepare User Model -To use this package you need to implement the `WalletOperations` into `User` model and utilize the `ManagesWallet` trait. +To use this package you need to implement the `WalletOperations` into `User` model and utilize the `ManagesWallet` trait. ```php @@ -75,9 +73,10 @@ class User extends Authenticatable implements WalletOperations ### Prepare Wallets -In Laravel Pay Pocket, you have the flexibility to define the order in which wallets are prioritized for payments through the use of Enums. The order of wallets in the Enum file determines their priority level. The first wallet listed has the highest priority and will be used first for deducting order values. +In Laravel Pay Pocket, you have the flexibility to define the order in which wallets are prioritized for payments through the use of Enums. The order of wallets in the Enum file determines their priority level. The first wallet listed has the highest priority and will be used first for deducting order values. For example, consider the following wallet types defined in the Enum class (published in step 3 of installation): + ```php namespace App\Enums; @@ -88,15 +87,17 @@ enum WalletEnums: string } ``` -**You have complete freedom to name your wallets as per your requirements and even add more wallet types to the Enum list.** +**You have complete freedom to name your wallets as per your requirements and even add more wallet types to the Enum list.** In this particular setup, `wallet_1` (`WALLET1`) is given the **highest priority**. When an order payment is processed, the system will first attempt to use `wallet_1` to cover the cost. If `wallet_1` does not have sufficient funds, `wallet_2` (`WALLET2`) will be used next. ### Example: + If the balance in `wallet_1` is 10 and the balance in `wallet_2` is 20, and you need to pay an order value of 15, the payment process will first utilize the entire balance of `wallet_1`. Since `wallet_1`'s balance is insufficient to cover the full amount, the remaining 5 will be deducted from `wallet_2`. After the payment, `wallet_2` will have a remaining balance of 15." ## Usage, APIs and Operations: + ### Deposit ```php @@ -113,13 +114,24 @@ use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; LaravelPayPocket::deposit($user, 'wallet_1', 123.45); ``` + Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`. +#### Transaction Info ([#8][i8]) + +In a case where you want to enter descriptions for a particular transaction, the `$notes` param allows you to provide information about why a transaction happened. + +```php +$user = auth()->user(); +$user->deposit('wallet_1', 67.89, 'You ordered pizza.'); +``` + ### Pay + ```php // Pay the value using the total combined balance available across all wallets $user->pay(12.34); - + // Or using provided facade use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; @@ -129,7 +141,8 @@ LaravelPayPocket::pay($user, 12.34); ### Balance -- **Wallets** +- **Wallets** + ```php $user->walletBalance // Total combined balance available across all wallets @@ -138,7 +151,8 @@ $user->walletBalance // Total combined balance available across all wallets LaravelPayPocket::checkBalance($user); ``` -- **Particular Wallet** +- **Particular Wallet** + ```php $user->getWalletBalanceByType('wallet_1') // Balance available in wallet_1 $user->getWalletBalanceByType('wallet_2') // Balance available in wallet_2 @@ -149,7 +163,8 @@ LaravelPayPocket::walletBalanceByType($user, 'wallet_1'); ``` ### Exceptions -Upon examining the `src/Exceptions` directory within the source code, + +Upon examining the `src/Exceptions` directory within the source code, you will discover a variety of exceptions tailored to address each scenario of invalid entry. Review the [demo](https://github.com/HPWebdeveloper/demo-pay-pocket) that accounts for some of the exceptions. ### Log @@ -157,7 +172,6 @@ you will discover a variety of exceptions tailored to address each scenario of i A typical `wallets_logs` table. ![Laravel Pay Pocket Log](https://github.com/HPWebdeveloper/laravel-pay-pocket/assets/16323354/a242d335-8bd2-4af1-aa38-4e95b8870941) - ## Testing ```bash @@ -185,9 +199,9 @@ Please review [our security policy](../../security/policy) on how to report secu ## Credits -- [Hamed Panjeh](https://github.com/HPWebdeveloper) -- [All Contributors](../../contributors) -- Icon in the above image: pocket by Creative Mahira from [Noun Project](https://thenounproject.com/browse/icons/term/pocket/) (CC BY 3.0) +- [Hamed Panjeh](https://github.com/HPWebdeveloper) +- [All Contributors](../../contributors) +- Icon in the above image: pocket by Creative Mahira from [Noun Project](https://thenounproject.com/browse/icons/term/pocket/) (CC BY 3.0) ## License From 37c2a04c0f19c8469372425ffaccea0d62d96ce8 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Mon, 8 Jan 2024 15:47:01 +0100 Subject: [PATCH 07/17] Updating README --- README.md | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 102222a..227f257 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,12 @@ php artisan vendor:publish --tag="pay-pocket-wallets" This command will automatically publish the `WalletEnums.php` file into your application's `app/Enums` directory. +## Updating + +If updating from version `<= 1.0.3`, new migrations have been added to support the new [Transaction Info Feature](#transaction-info) + +Follow the [Installation](#installation) Step 2 to update your migrations. + ## Preparation ### Prepare User Model @@ -100,17 +106,30 @@ If the balance in `wallet_1` is 10 and the balance in `wallet_2` is 20, and you ### Deposit +```php +deposit(type: 'wallet_1', amount: 123.45, notes: null) +``` + +Deposit funds into `wallet_1` + ```php $user = auth()->user(); +$user->deposit('wallet_1', 123.45); +``` -$user->deposit('wallet_1', 123.45); // Deposit funds into 'wallet_1' +Deposit funds into `wallet_2` -$user->deposit('wallet_2', 67.89); // Deposit funds into 'wallet_2' +```php +$user = auth()->user(); +$user->deposit('wallet_2', 67.89); +``` -// Or using provided facade +Or using provided facade +```php use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; +$user = auth()->user(); LaravelPayPocket::deposit($user, 'wallet_1', 123.45); ``` @@ -129,16 +148,27 @@ $user->deposit('wallet_1', 67.89, 'You ordered pizza.'); ### Pay ```php -// Pay the value using the total combined balance available across all wallets +pay(amount: 12.34, notes: null) +``` + +Pay the value using the total combined balance available across all allowed wallets + +```php +$user = auth()->user(); $user->pay(12.34); +``` -// Or using provided facade +Or using provided facade +```php use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; +$user = auth()->user(); LaravelPayPocket::pay($user, 12.34); ``` +By default the sytem will attempt to pay using all available wallets exept the `allowedWallets` param is provided. + ### Balance - **Wallets** @@ -206,3 +236,5 @@ Please review [our security policy](../../security/policy) on how to report secu ## License The MIT License (MIT). Please see [License File](LICENSE.md) for more information. + +[i8]: https://github.com/HPWebdeveloper/laravel-pay-pocket/issues/8 From 01b8f0bd7c66ee619e3f5f667c5336d5631a26a5 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Mon, 8 Jan 2024 16:14:22 +0100 Subject: [PATCH 08/17] Updating README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 227f257..d8f3077 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ ### Support Policy -| Version | Laravel | PHP | Release date | End of improvements | End of support | +| Version | Laravel | PHP | Release date | End of improvements | End of support | | | ------- | ------- | ------------- | ------------ | ------------------- | -------------- | --- | | 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | | | x.x | | | | | | | From d12241c26ad00f3281c1a41034a96c08ad7ab3fa Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Mon, 8 Jan 2024 16:17:05 +0100 Subject: [PATCH 09/17] Updating README --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d8f3077..55a995e 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ ### Support Policy -| Version | Laravel | PHP | Release date | End of improvements | End of support | | -| ------- | ------- | ------------- | ------------ | ------------------- | -------------- | --- | -| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | | -| x.x | | | | | | | +| Version | Laravel | PHP | Release date | End of improvements | End of support | +| ------- | ------- | ------------- | ------------ | ------------------- | -------------- | +| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | +| x.x | | | | | | ## Installation: From 62b3b01a0f988c6f19b4de68961cc4f4a73738a7 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Mon, 8 Jan 2024 16:19:39 +0100 Subject: [PATCH 10/17] Run laravel PINT --- config/pay-pocket.php | 2 +- src/Exceptions/InsufficientBalanceException.php | 2 +- src/Exceptions/InvalidDepositException.php | 2 +- src/Exceptions/InvalidValueException.php | 2 +- src/Exceptions/InvalidWalletTypeException.php | 2 +- src/Exceptions/WalletNotFoundException.php | 2 +- src/Interfaces/WalletOperations.php | 4 ++-- src/LaravelPayPocketServiceProvider.php | 9 ++++----- src/Traits/BalanceOperation.php | 2 +- src/Traits/HandlesDeposit.php | 2 +- src/Traits/HandlesPayment.php | 2 +- tests/TestCase.php | 12 ++++++------ 12 files changed, 21 insertions(+), 22 deletions(-) diff --git a/config/pay-pocket.php b/config/pay-pocket.php index 1b65891..19d16f2 100644 --- a/config/pay-pocket.php +++ b/config/pay-pocket.php @@ -9,4 +9,4 @@ * The third array item should contain optional parameters to pass to the generator */ 'log_reference_generator' => [\Illuminate\Support\Str::class, 'random', [15]], -]; \ No newline at end of file +]; diff --git a/src/Exceptions/InsufficientBalanceException.php b/src/Exceptions/InsufficientBalanceException.php index 1e87b6f..e7332f1 100644 --- a/src/Exceptions/InsufficientBalanceException.php +++ b/src/Exceptions/InsufficientBalanceException.php @@ -12,7 +12,7 @@ class InsufficientBalanceException extends Exception * @param string $message * @param int $code */ - public function __construct($message = 'Insufficient balance to cover the order', $code = 0, ?\Throwable $previous = null) + public function __construct($message = 'Insufficient balance to cover the order', $code = 0, \Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidDepositException.php b/src/Exceptions/InvalidDepositException.php index 937011e..da644ba 100644 --- a/src/Exceptions/InvalidDepositException.php +++ b/src/Exceptions/InvalidDepositException.php @@ -12,7 +12,7 @@ class InvalidDepositException extends Exception * @param string $message * @param int $code */ - public function __construct($message = 'Invalid deposit operation', $code = 0, ?\Throwable $previous = null) + public function __construct($message = 'Invalid deposit operation', $code = 0, \Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidValueException.php b/src/Exceptions/InvalidValueException.php index 7c351db..36a6d7e 100644 --- a/src/Exceptions/InvalidValueException.php +++ b/src/Exceptions/InvalidValueException.php @@ -12,7 +12,7 @@ class InvalidValueException extends Exception * @param string $message * @param int $code */ - public function __construct($message = 'Invalie value to deposit', $code = 0, ?\Throwable $previous = null) + public function __construct($message = 'Invalie value to deposit', $code = 0, \Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidWalletTypeException.php b/src/Exceptions/InvalidWalletTypeException.php index 5f35ef9..cfae06d 100644 --- a/src/Exceptions/InvalidWalletTypeException.php +++ b/src/Exceptions/InvalidWalletTypeException.php @@ -6,7 +6,7 @@ class InvalidWalletTypeException extends Exception { - public function __construct($message = 'Invalid wallet type', $code = 0, ?\Throwable $previous = null) + public function __construct($message = 'Invalid wallet type', $code = 0, \Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/WalletNotFoundException.php b/src/Exceptions/WalletNotFoundException.php index 4b314a6..ee62fde 100644 --- a/src/Exceptions/WalletNotFoundException.php +++ b/src/Exceptions/WalletNotFoundException.php @@ -6,7 +6,7 @@ class WalletNotFoundException extends Exception { - public function __construct($message = 'Wallet not found', $code = 0, ?\Throwable $previous = null) + public function __construct($message = 'Wallet not found', $code = 0, \Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index 47e132d..97e166d 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -10,7 +10,7 @@ public function getWalletBalanceByType(string $walletType); public function hasSufficientBalance($value): bool; - public function pay(int|float $orderValue, ?string $notes = null); + public function pay(int|float $orderValue, string $notes = null); - public function deposit(string $type, int|float $amount, ?string $notes = null): bool; + public function deposit(string $type, int|float $amount, string $notes = null): bool; } diff --git a/src/LaravelPayPocketServiceProvider.php b/src/LaravelPayPocketServiceProvider.php index 57638a6..3b4631e 100644 --- a/src/LaravelPayPocketServiceProvider.php +++ b/src/LaravelPayPocketServiceProvider.php @@ -28,18 +28,17 @@ public function configurePackage(Package $package): void public function bootingPackage() { $this->publishes([ - __DIR__ . '/../Enums/' => app_path('Enums'), + __DIR__.'/../Enums/' => app_path('Enums'), ], 'pay-pocket-wallets'); - $this->publishes([ - __DIR__ . '/../config/pay-pocket.php' => config_path('pay-pocket.php'), + __DIR__.'/../config/pay-pocket.php' => config_path('pay-pocket.php'), ], 'config'); } public function registeringPackage() { // Automatically apply the package configuration - $this->mergeConfigFrom(__DIR__ . '/../config/pay-pocket.php', 'pay-pocket'); + $this->mergeConfigFrom(__DIR__.'/../config/pay-pocket.php', 'pay-pocket'); } -} \ No newline at end of file +} diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index a82a027..cf6ceb2 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -57,7 +57,7 @@ protected function createLog($logType, $value, $notes = null): void 'ip' => \Request::ip(), 'value' => $value, 'notes' => $notes, - 'reference' => $reference + 'reference' => $reference, ]); $this->createdLog->changeStatus('Done'); diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 03cbaf8..d1e6cb9 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -15,7 +15,7 @@ trait HandlesDeposit /** * Deposit an amount to the user's wallet of a specific type. */ - public function deposit(string $type, int|float $amount, ?string $notes = null): bool + public function deposit(string $type, int|float $amount, string $notes = null): bool { $depositable = $this->getDepositableTypes(); diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 4bfa835..4615f51 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,7 +13,7 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue, ?string $notes = null): void + public function pay(int|float $orderValue, string $notes = null): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); diff --git a/tests/TestCase.php b/tests/TestCase.php index 6af0807..1722f4d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,9 +22,9 @@ protected function setUp(): void */ Factory::guessFactoryNamesUsing( - fn (string $modelName) => 'HPWebdeveloper\\LaravelPayPocket\\Tests\\Database\\Factories\\' . class_basename( + fn (string $modelName) => 'HPWebdeveloper\\LaravelPayPocket\\Tests\\Database\\Factories\\'.class_basename( $modelName - ) . 'Factory' + ).'Factory' ); } @@ -46,16 +46,16 @@ public function getEnvironmentSetUp($app) $migration->up(); */ - $migration = include __DIR__ . '/database/migrations/create_users_tables.php'; + $migration = include __DIR__.'/database/migrations/create_users_tables.php'; $migration->up(); - $migration = include __DIR__ . '/../database/migrations/create_wallets_logs_table.php.stub'; + $migration = include __DIR__.'/../database/migrations/create_wallets_logs_table.php.stub'; $migration->up(); - $migration = include __DIR__ . '/../database/migrations/create_wallets_table.php.stub'; + $migration = include __DIR__.'/../database/migrations/create_wallets_table.php.stub'; $migration->up(); - $migration = include __DIR__ . '/../database/migrations/update_wallets_logs_table.php.stub'; + $migration = include __DIR__.'/../database/migrations/update_wallets_logs_table.php.stub'; $migration->up(); } } From 4e7dc77381a9c1a792ad78a5d8f8ea8bfddad639 Mon Sep 17 00:00:00 2001 From: Hamed Panjeh Date: Thu, 11 Jan 2024 09:55:59 -0300 Subject: [PATCH 11/17] Rename migration --- ...ce_columns_to_wallets_logs_table.php.stub} | 0 src/LaravelPayPocketServiceProvider.php | 2 +- tests/TestCase.php | 48 +++++++++---------- 3 files changed, 25 insertions(+), 25 deletions(-) rename database/migrations/{update_wallets_logs_table.php.stub => add_notes_and_reference_columns_to_wallets_logs_table.php.stub} (100%) diff --git a/database/migrations/update_wallets_logs_table.php.stub b/database/migrations/add_notes_and_reference_columns_to_wallets_logs_table.php.stub similarity index 100% rename from database/migrations/update_wallets_logs_table.php.stub rename to database/migrations/add_notes_and_reference_columns_to_wallets_logs_table.php.stub diff --git a/src/LaravelPayPocketServiceProvider.php b/src/LaravelPayPocketServiceProvider.php index 3b4631e..b8baa3a 100644 --- a/src/LaravelPayPocketServiceProvider.php +++ b/src/LaravelPayPocketServiceProvider.php @@ -21,7 +21,7 @@ public function configurePackage(Package $package): void ->hasMigrations( 'create_wallets_logs_table', 'create_wallets_table', - 'update_wallets_logs_table' + 'add_notes_and_reference_columns_to_wallets_logs_table' ); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 1722f4d..1dfaf4c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,6 +11,30 @@ class TestCase extends Orchestra { use RefreshDatabase; + public function getEnvironmentSetUp($app) + { + config()->set('app.key', 'base64:EWcFBKBT8lKlGK8nQhTHY+wg19QlfmbhtO9Qnn3NfcA='); + + config()->set('database.default', 'testing'); + + /* + $migration = include __DIR__.'/../database/migrations/create_laravel-pay-pocket_table.php.stub'; + $migration->up(); + */ + + $migration = include __DIR__.'/database/migrations/create_users_tables.php'; + $migration->up(); + + $migration = include __DIR__.'/../database/migrations/create_wallets_logs_table.php.stub'; + $migration->up(); + + $migration = include __DIR__.'/../database/migrations/create_wallets_table.php.stub'; + $migration->up(); + + $migration = include __DIR__.'/../database/migrations/add_notes_and_reference_columns_to_wallets_logs_table.php.stub'; + $migration->up(); + } + protected function setUp(): void { parent::setUp(); @@ -34,28 +58,4 @@ protected function getPackageProviders($app) LaravelPayPocketServiceProvider::class, ]; } - - public function getEnvironmentSetUp($app) - { - config()->set('app.key', 'base64:EWcFBKBT8lKlGK8nQhTHY+wg19QlfmbhtO9Qnn3NfcA='); - - config()->set('database.default', 'testing'); - - /* - $migration = include __DIR__.'/../database/migrations/create_laravel-pay-pocket_table.php.stub'; - $migration->up(); - */ - - $migration = include __DIR__.'/database/migrations/create_users_tables.php'; - $migration->up(); - - $migration = include __DIR__.'/../database/migrations/create_wallets_logs_table.php.stub'; - $migration->up(); - - $migration = include __DIR__.'/../database/migrations/create_wallets_table.php.stub'; - $migration->up(); - - $migration = include __DIR__.'/../database/migrations/update_wallets_logs_table.php.stub'; - $migration->up(); - } } From b52fe3f815a5caddfd2c165231fe18d9c32ac970 Mon Sep 17 00:00:00 2001 From: Hamed Panjeh Date: Thu, 11 Jan 2024 10:17:25 -0300 Subject: [PATCH 12/17] Update readme --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 55a995e..9a10153 100644 --- a/README.md +++ b/README.md @@ -167,8 +167,6 @@ $user = auth()->user(); LaravelPayPocket::pay($user, 12.34); ``` -By default the sytem will attempt to pay using all available wallets exept the `allowedWallets` param is provided. - ### Balance - **Wallets** @@ -237,4 +235,4 @@ Please review [our security policy](../../security/policy) on how to report secu The MIT License (MIT). Please see [License File](LICENSE.md) for more information. -[i8]: https://github.com/HPWebdeveloper/laravel-pay-pocket/issues/8 +[i8]: Tag link (will be updated soon) From b5a79741545348d476f6113b6fb1c49e554a4ddf Mon Sep 17 00:00:00 2001 From: Hamed Panjeh Date: Thu, 11 Jan 2024 11:31:38 -0300 Subject: [PATCH 13/17] Update readme --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9a10153..335bc91 100644 --- a/README.md +++ b/README.md @@ -50,15 +50,16 @@ You have successfully added two dedicated database tables, `wallets` and `wallet ```bash php artisan vendor:publish --tag="pay-pocket-wallets" +php artisan vendor:publish --tag="config" ``` This command will automatically publish the `WalletEnums.php` file into your application's `app/Enums` directory. ## Updating -If updating from version `<= 1.0.3`, new migrations have been added to support the new [Transaction Info Feature](#transaction-info) +If updating from version `<= 1.0.3`, new migration and config files have been added to support the new [Transaction Info Feature](#transaction-info) -Follow the [Installation](#installation) Step 2 to update your migrations. +Follow the [Installation](#installation) Steps 2 and 3 to update your migrations. ## Preparation From 8096fd86d46a5731878accc666843477a4405ab1 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Thu, 11 Jan 2024 18:27:14 +0100 Subject: [PATCH 14/17] Add reference to WalletsLog fillable properties --- config/pay-pocket.php | 9 ++++++--- src/Models/WalletsLog.php | 2 +- src/Traits/BalanceOperation.php | 4 +++- tests/OperationsWithFacadeTest.php | 10 ++++++++++ tests/OperationsWithoutFacadeTest.php | 10 ++++++++++ 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/config/pay-pocket.php b/config/pay-pocket.php index 19d16f2..cc14ad4 100644 --- a/config/pay-pocket.php +++ b/config/pay-pocket.php @@ -5,8 +5,11 @@ 'log_reference_length' => 12, 'log_reference_prefix' => null, /** - * The log reference generator should be static - * The third array item should contain optional parameters to pass to the generator + * The log reference generator should be a numeric array with 3 indexes + * First item should be a static class + * Second item sould be method availble in the static class + * third item should be an array of optional parameters to pass to the method + * The default generator looks like this: [\Illuminate\Support\Str::class, 'random', [12]] */ - 'log_reference_generator' => [\Illuminate\Support\Str::class, 'random', [15]], + 'log_reference_generator' => null, ]; diff --git a/src/Models/WalletsLog.php b/src/Models/WalletsLog.php index e278404..b7a4130 100644 --- a/src/Models/WalletsLog.php +++ b/src/Models/WalletsLog.php @@ -16,7 +16,7 @@ class WalletsLog extends Model use HasFactory; protected $fillable = [ - 'from', 'to', 'type', 'ip', 'value', 'wallet_name', 'notes', + 'from', 'to', 'type', 'ip', 'value', 'wallet_name', 'notes', 'reference', ]; public function loggable(): MorphTo diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index cf6ceb2..adc3783 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -43,7 +43,9 @@ protected function createLog($logType, $value, $notes = null): void $newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value; - $refGen = config('pay-pocket.log_reference_generator', [Str::class, 'random', [12]]); + $refGen = config('pay-pocket.log_reference_generator', [ + Str::class, 'random', [config('pay-pocket.log_reference_length', 12)] + ]); $reference = config('pay-pocket.reference_string_prefix', ''); $reference .= isset($refGen[0], $refGen[1]) ? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? []) diff --git a/tests/OperationsWithFacadeTest.php b/tests/OperationsWithFacadeTest.php index 6a44c9e..2421416 100644 --- a/tests/OperationsWithFacadeTest.php +++ b/tests/OperationsWithFacadeTest.php @@ -119,3 +119,13 @@ expect(WalletsLog::where('notes', $description)->exists())->toBe(true); }); + +test('transaction reference is added to wallet log', function () { + $user = User::factory()->create(); + + $type = 'wallet_2'; + + LaravelPayPocket::deposit($user, $type, 234.56); + + expect(WalletsLog::whereNotNull('reference')->exists())->toBe(true); +}); diff --git a/tests/OperationsWithoutFacadeTest.php b/tests/OperationsWithoutFacadeTest.php index 6a9f3d8..e64143f 100644 --- a/tests/OperationsWithoutFacadeTest.php +++ b/tests/OperationsWithoutFacadeTest.php @@ -120,3 +120,13 @@ expect(WalletsLog::where('notes', $description)->exists())->toBe(true); }); + +test('transaction reference is added to wallet log', function () { + $user = User::factory()->create(); + + $type = 'wallet_2'; + + $user->deposit($type, 234.56); + + expect(WalletsLog::whereNotNull('reference')->exists())->toBe(true); +}); From a0b91c6d3d0de3f28c29bd0db9c422dd96c6a582 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Thu, 11 Jan 2024 18:27:47 +0100 Subject: [PATCH 15/17] Run Pint --- src/Traits/BalanceOperation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index adc3783..abbd4a5 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -44,7 +44,7 @@ protected function createLog($logType, $value, $notes = null): void $newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value; $refGen = config('pay-pocket.log_reference_generator', [ - Str::class, 'random', [config('pay-pocket.log_reference_length', 12)] + Str::class, 'random', [config('pay-pocket.log_reference_length', 12)], ]); $reference = config('pay-pocket.reference_string_prefix', ''); $reference .= isset($refGen[0], $refGen[1]) From 15f2db5c7563ac2716291187f1826e3564eaebd2 Mon Sep 17 00:00:00 2001 From: Hamed Panjeh Date: Thu, 11 Jan 2024 15:24:46 -0300 Subject: [PATCH 16/17] Modify comment in config file --- config/pay-pocket.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/config/pay-pocket.php b/config/pay-pocket.php index cc14ad4..5c8e375 100644 --- a/config/pay-pocket.php +++ b/config/pay-pocket.php @@ -1,15 +1,19 @@ 12, 'log_reference_prefix' => null, - /** - * The log reference generator should be a numeric array with 3 indexes - * First item should be a static class - * Second item sould be method availble in the static class - * third item should be an array of optional parameters to pass to the method - * The default generator looks like this: [\Illuminate\Support\Str::class, 'random', [12]] - */ 'log_reference_generator' => null, ]; From f0dcea0ba927ca3ae41cd8bc18c56d8d80e9c2b6 Mon Sep 17 00:00:00 2001 From: Hamed Panjeh Date: Thu, 11 Jan 2024 15:36:09 -0300 Subject: [PATCH 17/17] ./vendor/bin/pint --- src/Exceptions/InsufficientBalanceException.php | 2 +- src/Exceptions/InvalidDepositException.php | 2 +- src/Exceptions/InvalidValueException.php | 2 +- src/Exceptions/InvalidWalletTypeException.php | 2 +- src/Exceptions/WalletNotFoundException.php | 2 +- src/Interfaces/WalletOperations.php | 4 ++-- src/Traits/HandlesDeposit.php | 2 +- src/Traits/HandlesPayment.php | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Exceptions/InsufficientBalanceException.php b/src/Exceptions/InsufficientBalanceException.php index e7332f1..1e87b6f 100644 --- a/src/Exceptions/InsufficientBalanceException.php +++ b/src/Exceptions/InsufficientBalanceException.php @@ -12,7 +12,7 @@ class InsufficientBalanceException extends Exception * @param string $message * @param int $code */ - public function __construct($message = 'Insufficient balance to cover the order', $code = 0, \Throwable $previous = null) + public function __construct($message = 'Insufficient balance to cover the order', $code = 0, ?\Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidDepositException.php b/src/Exceptions/InvalidDepositException.php index da644ba..937011e 100644 --- a/src/Exceptions/InvalidDepositException.php +++ b/src/Exceptions/InvalidDepositException.php @@ -12,7 +12,7 @@ class InvalidDepositException extends Exception * @param string $message * @param int $code */ - public function __construct($message = 'Invalid deposit operation', $code = 0, \Throwable $previous = null) + public function __construct($message = 'Invalid deposit operation', $code = 0, ?\Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidValueException.php b/src/Exceptions/InvalidValueException.php index 36a6d7e..7c351db 100644 --- a/src/Exceptions/InvalidValueException.php +++ b/src/Exceptions/InvalidValueException.php @@ -12,7 +12,7 @@ class InvalidValueException extends Exception * @param string $message * @param int $code */ - public function __construct($message = 'Invalie value to deposit', $code = 0, \Throwable $previous = null) + public function __construct($message = 'Invalie value to deposit', $code = 0, ?\Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/InvalidWalletTypeException.php b/src/Exceptions/InvalidWalletTypeException.php index cfae06d..5f35ef9 100644 --- a/src/Exceptions/InvalidWalletTypeException.php +++ b/src/Exceptions/InvalidWalletTypeException.php @@ -6,7 +6,7 @@ class InvalidWalletTypeException extends Exception { - public function __construct($message = 'Invalid wallet type', $code = 0, \Throwable $previous = null) + public function __construct($message = 'Invalid wallet type', $code = 0, ?\Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/WalletNotFoundException.php b/src/Exceptions/WalletNotFoundException.php index ee62fde..4b314a6 100644 --- a/src/Exceptions/WalletNotFoundException.php +++ b/src/Exceptions/WalletNotFoundException.php @@ -6,7 +6,7 @@ class WalletNotFoundException extends Exception { - public function __construct($message = 'Wallet not found', $code = 0, \Throwable $previous = null) + public function __construct($message = 'Wallet not found', $code = 0, ?\Throwable $previous = null) { parent::__construct($message, $code, $previous); } diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index 97e166d..47e132d 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -10,7 +10,7 @@ public function getWalletBalanceByType(string $walletType); public function hasSufficientBalance($value): bool; - public function pay(int|float $orderValue, string $notes = null); + public function pay(int|float $orderValue, ?string $notes = null); - public function deposit(string $type, int|float $amount, string $notes = null): bool; + public function deposit(string $type, int|float $amount, ?string $notes = null): bool; } diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index d1e6cb9..03cbaf8 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -15,7 +15,7 @@ trait HandlesDeposit /** * Deposit an amount to the user's wallet of a specific type. */ - public function deposit(string $type, int|float $amount, string $notes = null): bool + public function deposit(string $type, int|float $amount, ?string $notes = null): bool { $depositable = $this->getDepositableTypes(); diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 4615f51..4bfa835 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,7 +13,7 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue, string $notes = null): void + public function pay(int|float $orderValue, ?string $notes = null): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.');