forked from Payfast/mod-hikashop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payfast_common.inc
324 lines (275 loc) · 8.84 KB
/
payfast_common.inc
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
<?php
/**
* payfast_common.inc
*
* Copyright (c) 2008 PayFast (Pty) Ltd
* You (being anyone who is not PayFast (Pty) Ltd) may download and use this plugin / code in your own website in conjunction with a registered and active PayFast account. If your PayFast account is terminated for any reason, you may not use this plugin / code or part thereof.
* Except as expressly indicated in this licence, you may not use, copy, modify or distribute this plugin / code or part thereof in any way.
*
* @author Jonathan Smit
*/
//// Create user agent string
// User agent constituents (for cURL)
define( 'PF_SOFTWARE_NAME', 'HikaShop' );
define( 'PF_SOFTWARE_VER', "2.2.3" );
define( 'PF_MODULE_NAME', 'PayFast-HikaShop' );
define( 'PF_MODULE_VER', '1.0.0' );
// Features
// - PHP
$pfFeatures = 'PHP '. phpversion() .';';
// - cURL
if( in_array( 'curl', get_loaded_extensions() ) )
{
define( 'PF_CURL', '' );
$pfVersion = curl_version();
$pfFeatures .= ' curl '. $pfVersion['version'] .';';
}
else
$pfFeatures .= ' nocurl;';
// Create user agrent
define( 'PF_USER_AGENT', PF_SOFTWARE_NAME .'/'. PF_SOFTWARE_VER .' ('. trim( $pfFeatures ) .') '. PF_MODULE_NAME .'/'. PF_MODULE_VER );
// General Defines
define( 'PF_TIMEOUT', 15 );
define( 'PF_EPSILON', 0.01 );
// Messages
// Error
define( 'PF_ERR_AMOUNT_MISMATCH', 'Amount mismatch' );
define( 'PF_ERR_BAD_ACCESS', 'Bad access of page' );
define( 'PF_ERR_BAD_SOURCE_IP', 'Bad source IP address' );
define( 'PF_ERR_CONNECT_FAILED', 'Failed to connect to PayFast' );
define( 'PF_ERR_INVALID_SIGNATURE', 'Security signature mismatch' );
define( 'PF_ERR_MERCHANT_ID_MISMATCH', 'Merchant ID mismatch' );
define( 'PF_ERR_NO_SESSION', 'No saved session found for ITN transaction' );
define( 'PF_ERR_ORDER_ID_MISSING_URL', 'Order ID not present in URL' );
define( 'PF_ERR_ORDER_ID_MISMATCH', 'Order ID mismatch' );
define( 'PF_ERR_ORDER_INVALID', 'This order ID is invalid' );
define( 'PF_ERR_ORDER_PROCESSED', 'This order has already been processed' );
define( 'PF_ERR_PDT_FAIL', 'PDT query failed' );
define( 'PF_ERR_PDT_TOKEN_MISSING', 'PDT token not present in URL' );
define( 'PF_ERR_SESSIONID_MISMATCH', 'Session ID mismatch' );
define( 'PF_ERR_UNKNOWN', 'Unkown error occurred' );
// General
define( 'PF_MSG_OK', 'Payment was successful' );
define( 'PF_MSG_FAILED', 'Payment has failed' );
define( 'PF_MSG_PENDING',
'The payment is pending. Please note, you will receive another Instant'.
' Transaction Notification when the payment status changes to'.
' "Completed", or "Failed"' );
/**
* pflog
*
* Log function for logging output.
*
* @author Jonathan Smit
* @param $msg String Message to log
* @param $close Boolean Whether to close the log file or not
*/
function pflog( $msg = '', $close = false )
{
static $fh = 0;
// Only log if debugging is enabled
if( PF_DEBUG )
{
if( $close )
{
fclose( $fh );
}
else
{
// If file doesn't exist, create it
if( !$fh )
{
$pathinfo = pathinfo( __FILE__ );
$fh = fopen( $pathinfo['dirname'] .'/payfast.log', 'a+' );
}
// If file was successfully created
if( $fh )
{
$line = date( 'Y-m-d H:i:s' ) .' : '. $msg ."\n";
fwrite( $fh, $line );
}
}
}
}
/**
* pfGetData
*
* @author Jonathan Smit
*/
function pfGetData()
{
// Posted variables from ITN
$pfData = $_POST;
// Strip any slashes in data
foreach( $pfData as $key => $val )
$pfData[$key] = stripslashes( $val );
// Return "false" if no data was received
if( sizeof( $pfData ) == 0 )
return( false );
else
return( $pfData );
}
/**
* pfValidSignature
*
* @author Jonathan Smit
*/
function pfValidSignature( $pfData = null, &$pfParamString = null, $pfPassphrase=null )
{
// Dump the submitted variables and calculate security signature
foreach( $pfData as $key => $val )
{
if( $key != 'signature' ){
$pfParamString .= $key .'='. urlencode( $val ) .'&';
}
else
{
break;
}
}
// Remove the last '&' from the parameter string
$pfParamString = substr( $pfParamString, 0, -1 );
if( is_null( $pfPassphrase ) )
{
$tempParamString = $pfParamString;
}
else
{
$tempParamString = $pfParamString."&passphrase=".urlencode( $pfPassphrase );
}
$signature = md5( $tempParamString );
$result = ( $pfData['signature'] == $signature );
pflog( 'Signature = '. ( $result ? 'valid' : 'invalid' ) );
pflog('PFString = '.$pfParamString);
return( $result );
}
/**
* pfValidData
*
* @author Jonathan Smit
* @param $pfHost String Hostname to use
* @param $pfParamString String
*/
function pfValidData( $pfHost = 'www.payfast.co.za', $pfParamString = '' )
{
pflog( 'Host = '. $pfHost );
pflog( 'Params = '. $pfParamString );
// Use cURL (if available)
if( defined( 'PF_CURL' ) )
{
// Variable initialization
$url = 'https://'. $pfHost .'/eng/query/validate';
// Create default cURL object
$ch = curl_init();
// Set cURL options - Use curl_setopt for freater PHP compatibility
// Base settings
curl_setopt( $ch, CURLOPT_USERAGENT, PF_USER_AGENT ); // Set user agent
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return output as string rather than outputting it
curl_setopt( $ch, CURLOPT_HEADER, false ); // Don't include header in output
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
// Standard settings
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $pfParamString );
curl_setopt( $ch, CURLOPT_TIMEOUT, PF_TIMEOUT );
// Execute CURL
$response = curl_exec( $ch );
curl_close( $ch );
}
// Use fsockopen
else
{
// Variable initialization
$header = '';
$res = '';
$headerDone = false;
// Construct Header
$header = "POST /eng/query/validate HTTP/1.0\n";
$header .= "Host: ". $pfHost ."\n";
$header .= "User-Agent: ". PF_USER_AGENT ."\n";
$header .= "Content-Type: application/x-www-form-urlencoded\n";
$header .= "Content-Length: " . strlen( $pfParamString ) . "\n\n";
// Connect to server
$socket = fsockopen( 'ssl://'. $pfHost, 443, $errno, $errstr, PF_TIMEOUT );
// Send command to server
fputs( $socket, $header . $pfParamString );
// Read the response from the server
while( !feof( $socket ) )
{
$line = fgets( $socket, 1024 );
// Check if we are finished reading the header yet
if( strcmp( $line, "\n" ) == 0 )
{
// read the header
$headerDone = true;
}
// If header has been processed
else if( $headerDone )
{
// Read the main response
$response .= $line;
}
}
}
pflog( "Response:\n". print_r( $response, true ) );
// Interpret Response
$lines = explode( "\n", $response );
$verifyResult = trim( $lines[0] );
if( strcasecmp( $verifyResult, 'VALID' ) == 0 )
return( true );
else
return( false );
}
/**
* pfValidIP
*
* @author Jonathan Smit
* @param $sourceIP String Source IP address
*/
function pfValidIP( $sourceIP )
{
// Variable initialization
$validHosts = array(
'www.payfast.co.za',
'sandbox.payfast.co.za',
'w1w.payfast.co.za',
'w2w.payfast.co.za',
);
$validIps = array();
foreach( $validHosts as $pfHostname )
{
$ips = gethostbynamel( $pfHostname );
if( $ips !== false )
$validIps = array_merge( $validIps, $ips );
}
// Remove duplicates
$validIps = array_unique( $validIps );
pflog( "Valid IPs:\n". print_r( $validIps, true ) );
if( in_array( $sourceIP, $validIps ) )
return( true );
else
return( false );
}
/**
* pfAmountsEqual
*
* Checks to see whether the given amounts are equal using a proper floating
* point comparison with an Epsilon which ensures that insignificant decimal
* places are ignored in the comparison.
*
* eg. 100.00 is equal to 100.0001
*
* @author Jonathan Smit
* @param $amount1 Float 1st amount for comparison
* @param $amount2 Float 2nd amount for comparison
*/
function pfAmountsEqual( $amount1, $amount2 )
{
if( abs( floatval( $amount1 ) - floatval( $amount2 ) ) > PF_EPSILON )
return( false );
else
return( true );
}
// }}}
?>