From b24be423e889370bd28d0bd767143f221235fadd Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 19 Mar 2023 15:09:50 +1100 Subject: [PATCH] Global Tax Module --- app/Services/Tax/ProcessRule.php | 24 +++++++++ app/Services/Tax/TaxService.php | 22 ++++++++ app/Services/Tax/VatNumberCheck.php | 52 ++++++++++++++++++ tests/Unit/VatNumberTest.php | 82 +++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 app/Services/Tax/ProcessRule.php create mode 100644 app/Services/Tax/TaxService.php create mode 100644 app/Services/Tax/VatNumberCheck.php create mode 100644 tests/Unit/VatNumberTest.php diff --git a/app/Services/Tax/ProcessRule.php b/app/Services/Tax/ProcessRule.php new file mode 100644 index 000000000000..42054872fa0f --- /dev/null +++ b/app/Services/Tax/ProcessRule.php @@ -0,0 +1,24 @@ +checkvat_number(); + } + + private function checkvat_number(): array + { + $wsdl = "https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl"; + try { + $client = new \SoapClient($wsdl); + $params = [ + 'countryCode' => $this->country_code, + 'vatNumber' => $this->vat_number + ]; + $response = $client->checkVat($params); + + if ($response->valid) { + return [ + 'valid' => true, + 'name' => $response->name, + 'address' => $response->address + ]; + } else { + return ['valid' => false]; + } + } catch (\SoapFault $e) { + // Handle error, e.g., log or display an error message + return ['error' => $e->getMessage()]; + } + } + +} diff --git a/tests/Unit/VatNumberTest.php b/tests/Unit/VatNumberTest.php new file mode 100644 index 000000000000..326eea1d2244 --- /dev/null +++ b/tests/Unit/VatNumberTest.php @@ -0,0 +1,82 @@ +run(); + + if (isset($result['valid'])) { + if ($result['valid']) { + echo "The VAT number is valid.\n"; + echo "Name: " . $result['name'] . "\n"; + echo "Address: " . $result['address'] . "\n"; + } else { + echo "The VAT number is invalid.\n"; + } + } else { + echo "Error: " . $result['error'] . "\n"; + } + + $this->assertFalse($result['valid']); + } + + private function testValidVatNumber() + { + // Usage example + $country_code = "AT"; // Ireland + $vat_number = "U12345678"; // Example VAT number + $result = ''; + + $vat_checker = new VatNumberCheck($vat_number, $country_code); + $result = $vat_checker->run(); + + if (isset($result['valid'])) { + if ($result['valid']) { + echo "The VAT number is valid.\n"; + echo "Name: " . $result['name'] . "\n"; + echo "Address: " . $result['address'] . "\n"; + } else { + echo "The VAT number is invalid.\n"; + } + } else { + echo "Error: " . $result['error'] . "\n"; + } + + $this->assertFalse($result['valid']); + + } + +} +