Skip to content

Commit e698a2e

Browse files
committed
### 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
1 parent 345fd11 commit e698a2e

File tree

7 files changed

+67
-6
lines changed

7 files changed

+67
-6
lines changed

changelog.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [3.1.2] - 2019-07-30
10+
### Added
11+
- Allow to free buy with a negative balance
12+
- Add parameter `$allowZero` to method `canWithdraw`
13+
14+
### Fixed
15+
- method canWithdraw, with a negative price, almost always true
16+
917
## [3.1.1] - 2019-07-29
1018
### Added
1119
- Add getCurrencyAttribute
@@ -354,7 +362,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
354362
- Exceptions: AmountInvalid, BalanceIsEmpty.
355363
- Models: Transfer, Transaction.
356364

357-
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.1...HEAD
365+
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.2...HEAD
366+
[3.1.2]: https://github.com/bavix/laravel-wallet/compare/3.1.1...3.1.2
358367
[3.1.1]: https://github.com/bavix/laravel-wallet/compare/3.1.0...3.1.1
359368
[3.1.0]: https://github.com/bavix/laravel-wallet/compare/3.0.4...3.1.0
360369
[3.0.4]: https://github.com/bavix/laravel-wallet/compare/3.0.3...3.0.4

src/Interfaces/Wallet.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null):
6161

6262
/**
6363
* @param int $amount
64+
* @param bool $allowZero
6465
* @return bool
6566
*/
66-
public function canWithdraw(int $amount): bool;
67+
public function canWithdraw(int $amount, bool $allowZero = null): bool;
6768

6869
/**
6970
* @return int

src/Services/CommonService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ public function deposit(Wallet $wallet, int $amount, ?array $meta, bool $confirm
120120
/**
121121
* @param Wallet $wallet
122122
* @param int $amount
123+
* @param bool $allowZero
123124
* @return void
124125
* @throws BalanceIsEmpty
125126
* @throws InsufficientFunds
126127
*/
127-
public function verifyWithdraw(Wallet $wallet, int $amount): void
128+
public function verifyWithdraw(Wallet $wallet, int $amount, bool $allowZero = null): void
128129
{
129130
/**
130131
* @var HasWallet $wallet
@@ -133,7 +134,7 @@ public function verifyWithdraw(Wallet $wallet, int $amount): void
133134
throw new BalanceIsEmpty(trans('wallet::errors.wallet_empty'));
134135
}
135136

136-
if (!$wallet->canWithdraw($amount)) {
137+
if (!$wallet->canWithdraw($amount, $allowZero)) {
137138
throw new InsufficientFunds(trans('wallet::errors.insufficient_funds'));
138139
}
139140
}

src/Traits/CartPay.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function payFreeCart(Cart $cart): array
2929
}
3030

3131
app(CommonService::class)
32-
->verifyWithdraw($this, 0);
32+
->verifyWithdraw($this, 0, true);
3333

3434
$self = $this;
3535
return DB::transaction(static function() use ($self, $cart) {

src/Traits/HasWallet.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,18 @@ public function withdraw(int $amount, ?array $meta = null, bool $confirmed = tru
137137
* Checks if you can withdraw funds
138138
*
139139
* @param int $amount
140+
* @param bool $allowZero
140141
* @return bool
141142
*/
142-
public function canWithdraw(int $amount): bool
143+
public function canWithdraw(int $amount, bool $allowZero = null): bool
143144
{
145+
/**
146+
* Allow to buy for free with a negative balance
147+
*/
148+
if ($allowZero && $amount === 0) {
149+
return true;
150+
}
151+
144152
return $this->balance >= $amount;
145153
}
146154

tests/BalanceTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ public function testDepositWalletExists(): void
2727
$this->assertTrue($buyer->wallet->exists);
2828
}
2929

30+
/**
31+
* @return void
32+
*/
33+
public function testCanWithdraw(): void
34+
{
35+
/**
36+
* @var Buyer $buyer
37+
*/
38+
$buyer = factory(Buyer::class)->create();
39+
$this->assertTrue($buyer->canWithdraw(0));
40+
41+
$buyer->forceWithdraw(1);
42+
$this->assertFalse($buyer->canWithdraw(0));
43+
$this->assertTrue($buyer->canWithdraw(0, true));
44+
}
45+
3046
/**
3147
* @return void
3248
*/

tests/ProductTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,32 @@ public function testPayFree(): void
219219
$this->assertEquals($product->balance, 0);
220220
}
221221

222+
public function testFreePay(): void
223+
{
224+
/**
225+
* @var Buyer $buyer
226+
* @var Item $product
227+
*/
228+
$buyer = factory(Buyer::class)->create();
229+
$product = factory(Item::class)->create([
230+
'quantity' => 1,
231+
]);
232+
233+
$buyer->forceWithdraw(1000);
234+
$this->assertEquals($buyer->balance, -1000);
235+
236+
$transfer = $buyer->payFree($product);
237+
$this->assertEquals($transfer->deposit->type, Transaction::TYPE_DEPOSIT);
238+
$this->assertEquals($transfer->withdraw->type, Transaction::TYPE_WITHDRAW);
239+
240+
$this->assertEquals($buyer->balance, -1000);
241+
$this->assertEquals($product->balance, 0);
242+
243+
$buyer->refund($product);
244+
$this->assertEquals($buyer->balance, -1000);
245+
$this->assertEquals($product->balance, 0);
246+
}
247+
222248
/**
223249
* @return void
224250
*/

0 commit comments

Comments
 (0)