/
sagepay_form.php
579 lines (474 loc) · 18.2 KB
/
sagepay_form.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Sage Pay Form class
*
* This CodeIgniter library to integrate the Sage Pay Go Form service
* http://www.sagepay.com/products_services/sage_pay_go/integration/form
*
* @package sagepay_form
* @author Ollie Rattue, Too many tabs <orattue[at]toomanytabs.com>
* @copyright Copyright (c) 2011, Ollie Rattue
* @license http://www.opensource.org/licenses/mit-license.php
* @link https://github.com/ollierattue/codeigniter-sagepay-form
*/
class sagepay_form
{
var $protocol_version = "2.23";
var $config;
var $vendor_name;
var $your_site_fqdn;
var $transaction_type;
var $encryption_password;
var $purchase_url;
var $partner_id;
var $currency;
var $vendor_tx_code;
var $total;
var $description;
var $billing_first_names;
var $billing_surname;
var $billing_address1;
var $billing_address2;
var $billing_city;
var $billing_postcode;
var $billing_country;
var $billing_state;
var $billing_phone;
var $delivery_first_names;
var $delivery_surname;
var $delivery_address1;
var $delivery_address2;
var $delivery_city;
var $delivery_postcode;
var $delivery_country;
var $delivery_state;
var $delivery_phone;
var $send_email;
var $vendor_email;
var $customer_email;
var $email_message;
var $allow_gift_aid = 0;
var $billing_agreement = 0;
var $apply_avscv2 = 0;
var $apply_3d_secure = 0;
var $submit_btn; // Image/Form button
var $CI;
/**
* Constructor
*
* @access public
*/
public function __construct()
{
if (!isset($this->CI))
{
$this->CI =& get_instance();
}
$this->CI->load->helper('url');
$this->CI->load->helper('form');
$this->CI->load->config('sagepay_form_config');
log_message('debug', "Sage Pay Form Class Initialized");
$this->button('Proceed to payment');
$this->vendor_name = $this->CI->config->item('vendor_name');
$this->your_site_fqdn = $this->CI->config->item('your_site_fqdn');
$this->encryption_password = $this->CI->config->item('encryption_password');
$this->partner_id = $this->CI->config->item('partner_id');
$this->currency = $this->CI->config->item('currency');
$this->transaction_type = $this->CI->config->item('transaction_type');
$this->send_email = $this->CI->config->item('send_email');
$this->vendor_email = $this->CI->config->item('vendor_email');
$this->purchase_url = $this->CI->config->item('purchase_url');
}
// --------------------------------------------------------------------
function set_field($field = NULL, $value = NULL)
{
$this->{$field} = $value;
}
// --------------------------------------------------------------------
function set_same_delivery_address()
{
$this->delivery_first_names = $this->billing_first_names;
$this->delivery_surname = $this->billing_surname;
$this->delivery_address1 = $this->billing_address1;
$this->delivery_address2 = $this->billing_address2;
$this->delivery_city = $this->billing_city;
$this->delivery_postcode = $this->billing_postcode;
$this->delivery_country = $this->billing_country;
$this->delivery_state = $this->billing_state;
$this->delivery_phone = $this->billing_phone;
}
// --------------------------------------------------------------------
// Creates a unique string
// Called by controller and the value would be stored in db against the purchase
function create_vendor_tx_code()
{
$timestamp = date("y-m-d-H-i-s", time());
$random_number = rand(0,32000)*rand(0,32000);
$this->vendor_tx_code = "{$timestamp}-{$random_number}";
return $this->vendor_tx_code;
}
// --------------------------------------------------------------------
function form($form_name = 'SagePayForm')
{
$strCrypt = $this->_build_form_crypt();
$str = '<form action="'.$this->purchase_url.'" method="POST" id="SagePayForm" name="'.$form_name.'">' . "\n";
$str .= form_hidden('navigate', "") . "\n";
$str .= form_hidden('VPSProtocol', $this->protocol_version) . "\n";
$str .= form_hidden('TxType', $this->transaction_type) . "\n";
$str .= form_hidden('Vendor', $this->vendor_name) . "\n";
$str .= form_hidden('Crypt', $strCrypt) . "\n";
$str .= $this->submit_btn;
$str .= form_close() . "\n";
return $str;
}
// --------------------------------------------------------------------
// This function actually generates an entire HTML page consisting of
// a form with hidden elements which is submitted to Sage Pay via the
// BODY element's onLoad attribute. We do this so that you can validate
// any POST vars from your custom form before submitting to Sage Pay.
// You would have your own form which is submitted to your script
// to validate the data, which in turn calls this function to create
// another hidden form and submit to Sage Pay.
function auto_form()
{
$this->button('Click here if you\'re not automatically redirected...');
echo '<html>' . "\n";
echo '<head><title>Processing Payment...</title></head>' . "\n";
echo '<body onLoad="document.forms[\'sagepay_auto_form\'].submit();">' . "\n";
echo '<p>Please wait, your order is being processed and you will be redirected to our payment partner.</p>' . "\n";
echo $this->form('sagepay_auto_form');
echo '</body></html>';
}
// --------------------------------------------------------------------
function button($value = NULL)
{
// changes the default caption of the submit button
$this->submit_btn = form_submit('sagepay_submit', $value);
}
// --------------------------------------------------------------------
function _build_form_crypt()
{
// ** TODO ADD in basket basket support **
// Now to build the Form crypt field.
if ($this->vendor_tx_code == '')
{
// This is a fallback in the instance that the controller
// did not call this function which it should to store the
// value in the db for records.
$this->create_vendor_tx_code();
}
$strPost = "VendorTxCode={$this->vendor_tx_code}";
// Optional: If you are a Sage Pay Partner and wish to flag the transactions with your unique partner id, it should be passed here
if (strlen($this->partner_id) > 0)
{
$strPost .= "&ReferrerID={$this->partner_id}";
}
$strPost .= "&Amount=".number_format($this->total, 2); // Formatted to 2 decimal places with leading digit
$strPost .= "&Currency={$this->currency}";
// Up to 100 chars of free format description
$strPost .= "&Description={$this->description}"; //*********** HARD CODDED ****************
/* The SuccessURL is the page to which Form returns the customer if the transaction is successful
** You can change this for each transaction, perhaps passing a session ID or state flag if you wish */
$strPost .= "&SuccessURL={$this->your_site_fqdn}{$this->success_url}"; //*********** HARD CODDED ****************
/* The FailureURL is the page to which Form returns the customer if the transaction is unsuccessful
** You can change this for each transaction, perhaps passing a session ID or state flag if you wish */
$strPost .= "&FailureURL={$this->your_site_fqdn}{$this->failure_url}"; //*********** HARD CODDED ****************
// This is an Optional setting. Here we are just using the Billing names given.
$strPost .= "&CustomerName={$this->billing_first_names} {$this->billing_surname}";
/* Email settings:
** Flag 'SendEMail' is an Optional setting.
** 0 = Do not send either customer or vendor e-mails,
** 1 = Send customer and vendor e-mails if address(es) are provided(DEFAULT).
** 2 = Send Vendor Email but not Customer Email. If you do not supply this field, 1 is assumed and e-mails are sent if addresses are provided. **/
if ($this->send_email == 0)
{
$strPost .= "&SendEMail=0";
}
else
{
if ($this->send_email == 1)
{
$strPost .= "&SendEMail=1";
}
else
{
$strPost .= "&SendEMail=2";
}
if (strlen($this->customer_email) > 0)
{
$strPost .= "&CustomerEMail={$this->customer_email}"; // This is an Optional setting
}
if ($this->vendor_email <> "")
{
$strPost .= "&VendorEMail={$this->vendor_email}"; // This is an Optional setting
}
// You can specify any custom message to send to your customers in their confirmation e-mail here
// The field can contain HTML if you wish, and be different for each order. This field is optional
if (strlen($this->email_message) > 0)
{
$strPost .= "&eMailMessage={$this->email_message}";
}
}
// Billing Details:
$strPost .= "&BillingFirstnames={$this->billing_first_names}";
$strPost .= "&BillingSurname={$this->billing_surname}";
$strPost .= "&BillingAddress1={$this->billing_address1}";
if (strlen($this->billing_address2) > 0)
{
$strPost .= "&BillingAddress2={$this->billing_address2}";
}
$strPost .= "&BillingCity={$this->billing_city}";
$strPost .= "&BillingPostCode={$this->billing_postcode}";
$strPost .= "&BillingCountry={$this->billing_country}";
if (strlen($this->billing_state) > 0)
{
$strPost .= "&BillingState={$this->billing_state}";
}
if (strlen($this->billing_phone) > 0)
{
$strPost .= "&BillingPhone={$this->billing_phone}";
}
// Delivery Details:
$strPost .= "&DeliveryFirstnames={$this->delivery_first_names}";
$strPost .= "&DeliverySurname={$this->delivery_surname}";
$strPost .= "&DeliveryAddress1={$this->delivery_address1}";
if (strlen($this->delivery_address2) > 0)
{
$strPost .= "&DeliveryAddress2={$this->delivery_address2}";
}
$strPost .= "&DeliveryCity={$this->delivery_city}";
$strPost .= "&DeliveryPostCode={$this->delivery_postcode}";
$strPost .= "&DeliveryCountry={$this->delivery_country}";
if (strlen($this->delivery_state) > 0)
{
$strPost .= "&DeliveryState={$this->delivery_state}";
}
if (strlen($this->delivery_phone) > 0)
{
$strPost .= "&DeliveryPhone={$this->delivery_phone}";
}
// For charities registered for Gift Aid, set to 1 to display the Gift Aid check box on the payment pages
if ($this->allow_gift_aid == 1)
{
$strPost .= "&AllowGiftAid=1"; //*********** HARD CODDED ****************
}
else
{
$strPost .= "&AllowGiftAid=0";
}
/* Allow fine control over AVS/CV2 checks and rules by changing this value. 0 is Default
** It can be changed dynamically, per transaction, if you wish. See the Form Protocol document */
if ($this->transaction_type !== "AUTHENTICATE")
{
switch($this->apply_avscv2)
{
case "1":
case "2":
case "3":
$strPost .= "&ApplyAVSCV2={$this->apply_avscv2}";
break;
default:
$strPost .= "&ApplyAVSCV2=0";
break;
}
}
/* Allow fine control over 3D-Secure checks and rules by changing this value. 0 is Default
** It can be changed dynamically, per transaction, if you wish. See the Form Protocol document */
switch($this->apply_3d_secure)
{
case "1":
case "2":
case "3":
$strPost .= "&Apply3DSecure={$this->apply_3d_secure}"; //*********** HARD CODDED ****************
break;
default:
$strPost .= "&Apply3DSecure=0"; //*********** HARD CODDED ****************
break;
}
/* This field must be set for PAYPAL REFERENCE transactions
All non-PayPal transactions can be repeated without this
flag.
If you wish to register this transaction as the first in a series of
regular payments, this field should be set to 1.
If you do not have a PayPal account set up for use via Sage Pay, then this field
is not necessary and should be omitted or set to 0. */
if ($this->billing_agreement == 1)
{
$strPost .= "&BillingAgreement=1";
}
else
{
$strPost .= "&BillingAgreement=0";
}
// Encrypt the plaintext string for inclusion in the hidden field
$strCrypt = $this->_encode_crypt($strPost);
return $strCrypt;
}
// --------------------------------------------------------------------
/* The getToken function. **
** NOTE: A function of convenience that extracts the value from the "name=value&name2=value2..." reply string **
** Works even if one of the values is a URL containing the & or = signs. */
function getToken($thisString) {
// List the possible tokens
$Tokens = array(
"Status",
"StatusDetail",
"VendorTxCode",
"VPSTxId",
"TxAuthNo",
"Amount",
"AVSCV2",
"AddressResult",
"PostCodeResult",
"CV2Result",
"GiftAid",
"3DSecureStatus",
"CAVV",
"AddressStatus",
"CardType",
"Last4Digits",
"PayerStatus","CardType");
// Initialise arrays
$output = array();
$resultArray = array();
// Get the next token in the sequence
for ($i = count($Tokens)-1; $i >= 0 ; $i--){
// Find the position in the string
$start = strpos($thisString, $Tokens[$i]);
// If it's present
if ($start !== false){
// Record position and token name
$resultArray[$i]->start = $start;
$resultArray[$i]->token = $Tokens[$i];
}
}
// Sort in order of position
sort($resultArray);
// Go through the result array, getting the token values
for ($i = 0; $i<count($resultArray); $i++){
// Get the start point of the value
$valueStart = $resultArray[$i]->start + strlen($resultArray[$i]->token) + 1;
// Get the length of the value
if ($i==(count($resultArray)-1)) {
$output[$resultArray[$i]->token] = substr($thisString, $valueStart);
} else {
$valueLength = $resultArray[$i+1]->start - $resultArray[$i]->start - strlen($resultArray[$i]->token) - 2;
$output[$resultArray[$i]->token] = substr($thisString, $valueStart, $valueLength);
}
}
// Return the ouput array
return $output;
}
// --------------------------------------------------------------------
// Filters unwanted characters out of an input string. Useful for tidying up FORM field inputs.
function _cleanInput($strRawText,$strType)
{
if ($strType=="Number") {
$strClean="0123456789.";
$bolHighOrder=false;
}
else if ($strType=="VendorTxCode") {
$strClean="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
$bolHighOrder=false;
}
else {
$strClean=" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,'/{}@():?-_&£$=%~<>*+\"";
$bolHighOrder=true;
}
$strCleanedText="";
$iCharPos = 0;
do
{
// Only include valid characters
$chrThisChar=substr($strRawText,$iCharPos,1);
if (strspn($chrThisChar,$strClean,0,strlen($strClean))>0) {
$strCleanedText=$strCleanedText . $chrThisChar;
}
else if ($bolHighOrder==true) {
// Fix to allow accented characters and most high order bit chars which are harmless
if (bin2hex($chrThisChar)>=191) {
$strCleanedText=$strCleanedText . $chrThisChar;
}
}
$iCharPos=$iCharPos+1;
}
while ($iCharPos<strlen($strRawText));
$cleanInput = ltrim($strCleanedText);
return $cleanInput;
}
// --------------------------------------------------------------------
// Wrapper function to encrypt data to store in hidden field which is sent to Sage Pay
function _encode_crypt($post = NULL)
{
return $this->_base64Encode($this->_SimpleXor($post, $this->encryption_password));
}
// --------------------------------------------------------------------
// Wrapper function to decrypt the response data sent back from Sage Pay to success/failure url via url string
function decode_crypt($crypt = NULL)
{
return $this->_simpleXor($this->_base64Decode($crypt), $this->encryption_password);
}
// --------------------------------------------------------------------
/* Base 64 Encoding function **
** PHP does it natively but just for consistency and ease of maintenance, let's declare our own function **/
function _base64Encode($plain) {
// Initialise output variable
$output = "";
// Do encoding
$output = base64_encode($plain);
// Return the result
return $output;
}
// --------------------------------------------------------------------
/* Base 64 decoding function **
** PHP does it natively but just for consistency and ease of maintenance, let's declare our own function **/
function _base64Decode($scrambled) {
// Initialise output variable
$output = "";
// Fix plus to space conversion issue
$scrambled = str_replace(" ","+",$scrambled);
// Do encoding
$output = base64_decode($scrambled);
// Return the result
return $output;
}
// --------------------------------------------------------------------
/* The SimpleXor encryption algorithm **
** NOTE: This is a placeholder really. Future releases of Form will use AES or TwoFish. Proper encryption **
** This simple function and the Base64 will deter script kiddies and prevent the "View Source" type tampering **
** It won't stop a half decent hacker though, but the most they could do is change the amount field to something **
** else, so provided the vendor checks the reports and compares amounts, there is no harm done. It's still **
** more secure than the other PSPs who don't both encrypting their forms at all */
function _simpleXor($InString, $Key)
{
// Initialise key array
$KeyList = array();
// Initialise out variable
$output = "";
// Convert $Key into array of ASCII values
for($i = 0; $i < strlen($Key); $i++){
$KeyList[$i] = ord(substr($Key, $i, 1));
}
// Step through string a character at a time
for($i = 0; $i < strlen($InString); $i++) {
// Get ASCII code from string, get ASCII code from key (loop through with MOD), XOR the two, get the character from the result
// % is MOD (modulus), ^ is XOR
$output.= chr(ord(substr($InString, $i, 1)) ^ ($KeyList[$i % strlen($Key)]));
}
// Return the result
return $output;
}
// --------------------------------------------------------------------
// Function to check validity of email address entered in form fields
function _is_valid_email($email)
{
$result = TRUE;
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
$result = FALSE;
}
return $result;
}
// --------------------------------------------------------------------
}
/* End of file sagepay_form.php */
/* Location: ./application/libraries/sagepay_form.php */