1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
<?php
/**
* @file
* HybridAuth
* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
* (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
*/
use PayPal\Api\OpenIdSession;
use PayPal\Api\OpenIdTokeninfo;
use PayPal\Api\OpenIdUserinfo;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Exception\PayPalConnectionException;
use PayPal\Rest\ApiContext;
/**
* PayPal OAuth Class.
*
* @package HybridAuth providers package
* @version 1.0
* @license BSD License
*/
/**
* Hybrid_Providers_Paypal - PayPal provider adapter based on OAuth2 protocol.
*/
class Hybrid_Providers_Paypal extends Hybrid_Provider_Model
{
/**
* The access privileges that you are requesting for
* from the user. Pass empty array for all scopes.
*
* @var array $scope
* @see https://developer.paypal.com/docs/integration/direct/identity/attributes
*/
public $scope = [];
/**
* The provider api client
*
* @var ApiContext $api
*/
public $api;
/**
* TRUE if sandbox mode is ON otherwise FALSE
*
* @var bool $sandbox
*/
public $sandbox = true;
/**
* {@inheritdoc}
*/
function initialize()
{
if (!$this->config["keys"]["id"] || !$this->config["keys"]["secret"]) {
throw new Exception("Your application id and secret are required in order to connect to {$this->providerId}.", 4);
}
// Set scope from config.
if (isset($this->config["scope"])) {
$scope = $this->config["scope"];
if (is_string($scope)) {
$scope = explode(" ", $scope);
}
$scope = array_map("trim", $scope);
$this->scope = $scope;
}
// Set sandbox from config.
if (isset($this->config["sandbox"]) && is_bool($this->config["sandbox"])) {
$this->sandbox = $this->config["sandbox"];
}
// Include 3rd-party SDK.
$this->autoLoaderInit();
// Set up ApiContext.
$this->api = new ApiContext(
new OAuthTokenCredential(
$this->config["keys"]["id"],
$this->config["keys"]["secret"],
),
);
// Set up config.
$this->api->setConfig([
"log.LogEnabled" => Hybrid_Auth::$config["debug_mode"],
"log.FileName" => Hybrid_Auth::$config["debug_file"],
"log.LogLevel" => "DEBUG",
"http.CURLOPT_SSLVERSION" => CURL_SSLVERSION_TLSv1,
"mode" => $this->sandbox ? "sandbox" : "live",
]);
}
/**
* {@inheritdoc}
*/
function loginBegin()
{
$url = OpenIdSession::getAuthorizationUrl(
$this->endpoint,
$this->scope,
null,
null,
null,
$this->api,
);
// Redirect to PayPal.
Hybrid_Auth::redirect($url);
}
/**
* {@inheritdoc}
*/
function loginFinish()
{
if (!isset($_GET["code"])) {
throw new Exception("Authentication failed! User has canceled authentication!", 5);
}
$code = $_GET["code"];
try {
// Obtain Authorization Code from Code, Client ID and Client Secret
$accessToken = OpenIdTokeninfo::createFromAuthorizationCode(["code" => $code], null, null, $this->api);
if ($accessToken) {
$this->setUserConnected();
// Store tokens.
$this->token("id_token", $accessToken->getIdToken());
$this->token("access_token", $accessToken->getAccessToken());
$this->token("refresh_token", $accessToken->getRefreshToken());
}
} catch (PayPalConnectionException $e) {
throw new Hybrid_Exception($e->getMessage(), $e->getCode(), $e);
}
}
/**
* {@inheritdoc}
*/
function logout()
{
parent::logout();
if ($idToken = $this->token("id_token")) {
$url = OpenIdSession::getLogoutUrl(
$this->params["hauth_return_to"],
$idToken,
$this->api,
);
// Redirect to PayPal.
Hybrid_Auth::redirect($url);
}
}
/**
* {@inheritdoc}
*/
function getUserProfile()
{
try {
$params = ["access_token" => $this->token("access_token")];
$userInfo = OpenIdUserinfo::getUserinfo($params, $this->api);
$profile = new Hybrid_User_Profile();
$profile->identifier = $userInfo->getUserId();
$profile->firstName = $userInfo->getGivenName();
$profile->lastName = $userInfo->getFamilyName();
$profile->displayName = $userInfo->getName();
$profile->photoURL = $userInfo->getPicture();
$profile->gender = $userInfo->getGender();
$profile->email = $userInfo->getEmail();
$profile->emailVerified = $userInfo->getEmailVerified();
$profile->language = $userInfo->getLocale();
$profile->phone = $userInfo->getPhoneNumber();
if ($address = $userInfo->getAddress()) {
$profile->address = $address->getStreetAddress();
$profile->city = $address->getLocality();
$profile->zip = $address->getPostalCode();
$profile->country = $address->getCountry();
$profile->region = $address->getRegion();
}
if ($birthdate = $userInfo->getBirthday()) {
if (strpos($birthdate, "-") === FALSE) {
if ($birthdate !== "0000") {
$profile->birthYear = (int)$birthdate;
}
} else {
list($birthday_year, $birthday_month, $birthday_day) = explode("-", $birthdate);
$profile->birthDay = (int) $birthday_day;
$profile->birthMonth = (int) $birthday_month;
if ($birthday_year !== "0000") {
$profile->birthYear = (int) $birthday_year;
}
}
}
$this->user->profile = $profile;
return $this->user->profile;
} catch (Exception $e) {
throw new Hybrid_Exception($e->getMessage(), $e->getCode(), $e);
}
}
}
|