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