|
ZenCart_Documentation
1.5.0
http://www.collinsharper.com
|
00001 <?php 00020 if (!function_exists('curl_setopt_array')) { 00021 function curl_setopt_array(&$ch, $curl_options) 00022 { 00023 foreach ($curl_options as $option => $value) { 00024 if (!curl_setopt($ch, $option, $value)) { 00025 return false; 00026 } 00027 } 00028 return true; 00029 } 00030 } 00031 00032 $verboseMode = (isset($_GET['verbose']) ? TRUE : FALSE); 00033 $headerMode = (isset($_GET['headers']) ? TRUE : FALSE); 00034 $testBoth = (isset($_GET['both']) && $_GET['both'] == '0') ? FALSE : TRUE; 00035 $checkNoChex = (isset($_GET['nochex'])) ? TRUE : FALSE; 00036 $testSandbox = (isset($_GET['sandbox'])) ? TRUE : FALSE; 00037 00038 $_POST = array(); 00039 if (isset($GLOBALS)) unset($GLOBALS); 00040 if (isset($_GET)) unset($_GET); 00041 if (isset($_REQUEST)) unset($_REQUEST); 00042 $_POST['ipn_mode'] = 'communication_test'; 00043 if ($testSandbox) $_POST['test_ipn'] = 1; 00044 define('ENABLE_SSL','true'); 00045 define('MODULE_PAYMENT_PAYPAL_HANDLER', 'www.paypal.com/cgi-bin/webscr'); 00046 00047 00048 echo 'IPNCHECK.PHP - Version 1.3.9'; 00049 echo '<br /><br /><pre>'; 00050 $defaultMethod = $altMethod = ''; 00051 $info = ''; 00052 $postdata = ''; 00053 $postback = ''; 00054 $postback_array = array(); 00055 00056 //build post string 00057 foreach($_POST as $key=>$value) { 00058 $postdata .= $key . "=" . urlencode(stripslashes($value)) . "&"; 00059 $postback .= $key . "=" . urlencode(stripslashes($value)) . "&"; 00060 $postback_array[$key] = $value; 00061 } 00062 $postback .= "cmd=_notify-validate"; 00063 $postback_array['cmd'] = "_notify-validate"; 00064 if ($postdata == '=&') { 00065 die('IPN NOTICE :: No POST data to process -- Bad IPN data<br /><pre>' . print_r($_POST, true)); 00066 } 00067 $postdata_array = $_POST; 00068 ksort($postdata_array); 00069 00070 if (sizeof($postdata_array) == 0) die('Nothing to process. Please return to home page.'); 00071 00072 // send received data back to PayPal for validation 00073 $scheme = 'https://'; 00074 //Parse url 00075 $web = parse_url($scheme . MODULE_PAYMENT_PAYPAL_HANDLER); 00076 if ($checkNoChex == TRUE) $web = parse_url('https://www.nochex.com/nochex.dll/apc/apc'); 00077 if (isset($_POST['test_ipn']) && $_POST['test_ipn'] == 1) { 00078 $web = parse_url($scheme . 'www.sandbox.paypal.com/cgi-bin/webscr'); 00079 } 00080 //Set the port number 00081 if($web['scheme'] == "https") { 00082 $web['port']="443"; $web['protocol'] = "ssl://"; 00083 } else { 00084 $web['port']="80"; $web['protocol'] = ""; 00085 } 00086 00087 00088 $result = ''; 00089 $data = ''; 00090 if (function_exists('curl_init')) { 00091 $result = doPayPalIPNCurlPostback($web, $postback, $verboseMode, $headerMode); 00092 if (in_array($result, array('VERIFIED', 'SUCCESS', 'INVALID'))) { 00093 echo nl2br('IPN TESTING - Response Received via CURL -- <strong>COMMUNICATIONS OKAY</strong>' . "\n<!--" . $data . '-->'); 00094 $defaultMethod = 'CURL'; 00095 $altMethod = 'FSOCKOPEN'; 00096 } 00097 } else { 00098 echo nl2br('CURL not available. Will attempt to connect using fsockopen() instead.' . "\n"); 00099 } 00100 00101 if (!in_array($result, array('VERIFIED', 'SUCCESS', 'INVALID')) || $testBoth === TRUE) { 00102 $result = doPayPalIPNFsockopenPostback($web, $postback); 00103 echo nl2br('IPN TESTING - Confirmation/Validation response with fsockopen(): <strong>' .$result . "</strong>\n<!--" . $info . '-->'); 00104 if ($defaultMethod == '' && $result != 'FAILED') { 00105 $defaultMethod = 'FSOCKOPEN'; 00106 $altMethod = 'CURL'; 00107 } 00108 } 00109 if ($defaultMethod != '') { 00110 echo '<br><br>Default method likely to be used for communications is: <strong>' . $defaultMethod . '</strong>, with the fallback method being <strong>' . $altMethod . '</strong> if possible.'; 00111 } 00112 echo '<br><br>Script finished.'; 00113 00114 00115 /************************************/ 00116 00117 function doPayPalIPNFsockopenPostback($web, $postback) { 00118 global $info; 00119 $header = "POST " . $web['path'] . " HTTP/1.1\r\n"; 00120 $header .= "Host: " . $web['host'] . "\r\n"; 00121 $header .= "Content-type: application/x-www-form-urlencoded\r\n"; 00122 $header .= "Content-length: " . strlen($postback) . "\r\n"; 00123 $header .= "Connection: close\r\n\r\n"; 00124 $errnum = 0; 00125 $errstr = ''; 00126 $ssl = $web['protocol']; 00127 //Create paypal connection 00128 $fp=fsockopen($ssl . $web['host'], $web['port'], $errnum, $errstr, 30); 00129 00130 if(!$fp && $ssl == 'ssl://') { 00131 echo nl2br("\n" . 'IPN ERROR :: Could not establish fsockopen: ' . "\n" . 'Host Details = ' . $ssl . $web['host'] . ':' . $web['port'] . ' (' . $errnum . ') ' . $errstr . "\n Trying again with HTTPS over 443 ..."); 00132 $ssl = 'https://'; 00133 $web['port'] = '443'; 00134 $fp=fsockopen($ssl . $web['host'], $web['port'], $errnum, $errstr, 30); 00135 } 00136 if(!$fp && $ssl == 'https://') { 00137 echo nl2br("\n" . 'IPN ERROR :: Could not establish fsockopen: ' . "\n" . 'Host Details = ' . $ssl . $web['host'] . ':' . $web['port'] . ' (' . $errnum . ') ' . $errstr . "\n Trying again directly over 443 ..."); 00138 $ssl = ''; 00139 $web['port'] = '443'; 00140 $fp=fsockopen($ssl . $web['host'], $web['port'], $errnum, $errstr, 30); 00141 } 00142 if(!$fp) { 00143 echo nl2br("\n" . 'IPN ERROR :: Could not establish fsockopen: ' . "\n" . 'Host Details = ' . $ssl . $web['host'] . ':' . $web['port'] . ' (' . $errnum . ') ' . $errstr . "\n Trying again with HTTP over port 80 ..."); 00144 $ssl = 'http'; 00145 $web['port'] = '80'; 00146 $fp=fsockopen($ssl . $web['host'], $web['port'], $errnum, $errstr, 30); 00147 } 00148 if(!$fp) { 00149 echo nl2br("\n" . 'IPN ERROR :: Could not establish fsockopen: ' . "\n" . 'Host Details = ' . $ssl . $web['host'] . ':' . $web['port'] . ' (' . $errnum . ') ' . $errstr . "\n Trying again without any specified protocol, using port 80 ..."); 00150 $ssl = ''; 00151 $web['port'] = '80'; 00152 $fp=fsockopen($ssl . $web['host'], $web['port'], $errnum, $errstr, 30); 00153 } 00154 if(!$fp) { 00155 echo nl2br("\n" . 'IPN FATAL ERROR :: Could not establish fsockopen. ' . "\n" . 'Host Details = ' . $ssl . $web['host'] . ':' . $web['port'] . ' (' . $errnum . ') ' . $errstr . "\n"); 00156 die(); 00157 } 00158 00159 fputs($fp, $header . $postback . "\r\n\r\n"); 00160 $header_data = ''; 00161 $headerdone = false; 00162 //loop through the response from the server 00163 while(!feof($fp)) { 00164 $line = @fgets($fp, 1024); 00165 if (strcmp($line, "\r\n") == 0) { 00166 // this is a header row 00167 $headerdone = true; 00168 $header_data .= $line; 00169 } else if ($headerdone) { 00170 // header has been read. now read the contents 00171 $info[] = $line; 00172 } 00173 } 00174 //close fp - we are done with it 00175 fclose($fp); 00176 //break up results into a string 00177 $info = implode("", $info); 00178 $status = (strstr($info,'VERIFIED')) ? 'VERIFIED' : (strstr($info,'SUCCESS')) ? 'SUCCESS' : (strstr($info,'INVALID')) ? 'FSOCKOPEN() RESPONSE RECEIVED - Communications OKAY' : 'FAILED'; 00179 echo "\n\n" . '<!-- IPN INFO - Confirmation/Validation response ' . "\n-------------\n" . $header_data . $info . "\n--------------\n -->"; 00180 00181 return $status; 00182 } 00183 00184 00185 function doPayPalIPNCurlPostback($web, $vars, $verboseMode = FALSE, $headerMode = FALSE) { 00186 $status = 'Attempted connection on: ' .$web['scheme'] . '://' . $web['host'] . $web['path']; 00187 $ch = curl_init($web['scheme'] . '://' . $web['host'] . $web['path']); 00188 $curlOpts = array(CURLOPT_URL => $web['scheme'] . '://' . $web['host'] . $web['path'], 00189 CURLOPT_POST => TRUE, 00190 CURLOPT_POSTFIELDS => $vars, 00191 CURLOPT_TIMEOUT => 45, 00192 CURLOPT_CONNECTTIMEOUT => 30, 00193 CURLOPT_VERBOSE => ($verboseMode ? TRUE : FALSE), 00194 CURLOPT_HEADER => ($headerMode ? TRUE : FALSE), 00195 CURLOPT_FOLLOWLOCATION => FALSE, 00196 CURLOPT_RETURNTRANSFER => TRUE, 00197 CURLOPT_SSL_VERIFYPEER => FALSE, 00198 CURLOPT_SSL_VERIFYHOST => 2, 00199 CURLOPT_FORBID_REUSE => TRUE, 00200 CURLOPT_FRESH_CONNECT => TRUE, 00201 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 00202 CURLOPT_USERAGENT => 'Zen Cart(tm) - IPN Postback TEST', 00203 ); 00204 if ($web['port'] != '80') { 00205 $curlOpts[CURLOPT_PORT] = $web['port']; 00206 } 00207 /* if (CURL_PROXY_REQUIRED == 'True') { 00208 $proxy_tunnel_flag = (defined('CURL_PROXY_TUNNEL_FLAG') && strtoupper(CURL_PROXY_TUNNEL_FLAG) == 'FALSE') ? false : true; 00209 curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_tunnel_flag); 00210 curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 00211 curl_setopt ($ch, CURLOPT_PROXY, CURL_PROXY_SERVER_DETAILS); 00212 } 00213 */ 00214 curl_setopt_array($ch, $curlOpts); 00215 $response = curl_exec($ch); 00216 $commError = curl_error($ch); 00217 $commErrNo = curl_errno($ch); 00218 $errors = ($commErrNo != 0 ? "\n(" . $commErrNo . ') ' . $commError : ''); 00219 00220 if (($response == '' || $errors != '') && ($web['scheme'] != 'http')) { 00221 if ($verboseMode) echo nl2br("\n\n" . 'VERBOSE output:' . "\n-------------\n<pre>" . htmlspecialchars($response) . "</pre>\n--------------\n"); 00222 echo nl2br('CURL ERROR: ' . $status . $errors . "\n" . 'Trying direct HTTP on port 80 instead ...' . "\n"); 00223 $web['scheme'] = 'http'; 00224 $web['port'] = '80'; 00225 $status = 'Attempted alternate connection on: ' .$web['scheme'] . '://' . $web['host'] . $web['path'] . "\n<br />"; 00226 curl_setopt($ch, CURLOPT_URL, $web['scheme'] . '://' . $web['host'] . $web['path']); 00227 curl_setopt($ch, CURLOPT_PORT, $web['port']); 00228 $response = curl_exec($ch); 00229 $commError = curl_error($ch); 00230 $commErrNo = curl_errno($ch); 00231 } 00232 //$commInfo = @curl_getinfo($ch); 00233 curl_close($ch); 00234 //die("\n\n".'data:'.$response); 00235 if ($verboseMode) echo nl2br("\n\n" . 'VERBOSE output: ' . "\n-------------\n<pre>" . htmlspecialchars($response) . "</pre>\n--------------\n"); 00236 $errors = ($commErrNo != 0 ? "\n(" . $commErrNo . ') ' . $commError : ''); 00237 if ($errors != '') { 00238 echo nl2br('CURL ERROR: ' . $status . $errors . "\n" . 'ABORTING CURL METHOD ...' . "\n\n"); 00239 } 00240 00241 $status = (strstr($response,'VERIFIED')) ? 'VERIFIED' : (strstr($response,'SUCCESS')) ? 'SUCCESS' : (strstr($response,'INVALID')) ? 'CURL RESPONSE RECEIVED - Communications OKAY' : 'FAILED'; 00242 echo $status . '<br />'; 00243 00244 00245 return $response; 00246 } 00247