friendship ended with social-app. php is my new best friend
1<?php
2/**
3 * Class PayPal
4 *
5 * @created 29.07.2019
6 * @author smiley <smiley@chillerlan.net>
7 * @copyright 2019 smiley
8 * @license MIT
9 *
10 * @noinspection PhpUnused
11 */
12declare(strict_types=1);
13
14namespace chillerlan\OAuth\Providers;
15
16use chillerlan\OAuth\Core\{
17 AuthenticatedUser, ClientCredentials, ClientCredentialsTrait, CSRFToken, OAuth2Provider, TokenRefresh, UserInfo,
18};
19
20/**
21 * PayPal OAuth2
22 *
23 * @link https://developer.paypal.com/api/rest/
24 */
25class PayPal extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenRefresh, UserInfo{
26 use ClientCredentialsTrait;
27
28 public const IDENTIFIER = 'PAYPAL';
29
30 public const SCOPE_BASIC_AUTH = 'openid';
31 public const SCOPE_FULL_NAME = 'profile';
32 public const SCOPE_EMAIL = 'email';
33 public const SCOPE_ADDRESS = 'address';
34 public const SCOPE_ACCOUNT = 'https://uri.paypal.com/services/paypalattributes';
35
36 public const DEFAULT_SCOPES = [
37 self::SCOPE_BASIC_AUTH,
38 self::SCOPE_EMAIL,
39 ];
40
41 public const USES_BASIC_AUTH_IN_ACCESS_TOKEN_REQUEST = true;
42
43 protected string $accessTokenURL = 'https://api.paypal.com/v1/oauth2/token';
44 protected string $authorizationURL = 'https://www.paypal.com/connect';
45 protected string $apiURL = 'https://api.paypal.com';
46 protected string|null $applicationURL = 'https://developer.paypal.com/developer/applications/';
47 protected string|null $apiDocs = 'https://developer.paypal.com/docs/connect-with-paypal/reference/';
48
49 /** @codeCoverageIgnore */
50 public function me():AuthenticatedUser{
51 $json = $this->getMeResponseData('/v1/identity/oauth2/userinfo', ['schema' => 'paypalv1.1']);
52
53 $userdata = [
54 'data' => $json,
55 'displayName' => $json['name'],
56 'id' => $json['user_id'],
57 ];
58
59 if(!empty($json['emails'])){
60 foreach($json['emails'] as $email){
61 if($email['primary']){
62 $userdata['email'] = $email['value'];
63 break;
64 }
65 }
66 }
67
68 return new AuthenticatedUser($userdata);
69 }
70
71}