Skip to content

Commit e14734a

Browse files
committed
fixed bug with unconfirmed charges/deposits
1 parent 107b40b commit e14734a

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/Traits/HasWallet.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,11 @@ protected function assemble(Wallet $wallet, Transaction $withdraw, Transaction $
168168
*/
169169
protected function change(int $amount, ?array $meta, bool $confirmed): Transaction
170170
{
171-
$this->getBalanceAttribute();
172-
static::$cachedBalances[$this->getKey()] += $amount;
171+
if ($confirmed) {
172+
$this->getBalanceAttribute();
173+
static::$cachedBalances[$this->getKey()] += $amount;
174+
}
175+
173176
return $this->transactions()->create([
174177
'type' => $amount > 0 ? 'deposit' : 'withdraw',
175178
'payable_type' => $this->getMorphClass(),

tests/ProductTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,24 @@ public function testForceRefund(): void
9595
$this->assertEquals($buyer->balance, 0);
9696
}
9797

98+
/**
99+
* @return void
100+
* @expectedException \Bavix\Wallet\Exceptions\ProductEnded
101+
*/
102+
public function testOutOfStock(): void
103+
{
104+
/**
105+
* @var Buyer $buyer
106+
* @var Item $product
107+
*/
108+
$buyer = factory(Buyer::class)->create();
109+
$product = factory(Item::class)->create([
110+
'quantity' => 1,
111+
]);
112+
113+
$buyer->deposit($product->price);
114+
$buyer->pay($product);
115+
$buyer->pay($product);
116+
}
117+
98118
}

tests/WalletTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,39 @@ public function testTransferYourself(): void
141141
$this->assertEquals($user->balance, 0);
142142
}
143143

144+
/**
145+
* @return void
146+
* @expectedException \Bavix\Wallet\Exceptions\BalanceIsEmpty
147+
*/
148+
public function testBalanceIsEmpty(): void
149+
{
150+
/**
151+
* @var User $user
152+
*/
153+
$user = factory(User::class)->create();
154+
$this->assertEquals($user->balance, 0);
155+
$user->withdraw(1);
156+
}
157+
158+
/**
159+
* @return void
160+
*/
161+
public function testConfirmed(): void
162+
{
163+
/**
164+
* @var User $user
165+
*/
166+
$user = factory(User::class)->create();
167+
$this->assertEquals($user->balance, 0);
168+
169+
$user->deposit(1);
170+
$this->assertEquals($user->balance, 1);
171+
172+
$user->withdraw(1, null, false);
173+
$this->assertEquals($user->balance, 1);
174+
175+
$user->withdraw(1);
176+
$this->assertEquals($user->balance, 0);
177+
}
178+
144179
}

0 commit comments

Comments
 (0)