-
Notifications
You must be signed in to change notification settings - Fork 15
/
SagePay.class.php
534 lines (502 loc) · 17.4 KB
/
SagePay.class.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php
class SagePay {
private $urls;
private $Vendor;
private $Basket = array();
public $AccountType = 'E';
public $GiftAidPayment = 0;
public $ApplyAVSCV2 = 0;
public $Apply3DSecure = 0;
public $VendorTxCode;
public $Amount;
public $Currency;
public $Description;
public $CardHolder;
public $CardNumber;
public $StartDate;
public $ExpiryDate;
public $IssueNumber;
public $CV2;
public $CardType;
public $BillingSurname;
public $BillingFirstnames;
public $BillingAddress1;
public $BillingAddress2;
public $BillingCity;
public $BillingPostCode;
public $BillingCountry;
public $BillingState;
public $BillingPhone;
public $DeliverySurname;
public $DeliveryFirstnames;
public $DeliveryAddress1;
public $DeliveryAddress2;
public $DeliveryCity;
public $DeliveryPostCode;
public $DeliveryCountry;
public $DeliveryState;
public $DeliveryPhone;
public $CustomerEmail;
public $result = array('Status' => 'BEGIN');
public function __construct($vendor='', $mode='test') {
$sage_pay_urls = array(
'live' => array(
'register' => 'https://live.sagepay.com/gateway/service/vspdirect-register.vsp',
'3dsecure' => 'https://live.sagepay.com/gateway/service/direct3dcallback.vsp'
),
'test' => array(
'register' => 'https://test.sagepay.com/gateway/service/vspdirect-register.vsp',
'3dsecure' => 'https://test.sagepay.com/gateway/service/direct3dcallback.vsp'
),
'simulator' => array(
'register' => 'https://test.sagepay.com/Simulator/VSPDirectGateway.asp',
'3dsecure' => 'https://test.sagepay.com/Simulator/VSPDirectCallback.asp'
)
);
$this->urls = in_array($mode, array('live', 'test', 'simulator')) ? $sage_pay_urls[$mode] : $sage_pay_urls['test'];
$this->Vendor = $vendor;
}
/*
* Register the transaction with SagePay
*
*/
public function register() {
$errors = array();
if (!$this->Vendor) $errors['Vendor'] = 'The Vendor must be provided';
if (!$this->VendorTxCode) $errors['VendorTxCode'] = 'The Vendor must be provided';
if (!is_numeric($this->Amount)) $errors['Amount'] = 'The Amount field must be specified, and must be numeric.';
if (!$this->Currency) $errors['Currency'] = 'Currency must be specified, eg GBP.';
if (!$this->Description) $errors['Description'] = 'Description must be specified.';
if (!$this->CardHolder) $errors['CardHolder'] = 'CardHolder must be specified.';
if (!$this->CardNumber) $errors['CardNumber'] = 'CardNumber must be specified.';
if (!$this->ExpiryDate) $errors['ExpiryDate'] = 'ExpiryDate must be specified.';
if ($this->IssueNumber and !preg_match("/^\d{1,2}$/", $this->IssueNumber)) $errors['IssueNumber'] = 'IssueNumber is invalid.';
if ($this->CardType=='AMEX' and !preg_match("/^\d{4}$/", $this->CV2)) $errors['CV2'] = 'CV2 must be 4 numbers long.';
if ($this->CardType!='AMEX' and !preg_match("/^\d{3}$/", $this->CV2)) $errors['CV2'] = 'CV2 must be 3 numbers long.';
if (!in_array($this->CardType, array('VISA', 'MC', 'DELTA', 'SOLO', 'MAESTRO', 'UKE', 'AMEX', 'DC', 'JCB', 'LASER'))) $errors['CardType'] = 'CardType must be one of VISA, MC, DELTA, SOLO, MAESTRO, UKE, AMEX, DC, JCB, LASER';
if (!$this->BillingSurname) $errors['BillingSurname'] = 'BillingSurname must be specified.';
if (!$this->BillingFirstnames) $errors['BillingFirstnames'] = 'BillingFirstnames must be specified.';
if (!$this->BillingAddress1) $errors['BillingAddress1'] = 'BillingAddress1 must be specified.';
if (!$this->BillingCity) $errors['BillingCity'] = 'BillingCity must be specified.';
if (!$this->BillingPostCode) $errors['BillingPostCode'] = 'BillingPostCode must be specified.';
if (!$this->BillingCountry) $errors['BillingCountry'] = 'BillingCountry must be specified.';
if ($this->BillingCountry == 'US' and !$this->BillingState) $errors['BillingState'] = 'BillingState mut be specified.';
if (!$this->DeliverySurname) $errors['DeliverySurname'] = 'DeliverySurname must be specified.';
if (!$this->DeliveryFirstnames) $errors['DeliveryFirstnames'] = 'DeliveryFirstnames must be specified.';
if (!$this->DeliveryAddress1) $errors['DeliveryAddress1'] = 'DeliveryAddress1 must be specified.';
if (!$this->DeliveryCity) $errors['DeliveryCity'] = 'DeliveryCity must be specified.';
if (!$this->DeliveryPostCode) $errors['DeliveryPostCode'] = 'DeliveryPostCode must be specified.';
if (!$this->DeliveryCountry) $errors['DeliveryCountry'] = 'DeliveryCountry must be specified.';
if ($this->DeliveryCountry == 'US' and !$this->DeliveryState) $errors['DeliveryState'] = 'DeliveryState mut be specified.';
if ($this->CustomerEmail and !preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+\.([a-zA-Z0-9\._-]+)+$/", $this->CustomerEmail)) $errors['CustomerEmail'] = 'CustomerEmail is invalid.';
if (count($errors)) {
$this->result = array('Status' => 'ERRORCHECKFAIL', 'Errors' => $errors);
return 'ERRORCHECKFAIL';
}
$data = array(
'VPSProtocol' => 2.23,
'TxType' => 'PAYMENT',
'Vendor' => $this->Vendor,
'VendorTxCode' => $this->VendorTxCode,
'Amount' => number_format($this->Amount, 2, '.', ''),
'Currency' => $this->Currency,
'Description' => $this->Description,
'CardHolder' => $this->CardHolder,
'CardNumber' => $this->CardNumber,
'StartDate' => $this->StartDate,
'ExpiryDate' => $this->ExpiryDate,
'IssueNumber' => $this->IssueNumber,
'CV2' => $this->CV2,
'CardType' => $this->CardType,
'BillingSurname' => $this->BillingSurname,
'BillingFirstnames' => $this->BillingFirstnames,
'BillingAddress1' => $this->BillingAddress1,
'BillingAddress2' => $this->BillingAddress2,
'BillingCity' => $this->BillingCity,
'BillingPostCode' => $this->BillingPostCode,
'BillingCountry' => $this->BillingCountry,
'BillingState' => $this->BillingCountry == 'US' ? $this->BillingState : '',
'BillingPhone' => $this->BillingPhone,
'DeliverySurname' => $this->DeliverySurname,
'DeliveryFirstnames' => $this->DeliveryFirstnames,
'DeliveryAddress1' => $this->DeliveryAddress1,
'DeliveryAddress2' => $this->DeliveryAddress2,
'DeliveryCity' => $this->DeliveryCity,
'DeliveryPostCode' => $this->DeliveryPostCode,
'DeliveryCountry' => $this->DeliveryCountry,
'DeliveryState' => $this->DeliveryCountry == 'US' ? $this->DeliveryState : '',
'DeliveryPhone' => $this->DeliveryPhone,
'CustomerEmail' => $this->CustomerEmail,
'GiftAidPayment' => $this->GiftAidPayment,
'AccountType' => $this->AccountType,
'ClientIPAddress' => $_SERVER['REMOTE_ADDR'],
'ApplyAVSCV2' => $this->ApplyAVSCV2,
'Apply3DSecure' => $this->Apply3DSecure
);
if (sizeof($this->Basket)) {
$data['Basket'] = count($this->Basket);
foreach($this->Basket as $line) {
$data['Basket'] .= ':' . $line['description'];
$data['Basket'] .= ':' . $line['quantity'];
$data['Basket'] .= ':' . number_format($line['value'], 2, '.', '');
$data['Basket'] .= ':' . number_format($line['tax'], 2, '.', '');
$data['Basket'] .= ':' . number_format(($line['value'] + $line['tax']), 2, '.', '');
$data['Basket'] .= ':' . number_format(($line['quantity'] * ($line['value'] + $line['tax'])), 2, '.', '');
}
}
$this->result = $this->requestPost($this->urls['register'], $this->formatData($data));
if (in_array($this->result['Status'], array('INVALID', 'MALFORMED', 'REJECTED', 'NOTAUTHED', 'ERROR'))) {
$this->result['Errors'] = array();
foreach(explode("\n", $this->result['StatusDetail']) as $error) {
$this->result['Errors'] = array_merge($this->result['Errors'], $this->getError($error));
}
}
// unset the card details, as we're either complete,
// or we're at 3dsecure, in any case, they're not needed
// anymore, and in the case of 3dsecure, the instance will
// be stored, so can't keep card details.
unset($this->CardNumber);
unset($this->CardHolder);
unset($this->CV2);
if ($this->result['Status'] == '3DAUTH') {
$_SESSION['sagepay_obj'] = serialize($this);
}
return $this->result['Status'];
}
public function addLine($description, $quantity, $value, $tax=0) {
$this->Basket[] = array(
'description' => $description,
'quantity' => $quantity,
'value' => $value,
'tax' => $tax
);
}
public static function recover3d() {
$sagepay = unserialize($_SESSION['sagepay_obj']);
unset($_SESSION['sagepay_obj']);
return $sagepay;
}
public static function is3dResponse() {
if (isset($_REQUEST['PaRes']) and isset($_REQUEST['MD']) and isset($_SESSION['sagepay_obj'])) {
return true;
} else {
return false;
}
}
public function complete3d() {
$data = array(
'PARes' => $_REQUEST['PaRes'],
'MD' => $_REQUEST['MD']
);
$result = $this->requestPost($this->urls['3dsecure'], $this->formatData($data));
$this->result = $result;
return $this->result['Status'];
}
public function status() {
return $this->result['Status'];
}
private function getError($message) {
$chunks = explode(' : ', $message, 2);
if ($chunks[0] == '3048') { return array('CardNumber' => 'The card number is invalid.'); }
if ($chunks[0] == '4022') { return array('CardNumber' => 'The card number is not valid for the card type selected.'); }
if ($chunks[0] == '4023') { return array('CardNumber' => 'The issue number must be provided.'); }
return array($message);
}
/*
* Send a post request with cURL
* $url = URL to send reuqest to
* $data = POST data to send (in URL encoded Key=value pairs)
*
*/
private function requestPost($url, $data){
set_time_limit(60);
$output = array();
$curlSession = curl_init();
curl_setopt ($curlSession, CURLOPT_URL, $url);
curl_setopt ($curlSession, CURLOPT_HEADER, 0);
curl_setopt ($curlSession, CURLOPT_POST, 1);
curl_setopt ($curlSession, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlSession, CURLOPT_TIMEOUT, 30);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 2);
$response = explode(chr(10), curl_exec($curlSession));
if (curl_error($curlSession)){
$output['Status'] = "FAIL";
$output['StatusDetail'] = curl_error($curlSession);
}
curl_close ($curlSession);
for ($i=0; $i<count($response); $i++){
$splitAt = strpos($response[$i], "=");
$output[trim(substr($response[$i], 0, $splitAt))] = trim(substr($response[$i], ($splitAt+1)));
}
return $output;
}
/*
* Format data for sending in POST request
* $data = data as an associative array
*
*/
private function formatData($data){
$output = "";
foreach($data as $key => $value){
$output .= "&" . $key . "=". urlencode($value);
}
$output = substr($output,1);
return $output;
}
public static function countries() {
return array(
'AF' => 'Afghanistan',
'AX' => 'Åland Islands',
'AL' => 'Albania',
'DZ' => 'Algeria',
'AS' => 'American Samoa',
'AD' => 'Andorra',
'AO' => 'Angola',
'AI' => 'Anguilla',
'AQ' => 'Antarctica',
'AG' => 'Antigua and Barbuda',
'AR' => 'Argentina',
'AM' => 'Armenia',
'AW' => 'Aruba',
'AU' => 'Australia',
'AT' => 'Austria',
'AZ' => 'Azerbaijan',
'BS' => 'Bahamas',
'BH' => 'Bahrain',
'BD' => 'Bangladesh',
'BB' => 'Barbados',
'BY' => 'Belarus',
'BE' => 'Belgium',
'BZ' => 'Belize',
'BJ' => 'Benin',
'BM' => 'Bermuda',
'BT' => 'Bhutan',
'BO' => 'Bolivia',
'BA' => 'Bosnia and Herzegovina',
'BW' => 'Botswana',
'BV' => 'Bouvet Island',
'BR' => 'Brazil',
'IO' => 'British Indian Ocean Territory',
'BN' => 'Brunei Darussalam',
'BG' => 'Bulgaria',
'BF' => 'Burkina Faso',
'BI' => 'Burundi',
'KH' => 'Cambodia',
'CM' => 'Cameroon',
'CA' => 'Canada',
'CV' => 'Cape Verde',
'KY' => 'Cayman Islands',
'CF' => 'Central African Republic',
'TD' => 'Chad',
'CL' => 'Chile',
'CN' => 'China',
'CX' => 'Christmas Island',
'CC' => 'Cocos (Keeling) Islands',
'CO' => 'Colombia',
'KM' => 'Comoros',
'CG' => 'Congo',
'CD' => 'Congo, the Democratic Republic of the',
'CK' => 'Cook Islands',
'CR' => 'Costa Rica',
'HR' => 'Croatia',
'CU' => 'Cuba',
'CY' => 'Cyprus',
'CZ' => 'Czech Republic',
'CI' => 'Côte d\'Ivoire',
'DK' => 'Denmark',
'DJ' => 'Djibouti',
'DM' => 'Dominica',
'DO' => 'Dominican Republic',
'EC' => 'Ecuador',
'EG' => 'Egypt',
'SV' => 'El Salvador',
'GQ' => 'Equatorial Guinea',
'ER' => 'Eritrea',
'EE' => 'Estonia',
'ET' => 'Ethiopia',
'FK' => 'Falkland Islands (Malvinas)',
'FO' => 'Faroe Islands',
'FJ' => 'Fiji',
'FI' => 'Finland',
'FR' => 'France',
'GF' => 'French Guiana',
'PF' => 'French Polynesia',
'TF' => 'French Southern Territories',
'GA' => 'Gabon',
'GM' => 'Gambia',
'GE' => 'Georgia',
'DE' => 'Germany',
'GH' => 'Ghana',
'GI' => 'Gibraltar',
'GR' => 'Greece',
'GL' => 'Greenland',
'GD' => 'Grenada',
'GP' => 'Guadeloupe',
'GU' => 'Guam',
'GT' => 'Guatemala',
'GG' => 'Guernsey',
'GN' => 'Guinea',
'GW' => 'Guinea-Bissau',
'GY' => 'Guyana',
'HT' => 'Haiti',
'HM' => 'Heard Island and McDonald Islands',
'VA' => 'Holy See (Vatican City State)',
'HN' => 'Honduras',
'HK' => 'Hong Kong',
'HU' => 'Hungary',
'IS' => 'Iceland',
'IN' => 'India',
'ID' => 'Indonesia',
'IR' => 'Iran, Islamic Republic of',
'IQ' => 'Iraq',
'IE' => 'Ireland',
'IM' => 'Isle of Man',
'IL' => 'Israel',
'IT' => 'Italy',
'JM' => 'Jamaica',
'JP' => 'Japan',
'JE' => 'Jersey',
'JO' => 'Jordan',
'KZ' => 'Kazakhstan',
'KE' => 'Kenya',
'KI' => 'Kiribati',
'KP' => 'Korea, Democratic People\'s Republic of',
'KR' => 'Korea, Republic of',
'KW' => 'Kuwait',
'KG' => 'Kyrgyzstan',
'LA' => 'Lao People\'s Democratic Republic',
'LV' => 'Latvia',
'LB' => 'Lebanon',
'LS' => 'Lesotho',
'LR' => 'Liberia',
'LY' => 'Libyan Arab Jamahiriya',
'LI' => 'Liechtenstein',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'MO' => 'Macao',
'MK' => 'Macedonia, the former Yugoslav Republic of',
'MG' => 'Madagascar',
'MW' => 'Malawi',
'MY' => 'Malaysia',
'MV' => 'Maldives',
'ML' => 'Mali',
'MT' => 'Malta',
'MH' => 'Marshall Islands',
'MQ' => 'Martinique',
'MR' => 'Mauritania',
'MU' => 'Mauritius',
'YT' => 'Mayotte',
'MX' => 'Mexico',
'FM' => 'Micronesia, Federated States of',
'MD' => 'Moldova, Republic of',
'MC' => 'Monaco',
'MN' => 'Mongolia',
'ME' => 'Montenegro',
'MS' => 'Montserrat',
'MA' => 'Morocco',
'MZ' => 'Mozambique',
'MM' => 'Myanmar',
'NA' => 'Namibia',
'NR' => 'Nauru',
'NP' => 'Nepal',
'NL' => 'Netherlands',
'AN' => 'Netherlands Antilles',
'NC' => 'New Caledonia',
'NZ' => 'New Zealand',
'NI' => 'Nicaragua',
'NE' => 'Niger',
'NG' => 'Nigeria',
'NU' => 'Niue',
'NF' => 'Norfolk Island',
'MP' => 'Northern Mariana Islands',
'NO' => 'Norway',
'OM' => 'Oman',
'PK' => 'Pakistan',
'PW' => 'Palau',
'PS' => 'Palestinian Territory, Occupied',
'PA' => 'Panama',
'PG' => 'Papua New Guinea',
'PY' => 'Paraguay',
'PE' => 'Peru',
'PH' => 'Philippines',
'PN' => 'Pitcairn',
'PL' => 'Poland',
'PT' => 'Portugal',
'PR' => 'Puerto Rico',
'QA' => 'Qatar',
'RE' => 'Reunion Réunion',
'RO' => 'Romania',
'RU' => 'Russian Federation',
'RW' => 'Rwanda',
'BL' => 'Saint Barthélemy',
'SH' => 'Saint Helena',
'KN' => 'Saint Kitts and Nevis',
'LC' => 'Saint Lucia',
'MF' => 'Saint Martin (French part)',
'PM' => 'Saint Pierre and Miquelon',
'VC' => 'Saint Vincent and the Grenadines',
'WS' => 'Samoa',
'SM' => 'San Marino',
'ST' => 'Sao Tome and Principe',
'SA' => 'Saudi Arabia',
'SN' => 'Senegal',
'RS' => 'Serbia',
'SC' => 'Seychelles',
'SL' => 'Sierra Leone',
'SG' => 'Singapore',
'SK' => 'Slovakia',
'SI' => 'Slovenia',
'SB' => 'Solomon Islands',
'SO' => 'Somalia',
'ZA' => 'South Africa',
'GS' => 'South Georgia and the South Sandwich Islands',
'ES' => 'Spain',
'LK' => 'Sri Lanka',
'SD' => 'Sudan',
'SR' => 'Suriname',
'SJ' => 'Svalbard and Jan Mayen',
'SZ' => 'Swaziland',
'SE' => 'Sweden',
'CH' => 'Switzerland',
'SY' => 'Syrian Arab Republic',
'TW' => 'Taiwan',
'TJ' => 'Tajikistan',
'TZ' => 'Tanzania, United Republic of',
'TH' => 'Thailand',
'TL' => 'Timor-Leste',
'TG' => 'Togo',
'TK' => 'Tokelau',
'TO' => 'Tonga',
'TT' => 'Trinidad and Tobago',
'TN' => 'Tunisia',
'TR' => 'Turkey',
'TM' => 'Turkmenistan',
'TC' => 'Turks and Caicos Islands',
'TV' => 'Tuvalu',
'UG' => 'Uganda',
'UA' => 'Ukraine',
'AE' => 'United Arab Emirates',
'GB' => 'United Kingdom',
'US' => 'United States',
'UM' => 'United States Minor Outlying Islands',
'UY' => 'Uruguay',
'UZ' => 'Uzbekistan',
'VU' => 'Vanuatu',
'VE' => 'Venezuela, Bolivarian Republic of',
'VN' => 'Viet Nam',
'VG' => 'Virgin Islands, British',
'VI' => 'Virgin Islands, U.S.',
'WF' => 'Wallis and Futuna',
'EH' => 'Western Sahara',
'YE' => 'Yemen',
'ZM' => 'Zambia',
'ZW' => 'Zimbabwe'
);
}
public static function country($code) {
$countries = self::countries();
return $countries[$code];
}
}