$gift_coupon_flag = $_SESSION['gift_coupon_flag'];
 $product_download_flag = $_SESSION['product_download_flag'];
 // Send Giftcertificate Mails
 if ($gift_coupon_flag == 1) {
     sendgiftCertificateMail_Buyer($orderid, $_SESSION["sess_userid"], $usertype);
     sendgiftCertificateMail_Seller($orderid, $_SESSION["sess_userid"]);
     sendgiftCertificateMail_Admin($orderid, $_SESSION["sess_userid"]);
 }
 //Send Digital Certificate Mails
 if ($product_download_flag == 1) {
     sendDigitalCertificateMail_Buyer($orderid, $_SESSION["sess_userid"], $usertype);
     sendDigitalCertificateMail_Seller($orderid, $_SESSION["sess_userid"]);
     sendDigitalCertificateMail_Admin($orderid, $_SESSION["sess_userid"]);
 }
 if ($product_download_flag != 1 && $gift_coupon_flag != 1) {
     sendBuyerEmail($orderid, $_SESSION["sess_userid"], $usertype);
     sendSellerEmail($orderid, $_SESSION["sess_userid"]);
     sendAdminEmail($orderid, $_SESSION["sess_userid"]);
 }
 adjustStock($_SESSION["sess_userid"], "-");
 emptyCart($_SESSION["sess_userid"]);
 $_SESSION["sess_addresses_collected"] = false;
 $_SESSION["sess_stock_adjusted"] = false;
 $_SESSION["sess_addresses_collected"] = false;
 $_SESSION["sess_stock_adjusted"] = false;
 $_SESSION['couponCode_det'] = '';
 if (isset($_SESSION['guest_login']) && $_SESSION['guest_login'] != '') {
     session_unregister($_SESSION['guest_login']);
     session_unregister($_SESSION['sess_userid']);
     $_SESSION['guest_login'] = '';
     $_SESSION["sess_userid"] = '';
            $txtEmailDownload = $user_email_row['email'];
            $subjectDownload = "Digital Product Order placed at " . SITE_NAME;
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
            $headers .= "From: " . SITE_EMAIL . "<" . SITE_EMAIL . ">" . "\r\n";
            /*-------Email Template reading function call------------*/
            $mailcontent_digital_product = readEmailTemplate('digital_product');
            $product_download_details .= "\n\n*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*\r\n";
            $product_download_details .= "<font color='#FF0000'>Note: If you do not have pop-ups enabled on your browser, the product download many not work properly.So please enable pop-ups before downloading !!</font>";
            $mailcontent_digital_product = str_replace('[product_details]', $product_download_details, $mailcontent_digital_product);
            $mailcontent_digital_product = nl2br($mailcontent_digital_product);
            $mail_send_details = "<html><head><link href='" . SITE_URL . "/style/template1-admin.css' rel='stylesheet' type='text/css'></head><body><center><div align='left' id='layout'><div align='left' class='header'><div align='left' class='logo'><img src=" . $logofolder . "/" . $logoimage . " width='125' height='39'/></div></div><div>" . $mailcontent_digital_product . "</div></div><div align='left' class='clear_float'><img src=" . $imagefolder . "/clear.gif width=1 height=1/></div></div></div></center></body></html>";
            //$mailsent = mail($txtEmailDownload,$subjectDownload,$mail_send_details,$headers);
        }
        /*-----------------Digital Product Mail Sending Ends----------------*/
        // Send Giftcertificate Mails
        if ($gift_coupon_flag == 1) {
            sendgiftCertificateMail_Buyer($orderid, $CustomArray[0]);
        } else {
            sendBuyerEmail($orderid, $CustomArray[0]);
            sendSellerEmail($orderid, $CustomArray[0]);
            sendAdminEmail($orderid, $CustomArray[0]);
        }
        //Send Mail To Buyer
    }
    // end if paypal check
}
//end ipn check if
emptyCart($CustomArray[0]);
$_SESSION["sess_addresses_collected"] = false;
$_SESSION["sess_stock_adjusted"] = false;
            $sql_select_pay = "SELECT payment_completed_status  FROM  " . $tableprefix . "orders where order_id=" . $orderid . " ";
            $rs_select_pay = mysql_query($sql_select_pay) or die(mysql_error());
            $rowPay = mysql_fetch_array($rs_select_pay);
            $paymentComplete_Status = $rowPay['payment_completed_status'];
            if ($paymentComplete_Status == 'N') {
                if ($gift_flag == 1) {
                    sendgiftCertificateMail_Buyer($orderid, $userid);
                    sendgiftCertificateMail_Seller($orderid, $userid);
                    sendgiftCertificateMail_Admin($orderid, $userid);
                } else {
                    if ($digital_flag == 1) {
                        sendDigitalCertificateMail_Buyer($orderid, $userid, $usertype);
                        sendDigitalCertificateMail_Seller($orderid, $userid);
                        sendDigitalCertificateMail_Admin($orderid, $userid);
                    } else {
                        sendBuyerEmail($orderid, $userid, $usertype);
                        sendSellerEmail($orderid, $userid);
                        sendAdminEmail($orderid, $userid);
                    }
                }
                $sql_update_order_payment = "UPDATE " . $tableprefix . "orders SET payment_completed_status='Y' where order_id=" . $orderid . " ";
                $res_update = mysql_query($sql_update_order_payment) or die(mysql_error());
            }
            adjustStock($userid, "-");
            emptyCart($userid);
        }
    }
} else {
    //exit('DoDirectPayment failed: <br>' . print_r($httpParsedResponseAr, true));
    $errorArry = $httpParsedResponseAr;
    $errorMsg = "DoDirectPayment failed: <br>" . urldecode($httpParsedResponseAr['L_LONGMESSAGE0']);