Skip to content

Commit 0c4a3fc

Browse files
committed
Documentation update and minor fixes
1 parent a6dfd91 commit 0c4a3fc

File tree

9 files changed

+188
-5
lines changed

9 files changed

+188
-5
lines changed

docs/cart.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Item extends Model implements ProductInterface
3434

3535
public function getAmountProduct(Customer $customer)
3636
{
37-
return 100;
37+
return round($this->price * 100);
3838
}
3939

4040
public function getMetaProduct(): ?array
@@ -70,7 +70,7 @@ class Item extends Model implements ProductLimitedInterface
7070

7171
public function getAmountProduct(Customer $customer)
7272
{
73-
return 100;
73+
return round($this->price * 100);
7474
}
7575

7676
public function getMetaProduct(): ?array
@@ -112,14 +112,19 @@ $products = Item::query()
112112

113113
$cart = app(Cart::class);
114114
foreach ($products as $product) {
115-
// add product's
116-
$cart->addItem($product, $list[$product->slug]);
115+
$cart = $cart->withItem($product, quantity: $list[$product->slug]);
117116
}
118117

119-
$user->deposit($cart->getTotal());
118+
$cartTotal = $cart->getTotal($user); // 15127
119+
$user->deposit($cartTotal);
120120
$user->balanceInt; // 15127
121121
$user->balanceFloat; // 151.27
122122

123+
$cart = $cart->withItem(current($products), pricePerItem: 500); // 15127+500
124+
$user->deposit(500);
125+
$user->balanceInt; // 15627
126+
$user->balanceFloat; // 156.27
127+
123128
(bool)$user->payCart($cart); // true
124129
$user->balanceFloat; // 0
125130
```

docs/upgrade-guide.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,32 @@ $cart = app(\Bavix\Wallet\Objects\Cart::class)
224224

225225
$cart = $cart->withItem($product);
226226
```
227+
228+
## 8.1.x+ → 9.0.x
229+
230+
> The logic of storing transfers between accounts has changed.
231+
> Previously, money could be credited to the user directly, but starting from version nine, all transactions go strictly between wallets.
232+
> Thanks to this approach, finally, there will be full-fledged work with uuid identifiers in the project.
233+
234+
To migrate to the correct structure, you need to run the command:
235+
```
236+
artisan bx:transfer:fix
237+
```
238+
239+
If the command fails, then the command must be restarted.
240+
Continue until the command starts executing immediately (no bad entries left).
241+
242+
---
243+
244+
The product has been divided into two interfaces:
245+
- `ProductLimitedInterface`. Needed to create limited goods;
246+
- `ProductInterface`. Needed for an infinite number of products;
247+
248+
The old Product interface should be replaced with one of these.
249+
250+
Replace `Bavix\Wallet\Interfaces\Product` to `Bavix\Wallet\Interfaces\ProductLimitedInterface`.
251+
252+
253+
---
254+
255+

docs/wallet-transfer.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,37 @@ $firstWallet->balance; // -400
6969
$lastWallet->balance; // 500
7070
```
7171

72+
It worked!
73+
74+
## Change the meta and confirmation.
75+
76+
Check the user's balance.
77+
78+
```php
79+
$firstWallet->balanceInt; // 1_000
80+
$secondWallet->balanceInt; // 0
81+
```
82+
83+
We will execute the transfer, but without confirmation of the withdrawal of funds.
84+
85+
```php
86+
use Bavix\Wallet\External\Dto\Extra;
87+
use Bavix\Wallet\External\Dto\Option;
88+
89+
/** @var $firstWallet \Bavix\Wallet\Interfaces\Wallet */
90+
$transfer = $firstWallet->transfer($secondWallet, 500, new Extra(
91+
deposit: ['message' => 'Hello, secondWallet!'],
92+
withdraw: new Option(meta: ['something' => 'anything'], confirmed: false)
93+
));
94+
95+
$transfer->withdraw->meta; // ['something' => 'anything']
96+
$transfer->withdraw->confirmed; // false
97+
98+
$transfer->deposit->meta; // ['message' => 'Hello, secondWallet!']
99+
$transfer->deposit->confirmed; // true
100+
101+
$firstWallet->balanceInt; // 1_000
102+
$secondWallet->balanceInt; // 500
103+
```
104+
72105
It worked!

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ parameters:
1111
# laravel
1212
- Illuminate\Support\ServiceProvider
1313
- Illuminate\Database\Eloquent\Model
14+
- Illuminate\Console\Command
1415

1516
# php exceptions
1617
- LogicException
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Commands;
6+
7+
use Bavix\Wallet\Models\Transfer;
8+
use Bavix\Wallet\Models\Wallet;
9+
use Bavix\Wallet\Services\CastServiceInterface;
10+
use Illuminate\Console\Command;
11+
12+
final class TransferFixCommand extends Command
13+
{
14+
protected $signature = 'bx:transfer:fix';
15+
protected $description = 'Brings transfers to the correct form.';
16+
17+
public function handle(Wallet $wallet, Transfer $transfer, CastServiceInterface $castService): void
18+
{
19+
$query = $transfer::with(['from', 'to'])
20+
->orWhere('from_type', '<>', $wallet->getMorphClass())
21+
->orWhere('to_type', '<>', $wallet->getMorphClass())
22+
;
23+
24+
$query->each(fn (Transfer $object) => $this->fix($castService, $wallet, $object));
25+
}
26+
27+
private function fix(CastServiceInterface $castService, Wallet $wallet, Transfer $transfer): void
28+
{
29+
if ($transfer->from_type !== $wallet->getMorphClass()) {
30+
$fromWallet = $castService->getWallet($transfer->from);
31+
$transfer->from_type = $fromWallet->getMorphClass();
32+
$transfer->from_id = $fromWallet->getKey();
33+
}
34+
35+
if ($transfer->to_type !== $wallet->getMorphClass()) {
36+
$toWallet = $castService->getWallet($transfer->to);
37+
$transfer->to_type = $toWallet->getMorphClass();
38+
$transfer->to_id = $toWallet->getKey();
39+
}
40+
41+
if ($transfer->isDirty()) {
42+
$transfer->save();
43+
}
44+
}
45+
}

src/Models/Transfer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
* @property string $discount
1818
* @property int $deposit_id
1919
* @property int $withdraw_id
20+
* @property Wallet $from
2021
* @property string $from_type
2122
* @property int $from_id
23+
* @property Wallet $to
2224
* @property string $to_type
2325
* @property int $to_id
2426
* @property string $uuid

src/Objects/Cart.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public function withMeta(array $meta): self
4646
return $self;
4747
}
4848

49+
/**
50+
* @param positive-int $quantity
51+
*/
4952
public function withItem(ProductInterface $product, int $quantity = 1, int|string|null $pricePerItem = null): self
5053
{
5154
$self = clone $this;

src/WalletServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Bavix\Wallet;
66

7+
use Bavix\Wallet\Commands\TransferFixCommand;
78
use Bavix\Wallet\Internal\Assembler\AvailabilityDtoAssembler;
89
use Bavix\Wallet\Internal\Assembler\AvailabilityDtoAssemblerInterface;
910
use Bavix\Wallet\Internal\Assembler\BalanceUpdatedEventAssembler;
@@ -133,6 +134,7 @@ public function boot(): void
133134
public function register(): void
134135
{
135136
$this->mergeConfigFrom(dirname(__DIR__).'/config/config.php', 'wallet');
137+
$this->commands([TransferFixCommand::class]);
136138

137139
$configure = config('wallet', []);
138140

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Test\Units\Upgrade;
6+
7+
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
8+
use Bavix\Wallet\Test\Infra\Models\Buyer;
9+
use Bavix\Wallet\Test\Infra\TestCase;
10+
11+
/**
12+
* @internal
13+
*/
14+
final class TransferFixTest extends TestCase
15+
{
16+
public function testTransferFixCommand(): void
17+
{
18+
/** @var Buyer $buyer1 */
19+
/** @var Buyer $buyer2 */
20+
[$buyer1, $buyer2] = BuyerFactory::times(2)->create();
21+
$transfer = $buyer1->forceTransfer($buyer2, 1_000_000);
22+
23+
self::assertSame($buyer1->wallet->getMorphClass(), $transfer->from->getMorphClass());
24+
self::assertSame($buyer1->wallet->getKey(), $transfer->from->getKey());
25+
26+
self::assertSame($buyer2->wallet->getMorphClass(), $transfer->to->getMorphClass());
27+
self::assertSame($buyer2->wallet->getKey(), $transfer->to->getKey());
28+
29+
$buyer1->wallet->transfers()
30+
->update([
31+
'from_type' => $buyer1->getMorphClass(),
32+
'from_id' => $buyer1->getKey(),
33+
34+
'to_type' => $buyer2->getMorphClass(),
35+
'to_id' => $buyer2->getKey(),
36+
])
37+
;
38+
39+
$transfer->refresh();
40+
$transfer->from->refresh();
41+
$transfer->to->refresh();
42+
43+
self::assertSame($buyer1->getMorphClass(), $transfer->from->getMorphClass());
44+
self::assertSame($buyer1->getKey(), $transfer->from->getKey());
45+
46+
self::assertSame($buyer2->getMorphClass(), $transfer->to->getMorphClass());
47+
self::assertSame($buyer2->getKey(), $transfer->to->getKey());
48+
49+
$this->artisan('bx:transfer:fix')
50+
->assertSuccessful()
51+
;
52+
53+
$transfer->refresh();
54+
$transfer->from->refresh();
55+
$transfer->to->refresh();
56+
57+
self::assertSame($buyer1->wallet->getMorphClass(), $transfer->from->getMorphClass());
58+
self::assertSame($buyer1->wallet->getKey(), $transfer->from->getKey());
59+
60+
self::assertSame($buyer2->wallet->getMorphClass(), $transfer->to->getMorphClass());
61+
self::assertSame($buyer2->wallet->getKey(), $transfer->to->getKey());
62+
}
63+
}

0 commit comments

Comments
 (0)