Example #1
0
 /**
  * List all created charges
  *
  * @return array list of transactions
  * @throws Start_Error_Parameters if any of the parameters is invalid
  * @throws Start_Error_Authentication if the API Key is invalid
  * @throws Start_Error if there is a general error in the API endpoint
  * @throws Exception for any other errors
  */
 public static function all()
 {
     $url = Start::getEndPoint('charge_list');
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_CAINFO, Start::getCaPath());
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_USERPWD, Start::getApiKey() . ':');
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_USERAGENT, 'Start/PHP/' . Start::VERSION);
     $result = json_decode(curl_exec($ch), true);
     // Check for errors and such.
     $info = curl_getinfo($ch);
     $errno = curl_errno($ch);
     if ($result === false || $errno != 0) {
         // Do error checking
         throw new Exception(curl_error($ch));
     } else {
         if ($info['http_code'] < 200 || $info['http_code'] > 299) {
             // Got a non-200 error code.
             Start::handleErrors($result, $info['http_code']);
         }
     }
     curl_close($ch);
     return $result;
 }
Example #2
0
 public static function make_request($url, $data = array(), $method = '')
 {
     if (!defined('CURL_SSLVERSION_TLSv1_2')) {
         define('CURL_SSLVERSION_TLSv1_2', 6);
     }
     $ch = curl_init();
     if (Start::getUserAgent() != "") {
         $userAgent = Start::getUserAgent() . ' / StartPHP CURL ' . Start::VERSION;
     } else {
         $userAgent = 'StartPHP CURL' . Start::VERSION;
     }
     curl_setopt($ch, CURLOPT_CAINFO, Start::getCaPath());
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_USERPWD, Start::getApiKey() . ':');
     curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
     if (!empty($data)) {
         if ($method == 'PUT' || $method == 'GET') {
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
         }
         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen(json_encode($data))));
     }
     curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $result = json_decode(curl_exec($ch), true);
     // Check for errors and such.
     $info = curl_getinfo($ch);
     $errno = curl_errno($ch);
     if ($result === false || $errno != 0) {
         // Do error checking
         $curl_error = curl_error($ch);
         if ($errno == '1' || $errno == '35' || $errno == '51' || $errno == '60') {
             $exception_message = "You weren’t able to make API request due to SSL/TLS error. " . "  Here you can read how to solve this: https://docs.start.payfort.com/help/php/ssl#error_" . $errno . " Curl error: " . $curl_error;
         } else {
             $exception_message = "Curl error: " . $curl_error;
         }
         throw new Start_Error_SSLError($exception_message);
     } else {
         if ($info['http_code'] < 200 || $info['http_code'] > 299) {
             // Got a non-200 error code.
             Start::handleErrors($result, $info['http_code']);
         }
     }
     curl_close($ch);
     return $result;
 }
Example #3
0
 public static function make_request($url, $data = array(), $method)
 {
     $api_key = Start::getApiKey();
     $headers = array('Connection: close', "Authorization: Basic " . base64_encode("{$api_key}:"));
     if (!empty($data)) {
         if ($method == '') {
             $method = 'POST';
         }
         $content = json_encode($data);
         array_push($headers, 'Content-Type: application/json');
         array_push($headers, 'Content-Length: ' . strlen($content));
     } else {
         $method = 'GET';
         $content = '';
     }
     if (Start::getUserAgent() != "") {
         $user_agent = Start::getUserAgent() . ' / StartPHP Stream ' . Start::VERSION;
     } else {
         $user_agent = 'StartPHP Stream' . Start::VERSION;
     }
     $opts = array('http' => array('method' => $method, 'content' => $content, 'header' => $headers, 'timeout' => 20, 'ignore_errors' => true, 'user_agent' => $user_agent), 'ssl' => array('verify_peer' => true, 'cafile' => Start::getCaPath()));
     $context = stream_context_create($opts);
     $response = "{}";
     $exception_message = "";
     try {
         $response = file_get_contents($url, false, $context);
     } catch (Exception $e) {
         $exception_message = "You weren’t able to make API request due to SSL/TLS connection error. " . "Here you can read how to solve this: https://docs.start.payfort.com/help/php/ssl. " . "Error details: " . $e->getMessage();
         throw new Start_Error_SSLError($exception_message);
     }
     $result = json_decode($response, true);
     $headers = self::parseHeaders($http_response_header);
     if ($headers['http_code'] < 200 || $headers['http_code'] > 299) {
         Start::handleErrors($result, $headers['http_code']);
     } else {
         return $result;
     }
 }