Skip to content

Commit

Permalink
Warn if opts are in params
Browse files Browse the repository at this point in the history
  • Loading branch information
vcheung-stripe committed Oct 14, 2020
1 parent af383f8 commit 1fb4e74
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ApiRequestor
*/
private static $requestTelemetry;

private static $OPTIONS_KEYS = ['api_key', 'idempotency_key', 'stripe_account', 'stripe_version', 'api_base'];

/**
* ApiRequestor constructor.
*
Expand Down Expand Up @@ -350,6 +352,21 @@ private function _requestRaw($method, $url, $params, $headers)
$clientUAInfo = $this->httpClient()->getUserAgentInfo();
}

if ($params && \is_array($params)) {
$optionKeysInParams = \array_filter(
static::$OPTIONS_KEYS,
function ($key) use ($params) {
return \array_key_exists($key, $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.)', \join(', ', $optionKeysInParams));
\trigger_error($message, \E_USER_WARNING);
}
}

$absUrl = $this->_apiBase . $url;
$params = self::_encodeObjects($params);
$defaultHeaders = $this->_defaultHeaders($myApiKey, $clientUAInfo);
Expand Down
24 changes: 24 additions & 0 deletions tests/Stripe/BaseStripeClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,28 @@ public function testRequestCollectionThrowsForNonList()
$client = new BaseStripeClient(['api_key' => 'sk_test_client', 'api_base' => MOCK_URL]);
$client->requestCollection('get', '/v1/charges/ch_123', [], []);
}

/**
* @expectedException PHPUnit_Framework_Error_Warning
*/
public function testRequestWithOptsInParamsWarns()
{
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'stripe_account' => 'acct_123',
'api_base' => MOCK_URL,
]);
$charge = $client->request(
'get',
'/v1/charges/ch_123',
[
'api_key' => 'sk_test_client',
'stripe_account' => 'acct_123',
'api_base' => MOCK_URL
],
['stripe_account' => 'acct_456']
);
static::assertNotNull($charge);
static::assertSame('acct_456', $this->optsReflector->getValue($charge)->headers['Stripe-Account']);
}
}

0 comments on commit 1fb4e74

Please sign in to comment.