Skip to content

Commit

Permalink
Merge pull request #10 from 3m1n3nc3/features
Browse files Browse the repository at this point in the history
Add support for transaction note
  • Loading branch information
HPWebdeveloper authored Jan 11, 2024
2 parents ab404be + f0dcea0 commit 445bb28
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 83 deletions.
113 changes: 79 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,65 @@
[![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)
[![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)


**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

**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`
* **PHP Version**: 8.1+
* **Laravel Version**: `10.x`
* **[Composer](https://getcomposer.org/):** `composer require hpwebdeveloper/laravel-pay-pocket`

- **Author**: Hamed Panjeh
- **Vendor**: hpwebdeveloper
- **Package**: laravel-pay-pocket
- **Alias name**: Laravel PPP (Laravel Pay Pocket Package)
- **Version**: `1.x`
- **PHP Version**: 8.1+
- **Laravel Version**: `10.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 | |
| x.x | | | | | |

## Installation:

- **Step 1:** You can install the package via composer:
- **Step 1:** You can install the package via composer:

```bash
composer require hpwebdeveloper/laravel-pay-pocket
```

- **Step 2:** Publish and run the migrations with:
- **Step 2:** Publish and run the migrations with:

```bash
php artisan vendor:publish --tag="pay-pocket-migrations"
php artisan migrate
```

You have successfully added two dedicated database tables, `wallets` and `wallets_logs`, without making any modifications to the `users` table.

- **Step 3:** Publish the wallet types using
- **Step 3:** Publish the wallet types using

```bash
php artisan vendor:publish --tag="pay-pocket-wallets"
php artisan vendor:publish --tag="config"
```

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

## Updating

If updating from version `<= 1.0.3`, new migration and config files have been added to support the new [Transaction Info Feature](#transaction-info)

Follow the [Installation](#installation) Steps 2 and 3 to update your migrations.

## Preparation

### Prepare User Model

To use this package you need to implement the `WalletOperations` into `User` model and utilize the `ManagesWallet` trait.
To use this package you need to implement the `WalletOperations` into `User` model and utilize the `ManagesWallet` trait.

```php

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

### Prepare Wallets

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.
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 the Enum class (published in step 3 of installation):

```php
namespace App\Enums;

Expand All @@ -88,48 +94,84 @@ enum WalletEnums: string
}

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

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

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.

### Example:

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."

## Usage, APIs and Operations:

### Deposit

```php
deposit(type: 'wallet_1', amount: 123.45, notes: null)
```

Deposit funds into `wallet_1`

```php
$user = auth()->user();
$user->deposit('wallet_1', 123.45);
```

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

$user->deposit('wallet_2', 67.89); // Deposit funds into 'wallet_2'
```php
$user = auth()->user();
$user->deposit('wallet_2', 67.89);
```

// Or using provided facade
Or using provided facade

```php
use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket;

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

```

Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`.

#### Transaction Info ([#8][i8])

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.

```php
$user = auth()->user();
$user->deposit('wallet_1', 67.89, 'You ordered pizza.');
```

### Pay

```php
pay(amount: 12.34, notes: null)
```

Pay the value using the total combined balance available across all allowed wallets

```php
// Pay the value using the total combined balance available across all wallets
$user = auth()->user();
$user->pay(12.34);

// Or using provided facade
```

Or using provided facade

```php
use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket;

$user = auth()->user();
LaravelPayPocket::pay($user, 12.34);
```

### Balance

- **Wallets**
- **Wallets**

```php
$user->walletBalance // Total combined balance available across all wallets

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

- **Particular Wallet**
- **Particular Wallet**

```php
$user->getWalletBalanceByType('wallet_1') // Balance available in wallet_1
$user->getWalletBalanceByType('wallet_2') // Balance available in wallet_2
Expand All @@ -149,15 +192,15 @@ LaravelPayPocket::walletBalanceByType($user, 'wallet_1');
```

### Exceptions
Upon examining the `src/Exceptions` directory within the source code,

Upon examining the `src/Exceptions` directory within the source code,
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.

### Log

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


## Testing

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

## Credits

- [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)
- [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

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[i8]: Tag link (will be updated soon)
15 changes: 14 additions & 1 deletion config/pay-pocket.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
<?php

// config for HPWebdeveloper/LaravelPayPocket
return [

/**
* The 'log_reference_generator' should be a numeric array with three elements:
* - The first element should be the fully qualified name of a class that contains static methods.
* This includes the namespace of the class.
* - The second element should be the name of a static method available in the specified class.
* - The third element should be an array of optional parameters to pass to the static method.
* For example, the default generator is configured as follows:
* [\Illuminate\Support\Str::class, 'random', [12]], which uses the 'random' static method
* from the \Illuminate\Support\Str class with 12 as a parameter.
*/
return [
'log_reference_length' => 12,
'log_reference_prefix' => null,
'log_reference_generator' => null,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('wallets_logs', function (Blueprint $table) {
if (!Schema::hasColumn('wallets_logs', 'notes')) {
$table->string('notes')->nullable()->after('status');
}
if (!Schema::hasColumn('wallets_logs', 'reference')) {
$table->string('reference')->nullable()->after('ip');
}
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('wallets_logs', function (Blueprint $table) {
if (Schema::hasColumn('wallets_logs', 'notes')) {
$table->dropColumn('notes');
}
if (Schema::hasColumn('wallets_logs', 'reference')) {
$table->dropColumn('reference');
}
});
}
};
4 changes: 2 additions & 2 deletions src/Interfaces/WalletOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function getWalletBalanceByType(string $walletType);

public function hasSufficientBalance($value): bool;

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

public function deposit(string $type, int|float $amount): bool;
public function deposit(string $type, int|float $amount, ?string $notes = null): bool;
}
16 changes: 15 additions & 1 deletion src/LaravelPayPocketServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@ public function configurePackage(Package $package): void
->name('laravel-pay-pocket')
->hasConfigFile()
->hasViews()
->hasMigrations('create_wallets_logs_table', 'create_wallets_table');
->hasMigrations(
'create_wallets_logs_table',
'create_wallets_table',
'add_notes_and_reference_columns_to_wallets_logs_table'
);
}

public function bootingPackage()
{
$this->publishes([
__DIR__.'/../Enums/' => app_path('Enums'),
], 'pay-pocket-wallets');

$this->publishes([
__DIR__.'/../config/pay-pocket.php' => config_path('pay-pocket.php'),
], 'config');
}

public function registeringPackage()
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/pay-pocket.php', 'pay-pocket');
}
}
2 changes: 1 addition & 1 deletion src/Models/WalletsLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WalletsLog extends Model
use HasFactory;

protected $fillable = [
'from', 'to', 'type', 'ip', 'value', 'wallet_name',
'from', 'to', 'type', 'ip', 'value', 'wallet_name', 'notes', 'reference',
];

public function loggable(): MorphTo
Expand Down
8 changes: 4 additions & 4 deletions src/Services/PocketServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

class PocketServices
{
public function deposit($user, $type, $amount)
public function deposit($user, $type, $amount, $notes = null)
{
return $user->deposit($type, $amount);
return $user->deposit($type, $amount, $notes);
}

public function pay($user, $orderValue)
public function pay($user, $orderValue, $notes = null)
{
return $user->pay($orderValue);
return $user->pay($orderValue, $notes);
}

public function checkBalance($user)
Expand Down
Loading

0 comments on commit 445bb28

Please sign in to comment.