Skip to content

Commit d71841d

Browse files
committed
add payFree & trans
1 parent 7bc05e1 commit d71841d

File tree

8 files changed

+46
-7
lines changed

8 files changed

+46
-7
lines changed

resources/lang/en/errors.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'price_positive' => 'The price should be positive',
5+
'product_stock' => 'The product is out of stock',
6+
'wallet_empty' => 'Wallet is empty',
7+
'insufficient_funds' => 'Insufficient funds',
8+
];

src/Exceptions/BalanceIsEmpty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bavix\Wallet\Exceptions;
44

5-
class BalanceIsEmpty extends \LogicException
5+
class BalanceIsEmpty extends InsufficientFunds
66
{
77

88
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Exceptions;
4+
5+
class InsufficientFunds extends \LogicException
6+
{
7+
8+
}

src/Traits/CanBePaid.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ trait CanBePaid
1414

1515
use HasWallet;
1616

17+
/**
18+
* @param Product $product
19+
* @return Transfer
20+
*/
21+
public function payFree(Product $product): Transfer
22+
{
23+
if (!$product->canBuy($this)) {
24+
throw new ProductEnded(trans('wallet::errors.product_stock'));
25+
}
26+
27+
return $this->transfer($product, 0, $product->getMetaProduct());
28+
}
29+
1730
/**
1831
* @param Product $product
1932
* @param bool $force
@@ -23,7 +36,7 @@ trait CanBePaid
2336
public function pay(Product $product, bool $force = false): Transfer
2437
{
2538
if (!$product->canBuy($this, $force)) {
26-
throw new ProductEnded('The product is out of stock');
39+
throw new ProductEnded(trans('wallet::errors.product_stock'));
2740
}
2841

2942
if ($force) {

src/Traits/HasWallet.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Bavix\Wallet\Exceptions\AmountInvalid;
66
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
7+
use Bavix\Wallet\Exceptions\InsufficientFunds;
78
use Bavix\Wallet\Tax;
89
use Bavix\Wallet\Interfaces\Wallet;
910
use Bavix\Wallet\Models\Wallet as WalletModel;
@@ -46,8 +47,8 @@ trait HasWallet
4647
*/
4748
private function checkAmount(int $amount): void
4849
{
49-
if ($amount <= 0) {
50-
throw new AmountInvalid('The amount must be greater than zero');
50+
if ($amount < 0) {
51+
throw new AmountInvalid(trans('wallet::errors.price_positive'));
5152
}
5253
}
5354

@@ -92,8 +93,12 @@ public function deposit(int $amount, ?array $meta = null, bool $confirmed = true
9293
*/
9394
public function withdraw(int $amount, ?array $meta = null, bool $confirmed = true): Transaction
9495
{
96+
if (!$this->balance) {
97+
throw new BalanceIsEmpty(trans('wallet::errors.wallet_empty'));
98+
}
99+
95100
if (!$this->canWithdraw($amount)) {
96-
throw new BalanceIsEmpty('Balance insufficient for write-off');
101+
throw new InsufficientFunds(trans('wallet::errors.insufficient_funds'));
97102
}
98103

99104
return $this->forceWithdraw($amount, $meta, $confirmed);

src/WalletServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class WalletServiceProvider extends ServiceProvider
1515
*/
1616
public function boot(): void
1717
{
18+
$this->loadTranslationsFrom(
19+
\dirname(__DIR__) . '/resources/lang',
20+
'wallet'
21+
);
22+
1823
if (!$this->app->runningInConsole()) {
1924
return;
2025
}

tests/WalletFloatTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testWithdraw(): void
7171

7272
/**
7373
* @return void
74-
* @expectedException \Bavix\Wallet\Exceptions\AmountInvalid
74+
* @expectedException \Bavix\Wallet\Exceptions\BalanceIsEmpty
7575
*/
7676
public function testInvalidWithdraw(): void
7777
{

tests/WalletTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testWithdraw(): void
6666

6767
/**
6868
* @return void
69-
* @expectedException \Bavix\Wallet\Exceptions\AmountInvalid
69+
* @expectedException \Bavix\Wallet\Exceptions\BalanceIsEmpty
7070
*/
7171
public function testInvalidWithdraw(): void
7272
{

0 commit comments

Comments
 (0)