Curl.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. class ChargeBee_Curl {
  3. public static function utf8($value) {
  4. if (is_string($value))
  5. return utf8_encode($value);
  6. else
  7. return $value;
  8. }
  9. public static function doRequest($meth, $url, $env, $params = array(), $headers = array()) {
  10. list($response, $httpCode) = self::request($meth, $url, $env, $params, $headers);
  11. $respJson = self::processResponse($response, $httpCode);
  12. return $respJson;
  13. }
  14. public static function request($meth, $url, $env, $params, $headers) {
  15. $curl = curl_init();
  16. $opts = array();
  17. if ($meth == ChargeBee_Request::GET) {
  18. $opts[CURLOPT_HTTPGET] = 1;
  19. if (count($params) > 0) {
  20. $encoded = http_build_query($params, null, '&');
  21. $url = "$url?$encoded";
  22. }
  23. } else if ($meth == ChargeBee_Request::POST) {
  24. $opts[CURLOPT_POST] = 1;
  25. $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
  26. } else {
  27. throw new Exception("Invalid http method $meth");
  28. }
  29. $url = self::utf8($env->apiUrl($url));
  30. $opts[CURLOPT_URL] = $url;
  31. $opts[CURLOPT_RETURNTRANSFER] = true;
  32. $opts[CURLOPT_CONNECTTIMEOUT] = ChargeBee_Environment::$connectTimeout;
  33. $opts[CURLOPT_TIMEOUT] = ChargeBee_Environment::$timeout;
  34. $userAgent = "Chargebee-PHP-Client" . " v" . ChargeBee_Version::VERSION;
  35. $httpHeaders = self::addCustomHeaders($headers);
  36. array_push($httpHeaders, 'Accept: application/json', "User-Agent: " . $userAgent); // Adding headers to array
  37. $opts[CURLOPT_HTTPHEADER] = $httpHeaders;
  38. $opts[CURLOPT_USERPWD] = $env->getApiKey() . ':';
  39. if (ChargeBee::getVerifyCaCerts()) {
  40. $opts[CURLOPT_SSL_VERIFYPEER] = true;
  41. $opts[CURLOPT_SSL_VERIFYHOST] = 2;
  42. $opts[CURLOPT_CAINFO] = ChargeBee::getCaCertPath();
  43. }
  44. curl_setopt_array($curl, $opts);
  45. $response = curl_exec($curl);
  46. if ($response === false) {
  47. $errno = curl_errno($curl);
  48. $curlMsg = curl_error($curl);
  49. $message = "IO exception occurred when trying to connect to " . $url . " . Reason : " . $curlMsg;
  50. curl_close($curl);
  51. throw new ChargeBee_IOException($message, $errno);
  52. }
  53. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  54. curl_close($curl);
  55. return array($response, $httpCode);
  56. }
  57. public static function addCustomHeaders($headers) {
  58. $httpHeaders = array();
  59. foreach ($headers as $key => $val) {
  60. array_push($httpHeaders, $key.": ".$val);
  61. }
  62. return $httpHeaders;
  63. }
  64. public static function processResponse($response, $httpCode) {
  65. $respJson = json_decode($response, true);
  66. if(!$respJson){
  67. throw new Exception("Response not in JSON format. Might not be a ChargeBee Response.");
  68. }
  69. if ($httpCode < 200 || $httpCode > 299) {
  70. self::handleAPIRespError($httpCode, $respJson,$response);
  71. }
  72. return $respJson;
  73. }
  74. public static function handleAPIRespError($httpCode, $respJson,$response) {
  75. if(!isset($respJson['api_error_code'])){
  76. throw new Exception("No api_error_code attribute in content. Probably not a ChargeBee's error response. The content is \n " . $response);
  77. }
  78. $type="unknown";
  79. if(isset($respJson['type'])){
  80. $type = $respJson['type'];
  81. }
  82. if ($type == "payment") {
  83. throw new ChargeBee_PaymentException($httpCode, $respJson);
  84. } elseif ($type == "operation_failed") {
  85. throw new ChargeBee_OperationFailedException($httpCode, $respJson);
  86. } elseif ($type == "invalid_request") {
  87. throw new ChargeBee_InvalidRequestException($httpCode, $respJson);
  88. } else {
  89. throw new ChargeBee_APIError($httpCode, $respJson);
  90. }
  91. }
  92. }