Skip to content

Commit e791309

Browse files
authored
Stripe OAuth connection - Basic (#230)
Merge pull request #230 * Stripe OAuth connection - Basic * Add read_write scope * Stripe OAuth connection - Tests * Update README.md
1 parent ee6765b commit e791309

3 files changed

Lines changed: 273 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Included service implementations
8282
- SoundCloud
8383
- Spotify
8484
- Strava
85+
- Stripe
8586
- Ustream
8687
- Vimeo
8788
- Vkontakte
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace OAuth\OAuth2\Service;
4+
5+
use OAuth\OAuth2\Token\StdOAuth2Token;
6+
use OAuth\Common\Http\Exception\TokenResponseException;
7+
use OAuth\Common\Http\Uri\Uri;
8+
use OAuth\Common\Consumer\CredentialsInterface;
9+
use OAuth\Common\Http\Client\ClientInterface;
10+
use OAuth\Common\Storage\TokenStorageInterface;
11+
use OAuth\Common\Http\Uri\UriInterface;
12+
13+
class Stripe extends AbstractService
14+
{
15+
const SCOPE_READONLY = 'read_only';
16+
const SCOPE_READWRITE = 'read_write';
17+
18+
public function __construct(
19+
CredentialsInterface $credentials,
20+
ClientInterface $httpClient,
21+
TokenStorageInterface $storage,
22+
$scopes = array(),
23+
UriInterface $baseApiUri = null
24+
) {
25+
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
26+
27+
if (null === $baseApiUri) {
28+
$this->baseApiUri = new Uri('https://connect.stripe.com/');
29+
}
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getAuthorizationEndpoint()
36+
{
37+
return new Uri('https://connect.stripe.com/oauth/authorize');
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function getAccessTokenEndpoint()
44+
{
45+
return new Uri('https://connect.stripe.com/oauth/token');
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
protected function getAuthorizationMethod()
52+
{
53+
return static::AUTHORIZATION_METHOD_QUERY_STRING;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
protected function parseAccessTokenResponse($responseBody)
60+
{
61+
$data = json_decode($responseBody, true);
62+
63+
if (null === $data || ! is_array($data)) {
64+
throw new TokenResponseException('Unable to parse response.');
65+
} elseif (isset($data['error'])) {
66+
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
67+
}
68+
69+
$token = new StdOAuth2Token();
70+
$token->setAccessToken($data['access_token']);
71+
72+
unset($data['access_token']);
73+
74+
$token->setExtraParams($data);
75+
76+
return $token;
77+
}
78+
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
3+
namespace OAuthTest\Unit\OAuth2\Service;
4+
5+
use OAuth\OAuth2\Service\Stripe;
6+
use OAuth\Common\Token\TokenInterface;
7+
8+
class StripeTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @covers OAuth\OAuth2\Service\Stripe::__construct
12+
*/
13+
public function testConstructCorrectInterfaceWithoutCustomUri()
14+
{
15+
$service = new Stripe(
16+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
17+
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
18+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
19+
);
20+
21+
$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
22+
}
23+
24+
/**
25+
* @covers OAuth\OAuth2\Service\Stripe::__construct
26+
*/
27+
public function testConstructCorrectInstanceWithoutCustomUri()
28+
{
29+
$service = new Stripe(
30+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
31+
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
32+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
33+
);
34+
35+
$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
36+
}
37+
38+
/**
39+
* @covers OAuth\OAuth2\Service\Stripe::__construct
40+
*/
41+
public function testConstructCorrectInstanceWithCustomUri()
42+
{
43+
$service = new Stripe(
44+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
45+
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
46+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
47+
array(),
48+
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
49+
);
50+
51+
$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
52+
}
53+
54+
/**
55+
* @covers OAuth\OAuth2\Service\Stripe::__construct
56+
* @covers OAuth\OAuth2\Service\Stripe::getAuthorizationEndpoint
57+
*/
58+
public function testGetAuthorizationEndpoint()
59+
{
60+
$service = new Stripe(
61+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
62+
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
63+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
64+
);
65+
66+
$this->assertSame(
67+
'https://connect.stripe.com/oauth/authorize',
68+
$service->getAuthorizationEndpoint()->getAbsoluteUri()
69+
);
70+
}
71+
72+
/**
73+
* @covers OAuth\OAuth2\Service\Stripe::__construct
74+
* @covers OAuth\OAuth2\Service\Stripe::getAccessTokenEndpoint
75+
*/
76+
public function testGetAccessTokenEndpoint()
77+
{
78+
$service = new Stripe(
79+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
80+
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
81+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
82+
);
83+
84+
$this->assertSame(
85+
'https://connect.stripe.com/oauth/token',
86+
$service->getAccessTokenEndpoint()->getAbsoluteUri()
87+
);
88+
}
89+
90+
/**
91+
* @covers OAuth\OAuth2\Service\Stripe::__construct
92+
* @covers OAuth\OAuth2\Service\Stripe::getAuthorizationMethod
93+
*/
94+
public function testGetAuthorizationMethod()
95+
{
96+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
97+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0));
98+
99+
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
100+
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
101+
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
102+
103+
$storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
104+
$storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
105+
106+
$service = new Stripe(
107+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
108+
$client,
109+
$storage
110+
);
111+
112+
$uri = $service->request('https://luisrpalanca.com/my/awesome/path');
113+
$absoluteUri = parse_url($uri->getAbsoluteUri());
114+
115+
$this->assertSame('access_token=foo', $absoluteUri['query']);
116+
}
117+
118+
/**
119+
* @covers OAuth\OAuth2\Service\Stripe::__construct
120+
* @covers OAuth\OAuth2\Service\Stripe::parseAccessTokenResponse
121+
*/
122+
public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
123+
{
124+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
125+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
126+
127+
$service = new Stripe(
128+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
129+
$client,
130+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
131+
);
132+
133+
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
134+
135+
$service->requestAccessToken('foo');
136+
}
137+
138+
/**
139+
* @covers OAuth\OAuth2\Service\Stripe::__construct
140+
* @covers OAuth\OAuth2\Service\Stripe::parseAccessTokenResponse
141+
*/
142+
public function testParseAccessTokenResponseThrowsExceptionOnError()
143+
{
144+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
145+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));
146+
147+
$service = new Stripe(
148+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
149+
$client,
150+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
151+
);
152+
153+
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
154+
155+
$service->requestAccessToken('foo');
156+
}
157+
158+
/**
159+
* @covers OAuth\OAuth2\Service\Stripe::__construct
160+
* @covers OAuth\OAuth2\Service\Stripe::parseAccessTokenResponse
161+
*/
162+
public function testParseAccessTokenResponseValidWithoutRefreshToken()
163+
{
164+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
165+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
166+
167+
$service = new Stripe(
168+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
169+
$client,
170+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
171+
);
172+
173+
$this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
174+
}
175+
176+
/**
177+
* @covers OAuth\OAuth2\Service\Stripe::__construct
178+
* @covers OAuth\OAuth2\Service\Stripe::parseAccessTokenResponse
179+
*/
180+
public function testParseAccessTokenResponseValidWithRefreshToken()
181+
{
182+
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
183+
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'));
184+
185+
$service = new Stripe(
186+
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
187+
$client,
188+
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
189+
);
190+
191+
$this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
192+
}
193+
}
194+

0 commit comments

Comments
 (0)