Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

paypalnotify.php
Go to the documentation of this file.
1 <?php
4 #########################################################
5 # Copyright � EliteWeaver UK All rights reserved. #
6 #########################################################
7 # #
8 # Program : IPN Development Handler #
9 # Author : Marcus Cicero #
10 # File : notify.php #
11 # Function : Skeleton IPN Handler #
12 # Version : 2.0 #
13 # Last Modified : 10/04/2003 #
14 # Copyright � : EliteWeaver UK #
15 # #
16 #########################################################
17 # THIS SCRIPT IS FREEWARE AND IS NOT FOR RE-SALE! #
18 #########################################################
19 # END USER LICENCE AGREEMENT #
20 # Redistribution and use in source and/or binary forms #
21 # with or without modification, are permitted provided #
22 # that the above copyright notice is reproduced in the #
23 # script, documentation and/or any other materials that #
24 # may have been provided in the original distribution. #
25 #########################################################
26 # Copyright � EliteWeaver UK All rights reserved. #
27 #########################################################
28
29 // include_once( "classes/ezlog.php" );
30 // include_once( "eztrade/classes/ezorderconfirmation.php" );
31 // include_once( "eztrade/classes/ezpaypalreturn.php" );
32 // include_once( "eztrade/classes/ezorder.php" );
33 // include_once( "ezsession/classes/ezsession.php" );
34 // include_once( "classes/INIFile.php" );
35 // include_once( "ezmail/classes/ezmail.php" );
36
37 $PaypalEmail = $ini->variable( "Checkout", "PaypalEmail" );
38 $PaypalMode = $ini->variable( "Checkout", "PaypalMode" );
39 $SiteURL = $ini->variable( "Site", "SiteURL" );
40
41 // IPN validation modes, choose: 1, 2, or 3
42
43 if ( $PaypalMode == 'Sandbox')
44 $postmode=3;
45 elseif ( $PaypalMode == 'EliteWeaver' )
46 $postmode=2;
47 elseif ( $PaypalMode == 'Paypal' )
48 $postmode=1;
49
50 //* 1 = Live Via PayPal Network
51 //* 2 = Test Via EliteWeaver UK
52 //* 3 = Test Via PayPal Sandbox Network
53
54
55 // Debugger, 1 = on and 0 = off
56
57 $debugger=0;
58
59 // Convert super globals on older php builds
60
61 if (phpversion() <= '4.0.6')
62 {
63 $_SERVER = ($HTTP_SERVER_VARS);
64 $_POST = ($HTTP_POST_VARS); }
65
66
67
68 // No ipn post means this script does not exist
69
70 if (!@$_POST['txn_type'])
71 {
72 @header("Status: 404 Not Found"); exit; }
73
74 else
75 {
76 @header("Status: 200 OK"); // Prevents ipn reposts on some servers
77
78
79
80 // Add "cmd" to prepare for post back validation
81 // Read the ipn post from paypal or eliteweaver uk
82 // Fix issue with php magic quotes enabled on gpc
83 // Apply variable antidote (replaces array filter)
84 // Destroy the original ipn post (security reason)
85 // Reconstruct the ipn string ready for the post
86
87
88 $postipn = 'cmd=_notify-validate'; // Notify validate
89
90 foreach ($_POST as $ipnkey => $ipnval)
91 {
92 if (get_magic_quotes_gpc())
93 $ipnval = stripslashes ($ipnval); // Fix issue with magic quotes
94 if (!eregi("^[_0-9a-z-]{1,30}$",$ipnkey)
95 || !strcasecmp ($ipnkey, 'cmd'))
96 { // ^ Antidote to potential variable injection and poisoning
97 unset ($ipnkey); unset ($ipnval); } // Eliminate the above
98 if (@$ipnkey != '') { // Remove empty keys (not values)
99 @$_PAYPAL[$ipnkey] = $ipnval; // Assign data to new global array
100 unset ($_POST); // Destroy the original ipn post array, sniff...
101
102 $postipn.='&'.@$ipnkey.'='.urlencode(@$ipnval); }} // Notify string
103 $error=0; // No errors let's hope it's going to stays like this!
104
105
106
107 // IPN validation mode 1: Live Via PayPal Network
108
109 if ($postmode == 1)
110 {
111 $domain = "www.paypal.com"; }
112
113 // IPN validation mode 2: Test Via EliteWeaver UK
114
115 elseif ($postmode == 2)
116 {
117 $domain = "www.eliteweaver.co.uk"; }
118
119 // IPN validation mode 2: Test Via Paypal Sandbox
120
121 elseif ($postmode == 3)
122 {
123 $domain = "www.sandbox.paypal.com"; }
124
125 // IPN validation mode was not set to 1 or 2
126
127 else
128 {
129 $error=1;
130 $bmode=1;
131 if ($debugger) debugInfo(); }
132
133
134 @set_time_limit(60); // Attempt to double default time limit incase we switch to Get
135
136
137
138 // Post back the reconstructed instant payment notification
139
140 $socket = @fsockopen($domain,80,$errno,$errstr,30);
141 $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
142 $header.= "User-Agent: PHP/".phpversion()."\r\n";
143 $header.= "Referer: ".$_SERVER['HTTP_HOST'].
144 $_SERVER['PHP_SELF'].@$_SERVER['QUERY_STRING']."\r\n";
145 $header.= "Server: ".$_SERVER['SERVER_SOFTWARE']."\r\n";
146 $header.= "Host: ".$domain.":80\r\n";
147 $header.= "Content-Type: application/x-www-form-urlencoded\r\n";
148 $header.= "Content-Length: ".strlen($postipn)."\r\n";
149 $header.= "Accept: */*\r\n\r\n";
150
151 //* Note: "Connection: Close" is not required using HTTP/1.0
152
153
154
155 // Problem: Now is this your firewall or your ports?
156
157 if (!$socket && !$error)
158 {
159
160 // Switch to a Get request for a last ditch attempt!
161
162 $getrq=1;
163
164 if (phpversion() >= '4.3.0'
165 && function_exists('file_get_contents'))
166 {} // Checking for a new function
167 else
168 { // No? We'll create it instead
169
170 function file_get_contents($ipnget) {
171 $ipnget = @file($ipnget);
172 return $ipnget[0];
173 }}
174
175 $response = @file_get_contents('http://'.$domain.':80/cgi-bin/webscr?'.$postipn);
176
177 if (!$response)
178 {
179 $error=1;
180 $getrq=0;
181
182 if ($debugger) debugInfo();
183 // If this is as far as you get then you need a new web host!
184 }}
185
186
187
188 // If no problems have occured then we proceed with the processing
189
190 else
191 {
192 @fputs ($socket,$header.$postipn."\r\n\r\n"); // Required on some environments
193 while (!feof($socket))
194 {
195 $response = fgets ($socket,1024); }}
196 $response = trim ($response); // Also required on some environments
197
198
199
200 // uncomment '#' to assign posted variables to local variables
201 #extract($_PAYPAL); // if globals is on they are already local
202
203 // and/or >>>
204
205 // refer to each ipn variable by reference (recommended)
206 // $_PAYPAL['receiver_id']; etc... (see: ipnvars.txt)
207
208
209
210 // IPN was confirmed as both genuine and VERIFIED
211
212 if (!strcmp ($response, "VERIFIED"))
213 {
214
215
216 // The following example loop is for 'cart' notifications only!
217
218 for ($i=0; $i < $_PAYPAL['num_cart_items']; $i++)
219 { // ^ This is set to 0 for a reason related to platform compatibility!
220 $x=$i+1; // < In loop incrementation (please see above comment)
221 // Anything related to item specific cart data can be dealt with inside of this loop.
222 // Example: $item_name = ($_PAYPAL['item_name'.$x]);
223 // Must NOT be referenced outside this loop using this type of variable declaration!
224 }
225
226
227 // Check that the "payment_status" variable is: Completed
228 // If it is Pending you may want to inform your customer?
229 // Check your db to ensure this "txn_id" is not a duplicate
230 // You may want to check "payment_gross" or "mc_gross" matches listed prices?
231 // You definately want to check the "receiver_email", "receiver_id" or "business" is yours
232 // Update your db and process this payment accordingly
233
234 //***************************************************************//
235 //* Tip: Use the internal auditing function to do some of this! *//
236 //* **************************************************************************************//
237 //* Help: if(variableAudit('mc_gross','0.01') && *//
238 //* variableAudit('receiver_email','paypal@domain.com') && *//
239 //* variableAudit('payment_status','Completed')){ $do_this; } else { do_that; } *//
240 //****************************************************************************************//
241
242 // check order data for security
243
244 $session = new eZSession($sessionID);
245 $paypal = new eZPaypalReturn();
246
247 if ($session)
248 {
249 $totalCost = number_format( $session->variable( 'TotalCost' ), 2, '.', '' );
250 $sessionOrderID = $session->variable( 'OrderID' );
251 }
252 else
253 {
254 $totalCost = false;
255 $sessionOrderID = false;
256 }
257
258 if( ( variableAudit( 'mc_gross', $totalCost ) ||
259 variableAudit( 'payment_gross', $totalCost ) ) &&
260 variableAudit('receiver_email',$PaypalEmail) &&
261 variableAudit('payment_status','Completed') &&
262 ( $orderID == $sessionOrderID ) &&
263 !$paypal->isDuplicate($_PAYPAL['txn_id']) )
264
265 {
266 // confirm successful order
267 $confirmation = new eZOrderConfirmation( $orderID );
268 $result = $confirmation->confirmOrder( $sessionID );
269 }
270 else
271 {
272 // send an email to the admin for troubleshooting
273 $warningMail = "There is a problem with the following Paypal payment.\n";
274 $warningMail .= "\n";
275 $warningMail .= "totalCost: ".$totalCost."\n";
276 $warningMail .= "sessionOrderID: ".$sessionOrderID."\n";
277 $warningMail .= "\n";
278 $warningMail .= "Site: ".$SiteURL."\n";
279 $warningMail .= "\n";
280 $warningMail .= "Payment status: ".$_PAYPAL['payment_status']."\n";
281 if ($_PAYPAL['payment_status'] == 'Pending')
282 $warningMail .= "Pending reason: ".$_PAYPAL['pending_reason']."\n";
283 if ($_PAYPAL['payment_status'] == 'Reversed' || 'Refunded')
284 $warningMail .= "Reason code: ".$_PAYPAL['pending_reason']."\n";
285 if ($paypal->isDuplicate($_PAYPAL['txn_id']))
286 $warningMail .= "Note: duplicate transaction ID (txn_id)!\n";
287 $warningMail .= "See https://www.paypal.com/ipn for documentation.\n";
288 $warningMail .= "sessionID returned: ".$sessionID."\n";
289 $warningMail .= "orderID returned: ".$orderID."\n";
290 $warningMail .= "\n";
291 $warningMail .= "Paypal data returned:\n";
292 $warningMail .= print_r($_PAYPAL, true)."\n";
293 if ( $orderID != "" && is_numeric($orderID) )
294 {
295 $warningMail .= "Order information (if available):\n";
296 $order = new eZOrder($orderID);
297 $warningMail .= print_r($order, true)."\n";
298 }
299
300 $mail = new eZMail();
301 $mail->setFrom( $ini->variable( "eZTradeMain", "OrderSenderEmail" ) );
302 $mail->setTo( $PaypalEmail );
303 $mail->setSubject( "Paypal payment warning from ".$SiteURL );
304 $mail->setBody( $warningMail );
305 $mail->send();
306 // write same error data to error.log file
307 eZPBLog::writeWarning( "Paypal IPN failure!\n".$warningMail."\n" );
308 }
309 // localize Paypal variables
310 $paypal->setInvoice($_PAYPAL['invoice']);
311 $paypal->setReceiver_email($_PAYPAL['receiver_email']);
312 $paypal->setPayment_status($_PAYPAL['payment_status']);
313 $paypal->setPending_reason($_PAYPAL['pending_reason']);
314 $paypal->setReason_code($_PAYPAL['reason_code']);
315 $paypal->setPayment_date($_PAYPAL['payment_date']);
316 $paypal->setPayment_gross($_PAYPAL['payment_gross']);
317 $paypal->setPayment_fee($_PAYPAL['payment_fee']);
318 $paypal->setTxn_id($_PAYPAL['txn_id']);
319 $paypal->setTxn_type($_PAYPAL['txn_type']);
320 $paypal->setParent_txn_id('parent_txn_id');
321 $paypal->setFirst_name($_PAYPAL['first_name']);
322 $paypal->setLast_name($_PAYPAL['last_name']);
323 $paypal->setPayer_email($_PAYPAL['payer_email']);
324 $paypal->setPayer_status($_PAYPAL['payer_status']);
325 $paypal->setPayment_type($_PAYPAL['payment_type']);
326 $paypal->setNotify_version($_PAYPAL['notify_version']);
327 $paypal->setVerify_sign($_PAYPAL['verify_sign']);
328 $paypal->setMc_currency($_PAYPAL['mc_currency']);
329 $paypal->setPayer_business_name($_PAYPAL['payer_business_name']);
330 $paypal->setPayer_id($_PAYPAL['payer_id']);
331 $paypal->setMc_gross($_PAYPAL['mc_gross']);
332 $paypal->setMc_fee($_PAYPAL['mc_fee']);
333 $paypal->setSettle_amount($_PAYPAL['settle_amount']);
334 $paypal->setSettle_currency($_PAYPAL['settle_currency']);
335 $paypal->setExchange_rate($_PAYPAL['exchange_rate']);
336
337 // store variables in database
338 $paypal->store();
339
340
341 /* if ($confirmation)
342 if ($result)
343 eZPBLog::writeNotice( "Paypal IPN success! OrderID: ".$orderID." SessionID: ".$sessionID."\n" );
344 */
345
346
347 }
348 // IPN was not validated as genuine and is INVALID
349
350 elseif (!strcmp ($response, "INVALID"))
351 {
352
353 // Check your code for any post back validation problems
354 // Investigate the fact that this could be a spoofed IPN
355 // If updating your db, ensure this "txn_id" is not a duplicate
356
357 // send an email to the admin for troubleshooting
358 $paypal = new eZPaypalReturn();
359
360 $warningMail = "There is a problem with the following Paypal payment.\n";
361 $warningMail .= "Site: ".$SiteURL."\n";
362 $warningMail .= "\n";
363 $warningMail .= "Paypal response: ".strtoupper($response)."\n";
364 $warningMail .= "\n";
365 $warningMail .= "Payment status: ".strtoupper($_PAYPAL['payment_status'])."\n";
366 if ($paypal->isDuplicate($_PAYPAL['txn_id']))
367 $warningMail .= "Note: duplicate transaction ID (txn_id)!\n";
368 $warningMail .= "See https://www.paypal.com/ipn for documentation.\n";
369 $warningMail .= "sessionID returned: ".$sessionID."\n";
370 $warningMail .= "orderID returned: ".$orderID."\n";
371 $warningMail .= "\n";
372 $warningMail .= "Paypal data returned:\n";
373 $warningMail .= print_r($_PAYPAL, true)."\n";
374 if ( $orderID != "" && is_numeric($orderID) )
375 {
376 $warningMail .= "Order information (if available):\n";
377 $order = new eZOrder($orderID);
378 $warningMail .= print_r($order, true)."\n";
379 }
380
381 $mail = new eZMail();
382 $mail->setFrom( $ini->variable( "eZTradeMain", "OrderSenderEmail" ) );
383 $mail->setTo( $PaypalEmail );
384 $mail->setSubject( "Warning: INVALID payment attempt at ".$SiteURL );
385 $mail->setBody( $warningMail );
386 $mail->send();
387 // write same error data to error.log file
388 eZPBLog::writeWarning( "Paypal IPN failure!\n".$warningMail."\n" );
389
390 }
391
392
393
394 else
395 { // Just incase something serious should happen!
396 }}
397
398 if ($debugger) debugInfo();
399
400
401
402 #########################################################
403 # Inernal Functions : variableAudit & debugInfo #
404 #########################################################
405
406
407
408 // Function: variableAudit
409 // Easy LOCAL to IPN variable comparison
410 // Returns 1 for match or 0 for mismatch
411
412 function variableAudit($v,$c)
413 {
414 global $_PAYPAL;
415 if (!strcasecmp($_PAYPAL[$v],$c))
416 { return 1; } else { return 0; }
417 }
418
419
420
421 // Function: debugInfo
422 // Displays debug info
423 // Set $debugger to 1
424
425 function debugInfo()
426 {
427 global $_PAYPAL,
428 $postmode,
429 $socket,
430 $error,
431 $postipn,
432 $getrq,
433 $response;
434
435 $ipnc = strlen($postipn)-21;
436 $ipnv = count($_PAYPAL)+1;
437
438 @flush();
439 @header('Cache-control: private'."\r\n");
440 @header('Content-Type: text/plain'."\r\n");
441 @header('Content-Disposition: inline; filename=debug.txt'."\r\n");
442 @header('Content-transfer-encoding: ascii'."\r\n");
443 @header('Pragma: no-cache'."\r\n");
444 @header('Expires: 0'."\r\n\r\n");
445 echo '#########################################################'."\r\n";
446 echo '# Copyright � EliteWeaver UK All rights reserved. #'."\r\n";
447 echo '#########################################################'."\r\n";
448 echo '# END USER LICENCE AGREEMENT #'."\r\n";
449 echo '# Redistribution and use in source and/or binary forms #'."\r\n";
450 echo '# with or without modification, are permitted provided #'."\r\n";
451 echo '# that the above copyright notice is reproduced in the #'."\r\n";
452 echo '# script, documentation and/or any other materials that #'."\r\n";
453 echo '# may have been provided in the original distribution. #'."\r\n";
454 echo '#########################################################'."\r\n";
455 echo '# <-- PayPal IPN Variable Output & Status Debugger! --> #'."\r\n";
456 echo '#########################################################'."\r\n\r\n";
457 if (phpversion() >= '4.3.0' && $socket)
458 {
459 echo 'Socket Status: '."\r\n\r\n";
460 print_r (socket_get_status($socket));
461 echo "\r\n\r\n"; }
462 echo 'PayPal IPN: '."\r\n\r\n";
463 print_r($_PAYPAL);
464 echo "\r\n\r\n".'Validation String: '."\r\n\r\n".wordwrap($postipn, 64, "\r\n", 1);
465 echo "\r\n\r\n\r\n".'Validation Info: '."\r\n";
466 echo "\r\n\t".'PayPal IPN String Length Incoming => '.$ipnc."\r\n";
467 echo "\t".'PayPal IPN String Length Outgoing => '.strlen($postipn)."\r\n";
468 echo "\t".'PayPal IPN Variable Count Incoming => ';
469 print_r(count($_PAYPAL));
470 echo "\r\n\t".'PayPal IPN Variable Count Outgoing => '.$ipnv."\r\n";
471 if ($postmode == 1)
472 {
473 echo "\r\n\t".'IPN Validation Mode => Live -> PayPal, Inc.'; }
474 elseif ($postmode == 2)
475 {
476 echo "\r\n\t".'IPN Validation Mode => Test -> EliteWeaver.'; }
477 else
478 {
479 echo "\r\n\t".'IPN Validation Mode => Incorrect Mode Set!'; }
480 echo "\r\n\r\n\t\t".'IPN Validate Response => '.$response;
481 if (!$getrq && !$error)
482 {
483 echo "\r\n\t\t".'IPN Validate Method => POST (success)'."\r\n\r\n"; }
484 elseif ($getrq && !$error)
485 {
486 echo "\r\n\t\t".'IPN Validate Method => GET (success)'."\r\n\r\n"; }
487 elseif ($bmode)
488 {
489 echo "\r\n\t\t".'IPN Validate Method => NONE (stupid)'."\r\n\r\n"; }
490 elseif ($error)
491 {
492 echo "\r\n\t\t".'IPN Validate Method => BOTH (failed)'."\r\n\r\n"; }
493 else
494 {
495 echo "\r\n\t\t".'IPN Validate Method => BOTH (unknown)'."\r\n\r\n"; }
496 echo '#########################################################'."\r\n";
497 echo '# THIS SCRIPT IS FREEWARE AND IS NOT FOR RE-SALE! #'."\r\n";
498 echo '#########################################################'."\r\n\r\n";
499 @flush();
500
501 }
502
503
504 // Terminate the socket connection (if open) and exit
505
506 @fclose ($socket); exit;
507
508
509 #########################################################
510 # Copyright � EliteWeaver UK All rights reserved. #
511 #########################################################
512 # END USER LICENCE AGREEMENT #
513 # Redistribution and use in source and/or binary forms #
514 # with or without modification, are permitted provided #
515 # that the above copyright notice is reproduced in the #
516 # script, documentation and/or any other materials that #
517 # may have been provided in the original distribution. #
518 #########################################################
519 # THIS SCRIPT IS FREEWARE AND IS NOT FOR RE-SALE! #
520 #########################################################
521
522 ?>
$i
$i
Definition: admin/adlist.php:91
$session
$session
Definition: admin/articlelist.php:44
eZMail
Definition: ezmail.php:57
eZOrderConfirmation
Definition: ezorderconfirmation.php:68
eZOrder
Definition: ezorder.php:52
eZPBLog\writeWarning
static writeWarning( $warning)
Definition: ezpblog.php:177
eZPaypalReturn
Definition: ezpaypalreturn.php:10
eZSession
Definition: lib/ezsession/classes/ezsession.php:65
$ini
$ini
Definition: classes/admin/linklist.php:64
$result
$result
Definition: companysearch.php:94
$orderID
$orderID
Definition: confirmation.php:34
$mail
$mail
Definition: ezbulkmail/admin/maillist.php:61
$response
while(!feof($socket)) $response
Definition: paypalnotify.php:196
$SiteURL
$SiteURL
Definition: paypalnotify.php:39
variableAudit
variableAudit($v, $c)
Definition: paypalnotify.php:412
$socket
$socket
Definition: paypalnotify.php:140
$header
$header
Definition: paypalnotify.php:141
exit
exit
Definition: paypalnotify.php:506
$PaypalEmail
$PaypalEmail
Definition: paypalnotify.php:37
$debugger
$debugger
Definition: paypalnotify.php:57
$PaypalMode
$PaypalMode
Definition: paypalnotify.php:38
debugInfo
debugInfo()
Definition: paypalnotify.php:425
$bmode
$bmode
Definition: paypalnotify.php:130
$postipn
$postipn
Definition: paypalnotify.php:88
$error
foreach($_POST as $ipnkey=> $ipnval) $error
Definition: paypalnotify.php:103
$order
$order
Definition: productreport.php:182
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.