Skip to content

Commit c0238e4

Browse files
authored
Merge pull request #442 from bavix/develop
[8.0.4] Added validation when resetting confirmation. Caused unexpected syste…
2 parents ab999e4 + 10c4606 commit c0238e4

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

changelog.md

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

77
## [Unreleased]
88

9+
## [8.0.4] - 2022-02-19
10+
### Added
11+
- Added validation when resetting confirmation. Caused unexpected system behavior.
12+
913
## [8.0.3] - 2022-02-19
1014
### Fixed
1115
- Fixed "UUID Duplicate entry" bug on eager loading. #438 @DanielSpravtsev
@@ -789,7 +793,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
789793
- Exceptions: AmountInvalid, BalanceIsEmpty.
790794
- Models: Transfer, Transaction.
791795

792-
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/8.0.3...develop
796+
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/8.0.4...develop
797+
[8.0.4]: https://github.com/bavix/laravel-wallet/compare/8.0.3...8.0.4
793798
[8.0.3]: https://github.com/bavix/laravel-wallet/compare/8.0.2...8.0.3
794799
[8.0.2]: https://github.com/bavix/laravel-wallet/compare/8.0.1...8.0.2
795800
[8.0.1]: https://github.com/bavix/laravel-wallet/compare/8.0.0...8.0.1

src/Interfaces/Confirmable.php

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function safeConfirm(Transaction $transaction): bool;
3535

3636
/**
3737
* @throws UnconfirmedInvalid
38+
* @throws WalletOwnerInvalid
3839
* @throws LockProviderNotFoundException
3940
* @throws RecordNotFoundException
4041
* @throws RecordsNotFoundException

src/Traits/CanConfirm.php

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function safeConfirm(Transaction $transaction): bool
6363
* Removal of confirmation (forced), use at your own peril and risk.
6464
*
6565
* @throws UnconfirmedInvalid
66+
* @throws WalletOwnerInvalid
6667
* @throws LockProviderNotFoundException
6768
* @throws RecordNotFoundException
6869
* @throws RecordsNotFoundException
@@ -80,6 +81,13 @@ public function resetConfirm(Transaction $transaction): bool
8081
}
8182

8283
$wallet = app(CastServiceInterface::class)->getWallet($this);
84+
if ($wallet->getKey() !== $transaction->wallet_id) {
85+
throw new WalletOwnerInvalid(
86+
app(TranslatorServiceInterface::class)->get('wallet::errors.owner_invalid'),
87+
ExceptionInterface::WALLET_OWNER_INVALID
88+
);
89+
}
90+
8391
app(RegulatorServiceInterface::class)->decrease($wallet, $transaction->amount);
8492

8593
return $transaction->update(['confirmed' => false]);

tests/Units/Domain/ConfirmTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
use Bavix\Wallet\Exceptions\UnconfirmedInvalid;
99
use Bavix\Wallet\Exceptions\WalletOwnerInvalid;
1010
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
11+
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
1112
use Bavix\Wallet\Services\BookkeeperServiceInterface;
1213
use Bavix\Wallet\Services\RegulatorServiceInterface;
1314
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
1415
use Bavix\Wallet\Test\Infra\Factories\UserConfirmFactory;
16+
use Bavix\Wallet\Test\Infra\Factories\UserFactory;
1517
use Bavix\Wallet\Test\Infra\Models\Buyer;
18+
use Bavix\Wallet\Test\Infra\Models\User;
1619
use Bavix\Wallet\Test\Infra\Models\UserConfirm;
1720
use Bavix\Wallet\Test\Infra\TestCase;
1821

@@ -251,4 +254,45 @@ public function testUserConfirmByWallet(): void
251254
self::assertTrue($userConfirm->wallet->confirm($transaction));
252255
self::assertTrue($transaction->confirmed);
253256
}
257+
258+
public function testTransactionResetConfirmWalletOwnerInvalid(): void
259+
{
260+
$this->expectException(WalletOwnerInvalid::class);
261+
$this->expectExceptionCode(ExceptionInterface::WALLET_OWNER_INVALID);
262+
$this->expectExceptionMessageStrict(trans('wallet::errors.owner_invalid'));
263+
264+
/**
265+
* @var User $user1
266+
* @var User $user2
267+
*/
268+
[$user1, $user2] = UserFactory::times(2)->create();
269+
$user1->deposit(1000);
270+
271+
self::assertSame(1000, $user1->balanceInt);
272+
273+
$transfer = $user1->transfer($user2, 500);
274+
$user1->wallet->resetConfirm($transfer->deposit);
275+
}
276+
277+
public function testTransactionResetConfirmSuccess(): void
278+
{
279+
/**
280+
* @var User $user1
281+
* @var User $user2
282+
*/
283+
[$user1, $user2] = UserFactory::times(2)->create();
284+
$user1->deposit(1000);
285+
286+
self::assertSame(1000, $user1->balanceInt);
287+
app(DatabaseServiceInterface::class)->transaction(static function () use ($user1, $user2) {
288+
$transfer = $user1->transfer($user2, 500);
289+
self::assertTrue($user2->wallet->resetConfirm($transfer->deposit)); // confirm => false
290+
});
291+
292+
self::assertSame(500, (int) $user1->transactions()->sum('amount'));
293+
self::assertSame(500, (int) $user2->transactions()->sum('amount'));
294+
295+
self::assertSame(500, $user1->balanceInt);
296+
self::assertSame(0, $user2->balanceInt);
297+
}
254298
}

0 commit comments

Comments
 (0)