diff --git a/src/Facades/LaravelPayPocket.php b/src/Facades/LaravelPayPocket.php index 8d7092a..502c28c 100644 --- a/src/Facades/LaravelPayPocket.php +++ b/src/Facades/LaravelPayPocket.php @@ -8,7 +8,7 @@ /** * @see \HPWebdeveloper\LaravelPayPocket\Services\PocketServices * - * @method static void pay(WalletOperations $user, int|float $orderValue, ?string $notes = null) + * @method static \Illuminate\Support\Collection pay(WalletOperations $user, int|float $orderValue, ?string $notes = null) * @method static bool deposit(WalletOperations $user, string $type, int|float $amount, ?string $notes = null) * @method static int|float checkBalance(WalletOperations $user) * @method static int|float walletBalanceByType(WalletOperations $user, string $type) @@ -19,4 +19,4 @@ protected static function getFacadeAccessor(): string { return \HPWebdeveloper\LaravelPayPocket\Services\PocketServices::class; } -} +} \ No newline at end of file diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index 572bf82..b70134b 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -24,9 +24,13 @@ public function hasSufficientBalance(int|float $value): bool; /** * Pay the order value from the user's wallets. * + * @param int|float $orderValue + * @param ?string $notes + * * @throws InsufficientBalanceException + * @return \Illuminate\Support\Collection */ - public function pay(int|float $orderValue, ?string $notes = null): void; + public function pay(int|float $orderValue, ?string $notes = null): \Illuminate\Support\Collection; /** * Deposit an amount to the user's wallet of a specific type. @@ -37,4 +41,4 @@ public function deposit(string $type, int|float $amount, ?string $notes = null): * Get user's wallet balance. */ public function getWalletBalance(): int|float; -} +} \ No newline at end of file diff --git a/src/Services/PocketServices.php b/src/Services/PocketServices.php index 71f5244..cc8a5cc 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -18,11 +18,16 @@ public function deposit(WalletOperations $user, string $type, int|float $amount, /** * Pay the order value from the user's wallets. * + * @param WalletOperations $user + * @param int|float $orderValue + * @param ?string $notes + * * @throws InsufficientBalanceException + * @return \Illuminate\Support\Collection */ - public function pay(WalletOperations $user, int|float $orderValue, ?string $notes = null): void + public function pay(WalletOperations $user, int|float $orderValue, ?string $notes = null): \Illuminate\Support\Collection { - $user->pay($orderValue, $notes); + return $user->pay($orderValue, $notes); } /** @@ -40,4 +45,4 @@ public function walletBalanceByType(WalletOperations $user, string $type): int|f { return $user->getWalletBalanceByType($type); } -} +} \ No newline at end of file diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index 16fde2d..678d6a3 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -20,19 +20,21 @@ public function hasBalance(): bool /** * Decrement Balance and create a log entry. */ - public function decrementAndCreateLog(int|float $value, ?string $notes = null): void + public function decrementAndCreateLog(int|float $value, ?string $notes = null): WalletsLog { $this->createLog('dec', $value, $notes); $this->decrement('balance', $value); + return $this->createdLog; } /** * Increment Balance and create a log entry. */ - public function incrementAndCreateLog(int|float $value, ?string $notes = null): void + public function incrementAndCreateLog(int|float $value, ?string $notes = null): WalletsLog { $this->createLog('inc', $value, $notes); $this->increment('balance', $value); + return $this->createdLog; } /** @@ -68,7 +70,7 @@ protected function generateReference(): string $params = (array)config('pay-pocket.log_reference_params', [12]); $prefix = config('pay-pocket.log_reference_prefix'); - if (! is_callable([$className, $methodName])) { + if (!is_callable([$className, $methodName])) { throw new InvalidArgumentException('Invalid configuration: The combination of log_reference_generator_class and log_reference_generator_method is not callable.'); } @@ -76,4 +78,4 @@ protected function generateReference(): string return $prefix . $reference; } -} +} \ No newline at end of file diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 2ea0a44..5055c24 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -3,6 +3,7 @@ namespace HPWebdeveloper\LaravelPayPocket\Traits; use HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException; +use HPWebdeveloper\LaravelPayPocket\Models\WalletsLog; use Illuminate\Support\Facades\DB; trait HandlesPayment @@ -10,15 +11,19 @@ trait HandlesPayment /** * Pay the order value from the user's wallets. * + * @param int|float $orderValue + * @param ?string $notes + * * @throws InsufficientBalanceException + * @return \Illuminate\Support\Collection */ - public function pay(int|float $orderValue, ?string $notes = null): void + public function pay(int|float $orderValue, ?string $notes = null): \Illuminate\Database\Eloquent\Collection { - if (! $this->hasSufficientBalance($orderValue)) { + if (!$this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); } - DB::transaction(function () use ($orderValue, $notes) { + return DB::transaction(function () use ($orderValue, $notes) { $remainingOrderValue = $orderValue; /** @@ -26,13 +31,15 @@ public function pay(int|float $orderValue, ?string $notes = null): void */ $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); + $logs = (new WalletsLog())->newCollection(); + foreach ($walletsInOrder as $wallet) { - if (! $wallet || ! $wallet->hasBalance()) { + if (!$wallet || !$wallet->hasBalance()) { continue; } $amountToDeduct = min($wallet->balance, $remainingOrderValue); - $wallet->decrementAndCreateLog($amountToDeduct, $notes); + $logs->push($wallet->decrementAndCreateLog($amountToDeduct, $notes)); $remainingOrderValue -= $amountToDeduct; if ($remainingOrderValue <= 0) { @@ -43,6 +50,8 @@ public function pay(int|float $orderValue, ?string $notes = null): void if ($remainingOrderValue > 0) { throw new InsufficientBalanceException('Insufficient total wallet balance to cover the order.'); } + + return $logs; }); } -} +} \ No newline at end of file diff --git a/tests/OperationsWithFacadeTest.php b/tests/OperationsWithFacadeTest.php index 2421416..33f2b9b 100644 --- a/tests/OperationsWithFacadeTest.php +++ b/tests/OperationsWithFacadeTest.php @@ -129,3 +129,15 @@ expect(WalletsLog::whereNotNull('reference')->exists())->toBe(true); }); + +test('Payment returns log', function () { + $user = User::factory()->create(); + + $type = 'wallet_2'; + + LaravelPayPocket::deposit($user, $type, 234.56); + + $log = LaravelPayPocket::pay($user, 100.16); + + expect($log->sum('value'))->toBe(100.16); +}); \ No newline at end of file diff --git a/tests/OperationsWithoutFacadeTest.php b/tests/OperationsWithoutFacadeTest.php index e64143f..dd71986 100644 --- a/tests/OperationsWithoutFacadeTest.php +++ b/tests/OperationsWithoutFacadeTest.php @@ -130,3 +130,15 @@ expect(WalletsLog::whereNotNull('reference')->exists())->toBe(true); }); + +test('Payment returns log', function () { + $user = User::factory()->create(); + + $type = 'wallet_2'; + + $user->deposit($type, 234.56); + + $log = $user->pay(100.16); + + expect($log->sum('value'))->toBe(100.16); +}); \ No newline at end of file