/**
  * sends the PDF invoice as a file attachment to the buyer
  * compiles the email using the configured options and an HTML
  * version of the order. Updates the payments table.
  */
 public static function send_PDF_invoice($ipn_id)
 {
     $options = self::get_paypal_options();
     $payment_details = self::get_payment_details(array('ipn_id' => $ipn_id));
     $path = self::get_invoice_path($ipn_id);
     if ($payment_details) {
         if (!file_exists($path)) {
             self::generatePDF();
         }
         if (file_exists($path)) {
             $to_addr = $payment_details->ipn["payer_email"];
             $name = $payment_details->ipn["first_name"] . " " . $payment_details->ipn["last_name"];
             if (trim($name) != "") {
                 $to_addr = trim($name) . ' <' . $to_addr . '>';
             }
             $from_addr = $options["company_name"] . ' <' . $options["paypal_email"] . '>';
             $from_name = $options["company_name"];
             $subject = sprintf('%s [%04d]', $options["invoice_subject"], $payment_details->ipn_id);
             $message = str_replace('{{PP_ITEMS}}', self::get_payment_html($payment_details), $options["invoice_message"]);
             $headers = array("bcc" => $from_addr);
             $log = true;
             if ($log) {
                 $uploads_dir = wp_upload_dir();
                 $logfile = $uploads_dir["basedir"] . "/sppp-invoices/mail-log.txt";
                 if ($fh = fopen($logfile, "wb")) {
                     $log = sprintf("======\n\nSubject: %s\n\nMessage:\n%s\n\nAttachment: %s\n\n=======", $subject, $message, $path);
                     fwrite($fh, $log);
                     fclose($fh);
                     global $wpdb;
                     $tablename = SimplePayPalPlugin::get_payments_tablename();
                     $query = "UPDATE {$tablename} SET `invoice_sent` = UNIX_TIMESTAMP() WHERE `ipn_id` = {$ipn_id}";
                     $wpdb->query($query);
                     return true;
                 }
             } else {
                 self::apply_mail_filters();
                 if (wp_mail($to_addr, $subject, $message, $headers, array($path))) {
                     global $wpdb;
                     $tablename = SimplePayPalPlugin::get_payments_tablename();
                     $query = "UPDATE {$tablename} SET `invoice_sent` = UNIX_TIMESTAMP() WHERE `ipn_id` = {$ipn_id}";
                     $wpdb->query($query);
                     return true;
                 }
             }
         }
     }
     return false;
 }
            $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);
    }
}
SimplePayPalPlugin::register();