Example #1
0
	/**
	 * Checks the VAT number using EU VIES checkVatApprox SOAP call
	 *
	 * @param  array                $params
	 * @param  cbpaidPaymentBasket  $paymentBasket
	 * @param  string               $vat_verification  OUTPUT: raw log of the verification for storage
	 * @return int                  1: Verification Passed, 0: not passed
	 */
	public function checkVatApprox( $params, $paymentBasket, &$vat_verification )
	{
		try {
			$client						=	new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl", array( 'exceptions' => true ) );
			/** @noinspection PhpUndefinedMethodInspection */
			$result						=	$client->checkVatApprox($params);
			$checked					=	true;
		}
		catch ( \SoapFault $e ) {
			$result						=	$e->getMessage();
			$checked					=	false;
		}

		/** @var StdClass $result */

		// log result
		// echo 'Params: '; var_export( $params ); echo "<br />\nResult: "; var_export( $result );

		if ( ( ! $checked ) || is_soap_fault( $result ) ) {
			// FaultString can take the following specific values:
			// - INVALID_INPUT: The provided Country Code is invalid or the VAT number is empty;  This is the only final error state.
			// - SERVICE_UNAVAILABLE: The EU VIES SOAP service is unavailable, try again later;
			// - MS_UNAVAILABLE: The Member State service is unavailable at this time, try again later: http://ec.europa.eu/taxation_customs/vies/viesspec.do
			// - TIMEOUT: The Member State service could not be reached in time, try again later;
			// - SERVER_BUSY: The service can't process your request. Try again latter.
			if ( ! $checked ) {
				$vat_verification		=	$this->encodeVatVerification( 'SOAPSERVERFAULT', null, $params, $result );
				$userMessage			=	CBPTXT::Th("EU VIES VAT number verification server unreachable. VAT number could not be checked. Proceed with VAT or try again later.");
			} elseif ( isset( $result->faultstring ) ) {
				$vat_verification		=	$this->encodeVatVerification( $result->faultstring, $result->faultcode, $params, $result );
				$userMessage			=	( $result->faultstring == 'INVALID_INPUT' ? CBPTXT::T("Invalid EU VAT Number. EU VAT numbers start with country code and must be valid.") : null );
			} else {
				$vat_verification		=	$this->encodeVatVerification( 'SOAPFAULT', null, $params, $result );
				$userMessage			=	null;
			}
			cbpaidApp::getBaseClass()->setLogErrorMSG( 5, $paymentBasket, sprintf( CBPTXT::T('EU VAT VIES error condition: "%s" for request on VAT: "%s%s", faultcode: "%"'), $result->faultstring, $params['countryCode'], $params['vatNumber'], $result->faultcode ), $userMessage );
			return 0;
		}

		if ( ! $result->valid ) {
			$vat_verification			=	$this->encodeVatVerification( 'INVALID', $params['countryCode'] . $params['vatNumber'], $params, $result );
			$userMessage				=	CBPTXT::T("Invalid EU VAT Number. EU VAT numbers start with country code and must be valid.");
			cbpaidApp::getBaseClass()->_setErrorMSG( $userMessage );
			return 0;
		} else {
			$matchesToCheck					=	array( 'traderNameMatch', /* 'traderCompanyTypeMatch', */ 'traderStreetMatch', 'traderPostcodeMatch', 'traderCityMatch' );
			foreach ( $matchesToCheck as $match ) {
				// 1=VALID, 2=INVALID:
				if ( isset( $result->$match ) && ( $result->$match == 2 ) ) {
					$vat_verification	=	$this->encodeVatVerification( 'MISMATCH', strtoupper( substr( $match, 6, -5 ) ), $params, $result );
					return 0;
				}
			}
			// requestIdentifier, requestDate, valid
			// countryCode vatNumber
			// traderName traderCompanyType traderAddress traderStreet traderPostcode traderCity

			$vat_verification			=	$this->encodeVatVerification( 'VALID', ( $result->requestIdentifier ? $result->requestIdentifier : '-' ) . ' / ' . $result->requestDate, $params, $result );
			return 1;
		}
	}