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

Exchange

Everyone’s tasks are different and with the help of this functionality you can add exchange rates to your wallets.

The wallet currency is set via meta. Example:

$user->createWallet([
    'name' => 'My USD Wallet',
    'meta' => ['currency' => 'USD'],
]);

Service for working with currencies you need to write yourself or use library.

Service for working with currency

We will write a simple service. We will take the data from the array, and not from the database.

use Zotel\Wallet\Internal\Service\MathServiceInterface;
use Zotel\Wallet\Services\ExchangeServiceInterface;

class MyExchangeService implements ExchangeServiceInterface
{
    private array $rates = [
        'USD' => [
            'RUB' => 67.61,
        ],
    ];

    private MathServiceInterface $mathService;

    public function __construct(MathServiceInterface $mathService)
    {
        $this->mathService = $mathService;

        foreach ($this->rates as $from => $rates) {
            foreach ($rates as $to => $rate) {
                if (empty($this->rates[$to][$from])) {
                    $this->rates[$to][$from] = $this->mathService->div(1, $rate);
                }
            }
        }
    }

    /** [@param](https://github.com/param) float|int|string $amount */
    public function convertTo(string $fromCurrency, string $toCurrency, $amount): string
    {
        return $this->mathService->mul($amount, $this->rates[$fromCurrency][$toCurrency] ?? 1);
    }
}

Service Registration

The service you wrote must be registered, this is done in the file config/wallet.php.

return [
    // ...
    'services' => [
        'exchange' => MyExchangeService::class,
        // ...
    ],
    // ...
];

Exchange process

Create two wallets.

$usd = $user->createWallet([
    'name' => 'My Dollars',
    'meta' => ['currency' => 'USD'],
]);

$rub = $user->createWallet([
    'name' => 'My Ruble',
    'meta' => ['currency' => 'RUB'],
]);

We replenish the ruble wallet with 100 rubles.

$rub->deposit(10000);

We will exchange rubles into dollars.

$transfer = $rub->exchange($usd, 10000);
$rub->balance; // 0
$usd->balance; // 147, это $1.47

Unfortunately, the world is not perfect. You will not get back your 100 rubles.

$transfer = $usd->exchange($rub, $usd->balance);
$usd->balance; // 0
$rub->balance; // 9938

Due to conversion and mathematical rounding, you lost 62 kopecks. You have 99 rubles 38 kopecks left.


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