diff --git a/config/config.php b/config/config.php index c4ef3f09..2451593e 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/Services/TaxCollectionService.php b/src/Services/TaxCollectionService.php index 6a5b4f1f..eed2ce98 100644 --- a/src/Services/TaxCollectionService.php +++ b/src/Services/TaxCollectionService.php @@ -10,6 +10,9 @@ use Bavix\Wallet\Interfaces\Wallet; use Bavix\Wallet\Internal\Service\MathServiceInterface; +/** + * @internal + */ final readonly class TaxCollectionService implements TaxCollectionServiceInterface { public function __construct( diff --git a/src/Services/TaxCollectionServiceInterface.php b/src/Services/TaxCollectionServiceInterface.php index 1b9e65ef..fc7d17a5 100644 --- a/src/Services/TaxCollectionServiceInterface.php +++ b/src/Services/TaxCollectionServiceInterface.php @@ -7,6 +7,9 @@ use Bavix\Wallet\External\Enums\TransactionType; use Bavix\Wallet\Interfaces\Wallet; +/** + * @api + */ interface TaxCollectionServiceInterface { public function calculate(TransactionType $type, Wallet $wallet, float|int|string $amount): string; diff --git a/src/Services/TaxService.php b/src/Services/TaxService.php index adcb3175..340ae192 100644 --- a/src/Services/TaxService.php +++ b/src/Services/TaxService.php @@ -4,25 +4,39 @@ namespace Bavix\Wallet\Services; +use Bavix\Wallet\External\Enums\TransactionType; use Bavix\Wallet\Interfaces\MaximalTaxable; use Bavix\Wallet\Interfaces\MinimalTaxable; use Bavix\Wallet\Interfaces\Taxable; +use Bavix\Wallet\Interfaces\TaxInterface; use Bavix\Wallet\Interfaces\Wallet; use Bavix\Wallet\Internal\Service\MathServiceInterface; /** * @internal + * @deprecated use TaxCollectionServiceInterface instead. + * @see TaxCollectionServiceInterface */ final readonly class TaxService implements TaxServiceInterface { public function __construct( private MathServiceInterface $mathService, - private CastServiceInterface $castService + private CastServiceInterface $castService, + private TaxCollectionServiceInterface $taxCollectionService ) { } public function getFee(Wallet $wallet, float|int|string $amount): string { + if ($wallet instanceof TaxInterface) { + return $this->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 364307b0..38122b4e 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; @@ -121,6 +123,7 @@ use Illuminate\Database\Events\TransactionRolledBack; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider; +use Laravel\Cashier\Tax; final class WalletServiceProvider extends ServiceProvider implements DeferrableProvider { @@ -285,6 +288,7 @@ 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 +483,7 @@ private function servicesProviders(): array FormatterServiceInterface::class, PrepareServiceInterface::class, PurchaseServiceInterface::class, + TaxCollectionServiceInterface::class, TaxServiceInterface::class, TransactionServiceInterface::class, TransferServiceInterface::class,