Skip to content

Commit

Permalink
Return a collection of all related WalletLog for successfull call to …
Browse files Browse the repository at this point in the history
…the pay() method.
  • Loading branch information
3m1n3nc3 committed Jun 29, 2024
2 parents ef127e9 + 02d948b commit 6926242
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 35 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

All notable changes to `laravel-pay-pocket` will be documented in this file.

## 2.2.0 - 2024-06-27

### What's Changed

* Update: Change log_reference_length to log_reference_params by @3m1n3nc3 in https://github.com/HPWebdeveloper/laravel-pay-pocket/pull/34
* Modify comments by @HPWebdeveloper in https://github.com/HPWebdeveloper/laravel-pay-pocket/pull/36

**Full Changelog**: https://github.com/HPWebdeveloper/laravel-pay-pocket/compare/2.1.0...2.2.0

## 2.1.0 - 2024-06-27

### What's Changed

* Bump aglipanci/laravel-pint-action from 2.3.1 to 2.4 by @dependabot in https://github.com/HPWebdeveloper/laravel-pay-pocket/pull/26
* Bump dependabot/fetch-metadata from 2.0.0 to 2.1.0 by @dependabot in https://github.com/HPWebdeveloper/laravel-pay-pocket/pull/27
* Add check Laravel 11 compatibility by @HPWebdeveloper in https://github.com/HPWebdeveloper/laravel-pay-pocket/pull/28
* Test over the correct php version by @HPWebdeveloper in https://github.com/HPWebdeveloper/laravel-pay-pocket/pull/29
* Support Laravel 11 by @samehdoush in https://github.com/HPWebdeveloper/laravel-pay-pocket/pull/25

### New Contributors

* @samehdoush made their first contribution in https://github.com/HPWebdeveloper/laravel-pay-pocket/pull/25

**Full Changelog**: https://github.com/HPWebdeveloper/laravel-pay-pocket/compare/2.0.3...2.1.0

## 2.0.3 - 2024-04-12

### What's Changed
Expand Down
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,34 @@

**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
**Demo:** https://github.com/HPWebdeveloper/demo-pay-pocket

**Videos:**

- [Laravel Pay Pocket Package: Virtual Wallets in Your Project](https://www.youtube.com/watch?v=KoQyURiwsA4)

- [Laravel Exceptions: Why and How to Use? Practical Example.](https://www.youtube.com/watch?v=-Sr18w91v8Q)

- [PHP Enums in Laravel: Practical Example from Package](https://www.youtube.com/watch?v=iUOb-3HQtK8)


**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`
- **Version**: `2.x`
- **PHP Version**: 8.1+
- **Laravel Version**: `10.x`
- **Laravel Version**: `10.x`, `11.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 | |
| 2.x | ^10.0, ^11.0 |8.2, 8.3| June 27, 2024 | January 30, 2025 | |

## Installation:

Expand Down Expand Up @@ -199,7 +208,7 @@ you will discover a variety of exceptions tailored to address each scenario of i
### Log

A typical `wallets_logs` table.
![Laravel Pay Pocket Log](https://github.com/HPWebdeveloper/laravel-pay-pocket/assets/16323354/a242d335-8bd2-4af1-aa38-4e95b8870941)
![Laravel Pay Pocket Log](https://github.com/HPWebdeveloper/laravel-pay-pocket/assets/16323354/0d7f2237-88e1-4ac0-a4f2-ac200bad9273)

## Testing

Expand Down
10 changes: 5 additions & 5 deletions config/pay-pocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
| 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.
|
| This is how it works by default in the code:
| 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);
});
});

0 comments on commit 6926242

Please sign in to comment.