/** * function to process Instant Payment Notifications from Paypal */ public static function processIPN() { $options = SimplePayPalPluginAdmin::get_paypal_options(); $ppHost = isset($_POST['test_ipn']) ? $options["paypal_sandbox_url"] : $options["paypal_url"]; $req = 'cmd=_notify-validate'; $ipn_data = array(); /* prepare echo */ foreach ($_POST as $key => $value) { $value = urlencode(stripslashes($value)); $req .= "&" . $key . "=" . $value; $ipn_data[$key] = urldecode($value); } /* Validate IPN with PayPal using curl */ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ppHost); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $req); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($req))); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $curl_result = @curl_exec($ch); $curl_err = curl_error($ch); $curl_info = curl_getinfo($ch); $ci = ""; foreach ($curl_info as $k => $v) { $ci .= $k . " : " . $v . "\n"; } /* are we verified? If so, let's process the IPN */ if (strpos($curl_result, "VERIFIED") !== false) { /* decrease stock levels of items */ $i = 1; while (isset($_POST["item_number" . $i])) { if (isset($_POST["quantity" . $i])) { $paypal = SimplePaypalPluginAdmin::get_paypal_meta($_POST["item_number" . $i]); if ($paypal["stock_no"] > 0) { $paypal["stock"] = $paypal["stock_no"] - (int) $_POST["quantity" . $i]; if ($paypal["stock"] < 0) { $paypal["stock"] = 0; } update_post_meta($_POST["item_number" . $i], 'sppp', $paypal); update_post_meta($_POST["item_number" . $i], 'sppp-stock', $paypal["stock"]); } } $i++; } /* store IPN in database */ global $wpdb; $txn_id = isset($ipn_data["txn_id"]) ? $ipn_data["txn_id"] : ''; $txn_type = isset($ipn_data["txn_type"]) ? $ipn_data["txn_type"] : ''; $mc_gross = isset($ipn_data["mc_gross"]) ? $ipn_data["mc_gross"] : ''; $tablename = self::get_payments_tablename(); $wpdb->insert($tablename, array("payment_date" => time(), "payment_ipn" => serialize($ipn_data), "txn_id" => $txn_id, "txn_type" => $txn_type, "mc_gross" => $mc_gross), array("%d", "%s", "%s", "%s", "%s")); } if (is_email($options["paypal_ipn_email"])) { wp_mail($options["paypal_ipn_email"], "IPN CURL report", "CURL result: " . $curl_result . "\n\nCURL error: " . $curl_err . "\n\nCURL info: " . $ci . "\n\nIPN:\n\n" . $req, "From: " . $options["paypal_email"] . "\r\nReply-To: " . $options["paypal_email"] . "\r\n"); } curl_close($ch); }