|
| 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! |
0 commit comments