Request.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Stripe\ApiOperations;
  3. /**
  4. * Trait for resources that need to make API requests.
  5. *
  6. * This trait should only be applied to classes that derive from StripeObject.
  7. */
  8. trait Request
  9. {
  10. /**
  11. * @param array|null|mixed $params The list of parameters to validate
  12. *
  13. * @throws \Stripe\Error\Api if $params exists and is not an array
  14. */
  15. protected static function _validateParams($params = null)
  16. {
  17. if ($params && !is_array($params)) {
  18. $message = "You must pass an array as the first argument to Stripe API "
  19. . "method calls. (HINT: an example call to create a charge "
  20. . "would be: \"Stripe\\Charge::create(['amount' => 100, "
  21. . "'currency' => 'usd', 'source' => 'tok_1234'])\")";
  22. throw new \Stripe\Error\Api($message);
  23. }
  24. }
  25. /**
  26. * @param string $method HTTP method ('get', 'post', etc.)
  27. * @param string $url URL for the request
  28. * @param array $params list of parameters for the request
  29. * @param array|string|null $options
  30. *
  31. * @return array tuple containing (the JSON response, $options)
  32. */
  33. protected function _request($method, $url, $params = [], $options = null)
  34. {
  35. $opts = $this->_opts->merge($options);
  36. list($resp, $options) = static::_staticRequest($method, $url, $params, $opts);
  37. $this->setLastResponse($resp);
  38. return [$resp->json, $options];
  39. }
  40. /**
  41. * @param string $method HTTP method ('get', 'post', etc.)
  42. * @param string $url URL for the request
  43. * @param array $params list of parameters for the request
  44. * @param array|string|null $options
  45. *
  46. * @return array tuple containing (the JSON response, $options)
  47. */
  48. protected static function _staticRequest($method, $url, $params, $options)
  49. {
  50. $opts = \Stripe\Util\RequestOptions::parse($options);
  51. $requestor = new \Stripe\ApiRequestor($opts->apiKey, static::baseUrl());
  52. list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers);
  53. $opts->discardNonPersistentHeaders();
  54. return [$response, $opts];
  55. }
  56. }