2021-09-23 03:46:10 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Auth\OAuth2;
|
|
|
|
|
|
|
|
use Appwrite\Auth\OAuth2;
|
2022-02-02 06:13:54 +13:00
|
|
|
use Utopia\Exception;
|
2021-09-23 03:46:10 +12:00
|
|
|
|
|
|
|
class Stripe extends OAuth2
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected array $user = [];
|
|
|
|
|
2022-02-01 23:42:11 +13:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected array $tokens = [];
|
2021-09-23 03:46:10 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected string $stripeAccountId = '';
|
2021-09-23 03:46:10 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected array $scopes = [
|
2021-09-23 03:46:10 +12:00
|
|
|
'read_write',
|
|
|
|
];
|
|
|
|
|
2022-05-13 03:56:20 +12:00
|
|
|
/**
|
|
|
|
* @var array
|
2021-09-23 03:46:10 +12:00
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
protected array $grantType = [
|
|
|
|
'authorize' => 'authorization_code',
|
|
|
|
'refresh' => 'refresh_token',
|
2021-09-23 03:46:10 +12:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
public function getName(): string
|
2021-09-23 03:46:10 +12:00
|
|
|
{
|
|
|
|
return 'stripe';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
public function getLoginURL(): string
|
2021-09-23 03:46:10 +12:00
|
|
|
{
|
2022-05-13 03:56:20 +12:00
|
|
|
return 'https://connect.stripe.com/oauth/authorize?' . \http_build_query([
|
2021-09-23 03:46:10 +12:00
|
|
|
'response_type' => 'code', // The only option at the moment is "code."
|
|
|
|
'client_id' => $this->appID,
|
|
|
|
'redirect_uri' => $this->callback,
|
|
|
|
'scope' => \implode(' ', $this->getScopes()),
|
|
|
|
'state' => \json_encode($this->state)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $code
|
|
|
|
*
|
2022-02-01 09:20:17 +13:00
|
|
|
* @return array
|
2021-09-23 03:46:10 +12:00
|
|
|
*/
|
2022-02-02 05:47:19 +13:00
|
|
|
protected function getTokens(string $code): array
|
2021-09-23 03:46:10 +12:00
|
|
|
{
|
2022-05-13 03:56:20 +12:00
|
|
|
if (empty($this->tokens)) {
|
2022-02-01 23:42:11 +13:00
|
|
|
$this->tokens = \json_decode($this->request(
|
|
|
|
'POST',
|
|
|
|
'https://connect.stripe.com/oauth/token',
|
|
|
|
[],
|
|
|
|
\http_build_query([
|
|
|
|
'grant_type' => $this->grantType['authorize'],
|
|
|
|
'code' => $code
|
|
|
|
])
|
|
|
|
), true);
|
|
|
|
|
|
|
|
$this->stripeAccountId = $this->tokens['stripe_user_id'];
|
2022-02-01 09:39:14 +13:00
|
|
|
}
|
2022-02-01 09:20:17 +13:00
|
|
|
|
2022-02-01 23:42:11 +13:00
|
|
|
return $this->tokens;
|
2021-09-23 03:46:10 +12:00
|
|
|
}
|
|
|
|
|
2022-02-02 04:54:20 +13:00
|
|
|
/**
|
|
|
|
* @param string $refreshToken
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
public function refreshTokens(string $refreshToken): array
|
2022-02-02 04:54:20 +13:00
|
|
|
{
|
2022-02-02 06:13:54 +13:00
|
|
|
$this->tokens = \json_decode($this->request(
|
|
|
|
'POST',
|
|
|
|
'https://connect.stripe.com/oauth/token',
|
|
|
|
[],
|
|
|
|
\http_build_query([
|
|
|
|
'grant_type' => $this->grantType['refresh'],
|
|
|
|
'refresh_token' => $refreshToken,
|
|
|
|
])
|
|
|
|
), true);
|
|
|
|
|
2022-05-13 03:56:20 +12:00
|
|
|
if (empty($this->tokens['refresh_token'])) {
|
2022-02-04 05:05:06 +13:00
|
|
|
$this->tokens['refresh_token'] = $refreshToken;
|
|
|
|
}
|
|
|
|
|
2022-02-02 06:13:54 +13:00
|
|
|
$this->stripeAccountId = $this->tokens['stripe_user_id'];
|
2022-02-02 04:54:20 +13:00
|
|
|
return $this->tokens;
|
|
|
|
}
|
|
|
|
|
2021-09-23 03:46:10 +12:00
|
|
|
/**
|
2022-05-13 03:56:20 +12:00
|
|
|
* @param string $accessToken
|
2021-09-23 03:46:10 +12:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
public function getUserID(string $accessToken): string
|
2021-09-23 03:46:10 +12:00
|
|
|
{
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
2022-05-13 03:56:20 +12:00
|
|
|
return $user['id'] ?? '';
|
2021-09-23 03:46:10 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-13 03:56:20 +12:00
|
|
|
* @param string $accessToken
|
2021-09-23 03:46:10 +12:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
public function getUserEmail(string $accessToken): string
|
2021-09-23 03:46:10 +12:00
|
|
|
{
|
|
|
|
$user = $this->getUser($accessToken);
|
2022-05-13 03:56:20 +12:00
|
|
|
|
|
|
|
if (empty($user)) {
|
|
|
|
return '';
|
2021-09-23 03:46:10 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
return $user['email'] ?? '';
|
|
|
|
}
|
|
|
|
|
2022-04-28 07:08:32 +12:00
|
|
|
/**
|
2022-04-28 07:14:09 +12:00
|
|
|
* Check if the OAuth email is verified
|
2022-05-24 02:54:50 +12:00
|
|
|
*
|
2022-04-28 08:25:28 +12:00
|
|
|
* If present, the email is verified. This was verfied through a manual Stripe sign up process
|
2022-05-24 02:54:50 +12:00
|
|
|
*
|
2022-05-13 03:56:20 +12:00
|
|
|
* @param string $accessToken
|
2022-05-24 02:54:50 +12:00
|
|
|
*
|
2022-04-28 07:08:32 +12:00
|
|
|
* @return bool
|
|
|
|
*/
|
2022-04-28 08:27:21 +12:00
|
|
|
public function isEmailVerified(string $accessToken): bool
|
2022-04-28 07:08:32 +12:00
|
|
|
{
|
2022-04-28 08:10:48 +12:00
|
|
|
$email = $this->getUserEmail($accessToken);
|
2022-04-28 08:25:28 +12:00
|
|
|
|
2022-04-28 08:10:48 +12:00
|
|
|
return !empty($email);
|
2022-04-28 07:08:32 +12:00
|
|
|
}
|
|
|
|
|
2021-09-23 03:46:10 +12:00
|
|
|
/**
|
2022-05-13 03:56:20 +12:00
|
|
|
* @param string $accessToken
|
2021-09-23 03:46:10 +12:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-05-13 03:56:20 +12:00
|
|
|
public function getUserName(string $accessToken): string
|
2021-09-23 03:46:10 +12:00
|
|
|
{
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
2022-05-13 03:56:20 +12:00
|
|
|
return $user['name'] ?? '';
|
2021-09-23 03:46:10 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accessToken
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getUser(string $accessToken)
|
|
|
|
{
|
|
|
|
if (empty($this->user) && !empty($this->stripeAccountId)) {
|
|
|
|
$this->user = \json_decode(
|
2022-05-13 03:56:20 +12:00
|
|
|
$this->request(
|
|
|
|
'GET',
|
|
|
|
'https://api.stripe.com/v1/accounts/' . $this->stripeAccountId,
|
|
|
|
['Authorization: Bearer ' . \urlencode($accessToken)]
|
|
|
|
),
|
|
|
|
true
|
2021-09-23 03:46:10 +12:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
}
|