diff --git a/src/app/Providers/PaymentProvider.php b/src/app/Providers/PaymentProvider.php --- a/src/app/Providers/PaymentProvider.php +++ b/src/app/Providers/PaymentProvider.php @@ -303,7 +303,7 @@ } $methods = self::applyMethodWhitelist($type, $methods); - \Log::debug("Loaded payment methods" . var_export($methods, true)); + \Log::debug("Loaded payment methods " . var_export($methods, true)); Cache::put($cacheKey, $methods, now()->addHours(1)); diff --git a/src/app/Wallet.php b/src/app/Wallet.php --- a/src/app/Wallet.php +++ b/src/app/Wallet.php @@ -725,6 +725,10 @@ $amount = abs($amount) * -1; } + // Another process might have modified the balance since the object was created, refresh it. + // Note: We're not using $this->increment('balance', $amount) because that will not pass + // a proper balance value to the 'updated' event observer. + $this->balance = self::find($this->id)->balance; $this->balance += $amount; $this->save(); diff --git a/src/tests/Feature/WalletTest.php b/src/tests/Feature/WalletTest.php --- a/src/tests/Feature/WalletTest.php +++ b/src/tests/Feature/WalletTest.php @@ -599,7 +599,34 @@ */ public function testAwardAndPenalty(): void { - $this->markTestIncomplete(); + $user = $this->getTestUser('UserWallet1@UserWallet.com'); + $wallet = $user->wallets()->first(); + + // Test award + // Set the balance to a different value than the one in the database + // The tested methods should use value from the database + $wallet->balance = -100; + + $this->assertSame($wallet->id, $wallet->award(100, 'test')->id); + $this->assertSame(100, $wallet->balance); + $this->assertSame(100, $wallet->fresh()->balance); + $transaction = $wallet->transactions()->first(); + $this->assertSame(100, $transaction->amount); + $this->assertSame(Transaction::WALLET_AWARD, $transaction->type); + $this->assertSame('test', $transaction->description); + + $wallet->transactions()->delete(); + + // Test penalty + $this->assertSame($wallet->id, $wallet->penalty(100, 'test')->id); + $this->assertSame(0, $wallet->balance); + $this->assertSame(0, $wallet->fresh()->balance); + $transaction = $wallet->transactions()->first(); + $this->assertSame(-100, $transaction->amount); + $this->assertSame(Transaction::WALLET_PENALTY, $transaction->type); + $this->assertSame('test', $transaction->description); + + // TODO: Test input of type Payment } /**