Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [7.3.0]
### Added
- Credit Limits
- WalletCreatedEvent

### Updated
- Optimization of check for withdrawals;

## [7.2.0] - 2021-12-07
### Added
- Added balance update event
Expand Down Expand Up @@ -752,7 +760,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
- Exceptions: AmountInvalid, BalanceIsEmpty.
- Models: Transfer, Transaction.

[Unreleased]: https://github.com/bavix/laravel-wallet/compare/7.2.0...develop
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/7.3.0...develop
[7.3.0]: https://github.com/bavix/laravel-wallet/compare/7.2.0...7.3.0
[7.2.0]: https://github.com/bavix/laravel-wallet/compare/7.1.0...7.2.0
[7.1.0]: https://github.com/bavix/laravel-wallet/compare/7.0.0...7.1.0
[7.0.0]: https://github.com/bavix/laravel-wallet/compare/6.2.4...7.0.0
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@
"ext-json": "*"
},
"require-dev": {
"brianium/paratest": "^6.3",
"brianium/paratest": "^6.4",
"cknow/laravel-money": "^6.1",
"ergebnis/phpstan-rules": "^1.0",
"infection/infection": "~0.25",
"laravel/cashier": "^13.6",
"nunomaduro/collision": "^5.10",
"orchestra/testbench": "^6.23",
"phpstan/phpstan": "^1.1",
"phpstan/phpstan": "^1.2",
"phpunit/phpunit": "^9.5",
"psalm/plugin-laravel": "^1.5",
"rector/rector": "^0.12.3",
"rector/rector": "^0.12",
"symplify/easy-coding-standard": "^10.0",
"vimeo/psalm": "^4.12"
"vimeo/psalm": "^4.15"
},
"suggest": {
"bavix/laravel-wallet-swap": "Addition to the laravel-wallet library for quick setting of exchange rates",
Expand Down
6 changes: 6 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Bavix\Wallet\Internal\Assembler\TransferLazyDtoAssembler;
use Bavix\Wallet\Internal\Assembler\TransferQueryAssembler;
use Bavix\Wallet\Internal\Events\BalanceUpdatedEvent;
use Bavix\Wallet\Internal\Events\WalletCreatedEvent;
use Bavix\Wallet\Internal\Repository\TransactionRepository;
use Bavix\Wallet\Internal\Repository\TransferRepository;
use Bavix\Wallet\Internal\Repository\WalletRepository;
use Bavix\Wallet\Internal\Service\ClockService;
use Bavix\Wallet\Internal\Service\DatabaseService;
use Bavix\Wallet\Internal\Service\DispatcherService;
Expand Down Expand Up @@ -39,6 +41,7 @@
use Bavix\Wallet\Services\PurchaseService;
use Bavix\Wallet\Services\RegulatorService;
use Bavix\Wallet\Services\TaxService;
use Bavix\Wallet\Services\WalletService;

return [
/**
Expand Down Expand Up @@ -93,6 +96,7 @@
'prepare' => PrepareService::class,
'purchase' => PurchaseService::class,
'tax' => TaxService::class,
'wallet' => WalletService::class,
],

/**
Expand All @@ -101,6 +105,7 @@
'repositories' => [
'transaction' => TransactionRepository::class,
'transfer' => TransferRepository::class,
'wallet' => WalletRepository::class,
],

/**
Expand Down Expand Up @@ -129,6 +134,7 @@
*/
'events' => [
'balance_updated' => BalanceUpdatedEvent::class,
'wallet_created' => WalletCreatedEvent::class,
],

/**
Expand Down
3 changes: 3 additions & 0 deletions depfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,18 @@ ruleset:
- QueryInterface

RepositoryInterface:
- InternalException
- QueryInterface
- DtoInterface
- Model
Repository:
- RepositoryInterface
- TransformInterface
- InternalException
- ServiceInterface # json service only
- QueryInterface
- DtoInterface
- UIException
- Model

ServiceInterface:
Expand Down
3 changes: 3 additions & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
- [Confirm](confirm)
- [Exchange](exchange)
- [Withdraw taxing](taxing)
- [Credit Limits](credit-limits)

- Multi Wallets

- [New Wallet](new-wallet)
- [Transfer](wallet-transfer)
- [Transaction Filter](transaction-filter)

- Purchases

Expand All @@ -37,6 +39,7 @@
- Events

- [BalanceUpdatedEvent](balance-updated-event)
- [WalletCreatedEvent](wallet-created-event)
- [Event Customize](event-customize)

- Additions
Expand Down
36 changes: 36 additions & 0 deletions docs/credit-limits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Credit Limits

If you need the ability to have wallets have a credit limit, then this functionality is for you.

The functionality does nothing, it only allows you not to use "force" for most of the operations within the credit limit. You yourself write the logic for collecting interest, notifications on debts, etc.

By default, the credit limit is zero.

An example of working with a credit limit:
```php
/**
* @var \Bavix\Wallet\Interfaces\Customer $customer
* @var \Bavix\Wallet\Models\Wallet $wallet
* @var \Bavix\Wallet\Interfaces\Product $product
*/
$wallet = $customer->wallet; // get default wallet
$wallet->meta['credit'] = 10000; // credit limit
$wallet->save(); // update credit limit

$wallet->balanceInt; // 0
$product->getAmountProduct($customer); // 500

$wallet->pay($product); // success
$wallet->balanceInt; // -500
```

For multi-wallets when creating:
```php
/** @var \Bavix\Wallet\Traits\HasWallets $user */
$wallet = $user->createWallet([
'name' => 'My Wallet',
'meta' => ['credit' => 500],
]);
```

It worked!
48 changes: 48 additions & 0 deletions docs/transaction-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Transaction Filter

Often developers ask me about the `transactions` method.
Yes, this method displays ALL transactions for the wallet owner.
If you only need to filter one wallet at a time, now you can use the `walletTransactions` method.

```php
/** @var \Bavix\Wallet\Models\Wallet $wallet */

// Before version 7.3
$query = $wallet
->transactions()
->where('wallet_id', $wallet->getKey());

// 7.3+
$query = $wallet->walletTransactions();
```

Let's take a look at a livelier code example:
```php
$user->transactions()->count(); // 0

// default wallet
$user->deposit(100);
$user->wallet->deposit(200);
$user->wallet->withdraw(1);

// usd
$usd = $user->createWallet(['name' => 'USD']);
$usd->deposit(100);

// eur
$eur = $user->createWallet(['name' => 'EUR']);
$eur->deposit(100);

$user->transactions()->count(); // 5
$user->wallet->transactions()->count(); // 5
$usd->transactions()->count(); // 5
$eur->transactions()->count(); // 5
// the transactions method returns data relative to the owner of the wallet, for all transactions

$user->walletTransactions()->count(); // 3. we get the default wallet
$user->wallet->walletTransactions()->count(); // 3
$usd->walletTransactions()->count(); // 1
$eur->walletTransactions()->count(); // 1
```

It worked!
33 changes: 33 additions & 0 deletions docs/wallet-created-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Tracking the creation of wallets

The events are similar to the events for updating the balance, only for the creation of a wallet. A frequent case of transferring data via websockets to the front-end.

Version 7.3 introduces an interface to which you can subscribe.
This is done using standard Laravel methods.
More information in the [documentation](https://laravel.com/docs/8.x/events).

```php
use Bavix\Wallet\Internal\Events\WalletCreatedEventInterface;

protected $listen = [
WalletCreatedEventInterface::class => [
MyWalletCreatedListener::class,
],
];
```

And then we create a listener.

```php
use Bavix\Wallet\Internal\Events\WalletCreatedEventInterface;

class MyWalletCreatedListener
{
public function handle(WalletCreatedEventInterface $event): void
{
// And then the implementation...
}
}
```

It worked!
3 changes: 3 additions & 0 deletions src/Interfaces/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\RecordsNotFoundException;

Expand Down Expand Up @@ -91,6 +92,8 @@ public function getBalanceAttribute();

public function getBalanceIntAttribute(): int;

public function walletTransactions(): HasMany;

public function transactions(): MorphMany;

public function transfers(): MorphMany;
Expand Down
31 changes: 31 additions & 0 deletions src/Internal/Assembler/WalletCreatedEventAssembler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Assembler;

use Bavix\Wallet\Internal\Events\WalletCreatedEvent;
use Bavix\Wallet\Internal\Events\WalletCreatedEventInterface;
use Bavix\Wallet\Internal\Service\ClockServiceInterface;
use Bavix\Wallet\Models\Wallet;

final class WalletCreatedEventAssembler implements WalletCreatedEventAssemblerInterface
{
private ClockServiceInterface $clockService;

public function __construct(ClockServiceInterface $clockService)
{
$this->clockService = $clockService;
}

public function create(Wallet $wallet): WalletCreatedEventInterface
{
return new WalletCreatedEvent(
$wallet->holder_type,
$wallet->holder_id,
$wallet->uuid,
(int) $wallet->getKey(),
$this->clockService->now()
);
}
}
13 changes: 13 additions & 0 deletions src/Internal/Assembler/WalletCreatedEventAssemblerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Assembler;

use Bavix\Wallet\Internal\Events\WalletCreatedEventInterface;
use Bavix\Wallet\Models\Wallet;

interface WalletCreatedEventAssemblerInterface
{
public function create(Wallet $wallet): WalletCreatedEventInterface;
}
55 changes: 55 additions & 0 deletions src/Internal/Events/WalletCreatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Events;

use DateTimeImmutable;

final class WalletCreatedEvent implements WalletCreatedEventInterface
{
private string $holderType;
private int $holderId;
private string $walletUuid;
private int $walletId;
private DateTimeImmutable $createdAt;

public function __construct(
string $holderType,
int $holderId,
string $walletUuid,
int $walletId,
DateTimeImmutable $createdAt
) {
$this->holderType = $holderType;
$this->holderId = $holderId;
$this->walletUuid = $walletUuid;
$this->walletId = $walletId;
$this->createdAt = $createdAt;
}

public function getHolderType(): string
{
return $this->holderType;
}

public function getHolderId(): int
{
return $this->holderId;
}

public function getWalletUuid(): string
{
return $this->walletUuid;
}

public function getWalletId(): int
{
return $this->walletId;
}

public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}
}
20 changes: 20 additions & 0 deletions src/Internal/Events/WalletCreatedEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Events;

use DateTimeImmutable;

interface WalletCreatedEventInterface extends EventInterface
{
public function getHolderType(): string;

public function getHolderId(): int;

public function getWalletId(): int;

public function getWalletUuid(): string;

public function getCreatedAt(): DateTimeImmutable;
}
Loading