public function testAuthenticationError()
 {
     $this->_apiTestKey = 'INVALID_API_KEY';
     $this->_curlClient = new Services_Paymill_Apiclient_Curl($this->_apiTestKey, $this->_apiUrl);
     try {
         $resp = $this->_curlClient->request('clients/', array(), Services_Paymill_Apiclient_Interface::HTTP_GET);
     } catch (Exception $e) {
         $this->assertInstanceOf('Services_Paymill_Exception', $e);
         $this->assertEquals(401, $e->getCode());
         $this->assertContains('Access Denied', $e->getMessage(), 'Expected error message not found', true);
     }
 }
Example #2
0
 /**
  * Perform HTTP request to REST endpoint
  *
  * @param string $action
  * @param array $params
  * @param string $method
  * @return array
  */
 protected function _requestApi($action = '', $params = array(), $method = 'POST')
 {
     $curlOpts = array(CURLOPT_URL => $this->_apiUrl . $action, CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_USERAGENT => self::USER_AGENT, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_CAINFO => realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'paymill.crt');
     if (Services_Paymill_Apiclient_Interface::HTTP_GET === $method) {
         if (0 !== count($params)) {
             $curlOpts[CURLOPT_URL] .= false === strpos($curlOpts[CURLOPT_URL], '?') ? '?' : '&';
             $curlOpts[CURLOPT_URL] .= http_build_query($params, null, '&');
         }
     } else {
         $curlOpts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
     }
     if ($this->_apiKey) {
         $curlOpts[CURLOPT_USERPWD] = $this->_apiKey . ':';
     }
     $curl = curl_init();
     curl_setopt_array($curl, $curlOpts);
     $responseBody = curl_exec($curl);
     self::$lastRawCurlOptions = $curlOpts;
     self::$lastRawResponse = $responseBody;
     $responseInfo = curl_getinfo($curl);
     if ($responseBody === false) {
         $responseBody = array('error' => curl_error($curl));
     }
     curl_close($curl);
     if ('application/json' === $responseInfo['content_type']) {
         $responseBody = json_decode($responseBody, true);
     }
     return array('header' => array('status' => $responseInfo['http_code'], 'reason' => null), 'body' => $responseBody);
 }