|
| 1 | +## User Model |
| 2 | + |
| 3 | +Добавим `CanPay` trait и `Customer` interface в модель User. |
| 4 | + |
| 5 | +> Трейт `CanPay` уже наследует `HasWallet`, повторное использование вызовет ошибку. |
| 6 | +
|
| 7 | +```php |
| 8 | +use Bavix\Wallet\Traits\CanPay; |
| 9 | +use Bavix\Wallet\Interfaces\Customer; |
| 10 | + |
| 11 | +class User extends Model implements Customer |
| 12 | +{ |
| 13 | + use CanPay; |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +## Item Model |
| 18 | + |
| 19 | +Добавим `HasWallet` trait и `Product` interface в модель Item. |
| 20 | + |
| 21 | +```php |
| 22 | +use Bavix\Wallet\Traits\HasWallet; |
| 23 | +use Bavix\Wallet\Interfaces\Product; |
| 24 | +use Bavix\Wallet\Interfaces\Customer; |
| 25 | +use Bavix\Wallet\Interfaces\Taxable; |
| 26 | + |
| 27 | +class Item extends Model implements Product, Taxable |
| 28 | +{ |
| 29 | + use HasWallet; |
| 30 | + |
| 31 | + public function canBuy(Customer $customer, int $quantity = 1, bool $force = null): bool |
| 32 | + { |
| 33 | + /** |
| 34 | + * If the service can be purchased once, then |
| 35 | + * return !$customer->paid($this); |
| 36 | + */ |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + public function getAmountProduct(): int |
| 41 | + { |
| 42 | + return 100; |
| 43 | + } |
| 44 | + |
| 45 | + public function getMetaProduct(): ?array |
| 46 | + { |
| 47 | + return [ |
| 48 | + 'title' => $this->title, |
| 49 | + 'description' => 'Purchase of Product #' . $this->id, |
| 50 | + 'price' => $this->getAmountProduct(), |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + public function getUniqueId(): string |
| 55 | + { |
| 56 | + return (string)$this->getKey(); |
| 57 | + } |
| 58 | + |
| 59 | + public function getFeePercent() : float |
| 60 | + { |
| 61 | + return 0.03; // 3% |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Оплата с налогом |
| 67 | + |
| 68 | +Находим пользователя и проверяем его баланс. |
| 69 | + |
| 70 | +```php |
| 71 | +$user = User::first(); |
| 72 | +$user->balance; // int(103) |
| 73 | +``` |
| 74 | + |
| 75 | +Деньги есть, это хорошо. Проверим стоимость товара. |
| 76 | + |
| 77 | +```php |
| 78 | +$item = Item::first(); |
| 79 | +$item->getAmountProduct(); // int(100) |
| 80 | +``` |
| 81 | + |
| 82 | +Товар стоит 100 бубликов. Значит налог составит 3 бублика. |
| 83 | +Пользователю хватает средств для покупки товара. |
| 84 | + |
| 85 | +```php |
| 86 | +$user->pay($item); // success, 100 (product) + 3 (fee) = 103 |
| 87 | +$user->balance; // int(0) |
| 88 | +``` |
| 89 | + |
| 90 | +## Минимальный налог |
| 91 | + |
| 92 | +Добавим trait `MinimalTaxable` в модель `Item`. |
| 93 | + |
| 94 | +```php |
| 95 | +use Bavix\Wallet\Traits\HasWallet; |
| 96 | +use Bavix\Wallet\Interfaces\Product; |
| 97 | +use Bavix\Wallet\Interfaces\Customer; |
| 98 | +use Bavix\Wallet\Interfaces\MinimalTaxable; |
| 99 | + |
| 100 | +class Item extends Model implements Product, MinimalTaxable |
| 101 | +{ |
| 102 | + use HasWallet; |
| 103 | + |
| 104 | + public function canBuy(Customer $customer, int $quantity = 1, bool $force = null): bool |
| 105 | + { |
| 106 | + /** |
| 107 | + * If the service can be purchased once, then |
| 108 | + * return !$customer->paid($this); |
| 109 | + */ |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + public function getAmountProduct(): int |
| 114 | + { |
| 115 | + return 100; |
| 116 | + } |
| 117 | + |
| 118 | + public function getMetaProduct(): ?array |
| 119 | + { |
| 120 | + return [ |
| 121 | + 'title' => $this->title, |
| 122 | + 'description' => 'Purchase of Product #' . $this->id, |
| 123 | + 'price' => $this->getAmountProduct(), |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + public function getUniqueId(): string |
| 128 | + { |
| 129 | + return (string)$this->getKey(); |
| 130 | + } |
| 131 | + |
| 132 | + public function getFeePercent() : float |
| 133 | + { |
| 134 | + return 0.03; // 3% |
| 135 | + } |
| 136 | + |
| 137 | + public function getMinimalFee() : int |
| 138 | + { |
| 139 | + return 5; // 3%, minimum int(5) |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +#### Успешная операция |
| 145 | + |
| 146 | +Находим пользователя и проверяем баланс. |
| 147 | + |
| 148 | +```php |
| 149 | +$user = User::first(); |
| 150 | +$user->balance; // int(105) |
| 151 | +``` |
| 152 | + |
| 153 | +Денег хватает, проверяем стоимость товара. |
| 154 | + |
| 155 | +```php |
| 156 | +$item = Item::first(); |
| 157 | +$item->getAmountProduct(); // int(100) |
| 158 | +``` |
| 159 | + |
| 160 | +Налог 3%, а минимальный налог составляет 5 бубликов. |
| 161 | +Нашему пользователю хватает средств для покупки товара. |
| 162 | + |
| 163 | +```php |
| 164 | +$user->pay($item); // успешно, 100 (product) + 5 (minimal fee) = 105 |
| 165 | +$user->balance; // int(0) |
| 166 | +``` |
| 167 | + |
| 168 | +#### Ошибочная операция |
| 169 | + |
| 170 | +Находим пользователя и проверяем баланс. |
| 171 | + |
| 172 | +```php |
| 173 | +$user = User::first(); |
| 174 | +$user->balance; // int(103) |
| 175 | +``` |
| 176 | + |
| 177 | +Находим товар. |
| 178 | + |
| 179 | +```php |
| 180 | +$item = Item::first(); |
| 181 | +$item->getAmountProduct(); // int(100) |
| 182 | +``` |
| 183 | + |
| 184 | +Налог составит 5 бубликов, а у пользователя только 103 бублика. |
| 185 | +Возникнет ошибка при попытке оплаты, воспользуемся `safePay` чтобы это проверить. |
| 186 | + |
| 187 | +```php |
| 188 | +$user->safePay($item); // ошибка, 100 (product) + 5 (minimal fee) = 105 |
| 189 | +$user->balance; // int(103) |
| 190 | +``` |
| 191 | + |
| 192 | +Это работает! |
0 commit comments