PPPStripeApiResource

Class ApiResource.

Defined (1)

The class is defined in the following location(s).

/includes/api/stripe-php/lib/ApiResource.php  
  1. abstract class ApiResource extends StripeObject 
  2. private static $HEADERS_TO_PERSIST = array('Stripe-Account' => true, 'Stripe-Version' => true); 
  3.  
  4. public static function baseUrl() 
  5. return Stripe::$apiBase; 
  6.  
  7. /** 
  8. * @return ApiResource The refreshed resource. 
  9. */ 
  10. public function refresh() 
  11. $requestor = new ApiRequestor($this->_opts->apiKey, static::baseUrl()); 
  12. $url = $this->instanceUrl(); 
  13.  
  14. list($response, $this->_opts->apiKey) = $requestor->request( 
  15. 'get',  
  16. $url,  
  17. $this->_retrieveOptions,  
  18. $this->_opts->headers 
  19. ); 
  20. $this->setLastResponse($response); 
  21. $this->refreshFrom($response->json, $this->_opts); 
  22. return $this; 
  23.  
  24. /** 
  25. * @return string The name of the class, with namespacing and underscores 
  26. * stripped. 
  27. */ 
  28. public static function className() 
  29. $class = get_called_class(); 
  30. // Useful for namespaces: Foo\Charge 
  31. if ($postfixNamespaces = strrchr($class, '\\')) { 
  32. $class = substr($postfixNamespaces, 1); 
  33. // Useful for underscored 'namespaces': Foo_Charge 
  34. if ($postfixFakeNamespaces = strrchr($class, '')) { 
  35. $class = $postfixFakeNamespaces; 
  36. if (substr($class, 0, strlen('Stripe')) == 'Stripe') { 
  37. $class = substr($class, strlen('Stripe')); 
  38. $class = str_replace('_', '', $class); 
  39. $name = urlencode($class); 
  40. $name = strtolower($name); 
  41. return $name; 
  42.  
  43. /** 
  44. * @return string The endpoint URL for the given class. 
  45. */ 
  46. public static function classUrl() 
  47. $base = static::className(); 
  48. return "/v1/${base}s"; 
  49.  
  50. /** 
  51. * @return string The instance endpoint URL for the given class. 
  52. */ 
  53. public static function resourceUrl($id) 
  54. if ($id === null) { 
  55. $class = get_called_class(); 
  56. $message = "Could not determine which URL to request: " 
  57. . "$class instance has invalid ID: $id"; 
  58. throw new Error\InvalidRequest($message, null); 
  59. $id = Util\Util::utf8($id); 
  60. $base = static::classUrl(); 
  61. $extn = urlencode($id); 
  62. return "$base/$extn"; 
  63.  
  64. /** 
  65. * @return string The full API URL for this API resource. 
  66. */ 
  67. public function instanceUrl() 
  68. return static::resourceUrl($this['id']); 
  69.  
  70. private static function _validateParams($params = null) 
  71. if ($params && !is_array($params)) { 
  72. $message = "You must pass an array as the first argument to Stripe API " 
  73. . "method calls. (HINT: an example call to create a charge " 
  74. . "would be: \"PPP\\Stripe\\Charge::create(array('amount' => 100, " 
  75. . "'currency' => 'usd', 'card' => array('number' => " 
  76. . "4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")"; 
  77. throw new Error\Api($message); 
  78.  
  79. protected function _request($method, $url, $params = array(), $options = null) 
  80. $opts = $this->_opts->merge($options); 
  81. list($resp, $options) = static::_staticRequest($method, $url, $params, $opts); 
  82. $this->setLastResponse($resp); 
  83. return array($resp->json, $options); 
  84.  
  85. protected static function _staticRequest($method, $url, $params, $options) 
  86. $opts = Util\RequestOptions::parse($options); 
  87. $requestor = new ApiRequestor($opts->apiKey, static::baseUrl()); 
  88. list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers); 
  89. foreach ($opts->headers as $k => $v) { 
  90. if (!array_key_exists($k, self::$HEADERS_TO_PERSIST)) { 
  91. unset($opts->headers[$k]); 
  92. return array($response, $opts); 
  93.  
  94. protected static function _retrieve($id, $options = null) 
  95. $opts = Util\RequestOptions::parse($options); 
  96. $instance = new static($id, $opts); 
  97. $instance->refresh(); 
  98. return $instance; 
  99.  
  100. protected static function _all($params = null, $options = null) 
  101. self::_validateParams($params); 
  102. $url = static::classUrl(); 
  103.  
  104. list($response, $opts) = static::_staticRequest('get', $url, $params, $options); 
  105. $obj = Util\Util::convertToStripeObject($response->json, $opts); 
  106. if (!is_a($obj, 'PPP\\Stripe\\Collection')) { 
  107. $class = get_class($obj); 
  108. $message = "Expected type \"PPP\\Stripe\\Collection\", got \"$class\" instead"; 
  109. throw new Error\Api($message); 
  110. $obj->setLastResponse($response); 
  111. $obj->setRequestParams($params); 
  112. return $obj; 
  113.  
  114. protected static function _create($params = null, $options = null) 
  115. self::_validateParams($params); 
  116. $base = static::baseUrl(); 
  117. $url = static::classUrl(); 
  118.  
  119. list($response, $opts) = static::_staticRequest('post', $url, $params, $options); 
  120. $obj = Util\Util::convertToStripeObject($response->json, $opts); 
  121. $obj->setLastResponse($response); 
  122. return $obj; 
  123.  
  124. /** 
  125. * @param string $id The ID of the API resource to update. 
  126. * @param array|null $params 
  127. * @param array|string|null $opts 
  128. * @return ApiResource the updated API resource 
  129. */ 
  130. protected static function _update($id, $params = null, $options = null) 
  131. self::_validateParams($params); 
  132. $base = static::baseUrl(); 
  133. $url = static::resourceUrl($id); 
  134.  
  135. list($response, $opts) = static::_staticRequest('post', $url, $params, $options); 
  136. $obj = Util\Util::convertToStripeObject($response->json, $opts); 
  137. $obj->setLastResponse($response); 
  138. return $obj; 
  139.  
  140. protected function _save($options = null) 
  141. $params = $this->serializeParameters(); 
  142. if (count($params) > 0) { 
  143. $url = $this->instanceUrl(); 
  144. list($response, $opts) = $this->_request('post', $url, $params, $options); 
  145. $this->refreshFrom($response, $opts); 
  146. return $this; 
  147.  
  148. protected function _delete($params = null, $options = null) 
  149. self::_validateParams($params); 
  150.  
  151. $url = $this->instanceUrl(); 
  152. list($response, $opts) = $this->_request('delete', $url, $params, $options); 
  153. $this->refreshFrom($response, $opts); 
  154. return $this;