function process_ipn_response($request = null) { if (!defined('PAYPAL_DEBUG_MODE')) { define("PAYPAL_DEBUG_MODE", false); } // this prevents some kind of error in the core $_SESSION = null; if (DEBUG_MODE) { SS_Log::add_writer(new SS_LogFileWriter(__DIR__ . "/log/paypal.transactions.txt"), SS_Log::WARN, '>'); } if (DEBUG_MODE) { SS_Log::log("IPN Started!", SS_Log::DEBUG); } // parse post variables, reformat the data to be sent back via socket $data = "cmd=_notify-validate"; foreach ($_POST as $key => $value) { $value = urlencode(stripslashes($value)); $data .= "&" . $key . "=" . $value; } // post back to PayPal system to validate $header = "POST /cgi-bin/webscr HTTP/1.1\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Host: www.paypal.com\r\n"; $header .= "Connection: close\r\n"; $header .= "Content-Length: " . strlen($data) . "\r\n\r\n"; $response = "NONE"; // send back the info $socket_handle = fsockopen("ssl://www.paypal.com", 443, $errno, $errstr, 30); if (DEBUG_MODE) { SS_Log::log("header_debug:\n" . print_r($header, true) . "\n\n", SS_Log::DEBUG); } if (DEBUG_MODE) { SS_Log::log("data_debug:\n" . print_r($data, true) . "\n\n", SS_Log::DEBUG); } if ($socket_handle) { fputs($socket_handle, $header . $data); while (!feof($socket_handle)) { $response = fgets($socket_handle, 1024); $response = trim($response); if (DEBUG_MODE) { SS_Log::log("response_debug:\n" . print_r($response, true) . "\n\n", SS_Log::DEBUG); } if (strcmp($response, "VERIFIED") == 0) { $response = "VERIFIED"; } else { if (strcmp($response, "INVALID") == 0) { $response = "INVALID"; } } } fclose($socket_handle); } if (DEBUG_MODE) { SS_Log::log("paypal response: " . $response, SS_Log::DEBUG); } if (DEBUG_MODE) { SS_Log::log(print_r($_POST, true), SS_Log::DEBUG); } if ($response == "INVALID") { // we only care about completed interactions exit(0); } // SUCCESS - Do something with the data $Payment = new PaypalPayment(); $Payment->PaypalStorePageID = $this->ID; $Payment->Date = SS_Datetime::now(); $Payment->TransactionID = isset($_POST['txn_id']) ? $_POST['txn_id'] : $_POST['ipn_track_id']; $Payment->GatewayResponse = implode("\n", $_POST); $Payment->Amount = isset($_POST['amount3']) ? $_POST['amount3'] : (isset($_POST['payment_gross']) ? $_POST['payment_gross'] : (isset($_POST['mc_gross']) ? $_POST['mc_gross'] : 0)); $Payment->ItemID = isset($_POST['item_number']) ? $_POST['item_number'] : null; $Payment->ItemName = isset($_POST['item_name']) ? $_POST['item_name'] : null; $Payment->Email = isset($_POST['payer_email']) ? $_POST['payer_email'] : null; $Payment->Status = isset($_POST['payment_status']) ? $_POST['payment_status'] : null; $Payment->Name = (isset($_POST['first_name']) ? $_POST['first_name'] : null) . ' ' . (isset($_POST['last_name']) ? $_POST['last_name'] : null); $Payment->Street = isset($_POST['address_street']) ? $_POST['address_street'] : null; $Payment->City = isset($_POST['address_city']) ? $_POST['address_city'] : null; $Payment->State = isset($_POST['address_state']) ? $_POST['address_state'] : null; $Payment->Country = isset($_POST['address_country']) ? $_POST['address_country'] : null; $Payment->Zip = isset($_POST['address_zip']) ? $_POST['address_zip'] : null; $Payment->PayerID = isset($_POST['payer_id']) ? $_POST['payer_id'] : null; $Payment->write(); $Payment->OnSuccessfulPayment(); return $Payment; }