/**
  * @test
  */
 public function ProveConversionToArray()
 {
     $response = array();
     $response['header']['status'] = 200;
     $response['body']['data'] = array("id" => "tran_54645bcb98ba7acfe204", "amount" => "4200", "origin_amount" => 4200, "status" => "closed", "description" => null, "livemode" => false, "refunds" => null, "currency" => "EUR", "created_at" => 1349946151, "updated_at" => 1349946151, "short_id" => '0000.1212.3434', "invoices" => array(), "payment" => new Models\Response\Payment(), "client" => new Models\Response\Client(), "preauthorization" => null, "fees" => array(), "app_id" => null);
     $responseObject = $this->_responseHandler->arrayToObject($response['body']);
     $this->assertInstanceOf('stdClass', $responseObject);
     $this->assertEquals($response['body']['data']['id'], $responseObject->data->id);
 }
Esempio n. 2
0
 /**
  * Sends a request based on the provided request model and according to the argumented method
  * @param \Paymill\Models\Request\Base $model
  * @param string $method (Create, update, delete, getAll, getOne)
  * @throws PaymillException
  * @return \Paymill\Models\Response\Base|\Paymill\Models\Response\Error
  */
 private function _request(Base $model, $method)
 {
     if (!is_a($this->_connectionClass, '\\Paymill\\API\\CommunicationAbstract')) {
         throw new PaymillException(null, 'The connection class is missing!');
     }
     $convertedResponse = null;
     $httpMethod = $this->_getHTTPMethod($method);
     $parameter = $model->parameterize($method);
     $serviceResource = $model->getServiceResource() . $model->getId();
     if (is_a($model, "\\Paymill\\Models\\Request\\Transaction") && $method === "create") {
         $source = !array_key_exists('source', $parameter) ? "PhpLib" . $this->getVersion() : "PhpLib" . $this->getVersion() . "_" . $parameter['source'];
         $parameter['source'] = $source;
     }
     try {
         $this->_lastRequest = $parameter;
         $response = $this->_connectionClass->requestApi($serviceResource, $parameter, $httpMethod);
         var_dump($parameter);
         var_dump($response);
         $this->_lastResponse = $response;
         $responseHandler = new ResponseHandler();
         if ($method === "getAllAsModel" && $responseHandler->validateResponse($response) && $this->_util->isNumericArray($response['body']['data'])) {
             foreach ($response['body']['data'] as $object) {
                 $convertedResponse[] = $responseHandler->convertResponse($object, $model->getServiceResource());
             }
         } elseif ($method === "getAll" && $responseHandler->validateResponse($response)) {
             $convertedResponse = $response['body']['data'];
         } elseif ($responseHandler->validateResponse($response)) {
             $convertedResponse = $responseHandler->convertResponse($response['body']['data'], $model->getServiceResource());
         } else {
             $convertedResponse = $responseHandler->convertErrorToModel($response, $model->getServiceResource());
         }
     } catch (\Exception $e) {
         $errorModel = new Error();
         $convertedResponse = $errorModel->setErrorMessage($e->getMessage());
     }
     if (is_a($convertedResponse, '\\Paymill\\Models\\Response\\Error')) {
         throw new PaymillException($convertedResponse->getResponseCode(), $convertedResponse->getErrorMessage(), $convertedResponse->getHttpStatusCode(), $convertedResponse->getRawObject());
     }
     return $convertedResponse;
 }
Esempio n. 3
0
 /**
  * Sends a request based on the provided request model and according to the argumented method
  * @param \Paymill\Models\Request\Base $model
  * @param string $method (Create, update, delete, getAll, getOne)
  * @throws PaymillException
  * @return \Paymill\Models\Response\Base|\Paymill\Models\Response\Error
  */
 private function _request(Base $model, $method)
 {
     if (!is_a($this->_connectionClass, '\\Paymill\\API\\CommunicationAbstract')) {
         throw new PaymillException(null, 'The connenction class is missing!');
     }
     $httpMethod = $this->_getHTTPMethod($method);
     $parameter = $model->parameterize($method);
     $serviceResource = $model->getServiceResource() . $model->getId();
     try {
         $this->_lastRequest = $parameter;
         $response = $this->_connectionClass->requestApi($serviceResource, $parameter, $httpMethod);
         $this->_lastResponse = $response;
         $responseHandler = new ResponseHandler();
         if ($method === 'getAll') {
             if ($responseHandler->validateResponse($response)) {
                 $convertedResponse = $response['body']['data'];
             } else {
                 $convertedResponse = $responseHandler->convertResponse($response, $model->getServiceResource());
             }
         } else {
             $convertedResponse = $responseHandler->convertResponse($response, $model->getServiceResource());
         }
     } catch (Exception $e) {
         $errorModel = new Error();
         $convertedResponse = $errorModel->setErrorMessage($e->getMessage());
     }
     if (is_a($convertedResponse, '\\Paymill\\Models\\Response\\Error')) {
         throw new PaymillException($convertedResponse->getResponseCode(), $convertedResponse->getErrorMessage(), $convertedResponse->getHttpStatusCode());
     }
     return $convertedResponse;
 }