forked from damonjones/InsigSagepayBundle
/
SagepayManager.php
319 lines (274 loc) · 9.6 KB
/
SagepayManager.php
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
namespace Insig\SagepayBundle;
use Insig\SagepayBundle\Exception\CurlException;
use Insig\SagepayBundle\Exception\InvalidRequestException;
use Insig\SagepayBundle\Exception\InvalidNotificationException;
use Insig\SagepayBundle\Model\RegistrationRequest;
use Insig\SagepayBundle\Model\NotificationRequest;
use Insig\SagepayBundle\Model\Transaction\Registration\Request as TransactionRegistrationRequest;
use Insig\SagepayBundle\Model\Transaction\Registration\Response as TransactionRegistrationResponse;
use Insig\SagepayBundle\Model\Transaction\Notification\Request as TransactionNotificationRequest;
use Insig\SagepayBundle\Model\Transaction\Notification\Response as TransactionNotificationResponse;
use Insig\SagepayBundle\Model\Token\Registration\Request as TokenRegistrationRequest;
use Insig\SagepayBundle\Model\Token\Registration\Response as TokenRegistrationResponse;
use Insig\SagepayBundle\Model\Token\Notification\Request as TokenNotificationRequest;
use Insig\SagepayBundle\Model\Token\Notification\Response as TokenNotificationResponse;
class SagepayManager
{
protected $validator;
protected $router;
// Sagepay URLs
protected $gatewayUrl;
protected $registerTokenUrl;
protected $removeTokenUrl;
protected $vpsProtocol;
protected $vendor;
// Site URLs/route names
protected $transactionNotificationUrl;
protected $tokenNotificationUrl;
protected $redirectUrls;
// public API ------------------------------------------------------------
public function setValidator($validator)
{
$this->validator = $validator;
}
public function setRouter($router)
{
$this->router = $router;
}
public function setGatewayUrl($url)
{
$this->gatewayUrl = $url;
}
public function setRegisterTokenUrl($url)
{
$this->registerTokenUrl = $url;
}
public function setRemoveTokenUrl($url)
{
$this->removeTokenUrl = $url;
}
public function setVpsProtocol($vpsProtocol)
{
$this->vpsProtocol = $vpsProtocol;
}
public function setVendor($vendor)
{
$this->vendor = $vendor;
}
public function getVendor()
{
return $this->vendor;
}
public function setTransactionNotificationUrl($url)
{
$this->transactionNotificationUrl = $url;
}
public function setTokenNotificationUrl($url)
{
$this->tokenNotificationUrl = $url;
}
public function setRedirectUrls(array $redirectUrls)
{
$this->redirectUrls = $redirectUrls;
}
// Transaction
/**
* Send Transaction Registration Request
*
* Sends a cURL POST of the request properties as an http_query
* Returns a Response object populated from the server's response
*
* @param \Insig\SagepayBundle\Model\Transaction\Registration\Request $request
* @return \Insig\SagepayBundle\Model\Transaction\Registration\Response $response
* @author Damon Jones
*/
public function sendTransactionRegistrationRequest(TransactionRegistrationRequest $request)
{
$response = $this->sendRegistrationRequest(
$request,
$this->gatewayUrl,
$this->transactionNotificationUrl
);
return new TransactionRegistrationResponse($response);
}
/**
* Create Transaction Notification
*
* @param string $data
* @throws \Insig\SagepayBundle\Exception\InvalidNotificationException
* @return \Insig\SagepayBundle\Model\Transaction\Notification\Request
* @author Damon Jones
*/
public function createTransactionNotification($data)
{
$notification = new TransactionNotificationRequest($data);
// validate the notification
$errors = $this->validator->validate($notification);
if (count($errors)) {
throw new InvalidNotificationException;
}
return $notification;
}
/**
* Create Transaction Notification Response
*
* @param string $status
* @param string $statusDetail
* @return \Insig\SagepayBundle\Model\Transaction\Notification\Response
* @author Damon Jones
*/
public function createTransactionNotificationResponse($status, $statusDetail = null)
{
/**
* You should send OK in all circumstances where no errors occur
* in validating the Notification POST, so even if Sage Pay send
* you a status of ABORT or NOTAUTHED in A3 above, you should
* reply with an OK and a RedirectURL that points to a page
* informing the customer that the transaction did not complete.
*/
return new TransactionNotificationResponse(
in_array($status, array('INVALID', 'ERROR')) ? $status : 'OK',
$this->convertRouteToAbsoluteUrl($this->redirectUrls[strtolower($status)]),
$statusDetail
);
}
// Token
/**
* Send Token Registration Request
*
* Sends a cURL POST of the request properties as an http_query
* Returns a Response object populated from the server's response
*
* @param \Insig\SagepayBundle\Model\Token\Registration\Request $request
* @return \Insig\SagepayBundle\Model\Token\Registration\Response $response
* @author Damon Jones
*/
public function sendTokenRegistrationRequest(TokenRegistrationRequest $request)
{
$response = $this->sendRegistrationRequest(
$request,
$this->registerTokenUrl,
$this->tokenNotificationUrl
);
return new TokenRegistrationResponse($response);
}
/**
* Create Token Notification
*
* @param string $string
* @throws \Insig\SagepayBundle\Exception\InvalidNotificationException
* @return \Insig\SagepayBundle\Model\Token\Notification\Request
* @author Damon Jones
*/
public function createTokenNotification($string)
{
$notification = new TokenNotificationRequest($string);
// validate the notification
$errors = $this->validator->validate($notification);
if (count($errors)) {
throw new InvalidNotificationException;
}
return $notification;
}
/**
* Create Token Notification Response
*
* @param string $status
* @param string $statusDetail
* @return \Insig\SagepayBundle\Model\Token\Notification\Response
* @author Damon Jones
*/
public function createTokenNotificationResponse($status, $statusDetail = null)
{
/**
* You should send OK in all circumstances where no errors occur
* in validating the Notification POST
*/
return new TokenNotificationResponse(
in_array($status, array('MALFORMED', 'INVALID')) ? $status : 'OK',
$this->convertRouteToAbsoluteUrl(
'OK' === $status ? $this->redirectUrls['token_ok'] : $this->redirectUrls['token_error']
),
$statusDetail
);
}
// Common
/**
* Is Notification Authentic
*
* @param \Insig\SagepayBundle\Model\NotificationRequest $notification
* @param string $securityKey
* @return boolean
* @author Damon Jones
*/
public function isNotificationAuthentic(NotificationRequest $request, $securityKey)
{
return
$request->getVpsSignature()
===
$request->getComputedSignature($this->vendor, $securityKey);
}
// protected methods
/**
* Converts a route name to an absolute URL
*
* @param string $route Follows the symfony1 convention of an '@' prefix
* @param string $parameters URL parameters array
* @return string
* @author Damon Jones
*/
protected function convertRouteToAbsoluteUrl($route, $parameters = array())
{
if ('@' === $route[0]) {
return $this->router->generate(substr($route, 1), $parameters, true);
} else {
return $route;
}
}
/**
* Send Registration Request
*
* Sends a cURL POST of the request properties as an http_query
* Returns a Response object populated from the server's response
*
* @param \Insig\SagepayBundle\Model\RegistrationRequest $request
* @throws \Insig\SagepayBundle\Exception\InvalidRequestException
* @throws \Insig\SagepayBundle\Exception\CurlException
* @return string $response
* @author Damon Jones
*/
protected function sendRegistrationRequest(RegistrationRequest $request, $registrationUrl, $notificationUrl)
{
$request->setVpsProtocol($this->vpsProtocol);
$request->setVendor($this->vendor);
$request->setNotificationUrl(
$this->convertRouteToAbsoluteUrl($notificationUrl)
);
$errors = $this->validator->validate($request);
if (count($errors)) {
throw new InvalidRequestException;
}
$curlSession = curl_init();
curl_setopt_array(
$curlSession,
array(
CURLOPT_URL => $registrationUrl,
CURLOPT_HEADER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request->getQueryString(),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => true
)
);
$response = curl_exec($curlSession);
$error = curl_error($curlSession);
curl_close($curlSession);
if (false === $response) {
throw new CurlException($error);
}
return $response;
}
}