Wallet logo Wallet

Make a refund


User Model

Add the CanBePaid trait and Customer interface to your User model.

use Bavix\Wallet\Traits\CanBePaid;
use Bavix\Wallet\Interfaces\Customer;

class User extends Model implements Customer
{
    use CanBePaid;
}

Item Model

Add the HasWallet trait and Product interface to Item model.

use Bavix\Wallet\Traits\HasWallet;
use Bavix\Wallet\Interfaces\Product;

class Item extends Model implements Product
{
    use HasWallet;

    public function canBuy(Customer $customer, bool $force = false): bool
    {
        /**
         * If the service can be purchased once, then
         *  return !$customer->paid($this);
         */
        return true; 
    }

    public function getAmountProduct(): int
    {
        return 100;
    }

    public function getMetaProduct(): ?array
    {
        return [
            'title' => $this->title, 
            'description' => 'Purchase of Product #' . $this->id, 
            'price' => $this->getAmountProduct(),
        ];
    }
}

Make a refund

Find the user and check the balance.

$user = User::first();
$user->balance; // int(0)

Find the goods and check the balance.

$item = Item::first();
$item->balance; // int(100)

Return of funds!

(bool)$user->paid($item); // bool(true)
(bool)$user->refund($item); // bool(true)
$item->balance; // int(0)
$user->balance; // int(100)

It worked!