/** * @param Http\Request $request * @return Http\Response * * @throws Http\BadResponseException */ public function process(Http\Request $request) { $request = clone $request; $cacheKey = implode('.', [$request->getMethod(), $request->getUrl(), $request->getHeader('Accept'), $request->getHeader('Accept-Encoding'), $request->getHeader('Authorization')]); if ($cached = $this->cache->load($cacheKey)) { if ($this->greedyCaching) { $cached = clone $cached; $this->onResponse && call_user_func($this->onResponse, $cached); return $cached; } /** @var $cached Http\Response */ if ($cached->hasHeader('Last-Modified')) { $request->addHeader('If-Modified-Since', $cached->getHeader('Last-Modified')); } elseif ($cached->hasHeader('ETag')) { $request->addHeader('If-None-Match', $cached->getHeader('ETag')); } } $response = $this->client->process($request); if ($this->isCacheable($response) || $this->greedyCaching) { $this->cache->save($cacheKey, clone $response); } if (isset($cached) && $response->getCode() === Http\Response::S304_NOT_MODIFIED) { $cached = clone $cached; /** @todo Should be responses somehow combined into one? */ $response = $cached->setPrevious($response); } $this->onResponse && call_user_func($this->onResponse, $response); return $response; }
/** * @param array $data * @return Geocoded * @throws GeocodingFailedException * @throws NoExactResultException */ private function execute(array $data) { try { $headers = ['Accept' => 'application/json', 'Content-Type' => 'application/json']; $request = new Request(Request::POST, self::ENDPOINT_URL, $headers, json_encode($data)); $response = $this->httpClient->process($request); if ($response->getCode() !== Response::S200_OK) { throw new GeocodingFailedException('Unexpected HTTP code from Smartform API.'); } $body = $response->getBody(); $json = $this->parseBody($body); } catch (BadResponseException $e) { throw new GeocodingFailedException('HTTP request to Smartform API failed.', NULL, $e); } if ($json->resultCode === self::RESULT_FAIL) { throw new GeocodingFailedException("Smartform geocoding failed. {$json->errorMessage}"); } if ($json->result->type === self::PRECISION_EXACT) { $address = $json->result->addresses[0]; $lat = $address->coordinates ? $address->coordinates->gpsLat : NULL; $lng = $address->coordinates ? $address->coordinates->gpsLng : NULL; $street = $address->values->FIRST_LINE; $city = $address->values->SECOND_LINE; $postalCode = $address->values->ZIP; $stateCode = $address->values->COUNTRY_CODE; return new Geocoded($lat, $lng, $city, $street, $postalCode, $stateCode); } else { $ex = new NoExactResultException('No exact address found!'); if ($json->result->hint) { $ex->setHint($json->result->hint); } throw $ex; } }
/** * @param string $url * @param boolean $retrying * @return Geocoded * @throws GeocodingFailedException * @throws QuotaLimitException * @throws NoExactResultException */ private function execute($url, $retrying = FALSE) { try { $request = new Request(Request::GET, $url); $response = $this->httpClient->process($request); if ($response->getCode() !== Response::S200_OK) { throw new GeocodingFailedException('Unexpected HTTP code from Google geocoding API.'); } $body = $response->getBody(); $json = $this->parseBody($body); } catch (BadResponseException $e) { throw new GeocodingFailedException('HTTP request to Google geocoding API failed.', NULL, $e); } if ($json->status === self::ERR_LIMIT_REACHED) { throw new QuotaLimitException('Google geocoding API quota limit exceeded.'); } if ($json->status === self::ERR_NO_RESULTS) { throw new NoExactResultException('Given address was not found at all!'); } if ($json->status === self::ERR_SERVER_ERROR) { if ($retrying) { throw new GeocodingFailedException('Repeated server error received from Geocoding API!'); } else { $this->executeQuery($url, $retrying = TRUE); } } // process only first result $result = $json->results[0]; if ($result->geometry->location_type !== self::PRECISION_EXACT) { throw new NoExactResultException('Exact address was not found!'); } $data = []; foreach ($result->address_components as $component) { foreach ($component->types as $type) { $data = $this->updateAddressComponent($data, $type, $component); } } $lat = $result->geometry->location->lat; $lng = $result->geometry->location->lng; $city = isset($data['city']) ? $data['city'] : NULL; $postalCode = isset($data['postalCode']) ? str_replace(' ', '', $data['postalCode']) : NULL; $stateCode = isset($data['stateCode']) ? $data['stateCode'] : NULL; if (isset($data['street'])) { $street = $data['street']; } else { $street = $city; } if (isset($data['premise'])) { $street .= ' ' . $data['premise']; if (isset($data['streetNumber'])) { $street .= '/' . $data['streetNumber']; } } elseif (isset($data['streetNumber'])) { $street .= ' ' . $data['streetNumber']; } return new Geocoded($lat, $lng, $city, $street, $postalCode, $stateCode); }
/** * Find company by identification given * * @param string $identification (8 digit string) * @return null|Company NULL if not found * @throws LookupFailedException */ public function find($identification) { if (!preg_match('~^[0-9]{8}$~', $identification)) { return NULL; } try { $request = new Request(Request::GET, self::ENDPOINT . $identification); $response = $this->httpClient->process($request); if ($response->getCode() !== Response::S200_OK) { throw new LookupFailedException('Unexpected HTTP code from ARES API.'); } $xmlDom = $this->xmlLoader->loadXml($response->getBody()); $xml = simplexml_import_dom($xmlDom); } catch (XmlException $e) { throw new LookupFailedException('Invalid XML from ARES', NULL, $e); } catch (BadResponseException $e) { throw new LookupFailedException('HTTP request to ARES failed', NULL, $e); } $ns = $xml->getDocNamespaces(); $data = $xml->children($ns['are'])->Odpoved->children($ns['dtt']); if (!$data->Pocet_zaznamu || (int) $data->Pocet_zaznamu === 0) { return NULL; } $companyData = $data->Vypis_basic; $address = $companyData->Adresa_ARES; $companyname = (string) $companyData->Obchodni_firma; $vatNumber = (string) $companyData->DIC; $postalCode = (string) $address->PSC; $city = (string) $address->Nazev_obce; $cityPart = (string) $address->Nazev_casti_obce; $cityDistrict = (string) $address->Nazev_mestske_casti; $street = (string) $address->Nazev_ulice; $houseNum = (string) $address->Cislo_domovni; $orientNum = (string) $address->Cislo_orientacni; $addrNum = (string) $address->Cislo_do_adresy; if (!empty($houseNum)) { $houseNumber = $houseNum; if (!empty($orientNum)) { $houseNumber .= '/' . $orientNum; } } elseif (!empty($addrNum)) { $houseNumber = $addrNum; } else { $houseNumber = ''; } if ($vatNumber === 'Skupinove_DPH') { $vatNumber = ''; } return new Company($identification, $companyname, $vatNumber, $city, $cityPart, $cityDistrict, $street, $houseNumber, $postalCode); }
/** * This is expected to be called when push notification from PayU is received on URL they call UrlOnline * * @param array $post POST data in HTTP request from PayU * @return PaymentStatus * @throws InvalidRequestException * @throws InvalidSignatureException * @throws RequestFailedException */ public function getPaymentStatus(array $post) { $this->checkReceivedPostData($post); $request = $this->buildStatusRequest($post); try { $response = $this->httpClient->process($request); if ($response->getCode() !== Response::S200_OK) { throw new RequestFailedException('Unexpected HTTP code from PayU'); } $xmlData = $response->getBody(); $xmlDom = $this->xmlLoader->loadXml($xmlData); $xml = simplexml_import_dom($xmlDom); if ((string) $xml->status !== 'OK') { throw new RequestFailedException('Unexpected response from PayU'); } $xmlSignature = md5($this->posId . $xml->trans->session_id . $xml->trans->order_id . $xml->trans->status . $xml->trans->amount . $xml->trans->desc . $xml->trans->ts . $this->key2); if ($xmlSignature !== (string) $xml->trans->sig) { throw new InvalidSignatureException('Signature in XML from PayU is corrupted'); } return new PaymentStatus((string) $xml->trans->order_id, (int) $xml->trans->status, (string) $xml->trans->create ? new DateTime($xml->trans->create) : NULL, (string) $xml->trans->init ? new DateTime($xml->trans->init) : NULL, (string) $xml->trans->sent ? new DateTime($xml->trans->sent) : NULL, (string) $xml->trans->recv ? new DateTime($xml->trans->recv) : NULL, (string) $xml->trans->cancel ? new DateTime($xml->trans->cancel) : NULL); } catch (BadResponseException $ex) { throw new RequestFailedException('HTTP Request to PayU failed', NULL, $ex); } catch (XmlException $ex) { throw new RequestFailedException('Invalid XML recieved from PayU', NULL, $ex); } }