M2_Stripe_ApiResource

The WordPress Core M2 Stripe ApiResource class.

Defined (1)

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

/lib/stripe-php/lib/Stripe/ApiResource.php  
  1. abstract class M2_Stripe_ApiResource extends M2_Stripe_Object 
  2. protected static function _scopedRetrieve($class, $id, $apiKey=null) 
  3. if ( false === stripos( $class, 'M2_' ) ) { 
  4. $class = 'M2_' . $class; 
  5. $instance = new $class($id, $apiKey); 
  6. $instance->refresh(); 
  7. return $instance; 
  8.  
  9. /** 
  10. * @returns Stripe_ApiResource The refreshed resource. 
  11. */ 
  12. public function refresh() 
  13. $requestor = new M2_Stripe_ApiRequestor($this->_apiKey); 
  14. $url = $this->instanceUrl(); 
  15.  
  16. list($response, $apiKey) = $requestor->request( 
  17. 'get',  
  18. $url,  
  19. $this->_retrieveOptions 
  20. ); 
  21. $this->refreshFrom($response, $apiKey); 
  22. return $this; 
  23.  
  24. /** 
  25. * @param string $class 
  26. * @returns string The name of the class, with namespacing and underscores 
  27. * stripped. 
  28. */ 
  29. public static function className($class) 
  30. // Useful for namespaces: Foo\Stripe_Charge 
  31. if ($postfix = strrchr($class, '\\')) { 
  32. $class = substr($postfix, 1); 
  33. if (substr($class, 0, strlen('Stripe')) == 'Stripe') { 
  34. $class = substr($class, strlen('Stripe')); 
  35. } elseif (substr($class, 0, strlen('M2_Stripe')) == 'M2_Stripe') { 
  36. $class = substr($class, strlen('M2_Stripe')); 
  37. $class = str_replace('_', '', $class); 
  38. $name = urlencode($class); 
  39. $name = strtolower($name); 
  40. return $name; 
  41.  
  42. /** 
  43. * @param string $class 
  44. * @returns string The endpoint URL for the given class. 
  45. */ 
  46. public static function classUrl($class) 
  47. $base = str_replace( 'M2_', '', $class ); 
  48. $base = self::_scopedLsb($class, 'className', $base); 
  49. return "/v1/${base}s"; 
  50.  
  51. /** 
  52. * @returns string The full API URL for this API resource. 
  53. */ 
  54. public function instanceUrl() 
  55. $id = $this['id']; 
  56. $class = get_class($this); 
  57. if ($id === null) { 
  58. $message = "Could not determine which URL to request: " 
  59. . "$class instance has invalid ID: $id"; 
  60. throw new M2_Stripe_InvalidRequestError($message, null); 
  61. $id = M2_Stripe_ApiRequestor::utf8($id); 
  62. $base = $this->_lsb('classUrl', $class); 
  63. $extn = urlencode($id); 
  64. return "$base/$extn"; 
  65.  
  66. private static function _validateCall($method, $params=null, $apiKey=null) 
  67. if ($params && !is_array($params)) { 
  68. $message = "You must pass an array as the first argument to Stripe API " 
  69. . "method calls. (HINT: an example call to create a charge " 
  70. . "would be: \"M2_StripeCharge::create(array('amount' => 100, " 
  71. . "'currency' => 'usd', 'card' => array('number' => " 
  72. . "4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")"; 
  73. throw new M2_Stripe_Error($message); 
  74.  
  75. if ($apiKey && !is_string($apiKey)) { 
  76. $message = 'The second argument to Stripe API method calls is an ' 
  77. . 'optional per-request apiKey, which must be a string. ' 
  78. . '(HINT: you can set a global apiKey by ' 
  79. . '"M2_Stripe::setApiKey(<apiKey>)")'; 
  80. throw new M2_Stripe_Error($message); 
  81.  
  82. protected static function _scopedAll($class, $params=null, $apiKey=null) 
  83. self::_validateCall('all', $params, $apiKey); 
  84. $requestor = new M2_Stripe_ApiRequestor($apiKey); 
  85. $url = self::_scopedLsb($class, 'classUrl', $class); 
  86. list($response, $apiKey) = $requestor->request('get', $url, $params); 
  87. return M2_Stripe_Util::convertToStripeObject($response, $apiKey); 
  88.  
  89. protected static function _scopedCreate($class, $params=null, $apiKey=null) 
  90. self::_validateCall('create', $params, $apiKey); 
  91. $requestor = new M2_Stripe_ApiRequestor($apiKey); 
  92. $url = self::_scopedLsb($class, 'classUrl', $class); 
  93. list($response, $apiKey) = $requestor->request('post', $url, $params); 
  94. return M2_Stripe_Util::convertToStripeObject($response, $apiKey); 
  95.  
  96. protected function _scopedSave($class, $apiKey=null) 
  97. self::_validateCall('save'); 
  98. $requestor = new M2_Stripe_ApiRequestor($apiKey); 
  99. $params = $this->serializeParameters(); 
  100.  
  101. if (count($params) > 0) { 
  102. $url = $this->instanceUrl(); 
  103. list($response, $apiKey) = $requestor->request('post', $url, $params); 
  104. $this->refreshFrom($response, $apiKey); 
  105. return $this; 
  106.  
  107. protected function _scopedDelete($class, $params=null) 
  108. self::_validateCall('delete'); 
  109. $requestor = new M2_Stripe_ApiRequestor($this->_apiKey); 
  110. $url = $this->instanceUrl(); 
  111. list($response, $apiKey) = $requestor->request('delete', $url, $params); 
  112. $this->refreshFrom($response, $apiKey); 
  113. return $this;