From 1d9e185f37837bd53ab2784b2b53ac554621ac2b Mon Sep 17 00:00:00 2001 From: Hamed Panjeh Date: Thu, 30 Nov 2023 23:49:24 -0300 Subject: [PATCH 1/6] Update readme --- README.md | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 55bebfd..22f1e94 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,19 @@ +![Laravel Pay Pocket](https://github.com/HPWebdeveloper/laravel-pay-pocket/assets/16323354/8e8ebcf6-f8d4-4811-b97c-fb6362e3f019) + # Laravel Pay Pocket [![Latest Version on Packagist](https://img.shields.io/packagist/v/hpwebdeveloper/laravel-pay-pocket.svg?style=flat-square)](https://packagist.org/packages/hpwebdeveloper/laravel-pay-pocket) [![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/hpwebdeveloper/laravel-pay-pocket/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/hpwebdeveloper/laravel-pay-pocket/actions?query=workflow%3Arun-tests+branch%3Amain) [![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) -[![Total Downloads](https://img.shields.io/packagist/dt/hpwebdeveloper/laravel-pay-pocket.svg?style=flat-square)](https://packagist.org/packages/hpwebdeveloper/laravel-pay-pocket) **Laravel Pay Pocket** is a simple package designed for Laravel applications, offering the flexibility to manage multiple wallet types within two dedicated database tables, `wallets` and `wallets_logs`. +**Note:** This package does not handle payments from payment platforms, but instead offers the concept of virtual money. + * **Author**: Hamed Panjeh * **Vendor**: hpwebdeveloper * **Package**: laravel-pay-pocket -* **Version**: +* **Version**: `1.x` * **PHP Version**: 8.1+ * **Laravel Version**: `10.x` * **[Composer](https://getcomposer.org/):** `composer require hpwebdeveloper/laravel-pay-pocket` @@ -18,10 +21,10 @@ ### Support Policy -| Version | Laravel | PHP | Release date | End of improvements | End of support | -|------------|----------------|-------------|--------------|---------------------|----------------| -| 1.x | ^10.0 | 8.1 | Nov 30, 2023 | Mar 1, 2024 | Sep 6, 2024 | | - +| Version | Laravel | PHP | Release date | End of improvements | End of support | +|---------|----------------|-------------|--------------|---------------------|----------------| +| 1.x | ^10.0 | 8.1 | Nov 30, 2023 | Mar 1, 2024 | | | +| x.x | | | | | | | ## Installation @@ -71,7 +74,7 @@ class User extends Authenticatable implements WalletOperations 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 an Enum: +For example, consider the following wallet types defined in the Enum class (published in the step 3 of installation): ```php namespace App\Enums; @@ -95,27 +98,28 @@ If the balance in `wallet_1` is 10 and the balance in `wallet_2` is 20, and you ```php $user = auth()->user(); -$user->deposit('wallet_1', 100.22); // Deposit funds into 'wallet_1' +$user->deposit('wallet_1', 123.45); // Deposit funds into 'wallet_1' -$user->deposit('wallet_2', 100); // Deposit funds into 'wallet_2' +$user->deposit('wallet_2', 67.89); // Deposit funds into 'wallet_2' // Or using provided facade use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; -LaravelPayPocket::deposit($user, 'wallet_1', 100.22); +LaravelPayPocket::deposit($user, 'wallet_1', 123.45); ``` +Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`. ### Pay ```php -$user->pay(200); // Pay the value using the total combined balance available across all wallets +$user->pay(12.34); // Pay the value using the total combined balance available across all wallets // Or using provided facade use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; -LaravelPayPocket::pay($user, 100.22); +LaravelPayPocket::pay($user, 12.34); ``` ### Balance @@ -136,12 +140,23 @@ $user->getWalletBalanceByType('wallet_2') // Balance available in wallet_2 // Or using provided facade -LaravelPayPocket::walletBalanceByType('wallet_1'); +LaravelPayPocket::walletBalanceByType($user, 'wallet_1'); ``` +Log +![Laravel Pay Pocket Log](https://github.com/HPWebdeveloper/laravel-pay-pocket/assets/16323354/a242d335-8bd2-4af1-aa38-4e95b8870941) + + ## Testing ```bash +composer install + +composer test + + +// Or + ./vender/bin/pest ``` @@ -161,6 +176,7 @@ Please review [our security policy](../../security/policy) on how to report secu - [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 d8c640ca5c6d982937e5bca9a678d00f74d1d939 Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeely Date: Tue, 12 Dec 2023 16:30:33 +0100 Subject: [PATCH 2/6] add return type, update docblock, get all wallets in a single go --- src/Traits/HandlesPayment.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index b673372..63f8a33 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -2,7 +2,6 @@ namespace HPWebdeveloper\LaravelPayPocket\Traits; -use App\Enums\WalletEnums; use HPWebdeveloper\LaravelPayPocket\Exceptions\InsufficientBalanceException; use Illuminate\Support\Facades\DB; @@ -11,11 +10,12 @@ trait HandlesPayment /** * Pay the order value from the user's wallets. * + * @param int|float $orderValue * @return void * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue) + public function pay(int|float $orderValue): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); @@ -24,10 +24,9 @@ public function pay(int|float $orderValue) DB::transaction(function () use ($orderValue) { $remainingOrderValue = $orderValue; - foreach ($this->walletsInOrder() as $walletInOrder) { - $walletEnumType = WalletEnums::tryFrom($walletInOrder); - $wallet = $this->wallets()->where('type', $walletEnumType)->first(); + $wallets = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); + foreach ($wallets as $wallet) { if (! $wallet || ! $wallet->hasBalance()) { continue; } From 9375c469512eac3f4fab13e5cf27ceca6c88ec2f Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeely Date: Tue, 12 Dec 2023 16:56:47 +0100 Subject: [PATCH 3/6] add php83 --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 7fefe35..3761563 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -13,7 +13,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest, windows-latest] - php: [8.2, 8.1] + php: [8.3, 8.2, 8.1] laravel: [10.*] stability: [prefer-lowest, prefer-stable] include: From 994a74304533986f8f1cabf26093a0fc8c507554 Mon Sep 17 00:00:00 2001 From: Hamed Panjeh <16323354+HPWebdeveloper@users.noreply.github.com> Date: Tue, 12 Dec 2023 13:39:15 -0300 Subject: [PATCH 4/6] Update HandlesPayment.php Modify variable name from $wallets to $walletsInOrder --- src/Traits/HandlesPayment.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 63f8a33..721f497 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -24,9 +24,9 @@ public function pay(int|float $orderValue): void DB::transaction(function () use ($orderValue) { $remainingOrderValue = $orderValue; - $wallets = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); + $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); - foreach ($wallets as $wallet) { + foreach ($walletsInOrder as $wallet) { if (! $wallet || ! $wallet->hasBalance()) { continue; } From bf92ab1f69641047a88b2559f8fce555c9032074 Mon Sep 17 00:00:00 2001 From: HPWebdeveloper Date: Tue, 12 Dec 2023 16:39:41 +0000 Subject: [PATCH 5/6] Fix styling --- 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/Traits/HandlesPayment.php | 2 -- 6 files changed, 5 insertions(+), 7 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/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 721f497..2e31745 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -10,8 +10,6 @@ trait HandlesPayment /** * Pay the order value from the user's wallets. * - * @param int|float $orderValue - * @return void * * @throws InsufficientBalanceException */ From 013f24d344e6ce25a78ec1a07ab77ef8bc4e8cc1 Mon Sep 17 00:00:00 2001 From: Sajjad Esmaeely Date: Tue, 12 Dec 2023 17:47:23 +0100 Subject: [PATCH 6/6] fix phpstan problem --- src/Models/Wallet.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Models/Wallet.php b/src/Models/Wallet.php index 3e07524..73d6813 100644 --- a/src/Models/Wallet.php +++ b/src/Models/Wallet.php @@ -13,7 +13,7 @@ * HPWebdeveloper\LaravelPayPocket\Models\Wallet * * @property mixed $balance - * @property HPWebdeveloper\LaravelPayPocket\Enums\WalletEnums $type + * @property WalletEnums $type */ class Wallet extends Model {