dinhdjj/laravel-transaction

This package is abandoned and no longer maintained. No replacement package was suggested.

Give laravel model ability transfer/receive balance

v1.0.0 2022-05-08 04:38 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Installation

You can install the package via composer:

composer require dinhdjj/laravel-transaction

You can publish and run the migrations with:

php artisan vendor:publish --tag="transaction-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="transaction-config"

This is the contents of the published config file:

return [
    'table' => env('TRANSACTION_TABLE', 'transactions'),
];

Optionally, you can publish the views using

php artisan vendor:publish --tag="transaction-views"

Usage

In your model you want give it ability transfer/receive balance Just uses Balancable trait implements Balancable interface

namespace App\Models\User;

use Dinhdjj\Transaction\Interfaces\Balancable as InterfacesBalancable;
use Dinhdjj\Transaction\Models\Transaction;
use Dinhdjj\Transaction\Traits\Balancable;

class User extends Model implements InterfaceBalancable
{
    use Balancable;

    protected function onReceiveBalance(Transaction $transaction): void
    {
    }

    protected function onTransferBalance(Transaction $transaction): void
    {
    }
}

To transfer/receiver

    $user1 = User::find(1);
    $user2 = User::find(2);

    // If you don't check don't worry it will throws exception
    if($user1->canTransferBalance(200), $user2->canReceiveBalance(200))
        $transaction = $user1->transferBalance($user2, 200, 'message');

    $user1->balance // get current balance
    $user1->transferredBalance 
    $user1->receivedBalance
    
    // get all transferred transactions
    // It used standard morph many relationship
    $user1->transferredTransactions 
    $user1->receivedTransactions()->get()

    // Other
    $user1->receiveBalanceFromAnonymous(...);
    // bypass the checking
    $user1->forceReceiveBalanceFromAnonymous(...);
    $user1->transferBalanceToAnonymous(...);
    $user1->forceTransferBalanceToAnonymous(...);

If you want use a part, BalanceReceivable, BalanceTransferable, HasTransferredTransactions, HasReceivedTransactions both traits and interfaces will help you

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.