validateVat() public method

Validates a given country code and VAT number and returns a \DragonBe\Vies\CheckVatResponse object
public validateVat ( string $countryCode, string $vatNumber, string $requesterCountryCode = null, string $requesterVatNumber = null ) : CheckVatResponse
$countryCode string The two-character country code of a European member country
$vatNumber string The VAT number (without the country identification) of a registered company
$requesterCountryCode string The two-character country code of a European member country
$requesterVatNumber string The VAT number (without the country identification) of a registered company
return CheckVatResponse
Example #1
3
 /**
  * Test to see the country code is rejected if not existing in the EU
  *
  * @dataProvider badCountryCodeProvider
  * @covers \DragonBe\Vies\Vies::validateVat
  * @expectedException \DragonBe\Vies\ViesException
  * @param $code
  */
 public function testExceptionIsRaisedForNonEuropeanUnionCountryCodesRequester($code)
 {
     $vies = new Vies();
     $vies->validateVat('BE', '0123.456.749', $code, 'does not matter');
 }
 /**
  * @return JsonModel
  */
 public function checkVatAction()
 {
     $financialId = (int) $this->getEvent()->getRequest()->getPost()->get('financialId');
     /**
      * @var $financial Financial
      */
     $financial = $this->getOrganisationService()->findEntityById('financial', $financialId);
     if (is_null($financial->getVat()) && is_null($this->getEvent()->getRequest()->getPost()->get('vat'))) {
         return new JsonModel(['success' => 'error', 'result' => $this->translate("txt-vat-number-empty")]);
     }
     //Overrule the vat when a VAT number is sent via the URL
     if (!is_null($this->getEvent()->getRequest()->getPost()->get('vat'))) {
         $vat = $this->getEvent()->getRequest()->getPost()->get('vat');
     } else {
         $vat = $financial->getVat();
     }
     $vies = new Vies();
     if (false === $vies->getHeartBeat()->isAlive()) {
         return new JsonModel(['success' => 'error', 'result' => 'Service is not available at the moment, please try again later.']);
     } else {
         try {
             $result = $vies->validateVat($financial->getOrganisation()->getCountry()->getCd(), trim(str_replace($financial->getOrganisation()->getCountry()->getCd(), '', $vat)));
             if ($result->isValid()) {
                 //Update the financial
                 $financial->setVatStatus(Financial::VAT_STATUS_VALID);
                 $financial->setDateVat(new \DateTime());
                 $this->getOrganisationService()->updateEntity($financial);
                 return new JsonModel(['success' => 'success', 'result' => 'Valid', 'status' => Financial::VAT_STATUS_VALID]);
             } else {
                 //Update the financial
                 $financial->setVatStatus(Financial::VAT_STATUS_INVALID);
                 $financial->setDateVat(new \DateTime());
                 $this->getOrganisationService()->updateEntity($financial);
                 return new JsonModel(['success' => 'error', 'result' => 'Invalid', 'status' => Financial::VAT_STATUS_INVALID]);
             }
         } catch (\Exception $e) {
             return new JsonModel(['success' => 'error', 'result' => $e->getMessage(), 'status' => Financial::VAT_STATUS_UNDEFINED]);
         }
     }
 }
 /**
  * @return \Zend\Http\Response|ViewModel
  *
  * @throws \Doctrine\ORM\ORMException
  */
 public function financialAction()
 {
     $affiliationService = $this->getAffiliationService()->setAffiliationId($this->getEvent()->getRouteMatch()->getParam('id'));
     if ($affiliationService->isEmpty()) {
         return $this->notFoundAction();
     }
     /** @var ProjectService $projectService */
     $projectService = $this->getProjectService()->setProject($affiliationService->getAffiliation()->getProject());
     if ($projectService->isEmpty()) {
         return $this->notFoundAction();
     }
     /** @var OrganisationService $organisationService */
     $organisationService = $this->getOrganisationService()->setOrganisation($affiliationService->getAffiliation()->getOrganisation());
     if ($organisationService->isEmpty()) {
         return $this->notFoundAction();
     }
     $formData = ['preferredDelivery' => \Organisation\Entity\Financial::EMAIL_DELIVERY, 'omitContact' => \Organisation\Entity\Financial::OMIT_CONTACT];
     $branch = null;
     $financialAddress = null;
     $organisationFinancial = null;
     if (!is_null($affiliationService->getAffiliation()->getFinancial())) {
         //We have a financial organisation, so populate the form with this data
         $organisationService = $this->getOrganisationService()->setOrganisation($affiliationService->getAffiliation()->getFinancial()->getOrganisation());
         $organisationFinancial = $affiliationService->getAffiliation()->getFinancial()->getOrganisation()->getFinancial();
         $branch = $affiliationService->getAffiliation()->getFinancial()->getBranch();
         $formData['attention'] = $affiliationService->getAffiliation()->getFinancial()->getContact()->getDisplayName();
         /** @var ContactService $contactService */
         $contactService = $this->getContactService()->setContact($affiliationService->getAffiliation()->getFinancial()->getContact());
         $formData['contact'] = $affiliationService->getAffiliation()->getFinancial()->getContact()->getId();
         if (!is_null($financialAddress = $contactService->getFinancialAddress())) {
             $financialAddress = $contactService->getFinancialAddress()->getAddress();
             $formData['address'] = $financialAddress->getAddress();
             $formData['zipCode'] = $financialAddress->getZipCode();
             $formData['city'] = $financialAddress->getCity();
             $formData['country'] = $financialAddress->getCountry()->getId();
         }
     }
     $formData['organisation'] = $organisationService->parseOrganisationWithBranch($branch);
     $formData['registeredCountry'] = $organisationService->getOrganisation()->getCountry()->getId();
     if (!is_null($organisationFinancial)) {
         $organisationFinancial = $affiliationService->getAffiliation()->getOrganisation()->getFinancial();
         $formData['preferredDelivery'] = $organisationFinancial->getEmail();
         $formData['vat'] = $organisationFinancial->getVat();
         $formData['omitContact'] = $organisationFinancial->getOmitContact();
     }
     $form = new FinancialForm($affiliationService, $this->getGeneralService());
     $form->setData($formData);
     if ($this->getRequest()->isPost() && $form->setData($_POST) && $form->isValid()) {
         $formData = $form->getData();
         //We need to find the organisation, first by tring the VAT, then via the name and country and then just create it
         $organisation = null;
         //Check if an organisation with the given VAT is already found
         $organisationFinancial = $this->getOrganisationService()->findFinancialOrganisationWithVAT($formData['vat']);
         //If the organisation is found, it has by default an organiation
         if (!is_null($organisationFinancial)) {
             $organisation = $organisationFinancial->getOrganisation();
         }
         //try to find the organisation based on te country and name
         if (is_null($organisation)) {
             $organisation = $this->getOrganisationService()->findOrganisationByNameCountry(trim($formData['organisation']), $this->getGeneralService()->findEntityById('Country', $formData['country']));
         }
         /**
          * If the organisation is still not found, create it
          */
         if (is_null($organisation)) {
             $organisation = new Organisation();
             $organisation->setOrganisation($formData['organisation']);
             $organisation->setCountry($this->getGeneralService()->findEntityById('Country', $formData['country']));
             /**
              * @var $organisationType Type
              */
             $organisationType = $this->getOrganisationService()->getEntityManager()->getReference('Organisation\\Entity\\Type', 0);
             $organisation->setType($organisationType);
         }
         /**
          *
          * Update the affiliationFinancial
          */
         $affiliationFinancial = $this->getAffiliationService()->getAffiliation()->getFinancial();
         if (is_null($affiliationFinancial)) {
             $affiliationFinancial = new Financial();
             $affiliationFinancial->setAffiliation($this->getAffiliationService()->getAffiliation());
         }
         $affiliationFinancial->setContact($this->getContactService()->setContactId($formData['contact'])->getContact());
         $affiliationFinancial->setOrganisation($organisation);
         $affiliationFinancial->setBranch(trim(substr($formData['organisation'], strlen($organisation->getOrganisation()))));
         $this->getAffiliationService()->updateEntity($affiliationFinancial);
         if (!is_null($affiliationService->getAffiliation()->getFinancial())) {
             $organisationFinancial = $affiliationService->getAffiliation()->getFinancial()->getOrganisation()->getFinancial();
         } else {
             $organisationFinancial = $affiliationService->getAffiliation()->getOrganisation()->getFinancial();
         }
         if (is_null($organisationFinancial)) {
             $organisationFinancial = new \Organisation\Entity\Financial();
         }
         $organisationFinancial->setOrganisation($organisation);
         /**
          * The presence of a VAT number triggers the creation of a financial organisation
          */
         if (!empty($formData['vat'])) {
             $organisationFinancial->setVat($formData['vat']);
             //Do an in-situ vat check
             $vies = new Vies();
             try {
                 $result = $vies->validateVat($organisationFinancial->getOrganisation()->getCountry()->getCd(), trim(str_replace($organisationFinancial->getOrganisation()->getCountry()->getCd(), '', $formData['vat'])));
                 if ($result->isValid()) {
                     $this->flashMessenger()->setNamespace('success')->addMessage(sprintf($this->translate("txt-vat-number-is-valid"), $affiliationService->getAffiliation()));
                     //Update the financial
                     $organisationFinancial->setVatStatus(\Organisation\Entity\Financial::VAT_STATUS_VALID);
                     $organisationFinancial->setDateVat(new \DateTime());
                 } else {
                     //Update the financial
                     $organisationFinancial->setVatStatus(\Organisation\Entity\Financial::VAT_STATUS_INVALID);
                     $organisationFinancial->setDateVat(new \DateTime());
                     $this->flashMessenger()->setNamespace('error')->addMessage(sprintf($this->translate("txt-vat-number-is-invalid"), $affiliationService->getAffiliation()));
                 }
             } catch (\Exception $e) {
                 $this->flashMessenger()->setNamespace('danger')->addMessage(sprintf($this->translate("txt-vat-information-could-not-be-verified"), $affiliationService->getAffiliation()));
             }
         } else {
             $organisationFinancial->setVat(null);
         }
         $organisationFinancial->setEmail($formData['preferredDelivery']);
         $organisationFinancial->setOmitContact($formData['omitContact']);
         $this->getOrganisationService()->updateEntity($organisationFinancial);
         /*
          * save the financial address
          */
         $contactService = clone $this->getContactService()->setContact($affiliationFinancial->getContact());
         if (!is_null($contactService->getFinancialAddress())) {
             $financialAddress = $contactService->getFinancialAddress()->getAddress();
         } else {
             $financialAddress = new Address();
             $financialAddress->setContact($affiliationService->getAffiliation()->getFinancial()->getContact());
             /**
              * @var $addressType AddressType
              */
             $addressType = $this->getContactService()->getEntityManager()->getReference('Contact\\Entity\\AddressType', AddressType::ADDRESS_TYPE_FINANCIAL);
             $financialAddress->setType($addressType);
         }
         $financialAddress->setAddress($formData['address']);
         $financialAddress->setZipCode($formData['zipCode']);
         $financialAddress->setCity($formData['city']);
         /**
          * @var Country $country
          */
         $country = $this->getContactService()->getEntityManager()->getReference('General\\Entity\\Country', $formData['country']);
         $financialAddress->setCountry($country);
         $this->getContactService()->updateEntity($financialAddress);
         $this->flashMessenger()->setNamespace('success')->addMessage(sprintf($this->translate("txt-affiliation-%s-has-successfully-been-updated"), $affiliationService->getAffiliation()));
         return $this->redirect()->toRoute('community/affiliation/affiliation', ['id' => $affiliationService->getAffiliation()->getId()], ['fragment' => 'invoicing']);
     }
     return new ViewModel(['affiliationService' => $affiliationService, 'projectService' => $projectService, 'form' => $form]);
 }
Example #4
0
 /**
  * Test exception ViesServiceException is thrown after SoapFault exception
  *
  * @dataProvider vatNumberProvider
  * @expectedException \DragonBe\Vies\ViesServiceException
  * @param $vat
  */
 public function testExceptionIsRaisedSoapFault($vat)
 {
     $soapFault = new \SoapFault("test", "myMessage");
     $stub = $this->getMockFromWsdl(dirname(__FILE__) . '/_files/checkVatService.wsdl');
     $stub->expects($this->any())->method('__soapCall')->will($this->throwException($soapFault));
     $vies = new Vies();
     $vies->setSoapClient($stub);
     $vies->validateVat('BE', $vat);
 }
Example #5
0
<?php

use DragonBe\Vies\Vies;
use DragonBe\Vies\ViesException;
use DragonBe\Vies\ViesServiceException;
require_once dirname(__DIR__) . '/vendor/autoload.php';
$vies = new Vies();
if (false === $vies->getHeartBeat()->isAlive()) {
    echo 'Back-end VIES service is not available at the moment, please try again later.' . PHP_EOL;
} else {
    $vatNumberProvider = [['BE' => '0811231190', 'HR' => '20649144807'], ['BE' => '1234567890', 'ES' => '9999999999'], ['AA' => '1234567890', 'NO' => '1234567890']];
    foreach ($vatNumberProvider as $vatNumbers) {
        foreach ($vatNumbers as $countryCode => $vatNumber) {
            echo 'Validating ' . $countryCode . $vatNumber . '... ';
            try {
                $result = $vies->validateVat($countryCode, $vatNumber);
                // - Validation routine worked as expected.
                echo $result->isValid() ? 'Valid' : 'Invalid';
                //
            } catch (ViesServiceException $e) {
                // - Recoverable exception.
                echo $e->getMessage();
                //   There is probably a temporary problem with back-end VIES service.
            } catch (ViesException $e) {
                // - Unrecoverable exception.
                echo $e->getMessage();
                //   Invalid country code etc.
            }
            echo PHP_EOL;
        }
    }