Skip to content

Commit 445bb28

Browse files
Merge pull request #10 from 3m1n3nc3/features
Add support for transaction note
2 parents ab404be + f0dcea0 commit 445bb28

13 files changed

+272
-83
lines changed

README.md

Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,65 @@
77
[![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)
88
[![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)
99

10-
1110
**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`.
1211

1312
**Demo** https://github.com/HPWebdeveloper/demo-pay-pocket
1413

1514
**Note:** This package does not handle payments from payment platforms, but instead offers the concept of virtual money, deposit, and withdrawal.
1615

17-
* **Author**: Hamed Panjeh
18-
* **Vendor**: hpwebdeveloper
19-
* **Package**: laravel-pay-pocket
20-
* **Alias name**: Laravel PPP (Laravel Pay Pocket Package)
21-
* **Version**: `1.x`
22-
* **PHP Version**: 8.1+
23-
* **Laravel Version**: `10.x`
24-
* **[Composer](https://getcomposer.org/):** `composer require hpwebdeveloper/laravel-pay-pocket`
25-
16+
- **Author**: Hamed Panjeh
17+
- **Vendor**: hpwebdeveloper
18+
- **Package**: laravel-pay-pocket
19+
- **Alias name**: Laravel PPP (Laravel Pay Pocket Package)
20+
- **Version**: `1.x`
21+
- **PHP Version**: 8.1+
22+
- **Laravel Version**: `10.x`
23+
- **[Composer](https://getcomposer.org/):** `composer require hpwebdeveloper/laravel-pay-pocket`
2624

2725
### Support Policy
2826

29-
| Version | Laravel | PHP | Release date | End of improvements | End of support |
30-
|---------|----------------|---------------|--------------|---------------------|----------------|
31-
| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | |
32-
| x.x | | | | | | |
33-
27+
| Version | Laravel | PHP | Release date | End of improvements | End of support |
28+
| ------- | ------- | ------------- | ------------ | ------------------- | -------------- |
29+
| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | |
30+
| x.x | | | | | |
3431

3532
## Installation:
3633

37-
- **Step 1:** You can install the package via composer:
34+
- **Step 1:** You can install the package via composer:
3835

3936
```bash
4037
composer require hpwebdeveloper/laravel-pay-pocket
4138
```
4239

43-
- **Step 2:** Publish and run the migrations with:
40+
- **Step 2:** Publish and run the migrations with:
4441

4542
```bash
4643
php artisan vendor:publish --tag="pay-pocket-migrations"
4744
php artisan migrate
4845
```
46+
4947
You have successfully added two dedicated database tables, `wallets` and `wallets_logs`, without making any modifications to the `users` table.
5048

51-
- **Step 3:** Publish the wallet types using
49+
- **Step 3:** Publish the wallet types using
5250

5351
```bash
5452
php artisan vendor:publish --tag="pay-pocket-wallets"
53+
php artisan vendor:publish --tag="config"
5554
```
5655

5756
This command will automatically publish the `WalletEnums.php` file into your application's `app/Enums` directory.
5857

58+
## Updating
59+
60+
If updating from version `<= 1.0.3`, new migration and config files have been added to support the new [Transaction Info Feature](#transaction-info)
61+
62+
Follow the [Installation](#installation) Steps 2 and 3 to update your migrations.
63+
5964
## Preparation
6065

6166
### Prepare User Model
6267

63-
To use this package you need to implement the `WalletOperations` into `User` model and utilize the `ManagesWallet` trait.
68+
To use this package you need to implement the `WalletOperations` into `User` model and utilize the `ManagesWallet` trait.
6469

6570
```php
6671

@@ -75,9 +80,10 @@ class User extends Authenticatable implements WalletOperations
7580

7681
### Prepare Wallets
7782

78-
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.
83+
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.
7984

8085
For example, consider the following wallet types defined in the Enum class (published in step 3 of installation):
86+
8187
```php
8288
namespace App\Enums;
8389

@@ -88,48 +94,84 @@ enum WalletEnums: string
8894
}
8995

9096
```
91-
**You have complete freedom to name your wallets as per your requirements and even add more wallet types to the Enum list.**
9297

98+
**You have complete freedom to name your wallets as per your requirements and even add more wallet types to the Enum list.**
9399

94100
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.
95101

96102
### Example:
103+
97104
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."
98105

99106
## Usage, APIs and Operations:
107+
100108
### Deposit
101109

110+
```php
111+
deposit(type: 'wallet_1', amount: 123.45, notes: null)
112+
```
113+
114+
Deposit funds into `wallet_1`
115+
102116
```php
103117
$user = auth()->user();
118+
$user->deposit('wallet_1', 123.45);
119+
```
104120

105-
$user->deposit('wallet_1', 123.45); // Deposit funds into 'wallet_1'
121+
Deposit funds into `wallet_2`
106122

107-
$user->deposit('wallet_2', 67.89); // Deposit funds into 'wallet_2'
123+
```php
124+
$user = auth()->user();
125+
$user->deposit('wallet_2', 67.89);
126+
```
108127

109-
// Or using provided facade
128+
Or using provided facade
110129

130+
```php
111131
use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket;
112132

133+
$user = auth()->user();
113134
LaravelPayPocket::deposit($user, 'wallet_1', 123.45);
114135

115136
```
137+
116138
Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`.
117139

140+
#### Transaction Info ([#8][i8])
141+
142+
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.
143+
144+
```php
145+
$user = auth()->user();
146+
$user->deposit('wallet_1', 67.89, 'You ordered pizza.');
147+
```
148+
118149
### Pay
150+
151+
```php
152+
pay(amount: 12.34, notes: null)
153+
```
154+
155+
Pay the value using the total combined balance available across all allowed wallets
156+
119157
```php
120-
// Pay the value using the total combined balance available across all wallets
158+
$user = auth()->user();
121159
$user->pay(12.34);
122-
123-
// Or using provided facade
160+
```
124161

162+
Or using provided facade
163+
164+
```php
125165
use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket;
126166

167+
$user = auth()->user();
127168
LaravelPayPocket::pay($user, 12.34);
128169
```
129170

130171
### Balance
131172

132-
- **Wallets**
173+
- **Wallets**
174+
133175
```php
134176
$user->walletBalance // Total combined balance available across all wallets
135177

@@ -138,7 +180,8 @@ $user->walletBalance // Total combined balance available across all wallets
138180
LaravelPayPocket::checkBalance($user);
139181
```
140182

141-
- **Particular Wallet**
183+
- **Particular Wallet**
184+
142185
```php
143186
$user->getWalletBalanceByType('wallet_1') // Balance available in wallet_1
144187
$user->getWalletBalanceByType('wallet_2') // Balance available in wallet_2
@@ -149,15 +192,15 @@ LaravelPayPocket::walletBalanceByType($user, 'wallet_1');
149192
```
150193

151194
### Exceptions
152-
Upon examining the `src/Exceptions` directory within the source code,
195+
196+
Upon examining the `src/Exceptions` directory within the source code,
153197
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.
154198

155199
### Log
156200

157201
A typical `wallets_logs` table.
158202
![Laravel Pay Pocket Log](https://github.com/HPWebdeveloper/laravel-pay-pocket/assets/16323354/a242d335-8bd2-4af1-aa38-4e95b8870941)
159203

160-
161204
## Testing
162205

163206
```bash
@@ -185,10 +228,12 @@ Please review [our security policy](../../security/policy) on how to report secu
185228

186229
## Credits
187230

188-
- [Hamed Panjeh](https://github.com/HPWebdeveloper)
189-
- [All Contributors](../../contributors)
190-
- Icon in the above image: pocket by Creative Mahira from [Noun Project](https://thenounproject.com/browse/icons/term/pocket/) (CC BY 3.0)
231+
- [Hamed Panjeh](https://github.com/HPWebdeveloper)
232+
- [All Contributors](../../contributors)
233+
- Icon in the above image: pocket by Creative Mahira from [Noun Project](https://thenounproject.com/browse/icons/term/pocket/) (CC BY 3.0)
191234

192235
## License
193236

194237
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
238+
239+
[i8]: Tag link (will be updated soon)

config/pay-pocket.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
<?php
22

33
// config for HPWebdeveloper/LaravelPayPocket
4-
return [
54

5+
/**
6+
* The 'log_reference_generator' should be a numeric array with three elements:
7+
* - The first element should be the fully qualified name of a class that contains static methods.
8+
* This includes the namespace of the class.
9+
* - The second element should be the name of a static method available in the specified class.
10+
* - The third element should be an array of optional parameters to pass to the static method.
11+
* For example, the default generator is configured as follows:
12+
* [\Illuminate\Support\Str::class, 'random', [12]], which uses the 'random' static method
13+
* from the \Illuminate\Support\Str class with 12 as a parameter.
14+
*/
15+
return [
16+
'log_reference_length' => 12,
17+
'log_reference_prefix' => null,
18+
'log_reference_generator' => null,
619
];
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('wallets_logs', function (Blueprint $table) {
15+
if (!Schema::hasColumn('wallets_logs', 'notes')) {
16+
$table->string('notes')->nullable()->after('status');
17+
}
18+
if (!Schema::hasColumn('wallets_logs', 'reference')) {
19+
$table->string('reference')->nullable()->after('ip');
20+
}
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::table('wallets_logs', function (Blueprint $table) {
30+
if (Schema::hasColumn('wallets_logs', 'notes')) {
31+
$table->dropColumn('notes');
32+
}
33+
if (Schema::hasColumn('wallets_logs', 'reference')) {
34+
$table->dropColumn('reference');
35+
}
36+
});
37+
}
38+
};

src/Interfaces/WalletOperations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function getWalletBalanceByType(string $walletType);
1010

1111
public function hasSufficientBalance($value): bool;
1212

13-
public function pay(int|float $orderValue);
13+
public function pay(int|float $orderValue, ?string $notes = null);
1414

15-
public function deposit(string $type, int|float $amount): bool;
15+
public function deposit(string $type, int|float $amount, ?string $notes = null): bool;
1616
}

src/LaravelPayPocketServiceProvider.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,27 @@ public function configurePackage(Package $package): void
1818
->name('laravel-pay-pocket')
1919
->hasConfigFile()
2020
->hasViews()
21-
->hasMigrations('create_wallets_logs_table', 'create_wallets_table');
21+
->hasMigrations(
22+
'create_wallets_logs_table',
23+
'create_wallets_table',
24+
'add_notes_and_reference_columns_to_wallets_logs_table'
25+
);
2226
}
2327

2428
public function bootingPackage()
2529
{
2630
$this->publishes([
2731
__DIR__.'/../Enums/' => app_path('Enums'),
2832
], 'pay-pocket-wallets');
33+
34+
$this->publishes([
35+
__DIR__.'/../config/pay-pocket.php' => config_path('pay-pocket.php'),
36+
], 'config');
37+
}
38+
39+
public function registeringPackage()
40+
{
41+
// Automatically apply the package configuration
42+
$this->mergeConfigFrom(__DIR__.'/../config/pay-pocket.php', 'pay-pocket');
2943
}
3044
}

src/Models/WalletsLog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WalletsLog extends Model
1616
use HasFactory;
1717

1818
protected $fillable = [
19-
'from', 'to', 'type', 'ip', 'value', 'wallet_name',
19+
'from', 'to', 'type', 'ip', 'value', 'wallet_name', 'notes', 'reference',
2020
];
2121

2222
public function loggable(): MorphTo

src/Services/PocketServices.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
class PocketServices
66
{
7-
public function deposit($user, $type, $amount)
7+
public function deposit($user, $type, $amount, $notes = null)
88
{
9-
return $user->deposit($type, $amount);
9+
return $user->deposit($type, $amount, $notes);
1010
}
1111

12-
public function pay($user, $orderValue)
12+
public function pay($user, $orderValue, $notes = null)
1313
{
14-
return $user->pay($orderValue);
14+
return $user->pay($orderValue, $notes);
1515
}
1616

1717
public function checkBalance($user)

0 commit comments

Comments
 (0)