HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/hi.naniguide.com/app/Services/Billing/StripeTaxRateService.php
<?php

namespace App\Services\Billing;

use App\Models\Setting;
use Stripe\StripeClient;

class StripeTaxRateService
{
    public function __construct(private readonly StripeClient $stripe)
    {
    }

    public function getOrCreateTaxRateId(array $taxRule): ?string
    {
        $type = (string) ($taxRule['type'] ?? '');
        $percentage = $taxRule['percentage'] ?? null;

        if ($type === '' || !is_numeric($percentage)) {
            return null;
        }

        $percentage = (float) $percentage;
        if ($percentage <= 0) {
            return null;
        }

        $country = isset($taxRule['country']) ? strtoupper(trim((string) $taxRule['country'])) : '';
        $state = isset($taxRule['state']) ? strtoupper(trim((string) $taxRule['state'])) : '';

        $cacheKey = $type . ':' . ($country !== '' ? $country : 'XX') . ':' . ($state !== '' ? $state : '-') . ':' . number_format($percentage, 4, '.', '');

        $cache = Setting::get('stripe_tax_rate_cache', []);
        if (!is_array($cache)) {
            $cache = [];
        }

        $existing = $cache[$cacheKey] ?? null;
        if (is_string($existing) && trim($existing) !== '') {
            return $existing;
        }

        $displayName = (string) ($taxRule['display_name'] ?? 'Tax');
        $inclusive = (bool) ($taxRule['inclusive'] ?? false);

        $payload = [
            'display_name' => $displayName,
            'inclusive' => $inclusive,
            'percentage' => $percentage,
        ];

        if ($country !== '' && strlen($country) === 2) {
            $payload['country'] = $country;
        }

        if ($country === 'US' && $state !== '') {
            $payload['state'] = $state;
        }

        $taxRate = $this->stripe->taxRates->create($payload);

        $cache[$cacheKey] = $taxRate->id;
        Setting::set('stripe_tax_rate_cache', $cache, 'tax', 'array');

        return $taxRate->id;
    }
}