/**
  * Downloads public key
  *
  * @return string
  *
  * @throws Paysera_WalletApi_Exception_CallbackException
  */
 protected function getPublicKey()
 {
     try {
         return $this->webClient->makeRequest(new Paysera_WalletApi_Http_Request($this->publicKeyUri))->getContent();
     } catch (Paysera_WalletApi_Exception_HttpException $exception) {
         throw new Paysera_WalletApi_Exception_CallbackException('Cannot get public key from Wallet server', 0, $exception);
     }
 }
 public function testDoubleInvalidResponse()
 {
     $this->webClient->expects($this->exactly(2))->method('makeRequest')->with(new Paysera_WalletApi_Http_Request(''))->will($this->returnValue(new Paysera_WalletApi_Http_Response(502, array(), '<html>
                 <head><title>502 Bad Gateway</title></head>
                 <body bgcolor="white">
                 <center><h1>502 Bad Gateway</h1></center>
                 <hr><center>nginx/1.0.15</center>
                 </body>
             </html>
             ')));
     $this->setExpectedException('Paysera_WalletApi_Exception_ResponseException');
     $this->service->makeRequest(new Paysera_WalletApi_Http_Request(''));
 }
 /**
  * Makes specified request.
  * URI in request object can be relative to current context (without endpoint and API path).
  * Content of request is not encoded or otherwise modified by the client
  *
  * @param Paysera_WalletApi_Http_Request $request
  * @param array                          $options
  *
  * @throws Paysera_WalletApi_Exception_ApiException
  * @return mixed|null
  */
 public function makeRequest(Paysera_WalletApi_Http_Request $request, $options = array())
 {
     $originalRequest = clone $request;
     $event = new Paysera_WalletApi_Event_RequestEvent($request, $options);
     $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::BEFORE_REQUEST, $event);
     $options = $event->getOptions();
     try {
         $response = $this->webClient->makeRequest($request);
     } catch (Paysera_WalletApi_Exception_HttpException $exception) {
         $event = new Paysera_WalletApi_Event_HttpExceptionEvent($exception, $request, $options);
         $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::ON_HTTP_EXCEPTION, $event);
         if ($event->getResponse() !== null) {
             $response = $event->getResponse();
         } else {
             throw $event->getException();
         }
     }
     $response->setRequest($request);
     $event = new Paysera_WalletApi_Event_ResponseEvent($response, $options);
     $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::AFTER_RESPONSE, $event);
     $options = $event->getOptions();
     try {
         $responseContent = $response->getContent();
         $result = $responseContent !== '' ? json_decode($responseContent, true) : null;
         if ($response->getStatusCode() === 200 && $responseContent === '' || $result === null && $responseContent !== '' && $responseContent !== 'null') {
             throw new Paysera_WalletApi_Exception_ResponseException(array('error' => 'internal_server_error', 'error_description' => sprintf('Bad response from server! Response: %s', $responseContent)), $response->getStatusCode(), $response->getStatusCodeMessage());
         }
         if ($response->isSuccessful()) {
             return $result;
         } else {
             throw new Paysera_WalletApi_Exception_ResponseException(is_array($result) ? $result : array(), $response->getStatusCode(), $response->getStatusCodeMessage());
         }
     } catch (Paysera_WalletApi_Exception_ResponseException $exception) {
         $event = new Paysera_WalletApi_Event_ResponseExceptionEvent($exception, $response, $options);
         $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::ON_RESPONSE_EXCEPTION, $event);
         if ($event->getResult() !== null) {
             return $event->getResult();
         } elseif ($event->isRepeatRequest()) {
             return $this->makeRequest($originalRequest, $event->getOptions());
         } else {
             throw $event->getException();
         }
     }
 }
 /**
  * Makes specified request with options reference.
  * URI in request object can be relative to current context (without endpoint and API path).
  * Content of request is not encoded or otherwise modified by the client
  *
  * @param Paysera_WalletApi_Http_Request $request
  * @param array                          $options
  *
  * @return Paysera_WalletApi_Http_Response
  *
  * @throws Paysera_WalletApi_Exception_ResponseException
  */
 private function makePlainRequestWithReference(Paysera_WalletApi_Http_Request $request, &$options = array())
 {
     $event = new Paysera_WalletApi_Event_RequestEvent($request, $options);
     $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::BEFORE_REQUEST, $event);
     $options = $event->getOptions();
     try {
         $response = $this->webClient->makeRequest($request);
     } catch (Paysera_WalletApi_Exception_HttpException $exception) {
         $event = new Paysera_WalletApi_Event_HttpExceptionEvent($exception, $request, $options);
         $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::ON_HTTP_EXCEPTION, $event);
         if ($event->getResponse() !== null) {
             $response = $event->getResponse();
         } else {
             throw $event->getException();
         }
     }
     $response->setRequest($request);
     $event = new Paysera_WalletApi_Event_ResponseEvent($response, $options);
     $this->eventDispatcher->dispatch(Paysera_WalletApi_Events::AFTER_RESPONSE, $event);
     return $response;
 }
 /**
  *
  * @param string $data
  * @param string $sign
  * @param string $publicKey
  *
  * @dataProvider invalidDataProvider
  */
 public function testCheckSignWithInvalid($data, $sign, $publicKey)
 {
     $this->webClient->expects($this->once())->method('makeRequest')->with(new Paysera_WalletApi_Http_Request('http://publickey.abc'))->will($this->returnValue(new Paysera_WalletApi_Http_Response(200, array(), $publicKey)));
     $this->assertFalse($this->service->checkSign($data, $sign));
 }