Skip to content

Commit c687871

Browse files
committed
add MinimalTaxable
1 parent a3a0654 commit c687871

File tree

6 files changed

+146
-2
lines changed

6 files changed

+146
-2
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Add confirm method's
1111
- Add method `hasWallet`
1212
- Add currency service (create usd, eur,...)
13+
- Add `MinimalTaxable`
1314

1415
## [3.0.4] - 2019-07-22
1516
### Fixed

src/Interfaces/MinimalTaxable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Interfaces;
4+
5+
interface MinimalTaxable extends Taxable
6+
{
7+
/**
8+
* @return int
9+
*/
10+
public function getMinimalFee(): int;
11+
}

src/Services/WalletService.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Bavix\Wallet\Services;
44

55
use Bavix\Wallet\Exceptions\AmountInvalid;
6+
use Bavix\Wallet\Interfaces\MinimalTaxable;
67
use Bavix\Wallet\Interfaces\Taxable;
78
use Bavix\Wallet\Interfaces\Wallet;
89
use Bavix\Wallet\Models\Wallet as WalletModel;
@@ -21,11 +22,25 @@ class WalletService
2122
*/
2223
public function fee(Wallet $wallet, int $amount): int
2324
{
25+
$result = 0;
26+
2427
if ($wallet instanceof Taxable) {
25-
return (int) ($amount * $wallet->getFeePercent() / 100);
28+
$result = (int) ($amount * $wallet->getFeePercent() / 100);
29+
30+
/**
31+
* Added minimum commission condition
32+
*
33+
* @see https://github.com/bavix/laravel-wallet/issues/64#issuecomment-514483143
34+
*/
35+
if ($wallet instanceof MinimalTaxable) {
36+
$minimal = $wallet->getMinimalFee();
37+
if ($result < $minimal) {
38+
$result = $wallet->getMinimalFee();
39+
}
40+
}
2641
}
2742

28-
return 0;
43+
return $result;
2944
}
3045

3146
/**

tests/MinTaxTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test;
4+
5+
use Bavix\Wallet\Models\Transaction;
6+
use Bavix\Wallet\Test\Models\Buyer;
7+
use Bavix\Wallet\Test\Models\ItemMinTax;
8+
9+
class MinTaxTest extends TestCase
10+
{
11+
12+
/**
13+
* @return void
14+
*/
15+
public function testPay(): void
16+
{
17+
/**
18+
* @var Buyer $buyer
19+
* @var ItemMinTax $product
20+
*/
21+
$buyer = factory(Buyer::class)->create();
22+
$product = factory(ItemMinTax::class)->create([
23+
'quantity' => 1,
24+
]);
25+
26+
$fee = (int)($product->price * $product->getFeePercent() / 100);
27+
if ($fee < $product->getMinimalFee()) {
28+
$fee = $product->getMinimalFee();
29+
}
30+
31+
$balance = $product->price + $fee;
32+
33+
$this->assertEquals($buyer->balance, 0);
34+
$buyer->deposit($balance);
35+
36+
$this->assertNotEquals($buyer->balance, 0);
37+
$transfer = $buyer->pay($product);
38+
$this->assertNotNull($transfer);
39+
40+
/**
41+
* @var Transaction $withdraw
42+
* @var Transaction $deposit
43+
*/
44+
$withdraw = $transfer->withdraw;
45+
$deposit = $transfer->deposit;
46+
47+
$this->assertEquals($withdraw->amount, -$balance);
48+
$this->assertEquals($deposit->amount, $product->getAmountProduct());
49+
$this->assertNotEquals($deposit->amount, $withdraw->amount);
50+
$this->assertEquals($transfer->fee, $fee);
51+
52+
$buyer->refund($product);
53+
$this->assertEquals($buyer->balance, $deposit->amount);
54+
$this->assertEquals($product->balance, 0);
55+
56+
$buyer->withdraw($buyer->balance);
57+
$this->assertEquals($buyer->balance, 0);
58+
}
59+
60+
}

tests/Models/ItemMinTax.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test\Models;
4+
5+
use Bavix\Wallet\Interfaces\MinimalTaxable;
6+
7+
class ItemMinTax extends Item implements MinimalTaxable
8+
{
9+
10+
/**
11+
* @inheritDoc
12+
*/
13+
public function getTable(): string
14+
{
15+
return 'items';
16+
}
17+
18+
/**
19+
* @inheritDoc
20+
*/
21+
public function getFeePercent(): float
22+
{
23+
return 3;
24+
}
25+
26+
/**
27+
* @return int
28+
*/
29+
public function getMinimalFee(): int
30+
{
31+
return 90;
32+
}
33+
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Bavix\Wallet\Test\Models\ItemMinTax;
4+
use Faker\Generator as Faker;
5+
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Model Factories
9+
|--------------------------------------------------------------------------
10+
|
11+
| This directory should contain each of the model factory definitions for
12+
| your application. Factories provide a convenient way to generate new
13+
| model instances for testing / seeding your application's database.
14+
|
15+
*/
16+
17+
$factory->define(ItemMinTax::class, function (Faker $faker) {
18+
return [
19+
'name' => $faker->domainName,
20+
'price' => random_int(1, 100),
21+
'quantity' => random_int(0, 10),
22+
];
23+
});

0 commit comments

Comments
 (0)