Skip to content

Commit

Permalink
Merge pull request #1704 from stripe/helenye/merge-php-beta
Browse files Browse the repository at this point in the history
Merge changes from stripe-php master
  • Loading branch information
helenye-stripe authored May 29, 2024
2 parents 8661523 + 97d97ce commit 3533dc3
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## 14.9.0-beta.1 - 2024-05-23
* [#1696](https://github.com/stripe/stripe-php/pull/1696) Update generated code for beta

## 14.8.0 - 2024-05-23
* [#1698](https://github.com/stripe/stripe-php/pull/1698) Update generated code
* Add support for new value `terminal_reader_invalid_location_for_payment` on enum `StripeError.code`
* [#1697](https://github.com/stripe/stripe-php/pull/1697) Rename section for object type generation

## 14.8.0-beta.1 - 2024-05-16
* [#1693](https://github.com/stripe/stripe-php/pull/1693) Update generated code for beta
Expand Down
2 changes: 1 addition & 1 deletion OPENAPI_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1044
v1044
61 changes: 37 additions & 24 deletions lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class ApiRequestor
*/
private $_apiBase;

/**
* @var null|array
*/
private $_appInfo;

/**
* @var HttpClient\ClientInterface
*/
Expand All @@ -38,14 +43,16 @@ class ApiRequestor
*
* @param null|string $apiKey
* @param null|string $apiBase
* @param null|array $appInfo
*/
public function __construct($apiKey = null, $apiBase = null)
public function __construct($apiKey = null, $apiBase = null, $appInfo = null)
{
$this->_apiKey = $apiKey;
if (!$apiBase) {
$apiBase = Stripe::$apiBase;
}
$this->_apiBase = $apiBase;
$this->_appInfo = $appInfo;
}

/**
Expand Down Expand Up @@ -125,7 +132,7 @@ public function request($method, $url, $params = null, $headers = null, $apiMode
$params = $params ?: [];
$headers = $headers ?: [];
list($rbody, $rcode, $rheaders, $myApiKey) =
$this->_requestRaw($method, $url, $params, $headers, $apiMode, $usage);
$this->_requestRaw($method, $url, $params, $headers, $apiMode, $usage);
$json = $this->_interpretResponse($rbody, $rcode, $rheaders);
$resp = new ApiResponse($rbody, $rcode, $rheaders, $json);

Expand All @@ -148,7 +155,7 @@ public function requestStream($method, $url, $readBodyChunkCallable, $params = n
$params = $params ?: [];
$headers = $headers ?: [];
list($rbody, $rcode, $rheaders, $myApiKey) =
$this->_requestRawStreaming($method, $url, $params, $headers, $apiMode, $usage, $readBodyChunkCallable);
$this->_requestRawStreaming($method, $url, $params, $headers, $apiMode, $usage, $readBodyChunkCallable);
if ($rcode >= 300) {
$this->_interpretResponse($rbody, $rcode, $rheaders);
}
Expand All @@ -167,7 +174,7 @@ public function handleErrorResponse($rbody, $rcode, $rheaders, $resp)
{
if (!\is_array($resp) || !isset($resp['error'])) {
$msg = "Invalid response object from API: {$rbody} "
. "(HTTP response code was {$rcode})";
. "(HTTP response code was {$rcode})";

throw new Exception\UnexpectedValueException($msg);
}
Expand Down Expand Up @@ -215,7 +222,7 @@ private static function _specificAPIError($rbody, $rcode, $rheaders, $resp, $err
return Exception\IdempotencyException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code);
}

// no break
// no break
case 404:
return Exception\InvalidRequestException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code, $param);

Expand Down Expand Up @@ -286,10 +293,10 @@ private static function _formatAppInfo($appInfo)
{
if (null !== $appInfo) {
$string = $appInfo['name'];
if (null !== $appInfo['version']) {
if (\array_key_exists('version', $appInfo) && null !== $appInfo['version']) {
$string .= '/' . $appInfo['version'];
}
if (null !== $appInfo['url']) {
if (\array_key_exists('url', $appInfo) && null !== $appInfo['url']) {
$string .= ' (' . $appInfo['url'] . ')';
}

Expand Down Expand Up @@ -322,20 +329,22 @@ private static function _isDisabled($disableFunctionsOutput, $functionName)
/**
* @static
*
* @param string $apiKey
* @param null $clientInfo
* @param string $apiKey the Stripe API key, to be used in regular API requests
* @param null $clientInfo client user agent information
* @param null $appInfo information to identify a plugin that integrates Stripe using this library
*
* @return array
*/
private static function _defaultHeaders($apiKey, $clientInfo = null)
private static function _defaultHeaders($apiKey, $clientInfo = null, $appInfo = null)
{
$uaString = 'Stripe/v1 PhpBindings/' . Stripe::VERSION;

$langVersion = \PHP_VERSION;
$uname_disabled = self::_isDisabled(\ini_get('disable_functions'), 'php_uname');
$uname = $uname_disabled ? '(disabled)' : \php_uname();

$appInfo = Stripe::getAppInfo();
// Fallback to global configuration to maintain backwards compatibility.
$appInfo = $appInfo ?: Stripe::getAppInfo();
$ua = [
'bindings_version' => Stripe::VERSION,
'lang' => 'php',
Expand Down Expand Up @@ -375,9 +384,9 @@ private function _prepareRequest($method, $url, $params, $headers, $apiMode)

if (!$myApiKey) {
$msg = 'No API key provided. (HINT: set your API key using '
. '"Stripe::setApiKey(<API-KEY>)". You can generate API keys from '
. 'the Stripe web interface. See https://stripe.com/api for '
. 'details, or email support@stripe.com if you have any questions.';
. '"Stripe::setApiKey(<API-KEY>)". You can generate API keys from '
. 'the Stripe web interface. See https://stripe.com/api for '
. 'details, or email support@stripe.com if you have any questions.';

throw new Exception\AuthenticationException($msg);
}
Expand All @@ -399,8 +408,8 @@ function ($key) use ($params) {
);
if (\count($optionKeysInParams) > 0) {
$message = \sprintf('Options found in $params: %s. Options should '
. 'be passed in their own array after $params. (HINT: pass an '
. 'empty array to $params if you do not have any.)', \implode(', ', $optionKeysInParams));
. 'be passed in their own array after $params. (HINT: pass an '
. 'empty array to $params if you do not have any.)', \implode(', ', $optionKeysInParams));
\trigger_error($message, \E_USER_WARNING);
}
}
Expand All @@ -409,7 +418,7 @@ function ($key) use ($params) {
if ('standard' === $apiMode) {
$params = self::_encodeObjects($params);
}
$defaultHeaders = $this->_defaultHeaders($myApiKey, $clientUAInfo);
$defaultHeaders = $this->_defaultHeaders($myApiKey, $clientUAInfo, $this->_appInfo);

if (Stripe::$accountId) {
$defaultHeaders['Stripe-Account'] = Stripe::$accountId;
Expand Down Expand Up @@ -477,9 +486,11 @@ private function _requestRaw($method, $url, $params, $headers, $apiMode, $usage)
$apiMode
);

if (isset($rheaders['request-id'])
&& \is_string($rheaders['request-id'])
&& '' !== $rheaders['request-id']) {
if (
isset($rheaders['request-id'])
&& \is_string($rheaders['request-id'])
&& '' !== $rheaders['request-id']
) {
self::$requestTelemetry = new RequestTelemetry(
$rheaders['request-id'],
Util\Util::currentTimeMillis() - $requestStartMs,
Expand Down Expand Up @@ -519,9 +530,11 @@ private function _requestRawStreaming($method, $url, $params, $headers, $apiMode
$readBodyChunkCallable
);

if (isset($rheaders['request-id'])
&& \is_string($rheaders['request-id'])
&& '' !== $rheaders['request-id']) {
if (
isset($rheaders['request-id'])
&& \is_string($rheaders['request-id'])
&& '' !== $rheaders['request-id']
) {
self::$requestTelemetry = new RequestTelemetry(
$rheaders['request-id'],
Util\Util::currentTimeMillis() - $requestStartMs
Expand Down Expand Up @@ -573,7 +586,7 @@ private function _interpretResponse($rbody, $rcode, $rheaders)
$jsonError = \json_last_error();
if (null === $resp && \JSON_ERROR_NONE !== $jsonError) {
$msg = "Invalid response body from API: {$rbody} "
. "(HTTP response code was {$rcode}, json_last_error() was {$jsonError})";
. "(HTTP response code was {$rcode}, json_last_error() was {$jsonError})";

throw new Exception\UnexpectedValueException($msg, $rcode);
}
Expand Down
31 changes: 28 additions & 3 deletions lib/BaseStripeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class BaseStripeClient implements StripeClientInterface, StripeStreamingClientIn
/** @var array<string, null|string> */
const DEFAULT_CONFIG = [
'api_key' => null,
'app_info' => null,
'client_id' => null,
'stripe_account' => null,
'stripe_version' => \Stripe\Util\ApiVersion::CURRENT,
Expand All @@ -42,10 +43,12 @@ class BaseStripeClient implements StripeClientInterface, StripeStreamingClientIn
* Configuration settings include the following options:
*
* - api_key (null|string): the Stripe API key, to be used in regular API requests.
* - app_info (null|array): information to identify a plugin that integrates Stripe using this library.
* Expects: array{name: string, version?: string, url?: string, partner_id?: string}
* - client_id (null|string): the Stripe client ID, to be used in OAuth requests.
* - stripe_account (null|string): a Stripe account ID. If set, all requests sent by the client
* will automatically use the {@code Stripe-Account} header with that account ID.
* - stripe_version (null|string): a Stripe API verion. If set, all requests sent by the client
* - stripe_version (null|string): a Stripe API version. If set, all requests sent by the client
* will include the {@code Stripe-Version} header with that API version.
*
* The following configuration settings are also available, though setting these should rarely be necessary
Expand Down Expand Up @@ -132,6 +135,16 @@ public function getFilesBase()
return $this->config['files_base'];
}

/**
* Gets the app info for this client.
*
* @return null|array information to identify a plugin that integrates Stripe using this library
*/
public function getAppInfo()
{
return $this->config['app_info'];
}

/**
* Sends a request to Stripe's API.
*
Expand All @@ -146,7 +159,7 @@ public function request($method, $path, $params, $opts)
{
$opts = $this->defaultOpts->merge($opts, true);
$baseUrl = $opts->apiBase ?: $this->getApiBase();
$requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
$requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl, $this->getAppInfo());
list($response, $opts->apiKey) = $requestor->request($method, $path, $params, $opts->headers, 'standard', ['stripe_client']);
$opts->discardNonPersistentHeaders();
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
Expand Down Expand Up @@ -218,7 +231,7 @@ public function requestStream($method, $path, $readBodyChunkCallable, $params, $
{
$opts = $this->defaultOpts->merge($opts, true);
$baseUrl = $opts->apiBase ?: $this->getApiBase();
$requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
$requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl, $this->getAppInfo());
list($response, $opts->apiKey) = $requestor->requestStream($method, $path, $readBodyChunkCallable, $params, $opts->headers, 'standard', ['stripe_client']);
}

Expand Down Expand Up @@ -346,6 +359,18 @@ private function validateConfig($config)
throw new \Stripe\Exception\InvalidArgumentException('files_base must be a string');
}

// app info
if (null !== $config['app_info'] && !\is_array($config['app_info'])) {
throw new \Stripe\Exception\InvalidArgumentException('app_info must be an array');
}

$appInfoKeys = ['name', 'version', 'url', 'partner_id'];
if (null !== $config['app_info'] && array_diff_key($config['app_info'], array_flip($appInfoKeys))) {
$msg = 'app_info must be of type array{name: string, version?: string, url?: string, partner_id?: string}';

throw new \Stripe\Exception\InvalidArgumentException($msg);
}

// check absence of extra keys
$extraConfigKeys = \array_diff(\array_keys($config), \array_keys(self::DEFAULT_CONFIG));
if (!empty($extraConfigKeys)) {
Expand Down
Loading

0 comments on commit 3533dc3

Please sign in to comment.