1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\PaypalRest; |
4
|
|
|
|
5
|
|
|
use Omnipay\Common\AbstractGateway; |
6
|
|
|
use Guzzle\Http\ClientInterface; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Ivan Kerin <[email protected]> |
11
|
|
|
* @copyright 2014, Clippings Ltd. |
12
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
13
|
|
|
*/ |
14
|
|
|
class Gateway extends AbstractGateway |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public function getName() |
20
|
|
|
{ |
21
|
|
|
return 'PaypalRest'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function getDefaultParameters() |
28
|
|
|
{ |
29
|
|
|
return array( |
30
|
|
|
'clientId' => '', |
31
|
|
|
'secret' => '', |
32
|
|
|
'token' => '', |
33
|
|
|
'partnerAttributionId' => '', |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function __construct(ClientInterface $httpClient = null, HttpRequest $httpRequest = null) |
38
|
|
|
{ |
39
|
|
|
parent::__construct($httpClient, $httpRequest); |
40
|
|
|
|
41
|
|
|
$this->httpClient->getEventDispatcher()->addListener( |
42
|
|
|
'request.error', |
43
|
|
|
function ($event) { |
44
|
|
|
if ($event['response']->isClientError()) { |
45
|
|
|
$event->stopPropagation(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getClientId() |
55
|
|
|
{ |
56
|
|
|
return $this->getParameter('clientId'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $value |
61
|
|
|
*/ |
62
|
|
|
public function setClientId($value) |
63
|
|
|
{ |
64
|
|
|
return $this->setParameter('clientId', $value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getSecret() |
71
|
|
|
{ |
72
|
|
|
return $this->getParameter('secret'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $value |
77
|
|
|
*/ |
78
|
|
|
public function setSecret($value) |
79
|
|
|
{ |
80
|
|
|
return $this->setParameter('secret', $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getPartnerAttributionId() |
87
|
|
|
{ |
88
|
|
|
return $this->getParameter('partnerAttributionId'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $value |
93
|
|
|
*/ |
94
|
|
|
public function setPartnerAttributionId($value) |
95
|
|
|
{ |
96
|
|
|
return $this->setParameter('partnerAttributionId', $value); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getTokenExpires() |
100
|
|
|
{ |
101
|
|
|
return $this->getParameter('tokenExpires'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setTokenExpires($value) |
105
|
|
|
{ |
106
|
|
|
return $this->setParameter('tokenExpires', $value); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getTokenResponse() |
110
|
|
|
{ |
111
|
|
|
return $this->token()->send(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getToken() |
118
|
|
|
{ |
119
|
|
|
if (false === $this->hasToken()) { |
120
|
|
|
$tokenResponse = $this->getTokenResponse(); |
121
|
|
|
|
122
|
|
|
if ($tokenResponse->isSuccessful()) { |
123
|
|
|
$this->setToken($tokenResponse->getAccessToken()); |
124
|
|
|
$this->setTokenExpires($tokenResponse->getExpires()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->getParameter('token'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $value |
133
|
|
|
* @return self |
134
|
|
|
*/ |
135
|
|
|
public function setToken($value) |
136
|
|
|
{ |
137
|
|
|
return $this->setParameter('token', $value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Is there a bearer token and is it still valid? |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function hasToken() |
146
|
|
|
{ |
147
|
|
|
$token = $this->getParameter('token'); |
148
|
|
|
$expires = $this->getParameter('tokenExpires'); |
149
|
|
|
|
150
|
|
|
return (false === empty($token) and time() < $expires); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $class |
155
|
|
|
* @param array $parameters |
156
|
|
|
* @return Omnipay\PaypalRest\Message\AbstractRequest |
157
|
|
|
*/ |
158
|
|
|
public function createRequestWithToken($class, array $parameters) |
159
|
|
|
{ |
160
|
|
|
$parameters['token'] = $this->getToken(); |
161
|
|
|
|
162
|
|
|
return $this->createRequest($class, $parameters); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param array $parameters |
167
|
|
|
* @return Omnipay\PaypalRest\Message\TokenRequest |
168
|
|
|
*/ |
169
|
|
|
public function token(array $parameters = array()) |
170
|
|
|
{ |
171
|
|
|
return $this->createRequest(__NAMESPACE__.'\Message\TokenRequest', $parameters); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array $parameters |
176
|
|
|
* @return Omnipay\PaypalRest\Message\PaymentRequest |
177
|
|
|
*/ |
178
|
|
|
public function purchase(array $parameters = array()) |
179
|
|
|
{ |
180
|
|
|
$parameters['intent'] = 'sale'; |
181
|
|
|
|
182
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\PaymentRequest', $parameters); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param array $parameters |
187
|
|
|
* @return Omnipay\PaypalRest\Message\PaymentCompleteRequest |
188
|
|
|
*/ |
189
|
|
|
public function completePurchase(array $parameters = array()) |
190
|
|
|
{ |
191
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\PaymentCompleteRequest', $parameters); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $parameters |
196
|
|
|
* @return Omnipay\PaypalRest\Message\PaymentRequest |
197
|
|
|
*/ |
198
|
|
|
public function authorise(array $parameters = array()) |
199
|
|
|
{ |
200
|
|
|
$parameters['intent'] = 'authorize'; |
201
|
|
|
|
202
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\PaymentRequest', $parameters); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param array $parameters |
207
|
|
|
* @return Omnipay\PaypalRest\Message\PaymentCompleteRequest |
208
|
|
|
*/ |
209
|
|
|
public function completeAuthorise(array $parameters = array()) |
210
|
|
|
{ |
211
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\PaymentCompleteRequest', $parameters); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param array $parameters |
216
|
|
|
* @return Omnipay\PaypalRest\Message\CaptureRequest |
217
|
|
|
*/ |
218
|
|
|
public function capture(array $parameters = array()) |
219
|
|
|
{ |
220
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\CaptureRequest', $parameters); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param array $parameters |
225
|
|
|
* @return Omnipay\PaypalRest\Message\VoidRequest |
226
|
|
|
*/ |
227
|
|
|
public function void(array $parameters = array()) |
228
|
|
|
{ |
229
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\VoidRequest', $parameters); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param array $parameters |
234
|
|
|
* @return Omnipay\PaypalRest\Message\CreateCardRequest |
235
|
|
|
*/ |
236
|
|
|
public function createCard(array $parameters = array()) |
237
|
|
|
{ |
238
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\CreateCardRequest', $parameters); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param array $parameters |
243
|
|
|
* @return Omnipay\PaypalRest\Message\DeleteCardRequest |
244
|
|
|
*/ |
245
|
|
|
public function deleteCard(array $parameters = array()) |
246
|
|
|
{ |
247
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\DeleteCardRequest', $parameters); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param array $parameters |
252
|
|
|
* @return Omnipay\PaypalRest\Message\UpdateCardRequest |
253
|
|
|
*/ |
254
|
|
|
public function updateCard(array $parameters = array()) |
255
|
|
|
{ |
256
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\UpdateCardRequest', $parameters); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param array $parameters |
261
|
|
|
* @return Omnipay\PaypalRest\Message\RefundRequest |
262
|
|
|
*/ |
263
|
|
|
public function refund(array $parameters = array()) |
264
|
|
|
{ |
265
|
|
|
if (false === isset($parameters['type'])) { |
266
|
|
|
$parameters['type'] = 'sale'; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $this->createRequestWithToken(__NAMESPACE__.'\Message\RefundRequest', $parameters); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|