Skip to content

Commit c4cad06

Browse files
committed
bug fix
1 parent 46e0d2a commit c4cad06

File tree

9 files changed

+188
-21
lines changed

9 files changed

+188
-21
lines changed

docs/_sidebar.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,3 @@
3434
- [Rate Service](rate)
3535
- [Create Wallet](rate-wallet)
3636
- [Taxing](rate-taxing)
37-
-

docs/confirm.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
## User Model
2+
3+
Add the `CanPay` trait and `Customer` interface to your User model.
4+
5+
```php
6+
use Bavix\Wallet\Traits\CanPay;
7+
use Bavix\Wallet\Interfaces\Customer;
8+
9+
class User extends Model implements Customer
10+
{
11+
use CanPay;
12+
}
13+
```
14+
15+
## Item Model
16+
17+
Add the `HasWallet` trait and `Product` interface to Item model.
18+
19+
```php
20+
use Bavix\Wallet\Traits\HasWallet;
21+
use Bavix\Wallet\Interfaces\Product;
22+
use Bavix\Wallet\Interfaces\Customer;
23+
24+
class Item extends Model implements Product
25+
{
26+
use HasWallet;
27+
28+
public function canBuy(Customer $customer, int $quantity = 1, bool $force = null): bool
29+
{
30+
/**
31+
* If the service can be purchased once, then
32+
* return !$customer->paid($this);
33+
*/
34+
return true;
35+
}
36+
37+
public function getAmountProduct(): int
38+
{
39+
return 100;
40+
}
41+
42+
public function getMetaProduct(): ?array
43+
{
44+
return [
45+
'title' => $this->title,
46+
'description' => 'Purchase of Product #' . $this->id,
47+
'price' => $this->getAmountProduct(),
48+
];
49+
}
50+
51+
public function getUniqueId(): string
52+
{
53+
return (string)$this->getKey();
54+
}
55+
}
56+
```
57+
58+
## Make a refund
59+
60+
Find the user and check the balance.
61+
62+
```php
63+
$user = User::first();
64+
$user->balance; // int(0)
65+
```
66+
67+
Find the goods and check the balance.
68+
69+
```php
70+
$item = Item::first();
71+
$item->balance; // int(100)
72+
```
73+
74+
Return of funds!
75+
76+
```php
77+
(bool)$user->paid($item); // bool(true)
78+
(bool)$user->refund($item); // bool(true)
79+
$item->balance; // int(0)
80+
$user->balance; // int(100)
81+
```
82+
83+
It worked!

docs/ru/wallet-transfer.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ $first->getKey() !== $last->getKey(); // true
3838
```php
3939
$name = 'New Wallet';
4040
$firstWallet = $first->createWallet(compact('name'));
41-
$secondWallet = $second->createWallet(compact('name'));
41+
$lastWallet = $last->createWallet(compact('name'));
4242

4343
$firstWallet->deposit(100);
4444
$firstWallet->balance; // int(100)
45-
$secondWallet->balance; // int(0)
45+
$lastWallet->balance; // int(0)
4646
```
4747

4848
Выполним перевод от первого второму.
4949

5050
```php
51-
$firstWallet->transfer($secondWallet, 5);
51+
$firstWallet->transfer($lastWallet, 5);
5252
$firstWallet->balance; // int(95)
53-
$secondWallet->balance; // int(5)
53+
$lastWallet->balance; // int(5)
5454
```
5555

5656
## Принудительный перевод
@@ -65,9 +65,9 @@ $lastWallet->balance; // int(0)
6565
Выполним перевод от первого второму.
6666

6767
```php
68-
$firstWallet->forceTransfer($secondWallet, 500);
68+
$firstWallet->forceTransfer($lastWallet, 500);
6969
$firstWallet->balance; // int(-400)
70-
$secondWallet->balance; // int(500)
70+
$lastWallet->balance; // int(500)
7171
```
7272

7373
Просто работает!

docs/upgrade-guide.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ class Item extends Model implements Product
5959

6060
}
6161
```
62+
63+
## 3.0.x → 3.1.x
64+
65+
Replace `Taxing` to `Taxable`.

docs/wallet-transfer.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ Create new wallets for users.
3636
```php
3737
$name = 'New Wallet';
3838
$firstWallet = $first->createWallet(compact('name'));
39-
$secondWallet = $second->createWallet(compact('name'));
39+
$lastWallet = $last->createWallet(compact('name'));
4040

4141
$firstWallet->deposit(100);
4242
$firstWallet->balance; // int(100)
43-
$secondWallet->balance; // int(0)
43+
$lastWallet->balance; // int(0)
4444
```
4545

46-
The transfer will be from the first user to the second.
46+
The transfer will be from the first user to the last.
4747

4848
```php
49-
$firstWallet->transfer($secondWallet, 5);
49+
$firstWallet->transfer($lastWallet, 5);
5050
$firstWallet->balance; // int(95)
51-
$secondWallet->balance; // int(5)
51+
$lastWallet->balance; // int(5)
5252
```
5353

5454
It worked!
@@ -65,9 +65,9 @@ $lastWallet->balance; // int(0)
6565
The transfer will be from the first user to the second.
6666

6767
```php
68-
$firstWallet->forceTransfer($secondWallet, 500);
68+
$firstWallet->forceTransfer($lastWallet, 500);
6969
$firstWallet->balance; // int(-400)
70-
$secondWallet->balance; // int(500)
70+
$lastWallet->balance; // int(500)
7171
```
7272

7373
It worked!

src/Services/WalletService.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,20 @@ public function checkAmount(int $amount): void
7272
*/
7373
public function getWallet(Wallet $object): WalletModel
7474
{
75-
if ($object instanceof WalletModel) {
76-
return $object;
77-
}
78-
7975
/**
80-
* @var HasWallet $object
76+
* @var WalletModel $wallet
8177
*/
82-
return $object->wallet;
78+
$wallet = $object;
79+
80+
if (!($object instanceof WalletModel)) {
81+
/**
82+
* @var HasWallet $object
83+
*/
84+
$wallet = $object->wallet;
85+
}
86+
87+
$wallet->exists or $wallet->save();
88+
return $wallet;
8389
}
8490

8591
/**
@@ -89,7 +95,6 @@ public function getWallet(Wallet $object): WalletModel
8995
public function getBalance(Wallet $object): int
9096
{
9197
$wallet = $this->getWallet($object);
92-
$wallet->exists or $wallet->save();
9398
$proxy = app(ProxyService::class);
9499
if (!$proxy->has($wallet->getKey())) {
95100
$proxy->set($wallet->getKey(), (int) $wallet->getOriginal('balance', 0));

tests/ConfirmTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Bavix\Wallet\Exceptions\ConfirmedInvalid;
66
use Bavix\Wallet\Exceptions\WalletOwnerInvalid;
77
use Bavix\Wallet\Test\Models\Buyer;
8+
use Bavix\Wallet\Test\Models\UserConfirm;
89

910
class ConfirmTest extends TestCase
1011
{
@@ -121,4 +122,23 @@ public function testWalletOwnerInvalid(): void
121122
$secondWallet->confirm($transaction);
122123
}
123124

125+
/**
126+
* @return void
127+
*/
128+
public function testUserConfirm(): void
129+
{
130+
/**
131+
* @var UserConfirm $userConfirm
132+
*/
133+
$userConfirm = factory(UserConfirm::class)->create();
134+
$transaction = $userConfirm->deposit(100, null, false);
135+
$this->assertEquals($userConfirm->wallet->id, $transaction->wallet->id);
136+
$this->assertEquals($userConfirm->id, $transaction->payable_id);
137+
$this->assertInstanceOf(UserConfirm::class, $transaction->payable);
138+
$this->assertFalse($transaction->confirmed);
139+
140+
$userConfirm->confirm($transaction);
141+
$this->assertTrue($transaction->confirmed);
142+
}
143+
124144
}

tests/Models/UserConfirm.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test\Models;
4+
5+
use Bavix\Wallet\Interfaces\Confirmable;
6+
use Bavix\Wallet\Interfaces\Wallet;
7+
use Bavix\Wallet\Traits\CanConfirm;
8+
use Bavix\Wallet\Traits\HasWallet;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
/**
12+
* Class UserConfirm
13+
*
14+
* @package Bavix\Wallet\Test\Models
15+
* @property string $name
16+
* @property string $email
17+
*/
18+
class UserConfirm extends Model implements Wallet, Confirmable
19+
{
20+
use HasWallet, CanConfirm;
21+
22+
/**
23+
* @var array
24+
*/
25+
protected $fillable = ['name', 'email'];
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getTable(): string
31+
{
32+
return 'users';
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Bavix\Wallet\Test\Models\UserConfirm;
4+
use Faker\Generator as Faker;
5+
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Model Factories
9+
|--------------------------------------------------------------------------
10+
|
11+
| This directory should contain each of the model factory definitions for
12+
| your application. Factories provide a convenient way to generate new
13+
| model instances for testing / seeding your application's database.
14+
|
15+
*/
16+
17+
$factory->define(UserConfirm::class, function (Faker $faker) {
18+
return [
19+
'name' => $faker->name,
20+
'email' => $faker->unique()->safeEmail,
21+
];
22+
});

0 commit comments

Comments
 (0)