Skip to content

Commit

Permalink
### Added
Browse files Browse the repository at this point in the history
- Allow to free buy with a negative balance
- Add parameter `$allowZero` to method `canWithdraw`

### Fixed
- method canWithdraw, with a negative price, almost always true
  • Loading branch information
rez1dent3 committed Jul 30, 2019
1 parent 345fd11 commit e698a2e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
11 changes: 10 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.1.2] - 2019-07-30
### Added
- Allow to free buy with a negative balance
- Add parameter `$allowZero` to method `canWithdraw`

### Fixed
- method canWithdraw, with a negative price, almost always true

## [3.1.1] - 2019-07-29
### Added
- Add getCurrencyAttribute
Expand Down Expand Up @@ -354,7 +362,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
- Exceptions: AmountInvalid, BalanceIsEmpty.
- Models: Transfer, Transaction.

[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.1...HEAD
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.2...HEAD
[3.1.2]: https://github.com/bavix/laravel-wallet/compare/3.1.1...3.1.2
[3.1.1]: https://github.com/bavix/laravel-wallet/compare/3.1.0...3.1.1
[3.1.0]: https://github.com/bavix/laravel-wallet/compare/3.0.4...3.1.0
[3.0.4]: https://github.com/bavix/laravel-wallet/compare/3.0.3...3.0.4
Expand Down
3 changes: 2 additions & 1 deletion src/Interfaces/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null):

/**
* @param int $amount
* @param bool $allowZero
* @return bool
*/
public function canWithdraw(int $amount): bool;
public function canWithdraw(int $amount, bool $allowZero = null): bool;

/**
* @return int
Expand Down
5 changes: 3 additions & 2 deletions src/Services/CommonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ public function deposit(Wallet $wallet, int $amount, ?array $meta, bool $confirm
/**
* @param Wallet $wallet
* @param int $amount
* @param bool $allowZero
* @return void
* @throws BalanceIsEmpty
* @throws InsufficientFunds
*/
public function verifyWithdraw(Wallet $wallet, int $amount): void
public function verifyWithdraw(Wallet $wallet, int $amount, bool $allowZero = null): void
{
/**
* @var HasWallet $wallet
Expand All @@ -133,7 +134,7 @@ public function verifyWithdraw(Wallet $wallet, int $amount): void
throw new BalanceIsEmpty(trans('wallet::errors.wallet_empty'));
}

if (!$wallet->canWithdraw($amount)) {
if (!$wallet->canWithdraw($amount, $allowZero)) {
throw new InsufficientFunds(trans('wallet::errors.insufficient_funds'));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CartPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function payFreeCart(Cart $cart): array
}

app(CommonService::class)
->verifyWithdraw($this, 0);
->verifyWithdraw($this, 0, true);

$self = $this;
return DB::transaction(static function() use ($self, $cart) {
Expand Down
10 changes: 9 additions & 1 deletion src/Traits/HasWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,18 @@ public function withdraw(int $amount, ?array $meta = null, bool $confirmed = tru
* Checks if you can withdraw funds
*
* @param int $amount
* @param bool $allowZero
* @return bool
*/
public function canWithdraw(int $amount): bool
public function canWithdraw(int $amount, bool $allowZero = null): bool
{
/**
* Allow to buy for free with a negative balance
*/
if ($allowZero && $amount === 0) {
return true;
}

return $this->balance >= $amount;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/BalanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ public function testDepositWalletExists(): void
$this->assertTrue($buyer->wallet->exists);
}

/**
* @return void
*/
public function testCanWithdraw(): void
{
/**
* @var Buyer $buyer
*/
$buyer = factory(Buyer::class)->create();
$this->assertTrue($buyer->canWithdraw(0));

$buyer->forceWithdraw(1);
$this->assertFalse($buyer->canWithdraw(0));
$this->assertTrue($buyer->canWithdraw(0, true));
}

/**
* @return void
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,32 @@ public function testPayFree(): void
$this->assertEquals($product->balance, 0);
}

public function testFreePay(): void
{
/**
* @var Buyer $buyer
* @var Item $product
*/
$buyer = factory(Buyer::class)->create();
$product = factory(Item::class)->create([
'quantity' => 1,
]);

$buyer->forceWithdraw(1000);
$this->assertEquals($buyer->balance, -1000);

$transfer = $buyer->payFree($product);
$this->assertEquals($transfer->deposit->type, Transaction::TYPE_DEPOSIT);
$this->assertEquals($transfer->withdraw->type, Transaction::TYPE_WITHDRAW);

$this->assertEquals($buyer->balance, -1000);
$this->assertEquals($product->balance, 0);

$buyer->refund($product);
$this->assertEquals($buyer->balance, -1000);
$this->assertEquals($product->balance, 0);
}

/**
* @return void
*/
Expand Down

0 comments on commit e698a2e

Please sign in to comment.