PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Eat Smpl/form(NotInUse)/sites/step3/lib/lib/ApiRequestor.php

https://gitlab.com/eatsmpl/site
PHP | 233 lines | 180 code | 25 blank | 28 comment | 24 complexity | b688af535a8e37eadfefccddb93b9090 MD5 | raw file
  1. <?php
  2. namespace Stripe;
  3. class ApiRequestor
  4. {
  5. private $_apiKey;
  6. private $_apiBase;
  7. private static $_httpClient;
  8. public function __construct($apiKey = null, $apiBase = null)
  9. {
  10. $this->_apiKey = $apiKey;
  11. if (!$apiBase) {
  12. $apiBase = Stripe::$apiBase;
  13. }
  14. $this->_apiBase = $apiBase;
  15. }
  16. private static function _encodeObjects($d)
  17. {
  18. if ($d instanceof ApiResource) {
  19. return Util\Util::utf8($d->id);
  20. } elseif ($d === true) {
  21. return 'true';
  22. } elseif ($d === false) {
  23. return 'false';
  24. } elseif (is_array($d)) {
  25. $res = array();
  26. foreach ($d as $k => $v) {
  27. $res[$k] = self::_encodeObjects($v);
  28. }
  29. return $res;
  30. } else {
  31. return Util\Util::utf8($d);
  32. }
  33. }
  34. /**
  35. * @param string $method
  36. * @param string $url
  37. * @param array|null $params
  38. * @param array|null $headers
  39. *
  40. * @return array An array whose first element is the response and second
  41. * element is the API key used to make the request.
  42. */
  43. public function request($method, $url, $params = null, $headers = null)
  44. {
  45. if (!$params) {
  46. $params = array();
  47. }
  48. if (!$headers) {
  49. $headers = array();
  50. }
  51. list($rbody, $rcode, $rheaders, $myApiKey) =
  52. $this->_requestRaw($method, $url, $params, $headers);
  53. $resp = $this->_interpretResponse($rbody, $rcode, $rheaders);
  54. return array($resp, $myApiKey);
  55. }
  56. /**
  57. * @param string $rbody A JSON string.
  58. * @param int $rcode
  59. * @param array $rheaders
  60. * @param array $resp
  61. *
  62. * @throws Error\InvalidRequest if the error is caused by the user.
  63. * @throws Error\Authentication if the error is caused by a lack of
  64. * permissions.
  65. * @throws Error\Card if the error is the error code is 402 (payment
  66. * required)
  67. * @throws Error\RateLimit if the error is caused by too many requests
  68. * hitting the API.
  69. * @throws Error\Api otherwise.
  70. */
  71. public function handleApiError($rbody, $rcode, $rheaders, $resp)
  72. {
  73. if (!is_array($resp) || !isset($resp['error'])) {
  74. $msg = "Invalid response object from API: $rbody "
  75. . "(HTTP response code was $rcode)";
  76. throw new Error\Api($msg, $rcode, $rbody, $resp, $rheaders);
  77. }
  78. $error = $resp['error'];
  79. $msg = isset($error['message']) ? $error['message'] : null;
  80. $param = isset($error['param']) ? $error['param'] : null;
  81. $code = isset($error['code']) ? $error['code'] : null;
  82. switch ($rcode) {
  83. case 400:
  84. // 'rate_limit' code is depreciated, but left here for backwards compatibility
  85. // for API versions earlier than 2015-09-08
  86. if ($code == 'rate_limit') {
  87. throw new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders);
  88. }
  89. // intentional fall-through
  90. case 404:
  91. throw new Error\InvalidRequest($msg, $param, $rcode, $rbody, $resp, $rheaders);
  92. case 401:
  93. throw new Error\Authentication($msg, $rcode, $rbody, $resp, $rheaders);
  94. case 402:
  95. throw new Error\Card($msg, $param, $code, $rcode, $rbody, $resp, $rheaders);
  96. case 429:
  97. throw new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders);
  98. default:
  99. throw new Error\Api($msg, $rcode, $rbody, $resp, $rheaders);
  100. }
  101. }
  102. private function _requestRaw($method, $url, $params, $headers)
  103. {
  104. $myApiKey = $this->_apiKey;
  105. if (!$myApiKey) {
  106. $myApiKey = Stripe::$apiKey;
  107. }
  108. if (!$myApiKey) {
  109. $msg = 'No API key provided. (HINT: set your API key using '
  110. . '"Stripe::setApiKey(<API-KEY>)". You can generate API keys from '
  111. . 'the Stripe web interface. See https://stripe.com/api for '
  112. . 'details, or email support@stripe.com if you have any questions.';
  113. throw new Error\Authentication($msg);
  114. }
  115. $absUrl = $this->_apiBase.$url;
  116. $params = self::_encodeObjects($params);
  117. $langVersion = phpversion();
  118. $uname = php_uname();
  119. $ua = array(
  120. 'bindings_version' => Stripe::VERSION,
  121. 'lang' => 'php',
  122. 'lang_version' => $langVersion,
  123. 'publisher' => 'stripe',
  124. 'uname' => $uname,
  125. );
  126. $defaultHeaders = array(
  127. 'X-Stripe-Client-User-Agent' => json_encode($ua),
  128. 'User-Agent' => 'Stripe/v1 PhpBindings/' . Stripe::VERSION,
  129. 'Authorization' => 'Bearer ' . $myApiKey,
  130. );
  131. if (Stripe::$apiVersion) {
  132. $defaultHeaders['Stripe-Version'] = Stripe::$apiVersion;
  133. }
  134. $hasFile = false;
  135. $hasCurlFile = class_exists('\CURLFile', false);
  136. foreach ($params as $k => $v) {
  137. if (is_resource($v)) {
  138. $hasFile = true;
  139. $params[$k] = self::_processResourceParam($v, $hasCurlFile);
  140. } elseif ($hasCurlFile && $v instanceof \CURLFile) {
  141. $hasFile = true;
  142. }
  143. }
  144. if ($hasFile) {
  145. $defaultHeaders['Content-Type'] = 'multipart/form-data';
  146. } else {
  147. $defaultHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
  148. }
  149. $combinedHeaders = array_merge($defaultHeaders, $headers);
  150. $rawHeaders = array();
  151. foreach ($combinedHeaders as $header => $value) {
  152. $rawHeaders[] = $header . ': ' . $value;
  153. }
  154. list($rbody, $rcode, $rheaders) = $this->httpClient()->request(
  155. $method,
  156. $absUrl,
  157. $rawHeaders,
  158. $params,
  159. $hasFile
  160. );
  161. return array($rbody, $rcode, $rheaders, $myApiKey);
  162. }
  163. private function _processResourceParam($resource, $hasCurlFile)
  164. {
  165. if (get_resource_type($resource) !== 'stream') {
  166. throw new Error\Api(
  167. 'Attempted to upload a resource that is not a stream'
  168. );
  169. }
  170. $metaData = stream_get_meta_data($resource);
  171. if ($metaData['wrapper_type'] !== 'plainfile') {
  172. throw new Error\Api(
  173. 'Only plainfile resource streams are supported'
  174. );
  175. }
  176. if ($hasCurlFile) {
  177. // We don't have the filename or mimetype, but the API doesn't care
  178. return new \CURLFile($metaData['uri']);
  179. } else {
  180. return '@'.$metaData['uri'];
  181. }
  182. }
  183. private function _interpretResponse($rbody, $rcode, $rheaders)
  184. {
  185. try {
  186. $resp = json_decode($rbody, true);
  187. } catch (Exception $e) {
  188. $msg = "Invalid response body from API: $rbody "
  189. . "(HTTP response code was $rcode)";
  190. throw new Error\Api($msg, $rcode, $rbody);
  191. }
  192. if ($rcode < 200 || $rcode >= 300) {
  193. $this->handleApiError($rbody, $rcode, $rheaders, $resp);
  194. }
  195. return $resp;
  196. }
  197. public static function setHttpClient($client)
  198. {
  199. self::$_httpClient = $client;
  200. }
  201. private function httpClient()
  202. {
  203. if (!self::$_httpClient) {
  204. self::$_httpClient = HttpClient\CurlClient::instance();
  205. }
  206. return self::$_httpClient;
  207. }
  208. }