1
0
Fork 0
mirror of synced 2024-09-29 08:51:28 +13:00
appwrite/src/Appwrite/Auth/OAuth2/Stripe.php

188 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Auth\OAuth2;
use Appwrite\Auth\OAuth2;
use Utopia\Exception;
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 = [];
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
protected string $stripeAccountId = '';
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $scopes = [
'read_write',
];
2022-05-13 03:56:20 +12:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $grantType = [
'authorize' => 'authorization_code',
'refresh' => 'refresh_token',
];
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getName(): string
{
return 'stripe';
}
/**
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getLoginURL(): string
{
2022-05-13 03:56:20 +12:00
return 'https://connect.stripe.com/oauth/authorize?' . \http_build_query([
'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
*/
2022-02-02 05:47:19 +13:00
protected function getTokens(string $code): array
{
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;
}
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
{
$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'])) {
$this->tokens['refresh_token'] = $refreshToken;
}
$this->stripeAccountId = $this->tokens['stripe_user_id'];
2022-02-02 04:54:20 +13:00
return $this->tokens;
}
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserID(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['id'] ?? '';
}
/**
2022-05-13 03:56:20 +12:00
* @param string $accessToken
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserEmail(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
if (empty($user)) {
return '';
}
return $user['email'] ?? '';
}
/**
* 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
*
* @return bool
*/
2022-04-28 08:27:21 +12:00
public function isEmailVerified(string $accessToken): bool
{
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-05-13 03:56:20 +12:00
* @param string $accessToken
*
* @return string
*/
2022-05-13 03:56:20 +12:00
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['name'] ?? '';
}
/**
* @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
);
}
return $this->user;
}
}