/**
  * Helper method for validating if the class contains accessor methods (getter and setter) for a given attribute
  *
  * @param BitPagosModel $class
  *        	An object of BitPagosModel
  * @param string $attributeName
  *        	Attribute name
  * @return bool
  */
 public static function validate(BitPagosModel $class, $attributeName)
 {
     $mode = ConfigManager::getInstance()->get('validation.level');
     if (!empty($mode) && $mode != 'disabled') {
         //Check if $attributeName is string
         if (gettype($attributeName) !== 'string') {
             return false;
         }
         //If the mode is disabled, bypass the validation
         foreach (array('set' . $attributeName, 'get' . $attributeName) as $methodName) {
             if (get_class($class) == get_class(new BitPagosModel())) {
                 // Silently return false on cases where you are using BitPagosModel instance directly
                 return false;
             } elseif (!method_exists($class, $methodName)) {
                 //Delegate the error based on the choice
                 $className = is_object($class) ? get_class($class) : (string) $class;
                 $errorMessage = "Missing Accessor: {$className}:{$methodName}. You might be using older version of SDK. If not, create an issue at https://github.com/welcome2ba/bitpagos-api-sdk-php/issues";
                 LoggingManager::getInstance(__CLASS__)->debug($errorMessage);
                 if ($mode == 'strict') {
                     trigger_error($errorMessage, E_USER_NOTICE);
                 }
                 return false;
             }
         }
         return true;
     }
     return false;
 }
Exemplo n.º 2
0
 /**
  * Default Constructor
  *
  * @param ApiContext $apiContext        	
  */
 public function __construct(ApiContext $apiContext)
 {
     $this->apiContext = $apiContext;
     $this->logger = LoggingManager::getInstance(__CLASS__);
 }
 /**
  * Executes an HTTP request
  *
  * @param string $data
  *        	query string OR POST content as a string
  * @return mixed
  * @throws BitPagosConnectionException
  */
 public function execute($data)
 {
     //Initialize the logger
     $this->logger->info($this->httpConfig->getMethod() . ' ' . $this->httpConfig->getUrl());
     //Initialize Curl Options
     $ch = curl_init($this->httpConfig->getUrl());
     curl_setopt_array($ch, $this->httpConfig->getCurlOptions());
     curl_setopt($ch, CURLOPT_URL, $this->httpConfig->getUrl());
     curl_setopt($ch, CURLOPT_HEADER, true);
     curl_setopt($ch, CURLINFO_HEADER_OUT, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHttpHeaders());
     //Determine Curl Options based on Method
     switch ($this->httpConfig->getMethod()) {
         case 'POST':
             curl_setopt($ch, CURLOPT_POST, true);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
             break;
         case 'PUT':
         case 'PATCH':
         case 'DELETE':
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
             break;
     }
     //Default Option if Method not of given types in switch case
     if ($this->httpConfig->getMethod() != NULL) {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod());
     }
     //Logging Each Headers for debugging purposes
     foreach ($this->getHttpHeaders() as $header) {
         //TODO: Strip out credentials and other secure info when logging.
         // $this->logger->debug($header);
     }
     //Execute Curl Request
     $result = curl_exec($ch);
     //Retrieve Response Status
     $this->httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     //Retry if Certificate Exception
     if (curl_errno($ch) == 60) {
         $this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file");
         curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
         $result = curl_exec($ch);
         //Retrieve Response Status
         $this->httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     }
     //Retry if Failing
     $retries = 0;
     if (in_array($this->httpStatus, self::$retryCodes) && $this->httpConfig->getHttpRetryCount() != null) {
         $this->logger->info("Got {$this->httpStatus} response from server. Retrying");
         do {
             $result = curl_exec($ch);
             //Retrieve Response Status
             $this->httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         } while (in_array($this->httpStatus, self::$retryCodes) && ++$retries < $this->httpConfig->getHttpRetryCount());
     }
     //Throw Exception if Retries and Certificates doenst work
     if (curl_errno($ch)) {
         $ex = new BitPagosConnectionException($this->httpConfig->getUrl(), curl_error($ch), curl_errno($ch));
         curl_close($ch);
         throw $ex;
     }
     // Get Request and Response Headers
     $this->requestHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
     //Using alternative solution to CURLINFO_HEADER_SIZE as it throws invalid number when called using PROXY.
     $responseHeaderSize = strlen($result) - curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
     $this->responseHeaders = substr($result, 0, $responseHeaderSize);
     $result = substr($result, $responseHeaderSize);
     $this->logger->debug("Request Headers \t: " . str_replace("\r\n", ", ", $this->requestHeaders));
     $this->logger->debug(($data && $data != '' ? "Request Data\t\t: " . $data : "No Request Payload") . "\n" . str_repeat('-', 128) . "\n");
     $this->logger->info("Response Status \t: " . $this->httpStatus);
     $this->logger->debug("Response Headers\t: " . str_replace("\r\n", ", ", $this->responseHeaders));
     //Close the curl request
     curl_close($ch);
     //More Exceptions based on HttpStatus Code
     if (in_array($this->httpStatus, self::$retryCodes)) {
         $ex = new BitPagosConnectionException($this->httpConfig->getUrl(), "Got Http response code {$this->httpStatus} when accessing {$this->httpConfig->getUrl()}. " . "Retried {$retries} times.");
         $ex->setData($result);
         $this->logger->error("Got Http response code {$this->httpStatus} when accessing {$this->httpConfig->getUrl()}. " . "Retried {$retries} times." . $result);
         $this->logger->debug("\n\n" . str_repeat('=', 128) . "\n");
         throw $ex;
     } else {
         if ($this->httpStatus < 200 || $this->httpStatus >= 300) {
             $ex = new BitPagosConnectionException($this->httpConfig->getUrl(), "Got Http response code {$this->httpStatus} when accessing {$this->httpConfig->getUrl()}.", $this->httpStatus);
             $ex->setData($result);
             $this->logger->error("Got Http response code {$this->httpStatus} when accessing {$this->httpConfig->getUrl()}. " . $result);
             $this->logger->debug("\n\n" . str_repeat('=', 128) . "\n");
             throw $ex;
         }
     }
     $this->logger->debug(($result && $result != '' ? "Response Data \t: " . $result : "No Response Body") . "\n\n" . str_repeat('=', 128) . "\n");
     //Return result object
     $this->responseBody =& $result;
     return $result;
 }