Skip to content

Commit

Permalink
Fix connect_base warning
Browse files Browse the repository at this point in the history
  • Loading branch information
richardm-stripe committed Jul 6, 2020
1 parent 7d25924 commit 5acf0ef
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
49 changes: 39 additions & 10 deletions lib/Service/OAuthService.php
Expand Up @@ -16,7 +16,7 @@ class OAuthService extends \Stripe\Service\AbstractService
*/
protected function requestConnect($method, $path, $params, $opts)
{
$opts = \Stripe\Util\RequestOptions::parse($opts);
$opts = $this->_parseOpts($opts);
$opts->apiBase = $this->_getBase($opts);

return $this->request($method, $path, $params, $opts);
Expand All @@ -34,7 +34,7 @@ public function authorizeUrl($params = null, $opts = null)
{
$params = $params ?: [];

$opts = \Stripe\Util\RequestOptions::parse($opts);
$opts = $this->_parseOpts($opts);
$base = $this->_getBase($opts);

$params['client_id'] = $this->_getClientId($params);
Expand Down Expand Up @@ -65,6 +65,16 @@ public function token($params = null, $opts = null)
return $this->requestConnect('post', '/oauth/token', $params, $opts);
}

/**
* Disconnects an account from your platform.
*
* @param null|array $params
* @param null|array $opts
*
* @throws \Stripe\Exception\OAuth\OAuthErrorException if the request fails
*
* @return StripeObject object containing the response from the API
*/
public function deauthorize($params = null, $opts = null)
{
$params = $params ?: [];
Expand Down Expand Up @@ -105,17 +115,36 @@ private function _getClientSecret($params = null)
return $this->client->getApiKey();
}

private function _getBase($opts)
/**
* @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
*
* @throws \Stripe\Exception\InvalidArgumentException
*
* @return \Stripe\Util\RequestOptions
*/
private function _parseOpts($opts)
{
// Throw an exception for the convenience of anybody migrating to
// \Stripe\Service\OAuthService from \Stripe\OAuth, where `connect_base`
// was the name of the parameter that behaves as `api_base` does here.
if (isset($opts->connect_base)) {
throw new \Stripe\Exception\InvalidArgumentException('Use `api_base`, not `connect_base`');
if (\is_array($opts)) {
if (\array_key_exists('connect_base', $opts)) {
// Throw an exception for the convenience of anybody migrating to
// \Stripe\Service\OAuthService from \Stripe\OAuth, where `connect_base`
// was the name of the parameter that behaves as `api_base` does here.
throw new \Stripe\Exception\InvalidArgumentException('Use `api_base`, not `connect_base`');
}
}

return (isset($opts->api_base)) ?
$opts['api_base'] :
return \Stripe\Util\RequestOptions::parse($opts);
}

/**
* @param \Stripe\Util\RequestOptions $opts
*
* @return string
*/
private function _getBase($opts)
{
return isset($opts->apiBase) ?
$opts->apiBase :
$this->client->getConnectBase();
}
}
8 changes: 8 additions & 0 deletions tests/Stripe/Service/OAuthServiceTest.php
Expand Up @@ -63,6 +63,14 @@ public function testAuthorizeUrlRaisesAuthenticationErrorWhenNoClientId()
$uriStr = $this->service->authorizeUrl();
}

public function testAuthorizeUrlRaisesInvalidArgumentExceptionWhenConnectBase()
{
$this->setUpService();
$this->expectException(\Stripe\Exception\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp('#Use `api_base`#');
$uriStr = $this->service->authorizeUrl(null, ['connect_base' => 'foo']);
}

public function testDeauthorizeRaisesAuthenticationErrorWhenNoClientId()
{
$this->setUpServiceWithNoClientId();
Expand Down

0 comments on commit 5acf0ef

Please sign in to comment.