Changeset View
Changeset View
Standalone View
Standalone View
externals/stripe-php/lib/Stripe/ApiResource.php
| <?php | <?php | ||||
| abstract class Stripe_ApiResource extends Stripe_Object | abstract class Stripe_ApiResource extends Stripe_Object | ||||
| { | { | ||||
| protected static function _scopedRetrieve($class, $id, $apiKey=null) | protected static function _scopedRetrieve($class, $id, $apiKey=null) | ||||
| { | { | ||||
| $instance = new $class($id, $apiKey); | $instance = new $class($id, $apiKey); | ||||
| $instance->refresh(); | $instance->refresh(); | ||||
| return $instance; | return $instance; | ||||
| } | } | ||||
| /** | |||||
| * @returns Stripe_ApiResource The refreshed resource. | |||||
| */ | |||||
| public function refresh() | public function refresh() | ||||
| { | { | ||||
| $requestor = new Stripe_ApiRequestor($this->_apiKey); | $requestor = new Stripe_ApiRequestor($this->_apiKey); | ||||
| $url = $this->instanceUrl(); | $url = $this->instanceUrl(); | ||||
| list($response, $apiKey) = $requestor->request('get', $url); | |||||
| list($response, $apiKey) = $requestor->request( | |||||
| 'get', | |||||
| $url, | |||||
| $this->_retrieveOptions | |||||
| ); | |||||
| $this->refreshFrom($response, $apiKey); | $this->refreshFrom($response, $apiKey); | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public static function classUrl($class) | /** | ||||
| * @param string $class | |||||
| * | |||||
| * @returns string The name of the class, with namespacing and underscores | |||||
| * stripped. | |||||
| */ | |||||
| public static function className($class) | |||||
| { | { | ||||
| // Useful for namespaces: Foo\Stripe_Charge | // Useful for namespaces: Foo\Stripe_Charge | ||||
| if ($postfix = strrchr($class, '\\')) | if ($postfixNamespaces = strrchr($class, '\\')) { | ||||
| $class = substr($postfix, 1); | $class = substr($postfixNamespaces, 1); | ||||
| if (substr($class, 0, strlen('Stripe')) == 'Stripe') | } | ||||
| // Useful for underscored 'namespaces': Foo_Stripe_Charge | |||||
| if ($postfixFakeNamespaces = strrchr($class, 'Stripe_')) { | |||||
| $class = $postfixFakeNamespaces; | |||||
| } | |||||
| if (substr($class, 0, strlen('Stripe')) == 'Stripe') { | |||||
| $class = substr($class, strlen('Stripe')); | $class = substr($class, strlen('Stripe')); | ||||
| } | |||||
| $class = str_replace('_', '', $class); | $class = str_replace('_', '', $class); | ||||
| $name = urlencode($class); | $name = urlencode($class); | ||||
| $name = strtolower($name); | $name = strtolower($name); | ||||
| return "/${name}s"; | return $name; | ||||
| } | } | ||||
| /** | |||||
| * @param string $class | |||||
| * | |||||
| * @returns string The endpoint URL for the given class. | |||||
| */ | |||||
| public static function classUrl($class) | |||||
| { | |||||
| $base = self::_scopedLsb($class, 'className', $class); | |||||
| return "/v1/${base}s"; | |||||
| } | |||||
| /** | |||||
| * @returns string The full API URL for this API resource. | |||||
| */ | |||||
| public function instanceUrl() | public function instanceUrl() | ||||
| { | { | ||||
| $id = $this['id']; | $id = $this['id']; | ||||
| $class = get_class($this); | $class = get_class($this); | ||||
| if (!$id) { | if ($id === null) { | ||||
| throw new Stripe_InvalidRequestError("Could not determine which URL to request: $class instance has invalid ID: $id"); | $message = "Could not determine which URL to request: " | ||||
| . "$class instance has invalid ID: $id"; | |||||
| throw new Stripe_InvalidRequestError($message, null); | |||||
| } | } | ||||
| $id = Stripe_ApiRequestor::utf8($id); | $id = Stripe_ApiRequestor::utf8($id); | ||||
| $base = self::classUrl($class); | $base = $this->_lsb('classUrl', $class); | ||||
| $extn = urlencode($id); | $extn = urlencode($id); | ||||
| return "$base/$extn"; | return "$base/$extn"; | ||||
| } | } | ||||
| private static function _validateCall($method, $params=null, $apiKey=null) | private static function _validateCall($method, $params=null, $apiKey=null) | ||||
| { | { | ||||
| if ($params && !is_array($params)) | if ($params && !is_array($params)) { | ||||
| throw new Stripe_Error("You must pass an array as the first argument to Stripe API method calls. (HINT: an example call to create a charge would be: \"StripeCharge::create(array('amount' => 100, 'currency' => 'usd', 'card' => array('number' => 4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")"); | $message = "You must pass an array as the first argument to Stripe API " | ||||
| if ($apiKey && !is_string($apiKey)) | . "method calls. (HINT: an example call to create a charge " | ||||
| throw new Stripe_Error('The second argument to Stripe API method calls is an optional per-request apiKey, which must be a string. (HINT: you can set a global apiKey by "Stripe::setApiKey(<apiKey>)")'); | . "would be: \"StripeCharge::create(array('amount' => 100, " | ||||
| . "'currency' => 'usd', 'card' => array('number' => " | |||||
| . "4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")"; | |||||
| throw new Stripe_Error($message); | |||||
| } | |||||
| if ($apiKey && !is_string($apiKey)) { | |||||
| $message = 'The second argument to Stripe API method calls is an ' | |||||
| . 'optional per-request apiKey, which must be a string. ' | |||||
| . '(HINT: you can set a global apiKey by ' | |||||
| . '"Stripe::setApiKey(<apiKey>)")'; | |||||
| throw new Stripe_Error($message); | |||||
| } | |||||
| } | } | ||||
| protected static function _scopedAll($class, $params=null, $apiKey=null) | protected static function _scopedAll($class, $params=null, $apiKey=null) | ||||
| { | { | ||||
| self::_validateCall('all', $params, $apiKey); | self::_validateCall('all', $params, $apiKey); | ||||
| $requestor = new Stripe_ApiRequestor($apiKey); | $requestor = new Stripe_ApiRequestor($apiKey); | ||||
| $url = self::classUrl($class); | $url = self::_scopedLsb($class, 'classUrl', $class); | ||||
| list($response, $apiKey) = $requestor->request('get', $url, $params); | list($response, $apiKey) = $requestor->request('get', $url, $params); | ||||
| return Stripe_Util::convertToStripeObject($response, $apiKey); | return Stripe_Util::convertToStripeObject($response, $apiKey); | ||||
| } | } | ||||
| protected static function _scopedCreate($class, $params=null, $apiKey=null) | protected static function _scopedCreate($class, $params=null, $apiKey=null) | ||||
| { | { | ||||
| self::_validateCall('create', $params, $apiKey); | self::_validateCall('create', $params, $apiKey); | ||||
| $requestor = new Stripe_ApiRequestor($apiKey); | $requestor = new Stripe_ApiRequestor($apiKey); | ||||
| $url = self::classUrl($class); | $url = self::_scopedLsb($class, 'classUrl', $class); | ||||
| list($response, $apiKey) = $requestor->request('post', $url, $params); | list($response, $apiKey) = $requestor->request('post', $url, $params); | ||||
| return Stripe_Util::convertToStripeObject($response, $apiKey); | return Stripe_Util::convertToStripeObject($response, $apiKey); | ||||
| } | } | ||||
| protected function _scopedSave($class) | protected function _scopedSave($class, $apiKey=null) | ||||
| { | { | ||||
| self::_validateCall('save'); | self::_validateCall('save'); | ||||
| if ($this->_unsavedValues) { | $requestor = new Stripe_ApiRequestor($apiKey); | ||||
| $requestor = new Stripe_ApiRequestor($this->_apiKey); | $params = $this->serializeParameters(); | ||||
| $params = array(); | |||||
| foreach ($this->_unsavedValues->toArray() as $k) | if (count($params) > 0) { | ||||
| $params[$k] = $this->$k; | |||||
| $url = $this->instanceUrl(); | $url = $this->instanceUrl(); | ||||
| list($response, $apiKey) = $requestor->request('post', $url, $params); | list($response, $apiKey) = $requestor->request('post', $url, $params); | ||||
| $this->refreshFrom($response, $apiKey); | $this->refreshFrom($response, $apiKey); | ||||
| } | } | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| protected function _scopedDelete($class, $params=null) | protected function _scopedDelete($class, $params=null) | ||||
| Show All 9 Lines | |||||