Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Wallet Laravel Package

zotel/laravel-wallet

View on GitHub
Deep Wiki
Context7

Atomic Service

Working with atomic wallet operations.

Before you start working with atomicity, you need to study the "Race Condition" section and configure the lock.

Sometimes it is necessary to apply actions to the user and the wallet atomically. For example, you want to raise an ad in the search and withdraw money from your wallet. You need an Atomic Service Interface.

use Zotel\Wallet\Services\AtomicServiceInterface;

app(AtomicServiceInterface::class)->block($wallet, function () use ($wallet, $entity) {
    $entity->increaseSales(); // update entity set sort_at=NOW() where id=123;
    $wallet->withdraw(100);
});

What's going on here? We block the wallet and raise the ad in the transaction (yes, atomic immediately starts the transaction - this is the main difference from LockServiceInterface). We raise the ad and deduct the amount from the wallet. If there are not enough funds to raise the ad, the error will complete the atomic operation and the transaction will roll back, and the lock on the wallet will be removed.

There is also an opportunity to block a lot of wallets. The operation is expensive, it generates N requests to the lock service. Maybe I'll optimize it in the future, but that's not for sure.


For example, we need to debit from two wallets at the same time. Then let's use the "blocks" method.

use Zotel\Wallet\Services\AtomicServiceInterface;

app(AtomicServiceInterface::class)->blocks([$wallet1, $wallet2], function () use ($wallet1, $wallet2) {
    $wallet1->withdraw(100);
    $wallet2->withdraw(100);
});

In this case, we blocked both wallets and started the process of debiting funds. Debiting from both wallets will be considered a successful operation. If there are not enough funds on some wallet, the operation is canceled.

It's simple!

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle