1
0
Fork 0
mirror of synced 2024-09-28 15:31:43 +12:00
appwrite/src/Appwrite/Auth/OAuth2/Paypal.php

214 lines
4.9 KiB
PHP
Raw Normal View History

2020-01-21 00:44:16 +13:00
<?php
namespace Appwrite\Auth\OAuth2;
2020-01-21 00:44:16 +13:00
use Appwrite\Auth\OAuth2;
2020-01-21 00:44:16 +13:00
// Reference Material
// https://developer.paypal.com/docs/api/overview/
2020-02-17 00:41:03 +13:00
class Paypal extends OAuth2
2020-01-21 00:44:16 +13:00
{
/**
* @var array
2020-01-21 00:44:16 +13:00
*/
2022-05-13 03:56:20 +12:00
private array $endpoint = [
2020-01-21 00:44:16 +13:00
'sandbox' => 'https://www.sandbox.paypal.com/',
'live' => 'https://www.paypal.com/',
];
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
private array $resourceEndpoint = [
2020-01-21 00:44:16 +13:00
'sandbox' => 'https://api.sandbox.paypal.com/v1/',
'live' => 'https://api.paypal.com/v1/',
];
/**
* @var string
*/
2022-05-13 03:56:20 +12:00
protected string $environment = 'live';
2020-01-21 00:44:16 +13:00
/**
* @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 = [];
2020-01-21 00:44:16 +13:00
/**
* @var array
*/
2022-05-13 03:56:20 +12:00
protected array $scopes = [
'openid',
'profile',
'email'
2020-01-21 00:44:16 +13:00
];
/**
* @return string
*/
public function getName(): string
{
return 'paypal';
}
/**
* @return string
*/
public function getLoginURL(): string
{
2022-05-13 03:56:20 +12:00
$url = $this->endpoint[$this->environment] . 'connect/?' .
\http_build_query([
2020-01-21 00:44:16 +13:00
'flowEntry' => 'static',
'response_type' => 'code',
'client_id' => $this->appID,
'scope' => \implode(' ', $this->getScopes()),
2020-01-21 00:44:16 +13:00
// paypal is not accepting localhost string into return uri
'redirect_uri' => \str_replace("localhost", "127.0.0.1", $this->callback),
'state' => \json_encode($this->state),
2020-01-21 00:44:16 +13:00
]);
return $url;
}
/**
* @param string $code
*
2022-02-01 09:20:17 +13:00
* @return array
2020-01-21 00:44:16 +13:00
*/
2022-02-02 05:47:19 +13:00
protected function getTokens(string $code): array
2020-01-21 00:44:16 +13: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',
$this->resourceEndpoint[$this->environment] . 'oauth2/token',
['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)],
\http_build_query([
'code' => $code,
'grant_type' => 'authorization_code',
])
), true);
}
2022-02-01 09:20:17 +13:00
2022-02-01 23:42:11 +13:00
return $this->tokens;
2020-01-21 00:44:16 +13: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
{
$this->tokens = \json_decode($this->request(
'POST',
$this->resourceEndpoint[$this->environment] . 'oauth2/token',
['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)],
\http_build_query([
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token',
])
), true);
2022-02-02 04:54:20 +13:00
2022-05-13 03:56:20 +12:00
if (empty($this->tokens['refresh_token'])) {
$this->tokens['refresh_token'] = $refreshToken;
}
2022-02-02 04:54:20 +13:00
return $this->tokens;
}
2020-01-21 00:44:16 +13:00
/**
* @param string $accessToken
*
* @return string
*/
public function getUserID(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['payer_id'] ?? '';
2020-01-21 00:44:16 +13:00
}
/**
* @param string $accessToken
*
* @return string
*/
public function getUserEmail(string $accessToken): string
{
$user = $this->getUser($accessToken);
if (isset($user['emails'])) {
2022-04-28 07:32:06 +12:00
$email = array_filter($user['emails'], function ($email) {
return $email['primary'] === true;
});
2022-05-13 03:56:20 +12:00
2022-04-28 07:32:06 +12:00
if (!empty($email)) {
return $email[0]['value'];
}
2020-01-21 00:44:16 +13:00
}
return '';
}
/**
* Check if the OAuth email is verified
2022-05-24 02:54:50 +12:00
*
2022-04-28 07:32:06 +12:00
* @link https://developer.paypal.com/docs/api/identity/v1/#userinfo_get
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 07:32:06 +12:00
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
if ($user['verified_account'] ?? false) {
2022-04-28 07:32:06 +12:00
return true;
}
return false;
}
2020-01-21 00:44:16 +13:00
/**
* @param string $accessToken
*
* @return string
*/
public function getUserName(string $accessToken): string
{
$user = $this->getUser($accessToken);
2022-05-13 03:56:20 +12:00
return $user['name'] ?? '';
2020-01-21 00:44:16 +13:00
}
/**
* @param string $accessToken
*
* @return array
*/
protected function getUser(string $accessToken): array
{
$header = [
'Content-Type: application/json',
2022-05-13 03:56:20 +12:00
'Authorization: Bearer ' . \urlencode($accessToken),
2020-01-21 00:44:16 +13:00
];
if (empty($this->user)) {
$user = $this->request(
'GET',
$this->resourceEndpoint[$this->environment] . 'identity/oauth2/userinfo?schema=paypalv1.1',
$header
);
$this->user = \json_decode($user, true);
2020-01-21 00:44:16 +13:00
}
return $this->user;
}
}