1
0
Fork 0
mirror of synced 2024-06-30 20:21:16 +12:00
appwrite/src/Appwrite/Auth/OAuth2/Paypal.php

173 lines
3.7 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
*/
private $endpoint = [
2020-01-21 00:44:16 +13:00
'sandbox' => 'https://www.sandbox.paypal.com/',
'live' => 'https://www.paypal.com/',
];
/**
* @var array
*/
2020-01-21 00:44:16 +13:00
private $resourceEndpoint = [
'sandbox' => 'https://api.sandbox.paypal.com/v1/',
'live' => 'https://api.paypal.com/v1/',
];
/**
* @var string
*/
2020-07-07 01:33:45 +12:00
protected $environment = 'live';
2020-01-21 00:44:16 +13:00
/**
* @var array
*/
protected $user = [];
/**
* @var array
*/
2020-01-21 00:44:16 +13:00
protected $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
{
$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
*
* @return string
*/
public function getAccessToken(string $code): string
{
$accessToken = $this->request(
'POST',
$this->resourceEndpoint[$this->environment] . 'oauth2/token',
['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',
])
);
$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',
'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;
}
}