/** * Verify IPN by sending it back to PayPal for confirmation */ function ipn_postback($mode = 'IPN', $pdtTX = '') { $postdata = ''; $postback = ''; $postback_array = array(); // build postback string if ($mode == 'PDT') { if ($pdtTX == '') { return FALSE; } // TX value not supplied, therefore PDT is disabled on merchant's PayPal profile. ipn_debug_email('PDT PROCESSING INITIATED.' . "\n" . 'Preparing to verify transaction via PDT.' . "\n\n" . 'The TX token for verification is: ' . print_r($_GET, TRUE)); $postback .= "cmd=_notify-synch"; $postback .= "&tx=" . $_GET['tx']; $postback .= "&at=" . trim(MODULE_PAYMENT_PAYPAL_PDTTOKEN); $postback .= "&"; $postback_array['cmd'] = "_notify-sync"; $postback_array['tx'] = $_GET['tx']; $postback_array['at'] = substr(MODULE_PAYMENT_PAYPAL_PDTTOKEN, 0, 5) . '**********' . substr(MODULE_PAYMENT_PAYPAL_PDTTOKEN, -5); } elseif ($mode == 'IPN') { $postback .= "cmd=_notify-validate"; $postback .= "&"; $postback_array['cmd'] = "_notify-validate"; } foreach ($_POST as $key => $value) { $postdata .= $key . "=" . urlencode(stripslashes($value)) . "&"; $postback .= $key . "=" . urlencode(stripslashes($value)) . "&"; $postback_array[$key] = $value; } if (substr($postdata, -2) == '=&') { ipn_debug_email('IPN NOTICE :: No POST data to process -- Bad IPN data'); return $postdata; } $postback = rtrim($postback, '&'); $postdata = rtrim($postdata, '&'); $postdata_array = $_POST; ksort($postdata_array); if ($mode == 'IPN') { ipn_debug_email('IPN INFO - POST VARS received (sorted):' . "\n" . stripslashes(urldecode(print_r($postdata_array, true)))); if (sizeof($postdata_array) == 0) { die('Nothing to process. Please return to home page.'); } } // send received data back to PayPal for validation $scheme = 'http://'; // Parse url $web = parse_url($scheme . (defined('MODULE_PAYMENT_PAYPAL_HANDLER') ? MODULE_PAYMENT_PAYPAL_HANDLER : 'www.paypal.com/cgi-bin/webscr')); if (isset($_POST['test_ipn']) && $_POST['test_ipn'] == 1) { $web = parse_url($scheme . 'www.sandbox.paypal.com/cgi-bin/webscr'); } // Set the port number if ($web['scheme'] == "https") { $web['port'] = "443"; $ssl = "ssl://"; } else { $web['port'] = "80"; $ssl = ""; } $result = ''; if (function_exists('curl_init')) { $result = doPayPalIPNCurlPostback($web, $postback, $postback_array, $mode); } if ($mode == 'PDT') { $info = $result['info']; $result = $result['status']; } // DEBUG ONLY: ipn_debug_email('After CURL: $result='.$result); if (!in_array(trim($result), array('VERIFIED', 'SUCCESS', 'INVALID', 'FAIL'))) { ipn_debug_email('IPN NOTICE: Could not get usable response via CURL. Trying fsockopen() as fallback.' . ($result != '' ? ' [' . $result . ']' : '')); $result = doPayPalIPNFsockopenPostback($web, $postback, $postback_array, $ssl, $mode); if ($mode == 'PDT') { $info = $result['info']; $result = $result['status']; } } return $mode == 'PDT' ? array('status' => $result, 'info' => $info) : trim($result); }
} if (isset($_POST['test_ipn']) && $_POST['test_ipn'] == 1) { $web = parse_url($scheme . 'www.sandbox.paypal.com/cgi-bin/webscr'); } //Set the port number if ($web['scheme'] == "https") { $web['port'] = "443"; $web['protocol'] = "ssl://"; } else { $web['port'] = "80"; $web['protocol'] = ""; } $result = ''; $data = ''; if (function_exists('curl_init')) { $result = doPayPalIPNCurlPostback($web, $postback, $verboseMode, $headerMode); if (in_array($result, array('VERIFIED', 'SUCCESS', 'INVALID'))) { echo nl2br('IPN TESTING - Response Received via CURL -- <strong>COMMUNICATIONS OKAY</strong>' . "\n<!--" . $data . '-->'); $defaultMethod = 'CURL'; $altMethod = 'FSOCKOPEN'; } } else { echo nl2br('CURL not available. Will attempt to connect using fsockopen() instead.' . "\n"); } if (!in_array($result, array('VERIFIED', 'SUCCESS', 'INVALID')) || $testBoth === TRUE) { $result = doPayPalIPNFsockopenPostback($web, $postback); echo nl2br('IPN TESTING - Confirmation/Validation response with fsockopen(): <strong>' . $result . "</strong>\n<!--" . $info . '-->'); if ($defaultMethod == '' && $result != 'FAILED') { $defaultMethod = 'FSOCKOPEN'; $altMethod = 'CURL'; }