|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Tharanga Kothalawala <[email protected]> |
|
4
|
|
|
* @date 15-02-2021 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace TSK\SSO\ThirdParty\Stripe; |
|
8
|
|
|
|
|
9
|
|
|
use Stripe\Account; |
|
|
|
|
|
|
10
|
|
|
use Stripe\Exception\ApiErrorException; |
|
|
|
|
|
|
11
|
|
|
use Stripe\Exception\OAuth\OAuthErrorException; |
|
|
|
|
|
|
12
|
|
|
use Stripe\OAuth; |
|
|
|
|
|
|
13
|
|
|
use TSK\SSO\ThirdParty; |
|
14
|
|
|
use TSK\SSO\ThirdParty\CommonAccessToken; |
|
15
|
|
|
use TSK\SSO\ThirdParty\Exception\NoThirdPartyEmailFoundException; |
|
16
|
|
|
use TSK\SSO\ThirdParty\Exception\ThirdPartyConnectionFailedException; |
|
17
|
|
|
use TSK\SSO\ThirdParty\ThirdPartyUser; |
|
18
|
|
|
use TSK\SSO\ThirdParty\VendorConnection; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @codeCoverageIgnore |
|
22
|
|
|
* @package TSK\SSO\ThirdParty\Stripe |
|
23
|
|
|
* @see https://stripe.com/docs/connect/oauth-standard-accounts |
|
24
|
|
|
*/ |
|
25
|
|
|
class StripeConnection implements VendorConnection |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var StripeConfiguration |
|
29
|
|
|
*/ |
|
30
|
|
|
private $configuration; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* StripeConnectConnection constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param StripeConfiguration $configuration |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(StripeConfiguration $configuration) |
|
38
|
|
|
{ |
|
39
|
|
|
if (class_exists('\Stripe\Stripe')) { |
|
40
|
|
|
\Stripe\Stripe::setApiKey($configuration->clientSecret()); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
$this->configuration = $configuration; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Use this to get a link to redirect a user to the third party login |
|
47
|
|
|
* |
|
48
|
|
|
* @return string|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getGrantUrl() |
|
51
|
|
|
{ |
|
52
|
|
|
return sprintf( |
|
53
|
|
|
"https://connect.stripe.com/oauth/authorize?response_type=code&client_id=%s&scope=read_write&state=%s&redirect_uri=%s", |
|
54
|
|
|
$this->configuration->clientId(), |
|
55
|
|
|
$this->configuration->ourSecretState(), |
|
56
|
|
|
$this->configuration->redirectUrl() |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Grants a new access token |
|
62
|
|
|
* |
|
63
|
|
|
* @return CommonAccessToken |
|
64
|
|
|
* @throws ThirdPartyConnectionFailedException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function grantNewAccessToken() |
|
67
|
|
|
{ |
|
68
|
|
|
if (empty($_GET['code']) |
|
69
|
|
|
|| empty($_GET['state']) |
|
70
|
|
|
|| $_GET['state'] !== $this->configuration->ourSecretState() |
|
71
|
|
|
) { |
|
72
|
|
|
throw new ThirdPartyConnectionFailedException('Invalid request!'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
|
|
$response = OAuth::token([ |
|
77
|
|
|
'grant_type' => 'authorization_code', |
|
78
|
|
|
'code' => $_GET['code'], |
|
79
|
|
|
]); |
|
80
|
|
|
} catch (OAuthErrorException $ex) { |
|
81
|
|
|
throw new ThirdPartyConnectionFailedException($ex->getMessage(), $ex->getCode(), $ex); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return new CommonAccessToken( |
|
85
|
|
|
$response->access_token, |
|
86
|
|
|
ThirdParty::STRIPE, |
|
87
|
|
|
$response->stripe_user_id |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Use this to retrieve the current user's third party user data using there existing granted access token |
|
93
|
|
|
* |
|
94
|
|
|
* @param CommonAccessToken $accessToken |
|
95
|
|
|
* |
|
96
|
|
|
* @return ThirdPartyUser |
|
97
|
|
|
* @throws NoThirdPartyEmailFoundException |
|
98
|
|
|
* @throws ThirdPartyConnectionFailedException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getSelf(CommonAccessToken $accessToken) |
|
101
|
|
|
{ |
|
102
|
|
|
try { |
|
103
|
|
|
$account = Account::retrieve($accessToken->email()); |
|
104
|
|
|
} catch (ApiErrorException $ex) { |
|
105
|
|
|
throw new ThirdPartyConnectionFailedException($ex->getMessage(), $ex->getCode(), $ex); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (empty($account->id)) { |
|
109
|
|
|
throw new NoThirdPartyEmailFoundException("Stripe account and/or email address cannot be retrieved"); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return new ThirdPartyUser( |
|
113
|
|
|
$account->id, |
|
114
|
|
|
!empty($account->business_profile->name) ? $account->business_profile->name : '', |
|
115
|
|
|
!empty($account->email) ? $account->email : '' |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Use this to revoke the access to the third party data. |
|
121
|
|
|
* This will completely remove the access from the vendor side. |
|
122
|
|
|
* |
|
123
|
|
|
* @param CommonAccessToken $accessToken |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
public function revokeAccess(CommonAccessToken $accessToken) |
|
128
|
|
|
{ |
|
129
|
|
|
try { |
|
130
|
|
|
OAuth::deauthorize([ |
|
131
|
|
|
'client_id' => $this->configuration->clientId(), |
|
132
|
|
|
'stripe_user_id' => $accessToken->email(), |
|
133
|
|
|
]); |
|
134
|
|
|
} catch (OAuthErrorException $ex) { |
|
135
|
|
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths