|
| 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 | +use Bavix\Wallet\Interfaces\Taxable; |
| 24 | + |
| 25 | +class Item extends Model implements Product, Taxable |
| 26 | +{ |
| 27 | + use HasWallet; |
| 28 | + |
| 29 | + public function canBuy(Customer $customer, int $quantity = 1, bool $force = null): bool |
| 30 | + { |
| 31 | + /** |
| 32 | + * If the service can be purchased once, then |
| 33 | + * return !$customer->paid($this); |
| 34 | + */ |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + public function getAmountProduct(): int |
| 39 | + { |
| 40 | + return 100; |
| 41 | + } |
| 42 | + |
| 43 | + public function getMetaProduct(): ?array |
| 44 | + { |
| 45 | + return [ |
| 46 | + 'title' => $this->title, |
| 47 | + 'description' => 'Purchase of Product #' . $this->id, |
| 48 | + 'price' => $this->getAmountProduct(), |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + public function getUniqueId(): string |
| 53 | + { |
| 54 | + return (string)$this->getKey(); |
| 55 | + } |
| 56 | + |
| 57 | + public function getFeePercent() : float |
| 58 | + { |
| 59 | + return 0.03; // 3% |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Tax process |
| 65 | + |
| 66 | +Find the user and check the balance. |
| 67 | + |
| 68 | +```php |
| 69 | +$user = User::first(); |
| 70 | +$user->balance; // int(103) |
| 71 | +``` |
| 72 | + |
| 73 | +Find the goods and check the cost. |
| 74 | + |
| 75 | +```php |
| 76 | +$item = Item::first(); |
| 77 | +$item->getAmountProduct(); // int(100) |
| 78 | +``` |
| 79 | + |
| 80 | +The user can buy a product, buy... |
| 81 | + |
| 82 | +```php |
| 83 | +$user->pay($item); // success, 100 (product) + 3 (fee) = 103 |
| 84 | +$user->balance; // int(0) |
| 85 | +``` |
| 86 | + |
| 87 | +## Minimal Taxing |
| 88 | + |
| 89 | +Add trait `MinimalTaxable` in class `Item`. |
| 90 | + |
| 91 | +```php |
| 92 | +use Bavix\Wallet\Traits\HasWallet; |
| 93 | +use Bavix\Wallet\Interfaces\Product; |
| 94 | +use Bavix\Wallet\Interfaces\Customer; |
| 95 | +use Bavix\Wallet\Interfaces\MinimalTaxable; |
| 96 | + |
| 97 | +class Item extends Model implements Product, MinimalTaxable |
| 98 | +{ |
| 99 | + use HasWallet; |
| 100 | + |
| 101 | + public function canBuy(Customer $customer, int $quantity = 1, bool $force = null): bool |
| 102 | + { |
| 103 | + /** |
| 104 | + * If the service can be purchased once, then |
| 105 | + * return !$customer->paid($this); |
| 106 | + */ |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + public function getAmountProduct(): int |
| 111 | + { |
| 112 | + return 100; |
| 113 | + } |
| 114 | + |
| 115 | + public function getMetaProduct(): ?array |
| 116 | + { |
| 117 | + return [ |
| 118 | + 'title' => $this->title, |
| 119 | + 'description' => 'Purchase of Product #' . $this->id, |
| 120 | + 'price' => $this->getAmountProduct(), |
| 121 | + ]; |
| 122 | + } |
| 123 | + |
| 124 | + public function getUniqueId(): string |
| 125 | + { |
| 126 | + return (string)$this->getKey(); |
| 127 | + } |
| 128 | + |
| 129 | + public function getFeePercent() : float |
| 130 | + { |
| 131 | + return 0.03; // 3% |
| 132 | + } |
| 133 | + |
| 134 | + public function getMinimalFee() : int |
| 135 | + { |
| 136 | + return 5; // 3%, minimum int(5) |
| 137 | + } |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +#### Successfully |
| 142 | + |
| 143 | +Find the user and check the balance. |
| 144 | + |
| 145 | +```php |
| 146 | +$user = User::first(); |
| 147 | +$user->balance; // int(105) |
| 148 | +``` |
| 149 | + |
| 150 | +Find the goods and check the cost. |
| 151 | + |
| 152 | +```php |
| 153 | +$item = Item::first(); |
| 154 | +$item->getAmountProduct(); // int(100) |
| 155 | +``` |
| 156 | + |
| 157 | +The user can buy a product, buy... |
| 158 | + |
| 159 | +```php |
| 160 | +$user->pay($item); // failed, 100 (product) + 5 (minimal fee) = 105 |
| 161 | +$user->balance; // int(0) |
| 162 | +``` |
| 163 | + |
| 164 | +#### Failed |
| 165 | + |
| 166 | +Find the user and check the balance. |
| 167 | + |
| 168 | +```php |
| 169 | +$user = User::first(); |
| 170 | +$user->balance; // int(103) |
| 171 | +``` |
| 172 | + |
| 173 | +Find the goods and check the cost. |
| 174 | + |
| 175 | +```php |
| 176 | +$item = Item::first(); |
| 177 | +$item->getAmountProduct(); // int(100) |
| 178 | +``` |
| 179 | + |
| 180 | +The user can buy a product, buy... |
| 181 | + |
| 182 | +```php |
| 183 | +$user->safePay($item); // failed, 100 (product) + 5 (minimal fee) = 105 |
| 184 | +$user->balance; // int(103) |
| 185 | +``` |
| 186 | + |
| 187 | +It worked! |
0 commit comments