2020-01-21 00:44:16 +13:00
|
|
|
<?php
|
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
namespace Appwrite\Auth\OAuth2;
|
2020-01-21 00:44:16 +13:00
|
|
|
|
2020-03-25 06:56:32 +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 string
|
|
|
|
*/
|
|
|
|
private $endpoint= [
|
|
|
|
'sandbox' => 'https://www.sandbox.paypal.com/',
|
|
|
|
'live' => 'https://www.paypal.com/',
|
|
|
|
];
|
|
|
|
|
|
|
|
private $resourceEndpoint = [
|
|
|
|
'sandbox' => 'https://api.sandbox.paypal.com/v1/',
|
|
|
|
'live' => 'https://api.paypal.com/v1/',
|
|
|
|
];
|
|
|
|
|
2020-01-21 02:00:57 +13:00
|
|
|
private $environment = 'live';
|
2020-01-21 00:44:16 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $user = [];
|
|
|
|
|
|
|
|
|
|
|
|
protected $scopes = [
|
|
|
|
'openid',
|
|
|
|
'profile',
|
|
|
|
'email'
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return 'paypal';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLoginURL(): string
|
|
|
|
{
|
|
|
|
$url = $this->endpoint[$this->environment] . 'connect/?'.
|
2020-06-20 23:05:43 +12:00
|
|
|
\http_build_query([
|
2020-01-21 00:44:16 +13:00
|
|
|
'flowEntry' => 'static',
|
|
|
|
'response_type' => 'code',
|
|
|
|
'client_id' => $this->appID,
|
2020-06-20 23:05:43 +12:00
|
|
|
'scope' => \implode(' ', $this->getScopes()),
|
2020-01-21 00:44:16 +13:00
|
|
|
// paypal is not accepting localhost string into return uri
|
2020-06-20 23:05:43 +12:00
|
|
|
'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
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAccessToken(string $code): string
|
|
|
|
{
|
|
|
|
$accessToken = $this->request(
|
|
|
|
'POST',
|
|
|
|
$this->resourceEndpoint[$this->environment] . 'oauth2/token',
|
2020-06-20 23:05:43 +12:00
|
|
|
['Authorization: Basic ' . \base64_encode($this->appID . ':' . $this->appSecret)],
|
|
|
|
\http_build_query([
|
2020-01-21 00:44:16 +13:00
|
|
|
'code' => $code,
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2020-06-20 23:05:43 +12:00
|
|
|
$accessToken = \json_decode($accessToken, true);
|
2020-01-21 00:44:16 +13:00
|
|
|
|
|
|
|
|
|
|
|
if (isset($accessToken['access_token'])) {
|
|
|
|
return $accessToken['access_token'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accessToken
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUserID(string $accessToken): string
|
|
|
|
{
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
|
|
|
if (isset($user['payer_id'])) {
|
|
|
|
return $user['payer_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accessToken
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUserEmail(string $accessToken): string
|
|
|
|
{
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
|
|
|
if (isset($user['emails'])) {
|
|
|
|
return $user['emails'][0]['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accessToken
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUserName(string $accessToken): string
|
|
|
|
{
|
|
|
|
$user = $this->getUser($accessToken);
|
|
|
|
|
|
|
|
if (isset($user['name'])) {
|
|
|
|
return $user['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $accessToken
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getUser(string $accessToken): array
|
|
|
|
{
|
|
|
|
$header = [
|
|
|
|
'Content-Type: application/json',
|
2020-06-20 23:05:43 +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
|
|
|
|
);
|
2020-06-20 23:05:43 +12:00
|
|
|
$this->user = \json_decode($user, true);
|
2020-01-21 00:44:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
}
|