Path: blob/master/externals/stripe-php/lib/Stripe/ApiResource.php
12256 views
<?php12abstract class Stripe_ApiResource extends Stripe_Object3{4protected static function _scopedRetrieve($class, $id, $apiKey=null)5{6$instance = new $class($id, $apiKey);7$instance->refresh();8return $instance;9}1011/**12* @returns Stripe_ApiResource The refreshed resource.13*/14public function refresh()15{16$requestor = new Stripe_ApiRequestor($this->_apiKey);17$url = $this->instanceUrl();1819list($response, $apiKey) = $requestor->request(20'get',21$url,22$this->_retrieveOptions23);24$this->refreshFrom($response, $apiKey);25return $this;26}2728/**29* @param string $class30*31* @returns string The name of the class, with namespacing and underscores32* stripped.33*/34public static function className($class)35{36// Useful for namespaces: Foo\Stripe_Charge37if ($postfixNamespaces = strrchr($class, '\\')) {38$class = substr($postfixNamespaces, 1);39}40// Useful for underscored 'namespaces': Foo_Stripe_Charge41if ($postfixFakeNamespaces = strrchr($class, 'Stripe_')) {42$class = $postfixFakeNamespaces;43}44if (substr($class, 0, strlen('Stripe')) == 'Stripe') {45$class = substr($class, strlen('Stripe'));46}47$class = str_replace('_', '', $class);48$name = urlencode($class);49$name = strtolower($name);50return $name;51}5253/**54* @param string $class55*56* @returns string The endpoint URL for the given class.57*/58public static function classUrl($class)59{60$base = self::_scopedLsb($class, 'className', $class);61return "/v1/${base}s";62}6364/**65* @returns string The full API URL for this API resource.66*/67public function instanceUrl()68{69$id = $this['id'];70$class = get_class($this);71if ($id === null) {72$message = "Could not determine which URL to request: "73. "$class instance has invalid ID: $id";74throw new Stripe_InvalidRequestError($message, null);75}76$id = Stripe_ApiRequestor::utf8($id);77$base = $this->_lsb('classUrl', $class);78$extn = urlencode($id);79return "$base/$extn";80}8182private static function _validateCall($method, $params=null, $apiKey=null)83{84if ($params && !is_array($params)) {85$message = "You must pass an array as the first argument to Stripe API "86. "method calls. (HINT: an example call to create a charge "87. "would be: \"StripeCharge::create(array('amount' => 100, "88. "'currency' => 'usd', 'card' => array('number' => "89. "4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")";90throw new Stripe_Error($message);91}9293if ($apiKey && !is_string($apiKey)) {94$message = 'The second argument to Stripe API method calls is an '95. 'optional per-request apiKey, which must be a string. '96. '(HINT: you can set a global apiKey by '97. '"Stripe::setApiKey(<apiKey>)")';98throw new Stripe_Error($message);99}100}101102protected static function _scopedAll($class, $params=null, $apiKey=null)103{104self::_validateCall('all', $params, $apiKey);105$requestor = new Stripe_ApiRequestor($apiKey);106$url = self::_scopedLsb($class, 'classUrl', $class);107list($response, $apiKey) = $requestor->request('get', $url, $params);108return Stripe_Util::convertToStripeObject($response, $apiKey);109}110111protected static function _scopedCreate($class, $params=null, $apiKey=null)112{113self::_validateCall('create', $params, $apiKey);114$requestor = new Stripe_ApiRequestor($apiKey);115$url = self::_scopedLsb($class, 'classUrl', $class);116list($response, $apiKey) = $requestor->request('post', $url, $params);117return Stripe_Util::convertToStripeObject($response, $apiKey);118}119120protected function _scopedSave($class, $apiKey=null)121{122self::_validateCall('save');123$requestor = new Stripe_ApiRequestor($apiKey);124$params = $this->serializeParameters();125126if (count($params) > 0) {127$url = $this->instanceUrl();128list($response, $apiKey) = $requestor->request('post', $url, $params);129$this->refreshFrom($response, $apiKey);130}131return $this;132}133134protected function _scopedDelete($class, $params=null)135{136self::_validateCall('delete');137$requestor = new Stripe_ApiRequestor($this->_apiKey);138$url = $this->instanceUrl();139list($response, $apiKey) = $requestor->request('delete', $url, $params);140$this->refreshFrom($response, $apiKey);141return $this;142}143}144145146