Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a collection of all related WalletLog for successfull calls to the pay() method. #37

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Return a collection of all related WalletLog for successfull call to …
…the pay() method.
  • Loading branch information
3m1n3nc3 committed Jun 29, 2024
commit 6926242a4aa409267755b74da08c4b1a9779b70c
8 changes: 4 additions & 4 deletions config/pay-pocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
| This configuration allows you to customize the generation of log reference strings
| within the LaravelPayPocket package.
|
| - [array] log_reference_params: The parameters to pass to the log reference generator.
| - [string] log_reference_prefix: The prefix for the generated reference string.
| - [class-string] log_reference_generator_class: The fully qualified name of the class containing static methods for generation.
| - [string] log_reference_generator_method: The name of the static method available in the generator class.
| - [array] log_reference_params: An array of parameters to pass to the log_reference_generator_method.
| - [string] log_reference_prefix: Prefix for the generated reference string.
| - [class-string] log_reference_generator_class: Fully qualified name of the class containing static methods for generation.
| - [string] log_reference_generator_method: Name of the static method available in the generator class.
|
| By default, the following generator is set up:
| Illuminate\Support\Str::random(12)
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/LaravelPayPocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ protected static function getFacadeAccessor(): string
{
return \HPWebdeveloper\LaravelPayPocket\Services\PocketServices::class;
}
}
}
7 changes: 3 additions & 4 deletions src/Interfaces/WalletOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ public function hasSufficientBalance(int|float $value): bool;
/**
* Pay the order value from the user's wallets.
*
* @param int|float $orderValue
* @param ?string $notes
* @param ?string $notes
* @return \Illuminate\Support\Collection<TKey,WalletsLog>
*
* @throws InsufficientBalanceException
* @return \Illuminate\Support\Collection<TKey,WalletsLog>
*/
public function pay(int|float $orderValue, ?string $notes = null): \Illuminate\Support\Collection;

Expand All @@ -41,4 +40,4 @@ public function deposit(string $type, int|float $amount, ?string $notes = null):
* Get user's wallet balance.
*/
public function getWalletBalance(): int|float;
}
}
8 changes: 3 additions & 5 deletions src/Services/PocketServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ 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
* @param ?string $notes
* @return \Illuminate\Support\Collection<TKey,WalletsLog>
*
* @throws InsufficientBalanceException
* @return \Illuminate\Support\Collection<TKey,WalletsLog>
*/
public function pay(WalletOperations $user, int|float $orderValue, ?string $notes = null): \Illuminate\Support\Collection
{
Expand All @@ -45,4 +43,4 @@ public function walletBalanceByType(WalletOperations $user, string $type): int|f
{
return $user->getWalletBalanceByType($type);
}
}
}
10 changes: 6 additions & 4 deletions src/Traits/BalanceOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function decrementAndCreateLog(int|float $value, ?string $notes = null):
{
$this->createLog('dec', $value, $notes);
$this->decrement('balance', $value);

return $this->createdLog;
}

Expand All @@ -34,6 +35,7 @@ public function incrementAndCreateLog(int|float $value, ?string $notes = null):
{
$this->createLog('inc', $value, $notes);
$this->increment('balance', $value);

return $this->createdLog;
}

Expand Down Expand Up @@ -67,15 +69,15 @@ protected function generateReference(): string
{
$className = config('pay-pocket.log_reference_generator_class');
$methodName = config('pay-pocket.log_reference_generator_method');
$params = (array)config('pay-pocket.log_reference_params', [12]);
$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.');
}

$reference = call_user_func([$className, $methodName], ...$params);

return $prefix . $reference;
return $prefix.$reference;
}
}
}
11 changes: 5 additions & 6 deletions src/Traits/HandlesPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ trait HandlesPayment
/**
* Pay the order value from the user's wallets.
*
* @param int|float $orderValue
* @param ?string $notes
* @param ?string $notes
* @return \Illuminate\Support\Collection<TKey,WalletsLog>
*
* @throws InsufficientBalanceException
* @return \Illuminate\Support\Collection<TKey,WalletsLog>
*/
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.');
}

Expand All @@ -34,7 +33,7 @@ public function pay(int|float $orderValue, ?string $notes = null): \Illuminate\D
$logs = (new WalletsLog())->newCollection();

foreach ($walletsInOrder as $wallet) {
if (!$wallet || !$wallet->hasBalance()) {
if (! $wallet || ! $wallet->hasBalance()) {
continue;
}

Expand All @@ -54,4 +53,4 @@ public function pay(int|float $orderValue, ?string $notes = null): \Illuminate\D
return $logs;
});
}
}
}
2 changes: 1 addition & 1 deletion tests/OperationsWithFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@
$log = LaravelPayPocket::pay($user, 100.16);

expect($log->sum('value'))->toBe(100.16);
});
});
2 changes: 1 addition & 1 deletion tests/OperationsWithoutFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@
$log = $user->pay(100.16);

expect($log->sum('value'))->toBe(100.16);
});
});
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.