|
1 | 1 | ## User Model |
2 | 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. |
| 3 | +Add the `CanConfirm` trait and `Confirmable` interface to your User model. |
18 | 4 |
|
19 | 5 | ```php |
| 6 | +use Bavix\Wallet\Interfaces\Confirmable; |
| 7 | +use Bavix\Wallet\Interfaces\Wallet; |
| 8 | +use Bavix\Wallet\Traits\CanConfirm; |
20 | 9 | use Bavix\Wallet\Traits\HasWallet; |
21 | | -use Bavix\Wallet\Interfaces\Product; |
22 | | -use Bavix\Wallet\Interfaces\Customer; |
23 | 10 |
|
24 | | -class Item extends Model implements Product |
| 11 | +class UserConfirm extends Model implements Wallet, Confirmable |
25 | 12 | { |
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 | | - } |
| 13 | + use HasWallet, CanConfirm; |
55 | 14 | } |
56 | 15 | ``` |
57 | 16 |
|
58 | | -## Make a refund |
59 | | - |
60 | | -Find the user and check the balance. |
| 17 | +### Example: |
61 | 18 |
|
62 | 19 | ```php |
63 | | -$user = User::first(); |
64 | 20 | $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 | | -``` |
| 21 | +$transaction = $user->deposit(100, null, false); // not confirm |
| 22 | +$transaction->confirmed; // bool(false) |
| 23 | +$user->balance; // int(0) |
73 | 24 |
|
74 | | -Return of funds! |
| 25 | +$user->confirm($transaction); // bool(true) |
| 26 | +$transaction->confirmed; // bool(true) |
75 | 27 |
|
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) |
| 28 | +$user->balance; // int(100) |
81 | 29 | ``` |
82 | 30 |
|
83 | 31 | It worked! |
0 commit comments