|
| 1 | +## User Model |
| 2 | + |
| 3 | +Add the `CanPay` trait and `Customer` interface to your User model. |
| 4 | + |
| 5 | +> The trait `CanPay` already inherits `HasWallet`, reuse will cause an error. |
| 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 | +Add the `HasWallet` trait and interface to `Item` model. |
| 20 | +If we want to achieve multi wallets for a product, then we need to add `HasWallets`. |
| 21 | + |
| 22 | +Starting from version 9.x there are two product interfaces: |
| 23 | +- For an unlimited number of products (`ProductInterface`); |
| 24 | +- For a limited number of products (`ProductLimitedInterface`); |
| 25 | + |
| 26 | +An example with an unlimited number of products: |
| 27 | +```php |
| 28 | +use Bavix\Wallet\Traits\HasWallet; |
| 29 | +use Bavix\Wallet\Traits\HasWallets; |
| 30 | +use Bavix\Wallet\Interfaces\Customer; |
| 31 | +use Bavix\Wallet\Interfaces\ProductInterface; |
| 32 | + |
| 33 | +class Item extends Model implements ProductInterface |
| 34 | +{ |
| 35 | + use HasWallet, HasWallets; |
| 36 | + |
| 37 | + public function getAmountProduct(Customer $customer): int|string |
| 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 | + ]; |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Example with a limited number of products: |
| 53 | +```php |
| 54 | +use Bavix\Wallet\Traits\HasWallet; |
| 55 | +use Bavix\Wallet\Traits\HasWallets; |
| 56 | +use Bavix\Wallet\Interfaces\Customer; |
| 57 | +use Bavix\Wallet\Interfaces\ProductLimitedInterface; |
| 58 | + |
| 59 | +class Item extends Model implements ProductLimitedInterface |
| 60 | +{ |
| 61 | + use HasWallet, HasWallets; |
| 62 | + |
| 63 | + public function canBuy(Customer $customer, int $quantity = 1, bool $force = false): bool |
| 64 | + { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + public function getAmountProduct(Customer $customer): int|string |
| 69 | + { |
| 70 | + return 100; |
| 71 | + } |
| 72 | + |
| 73 | + public function getMetaProduct(): ?array |
| 74 | + { |
| 75 | + return [ |
| 76 | + 'title' => $this->title, |
| 77 | + 'description' => 'Purchase of Product #' . $this->id, |
| 78 | + ]; |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +I do not recommend using the limited interface when working with a shopping cart. |
| 84 | +If you are working with a shopping cart, then you should override the `PurchaseServiceInterface` interface. |
| 85 | +With it, you can check the availability of all products with one request, there will be no N-queries in the database. |
| 86 | + |
| 87 | +## Proceed to purchase |
| 88 | + |
| 89 | +Find the user and check the balance. |
| 90 | + |
| 91 | +```php |
| 92 | +$user = User::first(); |
| 93 | +$user->balance; // 100 |
| 94 | +``` |
| 95 | + |
| 96 | +Find the goods and check the cost. |
| 97 | + |
| 98 | +```php |
| 99 | +$item = Item::first(); |
| 100 | +$item->getAmountProduct($user); // 100 |
| 101 | + |
| 102 | +$receiving = $item->createWallet([ |
| 103 | + 'name' => 'Dollar', |
| 104 | + 'meta' => [ |
| 105 | + 'currency' => 'USD', |
| 106 | + ], |
| 107 | +]); |
| 108 | +``` |
| 109 | + |
| 110 | +The user can buy a product, buy... |
| 111 | + |
| 112 | +```php |
| 113 | +$cart = app(Cart::class) |
| 114 | + ->withItem($item, receiving: $receiving) |
| 115 | +; |
| 116 | + |
| 117 | +$user->payCart($cart); |
| 118 | +$user->balance; // 0 |
| 119 | + |
| 120 | +$receiving->balanceInt; // $100 |
| 121 | +``` |
| 122 | + |
| 123 | +It worked! |
0 commit comments