|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Srmklive\PayPal\Traits\PayPalHttpClient; |
|
6
|
|
|
use Srmklive\PayPal\Traits\PayPalLocales; |
|
7
|
|
|
|
|
8
|
|
|
class PayPalRestAPI |
|
9
|
|
|
{ |
|
10
|
|
|
use PayPalHttpClient, PayPalLocales; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $curlConfig; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var bool |
|
19
|
|
|
*/ |
|
20
|
|
|
public $sandbox; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $credentials; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $endpoints; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $locale; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $response; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* PayPalRestAPI constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \Exception |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function __construct() |
|
48
|
|
|
{ |
|
49
|
3 |
|
if (function_exists('config')) { |
|
50
|
|
|
$mode = config('paypal.mode'); |
|
51
|
|
|
if (empty($mode) || !in_array($mode, ['sandbox', 'live'])) { |
|
52
|
|
|
$mode = 'live'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$sandbox = ($mode === 'sandbox') ? true : false; |
|
56
|
|
|
$credentials = config('paypal.'.$mode); |
|
57
|
|
|
$credentials['validate_ssl'] = config('paypal.validate_ssl'); |
|
58
|
|
|
$credentials['locale'] = config('paypal.locale'); |
|
59
|
|
|
|
|
60
|
|
|
$this->setApiCredentials($credentials, $sandbox); |
|
61
|
|
|
} |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set PayPal Rest API credentials & endpoints. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $credentials |
|
68
|
|
|
* @param bool $sandbox |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \Exception |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setApiCredentials($credentials, $sandbox) |
|
73
|
|
|
{ |
|
74
|
|
|
if (!empty($credentials['locale'])) { |
|
75
|
|
|
$this->setLocale($credentials['locale']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->credentials = $credentials; |
|
79
|
|
|
$this->sandbox = $sandbox; |
|
80
|
|
|
|
|
81
|
|
|
if ($this->sandbox === true) { |
|
82
|
|
|
$this->endpoints = [ |
|
|
|
|
|
|
83
|
|
|
'rest' => 'https://api.sandbox.paypal.com/', |
|
84
|
|
|
'redirect' => 'https://www.sandbox.paypal.com', |
|
85
|
|
|
]; |
|
86
|
|
|
} else { |
|
87
|
|
|
$this->endpoints = [ |
|
|
|
|
|
|
88
|
|
|
'rest' => 'https://api.paypal.com/', |
|
89
|
|
|
'redirect' => 'https://www.paypal.com', |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->setHttpClientConfiguration(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set default locale. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $locale |
|
100
|
|
|
* |
|
101
|
|
|
* @throws \Exception |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function setLocale($locale) |
|
104
|
|
|
{ |
|
105
|
2 |
|
if (!in_array($locale, $this->locales())) { |
|
106
|
1 |
|
throw new \Exception('Invalid locale'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
$this->locale = $locale; |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function getLocale() |
|
116
|
|
|
{ |
|
117
|
1 |
|
return $this->locale; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @throws \Exception |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function accessToken() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->apiUrl = $this->endpoints['rest'].'v1/oauth2/token'; |
|
126
|
|
|
|
|
127
|
|
|
$this->params = [ |
|
128
|
|
|
'auth' => [ |
|
129
|
|
|
$this->credentials['client'], |
|
130
|
|
|
$this->credentials['secret'], |
|
131
|
|
|
], |
|
132
|
|
|
'headers' => [ |
|
133
|
|
|
'Accept' => 'application/json', |
|
134
|
|
|
'Accept-Language' => $this->locale, |
|
135
|
|
|
], |
|
136
|
|
|
'form_params' => [ |
|
137
|
|
|
'grant_type' => 'client_credentials', |
|
138
|
|
|
], |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
$this->setResponse( |
|
142
|
|
|
$this->sendRequest() |
|
|
|
|
|
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$this->accessToken = $this->response['access_token']; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param array $response |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function setResponse($response) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->response = $response; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getResponse() |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->response; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..