diff --git a/config/config.php b/config/config.php index c4ef3f09a..2451593e6 100644 --- a/config/config.php +++ b/config/config.php @@ -49,6 +49,7 @@ use Bavix\Wallet\Services\PrepareService; use Bavix\Wallet\Services\PurchaseService; use Bavix\Wallet\Services\RegulatorService; +use Bavix\Wallet\Services\TaxCollectionService; use Bavix\Wallet\Services\TaxService; use Bavix\Wallet\Services\TransactionService; use Bavix\Wallet\Services\TransferService; @@ -265,6 +266,8 @@ 'purchase' => PurchaseService::class, // Service for handling tax operations. 'tax' => TaxService::class, + // Service for handling tax collection operations. + 'tax_collection' => TaxCollectionService::class, // Service for handling transaction operations. 'transaction' => TransactionService::class, // Service for handling transfer operations. diff --git a/src/External/Enums/TransactionType.php b/src/External/Enums/TransactionType.php new file mode 100644 index 000000000..d59f31194 --- /dev/null +++ b/src/External/Enums/TransactionType.php @@ -0,0 +1,13 @@ +getTaxPercent($type); + } + + $feeMinimum = null; + $feeMaximum = null; + if ($wallet instanceof TaxConstraintsInterface) { + $feeMinimum = $wallet->getMinimumTax($type); + $feeMaximum = $wallet->getMaximumTax($type); + } + + $fee = 0; + if ($feePercent !== null) { + $fee = $this->mathService->floor( + $this->mathService->div( + $this->mathService->mul($amount, $feePercent, 0), + 100, + $this->castService->getWallet($wallet) + ->decimal_places + ) + ); + } + + if ($feeMinimum !== null && $this->mathService->compare($fee, $feeMinimum) === -1) { + $fee = $feeMinimum; + } + + if ($feeMaximum !== null && $this->mathService->compare($feeMaximum, $fee) === -1) { + $fee = $feeMaximum; + } + + return (string) $fee; + } +} diff --git a/src/Services/TaxCollectionServiceInterface.php b/src/Services/TaxCollectionServiceInterface.php new file mode 100644 index 000000000..fc7d17a58 --- /dev/null +++ b/src/Services/TaxCollectionServiceInterface.php @@ -0,0 +1,16 @@ +taxCollectionService->calculate(TransactionType::Withdraw, $wallet, $amount); + } + + // backward compatibility $fee = 0; if ($wallet instanceof Taxable) { $fee = $this->mathService->floor( diff --git a/src/WalletServiceProvider.php b/src/WalletServiceProvider.php index 364307b07..abce6f24c 100644 --- a/src/WalletServiceProvider.php +++ b/src/WalletServiceProvider.php @@ -102,6 +102,8 @@ use Bavix\Wallet\Services\PurchaseServiceInterface; use Bavix\Wallet\Services\RegulatorService; use Bavix\Wallet\Services\RegulatorServiceInterface; +use Bavix\Wallet\Services\TaxCollectionService; +use Bavix\Wallet\Services\TaxCollectionServiceInterface; use Bavix\Wallet\Services\TaxService; use Bavix\Wallet\Services\TaxServiceInterface; use Bavix\Wallet\Services\TransactionService; @@ -285,6 +287,10 @@ private function services(array $configure, array $cache): void $this->app->singleton(FormatterServiceInterface::class, $configure['formatter'] ?? FormatterService::class); $this->app->singleton(PrepareServiceInterface::class, $configure['prepare'] ?? PrepareService::class); $this->app->singleton(PurchaseServiceInterface::class, $configure['purchase'] ?? PurchaseService::class); + $this->app->singleton( + TaxCollectionServiceInterface::class, + $configure['tax_collection'] ?? TaxCollectionService::class + ); $this->app->singleton(TaxServiceInterface::class, $configure['tax'] ?? TaxService::class); $this->app->singleton( TransactionServiceInterface::class, @@ -479,6 +485,7 @@ private function servicesProviders(): array FormatterServiceInterface::class, PrepareServiceInterface::class, PurchaseServiceInterface::class, + TaxCollectionServiceInterface::class, TaxServiceInterface::class, TransactionServiceInterface::class, TransferServiceInterface::class,