mannikj / laravel-wallet Goto Github PK
View Code? Open in Web Editor NEWAn easy-to-use virtual wallet implementation for Laravel
License: MIT License
An easy-to-use virtual wallet implementation for Laravel
License: MIT License
It makes no real sense to pollute models utilizing the HasWallet
trait with functions that actually belong to the Wallet
model itself out of the box.
Developers might add proxy functions for more convenient use themselves if they want to.
The HasWallet trait should only do what its name implies: Provide the necessary wallet relations.
e.g. :
$user->wallet->deposit(-10)
will update the balance but will insert Transaction with Soft delete
on the observer need to add check if the transaction is canceled before update balance
Hi
On TransactionObserver you have this code :
public function created($transaction)
{
$transaction->wallet->balance += $transaction->amount;
$transaction->wallet->save();
}
I suggest to change any balance update to be like this :
public function created($transaction)
{
$transaction->wallet->increment('balance',$transaction->amount);
}
This will make Atomic update on balance .
I tried to install the package on Laravel 11, but it gives this error when installing:
composer require mannikj/laravel-wallet
./composer.json has been updated
Running composer update mannikj/laravel-wallet
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.
Problem 1
- mannikj/laravel-wallet[v1.0.0, ..., v1.0.8, 2.0.0, ..., v2.1.1] require php ^7.0 -> your php version (8.2.0) does not satisfy that requirement.
- mannikj/laravel-wallet v3.0.0 requires php ^7.2 -> your php version (8.2.0) does not satisfy that requirement.
- mannikj/laravel-wallet[v4.0.0, v5.0.0] require php ^7.2.5 -> your php version (8.2.0) does not satisfy that requirement.
- mannikj/laravel-wallet[v6.0.0, ..., v6.2.0] require illuminate/support ^7.0|^8.0 -> found illuminate/support[v7.0.0, ..., v7.30.6, v8.0.0, ..., v8.83.27] but these were not loaded, likely because it conflicts with another require.
- mannikj/laravel-wallet v7.0.0 requires illuminate/support ^8.0 -> found illuminate/support[v8.0.0, ..., v8.83.27] but these were not loaded, likely because it conflicts with another require.
- mannikj/laravel-wallet v9.0.0 requires illuminate/support ^9.0 -> found illuminate/support[v9.0.0, ..., v9.52.16] but these were not loaded, likely because it conflicts with another require.
- mannikj/laravel-wallet[v10.0.0, ..., v10.1.0] require illuminate/support ^10.0 -> found illuminate/support[v10.0.0, ..., v10.48.3] but these were not loaded, likely because it conflicts with another require.
- Root composer.json requires mannikj/laravel-wallet * -> satisfiable by mannikj/laravel-wallet[v1.0.0, ..., v1.0.8, 2.0.0, v2.0.1, v2.1.0, v2.1.1, v3.0.0, v4.0.0, v5.0.0, v6.0.0, v6.2.0, v7.0.0, v9.0.0, v10.0.0, v10.1.0].
You can also try re-running composer require with an explicit version constraint, e.g. "composer require mannikj/laravel-wallet:*" to figure out if any version is installable, or "composer require mannikj/laravel-wallet:^2.1" if you know which you need.
Makes calculating cumulative sums on database level easier
Hello,
I came across your library by chance which is really nice :) . I was setting up the same thing but in your code I don't see any pessimistic or optimistic locks in the users' transactions. Or did I look at it wrong?
Thank's for response
This can be reproduced without actual transactions by just using hardcoded strings instead of actual calculation. Example below:
public function actualBalance(bool $save = false) {
$undefined = 0; /*$this->transactions()
->whereNotIn('type', \Wallet::biasedTransactionTypes())
->sum('amount');*/
$credits = "318.40"; /*$this->transactions()
->whereIn('type', \Wallet::addingTransactionTypes())
->sum(\DB::raw('abs(amount)'));*/
$debits = "465.60"; /*$this->transactions()
->whereIn('type', \Wallet::subtractingTransactionTypes())
->sum(\DB::raw('abs(amount)'));*/
$balance = $undefined + $credits - $debits;
dump(number_format($balance, 50));
if ($save) {
$this->balance = $balance;
$this->save();
}
return $balance;
}
Expected result from dump would be -147.2 but due to php floating point handling, is actually -147.20000000000004547473508864641189575195312500000000. This causes model's isDirty method to always return true when comparing to the original value, whether it already matches balance or not.
One way to fix this is to use BCMath extension and format calculation expression as
$balance = bcsub(bcadd((string)$undefined, $credits, 2), $debits, 2);
Note that this solution works for wallet with decimals. There are probably other ways to do rounding as well if BCMath dependency is not preferred.
hi
i am appreciate for useful package.
in walletTransactions() part return all Transactions i need to return just Transactions for that model id.
thanks a lot.
laravel/framework replaces illuminate/support and thus cannot coexist with it.
Config is currently used as
config('auto_recalculate_balance');
whereas it should be used, like other config variables, using wallet namespace.
config('wallet.auto_recalculate_balance');
how to get each user transition list
Hi, thanks for building this package!
I have a question, in the repository description, it says "you can equip your eloquent models with one or multiple digital wallets", and that's just what i'm trying to do, but i don't know how.
I would like to add to my project two wallets for the users, like:
I would appreciate if you tell me how can I accomplish that. Thank you very much! ^_^
A class import is missing
You have a missing class import. Try importing this class: MannikJ\Laravel\Wallet\Models\Wallet.
In this file:
vendor/mannikj/laravel-wallet/src/Models/Transaction.php: line => 119
Can you Help?
Hi guys,
So I've been using depsimon's version since laravel 5.7 with great success and now, upgrading to Laravel 8 I seem destined to change the dependency to yours. So I was wondering if you could supply the upgrade instructions. I see there's some differences i.r.t. the migrations, perhaps among other things.
What do I need to do?
Thanks in advance!
Error message: Column not found: 1054 Unknown column 'wallets.owner_id'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.