<?php
class NetpayController extends Controller {
    public function indexAction() {
        $ObjOrder          = new OrderModel($this->ObjView->languageIDs);
        $ObjPaymentgateway = new PaymentgatewayModel();
        $ObjPayment        = new PaymentModel();
        $ObjNetpay         = new NetpayModel();
        $ObjCurrency       = new CurrencyModel();
        $ObjUsers          = new UserModel();
        $ObjGiftcoupon     = new GiftcouponModel();
        $languageIDs       = $this->ObjView->languageIDs;
        extract($_POST);
        if ($FromController) //From CartController and GiftController
            {
            if ($OrderID) {
                $splitGM               = explode("GM", $OrderID);
                $splitOrderID          = explode("-", $splitGM[1]);
                $orderID               = $splitOrderID[0];
                $orderSubIncrementRcvd = $splitOrderID[1];
            }
            if ($orderID != '') {
                $getOrderDetails = $ObjOrder->getOrderDetails($orderID);
                //Hold the order
                $ObjOrder->updateOrderHold($orderID, 'Yes');
                //Order Number generation
                if ($getOrderDetails['orderNumber'] == 0) {
                    $orderNumber = $ObjOrder->setOrderNumber();
                    $ObjNetpay->updateNetpayOrderNumber($orderID, $orderNumber);
                }
                if ($ExtraInfo1 == 'cart' && $getOrderDetails['orderType'] == 'product') {
                    $orderSubIncrement = $this->ObjSession->getSession("ORDER_SUB_INCREMENT");
                } elseif ($ExtraInfo1 == 'gift' && $getOrderDetails['orderType'] == 'coupon') {
                    $orderSubIncrement = $this->ObjSession->getSession("ORDER_SUB_INCREMENT_GIFT");
                }
                $paymentSettingsGroupID                  = $getOrderDetails['paymentSettingsGroupID'];
                $settingsArray                           = array();
                $settingsArray['paymentSettingsGroupID'] = $paymentSettingsGroupID;
                $settingsArray['netpayPaymentType']      = $PaymentMethod;
                $settingsArray['netpayOrderID']          = $OrderID;
                $settingsArray['orderID']                = $orderID;
                //$ObjNetpay->updateNetPaySettings($settingsArray);
                $getPaymentSettingGroupDetails           = $ObjPaymentgateway->getPaymentSettingGroupDetails($paymentSettingsGroupID);
                //Updating Group Label & payment Type
                $ObjPayment->updateOrderStatus($orderID, '', '', $getPaymentSettingGroupDetails['paySetGroupLabel'], '', 'AUTH_CAPTURE');
            }
            echo '<html><body>
							  <FORM method="post" name="NetpayFinal" action="' . $postUrl . '">
							  <INPUT type="hidden" name="MerchantID" value="' . $MerchantID . '">
							  <INPUT type="hidden" name="PaymentMethod" value="' . $PaymentMethod . '">
							  <INPUT type="hidden" name="OrderID" value="' . $OrderID . '">
							  <INPUT type="hidden" name="Amount" value="' . $Amount . '">
							  <INPUT type="hidden" name="Currency" value="' . $Currency . '">
							  <INPUT type="hidden" name="ProdDesc" value="' . $ProdDesc . '">
							  <INPUT type="hidden" name="UserName" value="' . $UserName . '">
							  <INPUT type="hidden" name="UserEmail" value="' . $UserEmail . '">
							  <INPUT type="hidden" name="Signature" value="' . $Signature . '">
							  <INPUT type="hidden" name="TransactionType" value="MP">
							  <INPUT type="hidden" name="ExtraInfo1" value="' . $ExtraInfo1 . '">
							  </FORM>
							  <script language="javascript" type="text/javascript">
							 submitFn();
							function submitFn()
							{
							 
								document.forms["NetpayFinal"].submit(); 
							} 
						 </script>
						 </body></html>
							  ';
            exit;
        } else //From Netpay Website
            {
            if ($OrderID) {
                $splitGM               = explode("GM", $OrderID);
                $splitOrderID          = explode("-", $splitGM[1]);
                $orderID               = $splitOrderID[0];
                $orderSubIncrementRcvd = $splitOrderID[1];
            }
            if ($orderID != '') {
                $getOrderDetails = $ObjOrder->getOrderDetails($orderID);
                if ($ExtraInfo1 == 'cart' && $getOrderDetails['orderType'] == 'product') {
                    $paymentSettingsGroupID = $this->ObjSession->getSession("PAYMENTSETTINGSGROUPID");
                    $orderSubIncrement      = $this->ObjSession->getSession("ORDER_SUB_INCREMENT");
                    if ($paymentSettingsGroupID == '') {
                        $this->ObjSession->setSession("PAYMENTSETTINGSGROUPID", $getOrderDetails['paymentSettingsGroupID']);
                        $paymentSettingsGroupID = $getOrderDetails['paymentSettingsGroupID'];
                    }
                    if ($orderSubIncrement == '') {
                        $this->ObjSession->setSession("ORDER_SUB_INCREMENT", $orderSubIncrementRcvd);
                    }
                } elseif ($ExtraInfo1 == 'gift' && $getOrderDetails['orderType'] == 'coupon') {
                    $paymentSettingsGroupID = $this->ObjSession->getSession("PAYMENTSETTINGSGROUPIDGIFT");
                    $orderSubIncrement      = $this->ObjSession->getSession("ORDER_SUB_INCREMENT_GIFT");
                    if ($paymentSettingsGroupID == '') {
                        $this->ObjSession->setSession("PAYMENTSETTINGSGROUPIDGIFT", $getOrderDetails['paymentSettingsGroupID']);
                        $paymentSettingsGroupID = $getOrderDetails['paymentSettingsGroupID'];
                    }
                    if ($orderSubIncrement == '')
                        $this->ObjSession->setSession("ORDER_SUB_INCREMENT_GIFT", $orderSubIncrementRcvd);
                }
                $settingsArray                                 = array();
                $settingsArray['paymentSettingsGroupID']       = $paymentSettingsGroupID;
                $settingsArray['netpayPaymentType']            = $PaymentMethod;
                $settingsArray['netpayOrderID']                = $OrderID;
                $settingsArray['orderID']                      = $orderID;
                $getPaymentSettingGroupDetails                 = $ObjPaymentgateway->getPaymentSettingGroupDetails($paymentSettingsGroupID);
                $paymentGatewayArray['paymentGateway']         = $getPaymentSettingGroupDetails['paySetGroupLabel'];
                $paymentGatewayArray['paymentType']            = $PaymentMethod;
                $paymentGatewayArray['paymentSettingsGroupID'] = $paymentSettingsGroupID;
                //Payment status From Netpay: 1– Success, 0– Fail
                $orderStatus                                   = 'Process';
                $responseCustom                                = $OrderID . "~";
                $paymentResponse                               = $responseCustom . $Status . "~" . $ErrorDesc;
                $getOrderDetails                               = $ObjOrder->getOrderDetails($orderID);
                $payAmount                                     = $getOrderDetails['orderSubtotal'] + $getOrderDetails['orderOtherServiceCharge'];
                $getOrderDetails['orderTotal']                 = $payAmount;
                $paymentGatewayCharge                          = 0;
                $userPayAmount                                 = $getOrderDetails['orderTotalAmount'];
                $chkAmount                                     = number_format($userPayAmount, 2, ".", "");
                $netpayAmount                                  = number_format($Amount, 2, ".", "");
                if (($chkAmount != $netpayAmount) && $Status == '1') {
                    $siteName                       = $this->getSettingValue('site_settings', 'site_name');
                    $mail_settings                  = $this->getSettingValue('mail_settings');
                    $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
                    $ObjMail                        = new Mail($mail_settings);
                    $adminMail                      = $this->getSettingValue('site_settings', 'global_notification_email');
                    $subject                        = $siteName . ': Amount Mismatch in Netpay';
                    $body                           = '<p>Hello Admin,<p>
													 <p>Amount Mismatch in Netpay.<p>
													 <p>Order ID: ' . $getOrderDetails['orderNumber'] . '</p>
													 <p>Order Amount :' . $chkAmount . '</p>
													 <p>Netpay Amount :' . $netpayAmount . '</p>
													';
                    $ObjMail->IsHTML();
                    try {
                        $send = $ObjMail->sendMail(array(
                            $adminMail,
                            'System'
                        ), $adminMail, $subject, $body);
                        $ObjMail->Send();
                        $ObjMail->ClearAddresses();
                    }
                    catch (Exception $e) {
                        $mailError = $e->getMessage();
                    }
                    //$ErrorDesc='Mismatch in Order Amounts';
                    //$Status=0;
                    $_POST['amountMismatch'] = 'Yes';
                }
                $payment_gateway_charge_percent = $ObjPayment->getPaymentSettingByKey($paymentSettingsGroupID, 'payment_gateway_charge_percent');
                $payment_gateway_charge         = $ObjPayment->getPaymentSettingByKey($paymentSettingsGroupID, 'payment_gateway_charge');
                if ($payment_gateway_charge_percent != '') {
                    $paymentGatewayCharge = ($userPayAmount * $payment_gateway_charge_percent) / 100;
                }
                $paymentGatewayCharge = $paymentGatewayCharge + $payment_gateway_charge;
                if ($Status == '1') {
                    $chkTransactionDuplication   = $ObjPayment->getPaymentTransactionDetails($orderID);
                    $_POST['createdTransaction'] = 'No';
                    if (empty($chkTransactionDuplication)) {
                        $insertPaymentTransaction    = $ObjPayment->createTransaction($paymentGatewayArray, $getOrderDetails, $PaymentID, $paymentResponse, '', "AUTH_CAPTURE", $paymentGatewayCharge, $getPaymentSettingGroupDetails['paySetGroupLabel']);
                        //Variable For Checking Inventory Updation
                        $_POST['createdTransaction'] = 'Yes';
                    }
                    $successPaymentStatus = "Received";
                    //Payment Status to Received
                    $ObjPayment->updateOrderStatus($orderID, $orderStatus, $PaymentID, $getPaymentSettingGroupDetails['paySetGroupLabel'], $successPaymentStatus);
                } elseif ($Status == '0') {
                    //Unhold the order
                    $ObjOrder->updateOrderHold($orderID, 'No');
                    //Payment Status to Error
                    $ObjPayment->updateOrderStatus($orderID, $orderStatus, $PaymentID, $getPaymentSettingGroupDetails['paySetGroupLabel'], 'Error');
                    $insertPaymentTransaction = $ObjPayment->createTransactionError($paymentGatewayArray, $getOrderDetails, $PaymentID, $paymentResponse, '', "AUTH_CAPTURE", $paymentGatewayCharge, $getPaymentSettingGroupDetails['paySetGroupLabel']);
                    if ($ExtraInfo1 == 'cart' && $getOrderDetails['orderType'] == 'product')
                        $this->ObjSession->setSession("NETPAYERROR", $ErrorDesc);
                    elseif ($ExtraInfo1 == 'gift' && $getOrderDetails['orderType'] == 'coupon')
                        $this->ObjSession->setSession("NETPAYERRORGIFT", $ErrorDesc);
                }
                if ($ExtraInfo1 == 'cart' && $getOrderDetails['orderType'] == 'product') {
                    $this->ObjSession->unsetSession("CART_REDIRECT");
                    $this->ObjSession->setSession("ORDERID", $orderID);
                    //get order array
                    $getOrderDetails = $ObjOrder->getOrderDetails($orderID);
                    $userID          = $this->ObjSession->getSession("REGISTERED_USER_ID");
                    if ($userID == '') {
                        $userID = $getOrderDetails['userID'];
                        $this->ObjSession->setSession("REGISTERED_USER_ID", $userID);
                    }
                    $OrderDetails = $ObjOrder->getOrderDetails($orderID);
                    $errorMsg     = $this->ObjSession->getSession("NETPAYERROR");
                    if ($errorMsg == '') {
                        $this->ObjSession->unsetSession("ORDER_SUB_INCREMENT");
                        $this->ObjSession->unsetSession("CART_REDIRECT", 'Yes');
                        $this->ObjSession->setSession("CART_REDIRECT", 'Yes');
                        $paymentSettingsGroupID          = $this->ObjSession->getSession("PAYMENTSETTINGSGROUPID");
                        $_POST['paymentSettingsGroupID'] = $paymentSettingsGroupID;
                        $this->ObjSession->unsetSession("PAYMENTSETTINGSGROUPID");
                        $this->productPaymentSuccessAction($orderID, $_POST);
                        $this->setRedirect('/payment/index/id/' . $orderID . '/');
                        exit;
                    } elseif ($errorMsg != '') {
                        $this->setRedirect('/cart/');
                        exit;
                    }
                    exit;
                } elseif ($ExtraInfo1 == 'gift' && $getOrderDetails['orderType'] == 'coupon') {
                    //get order array
                    $getOrderDetails = $ObjOrder->getOrderDetails($orderID);
                    $userID          = $this->ObjSession->getSession("REGISTERED_USER_ID");
                    if ($userID == '') {
                        $userID = $getOrderDetails['userID'];
                        $this->ObjSession->setSession("REGISTERED_USER_ID", $userID);
                    }
                    $couponID = $this->ObjSession->getSession('gift_coupon_id');
                    if ($couponID == '') {
                        $getGiftCouponDetails = $ObjGiftcoupon->getGiftCouponDetails($orderID);
                        $couponID             = $getGiftCouponDetails['couponID'];
                        $this->ObjSession->setSession('gift_coupon_id', $couponID);
                    }
                    $OrderDetails = $ObjOrder->getOrderDetails($orderID);
                    $errorMsg     = $this->ObjSession->getSession("NETPAYERRORGIFT");
                    if ($errorMsg == '') {
                        $this->ObjSession->unsetSession("ORDER_SUB_INCREMENT_GIFT");
                        $paymentSettingsGroupID          = $this->ObjSession->getSession("PAYMENTSETTINGSGROUPIDGIFT");
                        $_POST['paymentSettingsGroupID'] = $paymentSettingsGroupID;
                        $this->ObjSession->unsetSession("PAYMENTSETTINGSGROUPIDGIFT");
                        $this->giftPaymentSuccessAction($orderID, $couponID, $_POST);
                        $this->setRedirect('/giftcoupon/thankyou/');
                        exit;
                    } elseif ($errorMsg != '') {
                        $this->setRedirect('/giftcoupon/');
                        exit;
                    }
                    exit;
                }
            }
        }
        if ($_REQUEST['OrderID'] != '') //Cancel Payment handling
            {
            $OrderID = $_REQUEST['OrderID'];
            if ($OrderID) {
                $splitGM               = explode("GM", $OrderID);
                $splitOrderID          = explode("-", $splitGM[1]);
                $orderID               = $splitOrderID[0];
                $orderSubIncrementRcvd = $splitOrderID[1];
                //Unhold the order
                $ObjOrder->updateOrderHold($orderID, 'No');
                $getOrderDetails = $ObjOrder->getOrderDetails($orderID);
                if ($getOrderDetails['orderType'] == 'product') {
                    $this->setRedirect('/cart/');
                    exit;
                } elseif ($getOrderDetails['orderType'] == 'coupon') {
                    $this->setRedirect('/giftcoupon/');
                    exit;
                }
            }
            $this->setRedirect('/index/');
            exit;
        }
        $this->setRedirect('/index/');
        exit;
    }
    //PAYMENT SUCCESS DEAL
    //Make sure that paymentSuccessAction & productPaymentSuccessAction are same.	
    public function productPaymentSuccessAction($orderID, $_POST) {
        $ObjProductModel  = new ProductModel();
        $ObjOrder         = new OrderModel($this->ObjView->languageIDs);
        $ObjPayment       = new PaymentModel();
        $ObjUsers         = new UserModel();
        $ObjCountry       = new CountryModel();
        $ObjState         = new StateModel();
        $ObjCurrency      = new CurrencyModel();
        $ObjEmailtemplate = new EmailtemplateModel();
        $ObjCart          = new CartModel();
        if (__AFFILIATE_MODULE)
            $ObjAffiliate = new AffiliateModel();
        $ObjBusiness                       = new BusinessModel();
        $ObjCashOnDelivery                 = new CashOnDeliveryRedemptionModel();
        $mail_settings                     = $this->getSettingValue('mail_settings');
        $mail_settings['smtp_password']    = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
        $ObjMail                           = new Mail($mail_settings);
        $languageIDs                       = $this->ObjView->languageIDs;
        $ObjPaymentgateway                 = new PaymentgatewayModel();
        //get order array
        $getOrderDetails                   = $ObjOrder->getOrderDetails($orderID, 'stateDetails');
        $orderItems                        = $ObjOrder->getOrderItems($orderID);
        $resProductTypeDetails             = $ObjOrder->getProductTypeDetails($orderItems[0]['productID']);
        //Affiliate
        $affiliateUserID                   = $this->ObjCookie->getCookie("affiliateUserID");
        //Admin LanguageID
        $languageIDsCustomAdmin            = $languageIDs;
        $languageIDsCustomAdmin['Current'] = $languageIDs['Default'];
        if ($_POST['paymentSettingsGroupID'] > 0) {
            $getPaymentSettingGroupDetails = $ObjPaymentgateway->getPaymentSettingGroupDetails($_POST['paymentSettingsGroupID']);
        }
        if ($this->ObjSession->getSession("ORDERID") == '') {
            $this->ObjSession->setSession("ORDERID", $getOrderDetails['orderID']);
        }
        $userID = $this->ObjSession->getSession("REGISTERED_USER_ID");
        if ($userID == '') {
            $userID = $getOrderDetails['userID'];
            $this->ObjSession->setSession("REGISTERED_USER_ID", $userID);
        }
        $userCreditAmount        = $ObjUsers->getAvailableBalance($userID);
        $userCreditAmount        = ($userCreditAmount <= 0) ? 0 : $userCreditAmount;
        $orderNumber             = $ObjOrder->completeOrder($orderID, $orderItems[0]['productID']);
        $creditAmount        = ($_POST['paymentSettingsGroupID'] <= 0) ? 0 : $userCreditAmount;
        $totalDiscountAmount = $getOrderDetails['orderPromoDiscountAmount'] + $creditAmount;
        $payAmount           = $getOrderDetails['orderSubtotal'] + $getOrderDetails['orderOtherServiceCharge'];
        $payAmount           = ($totalDiscountAmount > $payAmount) ? ($totalDiscountAmount - $payAmount) : ($payAmount - $totalDiscountAmount);
        $transactionAmt      = ($_POST['paymentSettingsGroupID'] <= 0) ? ($payAmount + $getOrderDetails['orderTaxAmount'] + $getOrderDetails['shippingAmount']) : $userCreditAmount;
        if ($transactionAmt > 0 && $getOrderDetails['orderQty'] > 0) {
            $transactionDesc     = 'Amount debited for product purchase. Order ID : ' . $orderNumber;
            $saveUserTransaction = $ObjOrder->saveUserTransaction($userID, $transactionAmt, $transactionDesc);
        }
        if ($_POST['paymentSettingsGroupID'] <= 0) {
            //insert into payment transaction
            $POST['paymentGateway']        = 'Account Credit';
            $POST['paymentType']           = 'Credit';
            $getOrderDetails['orderTotal'] = $payAmount;
            $insertPaymentTransaction      = $ObjPayment->createTransaction($POST, $getOrderDetails, '', 'Success');
            //update order status for the case of product
            $paymentAuthValue              = ($resProductTypeDetails['paymentCollection'] == 'Product_Completed') ? 'AUTH_ONLY' : 'AUTH_CAPTURE';
            if ($resProductTypeDetails['paymentCollection'] == 'Product_Completed') {
                $paymentAuthValue = 'AUTH_ONLY';
                $orderStatus      = 'Process';
            } else {
                $paymentAuthValue = 'AUTH_CAPTURE';
                $orderStatus      = ($resProductTypeDetails['certficateGenerate'] == 'order_complete') ? 'Completed' : 'Process';
            }
            $updateOrderStatus = $ObjPayment->updateOrderStatus($orderID, $orderStatus, '', 'Account Credit', 'Received', $paymentAuthValue);
        } else {
            $orderStatus = $getOrderDetails['orderStatus'];
            if ($orderStatus == 'Error') {
                if ($resProductTypeDetails['paymentCollection'] == 'Product_Completed') {
                    $orderStatus = 'Process';
                } else {
                    $orderStatus = ($resProductTypeDetails['certficateGenerate'] == 'order_complete') ? 'Completed' : 'Process';
                }
                $updateOrderStatus = $ObjPayment->updateOrderStatus($orderID, $orderStatus);
            }
        }
        if (__AFFILIATE_MODULE)
            $affUserStat = $ObjAffiliate->getStatusOfAffiliate($affiliateUserID);
        //Save affiliate referral purchases	
        if ($affiliateUserID && $affUserStat == 1 && __AFFILIATE_ENABLE) {
            $saveAffReferrals = $ObjAffiliate->saveAffReferralPurchase($affiliateUserID, $orderID);
        }
        $savaProductTippedDetails = $ObjOrder->savaProductTippedDetails($orderItems[0]['productID']);
        if ($_POST['createdTransaction'] == 'Yes') {
            $updateInventory = $ObjCart->updateProductInventory($orderID);
        }
        // sent mail to user
        //email body
        $emailbody        = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'product-order-confirmation-user');
        $from             = array(
            $emailbody['emailFromEmail'],
            $emailbody['emailFromName']
        );
        $subject          = $emailbody['emailSubject'];
        $siteName         = $this->getSettingValue('site_settings', 'site_name');
        $siteUrl          = $this->getSettingValue('site_settings', 'global_http_url');
        $companyLogo      = $this->getSettingValue('company_information', 'company_logo');
        $codeLength       = $this->getSettingValue('product_cart_settings', 'product_certificate_length');
        $codeGenerateType = $this->getSettingValue('product_cart_settings', 'code_spporting_letters');
        $this->ObjView->assign('companyLogo', $companyLogo);
        $getOrderDetails      = $ObjOrder->getOrderDetails($orderID, 'stateDetails');
        $resUserDetails       = $ObjUsers->getUserDetails($userID);
        $certificateInfo      = "The product certificate(s) will be sent to your email, as soon as the product is closed.";
        //Check If Product Order	
        $productOrderFlag     = $ObjOrder->checkProductOrderType($orderID);
        //Shipping details		
        $shippingOrderDetails = $ObjOrder->getOrderShippingDetails($orderID, 'stateDetails');
        $shippingInfo         = '';
        if ($productOrderFlag) {
            if ($shippingOrderDetails) {
                $shippingAdressFormat = '';
                $shippingAdressFormat .= $ObjCountry->getFormattedAddress($shippingOrderDetails['shippingAdress'], $this->ObjView->languageIDs);
            }
        }
        $billingAdressFormat = '';
        $billingAdressFormat .= $ObjCountry->getFormattedAddress($getOrderDetails['billingAddress'], $this->ObjView->languageIDs);
        $emailbody            = $emailbody['emailHtml'];
        $orderInformationText = $this->getTemplateDetails('ORDER INFORMATION', $emailbody);
        //get productOptionName using Admin default languageIDs
        $getOrderedItems      = $ObjOrder->getOrderedItems($this->ObjView->languageIDs, $orderID, '', '', $languageIDsCustomAdmin);
        if (!empty($getOrderedItems)) {
            foreach ($getOrderedItems as $ordItems) {
                $orderInformationAdmin .= '<tr><td bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">						
        ' . stripslashes($ordItems['productOptionName']);
                if ($ordItems['orderGiveAsGift'] == "Yes") {
                    $orderInformationAdmin .= ' <strong>' . $this->getLanguageValue("lblGiftOrderinfo") . '</strong>';
                }
                $orderInformationAdmin .= '</td>
        <td bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($ordItems['orderItemPrice']) . '</td>
        <td bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ordItems['orderItemQuantity'] . '</td>
        <td align="right" nowrap="nowrap" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($ordItems['orderItemSubtotal']) . '</td>
        </tr>';
            }
        }
        //get productOptionName using user languageIDs			
        $getOrderedItems = $ObjOrder->getOrderedItems($this->ObjView->languageIDs, $orderID);
        if (!empty($getOrderedItems)) {
            foreach ($getOrderedItems as $ordItems) {
                $orderInformationUser .= '<tr><td bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">						
        ' . stripslashes($ordItems['productOptionName']);
                if ($ordItems['orderGiveAsGift'] == "Yes") {
                    $orderInformationUser .= ' <strong>(Gift)</strong>';
                }
                $orderInformationUser .= '</td>
        <td bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($ordItems['orderItemPrice']) . '</td>
        <td bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ordItems['orderItemQuantity'] . '</td>
        <td align="right" nowrap="nowrap" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($ordItems['orderItemSubtotal']) . '</td>
        </tr>';
            }
        }
        $orderInformation .= '<tr>
        <td colspan="3" align="right" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">Subtotal Amount: </td>
        <td align="right" nowrap="nowrap" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($getOrderDetails['orderSubtotal']) . '</td>
        </tr> ';
        if ($getOrderDetails['shippingAmount'] > 0) {
            $orderInformation .= '<tr>
        <td colspan="3" align="right" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">Shipping Amount: </td>
        <td align="right" nowrap="nowrap" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($getOrderDetails['shippingAmount']) . '</td>
        </tr>';
        }
        if ($getOrderDetails['orderOtherServiceCharge'] > 0) {
            $orderInformation .= '<tr>
        <td colspan="3" align="right" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">Product Service Fee: </td>
        <td align="right" nowrap="nowrap" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($getOrderDetails['orderOtherServiceCharge']) . '</td>
        </tr>';
        }
        if ($getOrderDetails['orderTaxAmount'] > 0) {
            $orderInformation .= '<tr>
        <td colspan="3" align="right" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">Tax Amount: </td>
        <td align="right" nowrap="nowrap" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($getOrderDetails['orderTaxAmount']) . '</td>
        </tr>';
        }
        if ($getOrderDetails['orderPromoCode'] != '') {
            $orderInformation .= '<tr>
        <td colspan="3" align="right" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">Promo Discount:</td>
        <td align="right" nowrap="nowrap" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($getOrderDetails['orderPromoDiscountAmount']) . ' (' . $getOrderDetails['orderPromoCode'] . ')</td>
        </tr>';
        }
        if ($getOrderDetails['orderUserCreditAmount'] > 0) {
            $orderInformation .= '<tr>
        <td colspan="3" align="right" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">User Account Credit:</td>
        <td align="right" nowrap="nowrap" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;">' . $ObjCurrency->getFormattedCurrency($getOrderDetails['orderUserCreditAmount']) . '</td>
        </tr>';
        }
        $orderInformation .= '<tr><td colspan="3" align="right" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;"><b>Total Amount : </b></td>
        <td align="right" nowrap="nowrap" bgcolor="#FFFFFF" style=" font-family:Arial; font-size:15px;"><b>' . $ObjCurrency->getFormattedCurrency($getOrderDetails['orderTotalAmount']) . '</b></td>
        </tr>
        </table>';
        $emailbody            = $emailbody['emailHtml'];
        $orderInformationText = $this->getTemplateDetails('ORDER INFORMATION', $emailbody);
        //get productOptionName using Admin default languageIDs
        $orderDetailsAdmin    = '';
        $getOrderedItems      = $ObjOrder->getOrderedItems($this->ObjView->languageIDs, $orderID, '', '', $languageIDsCustomAdmin);
        if (!empty($getOrderedItems)) {
            foreach ($getOrderedItems as $ordItems) {
                $orderDetailsAdmin = $orderInformationText;
                $optionSKU         = '';
                if (isset($ordItems['optionSKU'])) {
                    $optionSKU = ' [SKU: ' . $ordItems['optionSKU'] . ']';
                }
                $orderDetailsAdmin = str_replace('{$productOptionName}', stripslashes($ordItems['productOptionName']) . $optionSKU, $orderDetailsAdmin);
                if ($ordItems['orderGiveAsGift'] != "Yes") {
                    $otherTemplate     = $this->getTemplateDetails('GIFT', $orderDetailsAdmin);
                    $orderDetailsAdmin = str_replace($otherTemplate, '', $orderDetailsAdmin);
                }
                $orderDetailsAdmin = str_replace('{$orderItemPrice}', $ObjCurrency->getFormattedCurrency($ordItems['orderItemPrice']), $orderDetailsAdmin);
                $orderDetailsAdmin = str_replace('{$orderItemQuantity}', $ordItems['orderItemQuantity'], $orderDetailsAdmin);
                $orderDetailsAdmin = str_replace('{$orderItemSubtotal}', $ObjCurrency->getFormattedCurrency($ordItems['orderItemSubtotal']), $orderDetailsAdmin);
                $orderInformationAdminCopy .= $orderDetailsAdmin;
            }
        }
        $orderDetailsUser = '';
        //get productOptionName using user languageIDs
        $getOrderedItems  = $ObjOrder->getOrderedItems($this->ObjView->languageIDs, $orderID);
        if (!empty($getOrderedItems)) {
            foreach ($getOrderedItems as $ordItems) {
                $orderDetailsUser = $orderInformationText;
                $orderDetailsUser = str_replace('{$productOptionName}', stripslashes($ordItems['productOptionName']), $orderDetailsUser);
                if ($ordItems['orderGiveAsGift'] != "Yes") {
                    $otherTemplate    = $this->getTemplateDetails('GIFT', $orderDetailsUser);
                    $orderDetailsUser = str_replace($otherTemplate, '', $orderDetailsUser);
                }
                $orderDetailsUser = str_replace('{$orderItemPrice}', $ObjCurrency->getFormattedCurrency($ordItems['orderItemPrice']), $orderDetailsUser);
                $orderDetailsUser = str_replace('{$orderItemQuantity}', $ordItems['orderItemQuantity'], $orderDetailsUser);
                $orderDetailsUser = str_replace('{$orderItemSubtotal}', $ObjCurrency->getFormattedCurrency($ordItems['orderItemSubtotal']), $orderDetailsUser);
                $orderInformationUserCopy .= $orderDetailsUser;
            }
        }
        $commonTemplate    = $this->getTemplateDetails('COMMON INFORMATION', $emailbody);
        $commonInformation = $commonTemplate;
        $commonTemplate    = str_replace('{$orderSubtotal}', $ObjCurrency->getFormattedCurrency($getOrderDetails['orderSubtotal']), $commonTemplate);
        $otherTemplate     = $this->getTemplateDetails('SHIPPING AMOUNT', $commonTemplate);
        if ($getOrderDetails['shippingAmount'] > 0) {
            $commonTemplate = str_replace('{$shippingAmount}', $ObjCurrency->getFormattedCurrency($getOrderDetails['shippingAmount']), $commonTemplate);
        } else {
            $commonTemplate = str_replace($otherTemplate, '', $commonTemplate);
        }
        $otherTemplate = $this->getTemplateDetails('SERVICE CHARGE', $commonTemplate);
        if ($getOrderDetails['orderOtherServiceCharge'] > 0) {
            $commonTemplate = str_replace('{$orderOtherServiceCharge}', $ObjCurrency->getFormattedCurrency($getOrderDetails['orderOtherServiceCharge']), $commonTemplate);
        } else {
            $commonTemplate = str_replace($otherTemplate, '', $commonTemplate);
        }
        $otherTemplate = $this->getTemplateDetails('TAX AMOUNT', $commonTemplate);
        if ($getOrderDetails['orderTaxAmount'] > 0) {
            $commonTemplate = str_replace('{$orderTaxAmount}', $ObjCurrency->getFormattedCurrency($getOrderDetails['orderTaxAmount']), $commonTemplate);
        } else {
            $commonTemplate = str_replace($otherTemplate, '', $commonTemplate);
        }
        $otherTemplate = $this->getTemplateDetails('PROMO CODE', $commonTemplate);
        if ($getOrderDetails['orderPromoCode'] != '') {
            $commonTemplate = str_replace('{$orderPromoDiscountAmount}', '-' . $ObjCurrency->getFormattedCurrency($getOrderDetails['orderPromoDiscountAmount']) . ' (' . $getOrderDetails['orderPromoCode'] . ')', $commonTemplate);
        } else {
            $commonTemplate = str_replace($otherTemplate, '', $commonTemplate);
        }
        $otherTemplate = $this->getTemplateDetails('USER CREDIT AMOUNT', $commonTemplate);
        if ($getOrderDetails['orderUserCreditAmount'] > 0) {
            $commonTemplate = str_replace('{$orderUserCreditAmount}', '-' . $ObjCurrency->getFormattedCurrency($getOrderDetails['orderUserCreditAmount']), $commonTemplate);
        } else {
            $commonTemplate = str_replace($otherTemplate, '', $commonTemplate);
        }
        $commonTemplate = str_replace('{$orderTotalAmount}', $ObjCurrency->getFormattedCurrency($getOrderDetails['orderTotalAmount']), $commonTemplate);
        //shipping information label
        if (count($shippingOrderDetails) <= 0) {
            $shippingLabelTemplate      = $this->getTemplateDetails('SHIPPING INFORMATION', $emailbody);
            $shippingLabelTemplateValue = str_replace($shippingLabelTemplate, '', $shippingLabelTemplate);
        }
        $amountMismatchInfoForUser         = '';
        $amountMismatchInfoForAdmin        = '';
        $amountMismatchInfoForAdminSubject = '';
        if ($_POST['amountMismatch'] == 'Yes') {
            $amountMismatchInfoForUser         = '<br><p>Amount Mismatch in the order. Kindly contact admin: ' . $this->getSettingValue('site_settings', 'contact_email') . '</p><br>';
            $amountMismatchInfoForAdmin        = '<br><p>Amount Mismatch in the order. Kindly contact the user.</p><br>';
            $amountMismatchInfoForAdminSubject = 'Amount Mismatch in the order. ';
        }
        //email body
        $subject  = $this->getSettingValue('site_settings', 'site_name') . ":-" . stripslashes($subject);
        $userName = stripslashes($resUserDetails['userFirstName']) . " " . stripslashes($resUserDetails['userLastName']);
        $bodytag  = str_replace('{$billingFirstName}', '<br>' . stripslashes($getOrderDetails['billingFirstName']), $emailbody);
        //shipping details
        $bodytag  = str_replace($shippingLabelTemplate, $shippingLabelTemplateValue, $bodytag);
        $bodytag  = str_replace('{$shippingInfo}', ' <br /> ' . $shippingAdressFormat, $bodytag);
        //Restrict Certificate Info for product Products
        if ($productOrderFlag)
            $bodytag = str_replace('{$certificateInfo}', '', $bodytag);
        else
            $bodytag = str_replace('{$certificateInfo}', $certificateInfo, $bodytag);
        $bodytag        = str_replace('{$billingAddress}', ' <br /> ' . $billingAdressFormat, $bodytag);
        $bodytag        = ($getOrderDetails['billingPhone'] != '') ? str_replace('{$billingPhone}', stripslashes($getOrderDetails['billingPhone']), $bodytag) : preg_replace('#(' . preg_quote('<!--PHONE NUMBER BEGIN-->') . ')(.*)(' . preg_quote('<!--PHONE NUMBER END-->') . ')#si', '', $bodytag);
        $bodytag        = ($getOrderDetails['billingEmail'] != '') ? str_replace('{$billingEmail}', stripslashes($getOrderDetails['billingEmail']), $bodytag) : preg_replace('#(' . preg_quote('<!--EMAILID BEGIN-->') . ')(.*)(' . preg_quote('<!--EMAILID END-->') . ')#si', '', $bodytag);
        //order information
        $bodytag        = str_replace($orderInformationText, $orderInformationUserCopy, $bodytag);
        $commonTemplate = $commonTemplate . $amountMismatchInfoForUser;
        $bodytag        = str_replace($commonInformation, $commonTemplate, $bodytag);
        $bodytag        = str_replace('{$orderInformation}', $orderInformationUser, $bodytag);
        $bodytag        = str_replace('{$userName}', $userName, $bodytag);
        $bodytag        = str_replace('{$siteUrl}', $siteUrl, $bodytag);
        $bodytag        = str_replace('{$companyLogo}', $companyLogo, $bodytag);
        $orderURL       = rtrim($siteUrl, "/") . "/user/invoice/id/" . $orderID . "/type/product/";
        $bodytag        = str_replace('{$orderURL}', $orderURL, $bodytag);
        $mailContent    = $bodytag;
        $emailID        = stripslashes($resUserDetails['userEmail']);
        $this->ObjView->assign('emailbody', $mailContent);
        $this->ObjView->assign('siteUrl', $siteUrl);
        $this->ObjView->assign('siteName', $siteName);
        $this->ObjView->assign('userName', $userName);
        $this->ObjView->assign('unsubscribeID', $emailID);
        $body = $this->ObjCommon->fetch($this->ObjView, "email_templates/OrderMail.tpl");
        //send mail
        $ObjMail->IsHTML();
        try {
            $send = $ObjMail->sendMail(array(
                $emailbody['emailFromEmail'],
                $emailbody['emailFromName']
            ), $emailID, $subject, $body);
            $ObjMail->Send();
        }
        catch (Exception $e) {
            $error = $e->getMessage();
        }
        //ADMIN MAIL
        $adminMail = $this->getSettingValue('site_settings', 'global_notification_email');
        if ($adminMail != '') {
            $emailbody      = $ObjEmailtemplate->getEmailTemlateDetails($languageIDsCustomAdmin, '', 'product-order-confirmation-admin');
            $emailbodyAdmin = $emailbody['emailHtml'];
            if ($getOrderDetails['paymentSettingsGroupID'] > 0) {
                $paymentType        = ($getOrderDetails['paymentType'] == 'AUTH_ONLY') ? 'AUTH ONLY' : 'AUTH CAPTURE';
                $paymentTemplate    = $this->getTemplateDetails('PAYMENT', $emailbodyAdmin);
                $paymentInformation = $paymentTemplate;
                $paymentTemplate    = str_replace('{$orderPaymentMethodName}', stripslashes($getOrderDetails['orderPaymentMethodName']), $paymentTemplate);
                if ($getPaymentSettingGroupDetails['paySetGroupKey'] != 'Free_Checkout') {
                    $paymentTemplate = str_replace('{$paymentType}', $paymentType, $paymentTemplate);
                } else {
                    $paymentTypeTemplate = $this->getTemplateDetails('PAYMENT TYPE', $paymentTemplate);
                    $paymentTemplate     = str_replace($paymentTypeTemplate, '', $paymentTemplate);
                }
            } else {
                $paymentTemplate = str_replace($paymentTemplate, '', $paymentTemplate);
            }
            $from    = array(
                $emailbody['emailFromEmail'],
                $emailbody['emailFromName']
            );
            $subject = $this->getSettingValue('site_settings', 'site_name') . ":-" . $emailbody['emailSubject'];
            $ObjMail->ClearAddresses();
            $bodytag         = $emailbody['emailHtml'];
            //shipping info
            $bodytag         = str_replace($shippingLabelTemplate, $shippingLabelTemplateValue, $bodytag);
            $bodytag         = str_replace('{$shippingInfo}', ' <br /> ' . $shippingAdressFormat, $bodytag);
            $bodytag         = str_replace('{$billingAddress}', ' <br /> ' . $billingAdressFormat, $bodytag);
            $bodytag         = ($getOrderDetails['billingPhone'] != '') ? str_replace('{$billingPhone}', stripslashes($getOrderDetails['billingPhone']), $bodytag) : preg_replace('#(' . preg_quote('<!--PHONE NUMBER BEGIN-->') . ')(.*)(' . preg_quote('<!--PHONE NUMBER END-->') . ')#si', '', $bodytag);
            $bodytag         = ($getOrderDetails['billingEmail'] != '') ? str_replace('{$billingEmail}', stripslashes($getOrderDetails['billingEmail']), $bodytag) : preg_replace('#(' . preg_quote('<!--EMAILID BEGIN-->') . ')(.*)(' . preg_quote('<!--EMAILID END-->') . ')#si', '', $bodytag);
            //order information
            $bodytag         = str_replace($orderInformationText, $orderInformationAdminCopy, $bodytag);
            $bodytag         = str_replace($commonInformation, $commonTemplate, $bodytag);
            $paymentTemplate = $paymentTemplate . $amountMismatchInfoForAdmin;
            $bodytag         = str_replace($paymentInformation, $paymentTemplate, $bodytag);
            $bodytag         = str_replace('{$userName}', $emailbody['emailFromName'], $bodytag);
            $bodytag         = str_replace('{$siteUrl}', $siteUrl, $bodytag);
            $bodytag         = str_replace('{$companyLogo}', $companyLogo, $bodytag);
            $mailContent     = $bodytag;
            $this->ObjView->assign('emailbody', $mailContent);
            $this->ObjView->assign('siteUrl', $siteUrl);
            $this->ObjView->assign('siteName', $siteName);
            $this->ObjView->assign('userName', $userName);
            $emailID = stripslashes($resUserDetails['userEmail']);
            $body    = $this->ObjCommon->fetch($this->ObjView, "email_templates/OrderMail.tpl", $languageIDsCustomAdmin);
            //send mail
            $ObjMail->IsHTML();
            try {
                $send = $ObjMail->sendMail(array(
                    $emailbody['emailFromEmail'],
                    $emailbody['emailFromName']
                ), $adminMail, $subject, $body);
                $ObjMail->Send();
                $ObjMail->ClearAddresses();
            }
            catch (Exception $e) {
                $error = $e->getMessage();
            }
        }
        //end of mail sending 
        $productID    = $orderItems[0]['productID'];
        $quantity     = $orderItems[0]['orderItemQuantity'];
        $productTitle = $orderItems[0]['productOptionName'];
        if ($orderStatus == 'Completed') {
            //Certificate Generation
            //settings for certificate generation
            $settings['siteUrl']          = $siteUrl;
            $settings['siteName']         = $siteName;
            $settings['companyLogo']      = $companyLogo;
            $settings['codeLength']       = $codeLength;
            $settings['codeGenerateType'] = $codeGenerateType;
            $settings['dateformat']       = $this->getSettingValue('site_settings', 'date_format');
            //Restrict Certificate Generation for product_product
            if ($resProductTypeDetails['productTypeKey'] != 'now_product') {
                $updateUsersReferrals = $ObjOrder->updateUsersReferral($userID, $orderID, '', '', 'Active');
            }
        }
        ///////////////////FOR SEND OUT OF STOCK EMAIL TO ADMIN///////////////////////////////////////////////////////////	
        $outOfStockProducts = $ObjOrder->checkProductOutOfStock($languageIDs, $orderID);
        if (count($outOfStockProducts) > 0) {
            $basicOrderDetails              = $ObjOrder->getBasicOrderDetails($languageIDs, $orderID);
            $mail_settings                  = $this->getSettingValue('mail_settings');
            $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
            $emailTempDet                   = $ObjEmailtemplate->getEmailTemlateDetails($languageIDsCustomAdmin, '', 'product-out-of-stock');
            $ObjMail                        = new Mail($mail_settings);
            $fromEmailId                    = $this->getSettingValue('site_settings', 'contact_email');
            $toEmailID                      = $adminMail;
            $Subject                        = $emailTempDet['emailSubject'];
            $Body                           = $emailTempDet['emailHtml'];
            $Body                           = str_replace('{$orderID}', $orderNumber, $Body);
            $Body                           = str_replace('{$cName}', $basicOrderDetails['billingFirstName'] . " " . $basicOrderDetails['billingLastName'], $Body);
            $settingsDateFormat             = $this->getSettingValue("site_settings", "date_format");
            $settingsTimeFormat             = $this->getSettingValue("site_settings", "time_format");
            $outOfStockOrderDate            = new DateTime($basicOrderDetails['orderStatusDate']);
            $outOfStockOrderTime            = new DateTime($basicOrderDetails['orderStatusDate']);
            switch ($settingsDateFormat) {
                case '%A, %B %e, %Y':
                    $outOfStockOrderDate = date_format($outOfStockOrderDate, 'l F d, Y');
                    break;
                case '%B %e, %Y':
                    $outOfStockOrderDate = date_format($outOfStockOrderDate, 'F d, Y');
                    break;
                case '%m/%d/%Y':
                    $outOfStockOrderDate = date_format($outOfStockOrderDate, 'm/d/Y');
                    break;
                case '%d/%m/%Y':
                    $outOfStockOrderDate = date_format($outOfStockOrderDate, 'd/m/Y');
                    break;
                case '%d-%m-%Y':
                    $outOfStockOrderDate = date_format($outOfStockOrderDate, 'd-m-Y');
                    break;
                case '%Y-%m-%d':
                    $outOfStockOrderDate = date_format($outOfStockOrderDate, 'Y-m-d');
                    break;
                default:
                    $outOfStockOrderDate = date_format($outOfStockOrderDate, 'M d, Y');
            }
            switch ($settingsTimeFormat) {
                case '%I:%M %p':
                    $outOfStockOrderTime = date_format($outOfStockOrderTime, 'h:i A');
                    break;
                case '%l:%M %p':
                    $outOfStockOrderTime = date_format($outOfStockOrderTime, 'g:i A');
                    break;
                case '%H:%M':
                    $outOfStockOrderTime = date_format($outOfStockOrderTime, 'G:i');
                    break;
                default:
                    $outOfStockOrderTime = date_format($outOfStockOrderTime, 'h:i A');
            }
            $outOfStockOrderDateTime = $outOfStockOrderDate . "," . $outOfStockOrderTime;
            $Body                    = str_replace('{$date}', $outOfStockOrderDateTime, $Body);
            //$Body = str_replace('{$date}', date("F j, Y, g:i a",strtotime($basicOrderDetails['orderStatusDate'])),$Body);
            $Body                    = str_replace('{$product}', $basicOrderDetails['productTitle'], $Body);
            foreach ($outOfStockProducts as $outOfStockVal) {
                $outOpt = "<li>" . $this->getLanguageValue("lblOptionName") . ": '" . $outOfStockVal . "'</li><br/>";
            }
            $Body = str_replace('{$outOpt}', $outOpt, $Body);
            $this->ObjView->assign('emailbody', $Body);
            $this->ObjView->assign('companyLogo', $companyLogo);
            $this->ObjView->assign('siteName', $siteName);
            $this->ObjView->assign('siteUrl', $siteUrl);
            $email_body = $this->ObjCommon->fetch($this->ObjView, 'email_templates/Layout.tpl', $languageIDsCustomAdmin);
            try {
                $ObjMail->IsHTML();
                $ObjMail->sendMail(array(
                    $fromEmailId,
                    $fromEmailName
                ), $toEmailID, $Subject, $email_body);
                $ObjMail->Send();
            }
            catch (Exception $e) {
                $e->getMessage();
            }
        }
        ///////////////////////////////////////////////////////////////////////
        $this->ObjSession->unsetSession("ORDERID");
        $this->ObjSession->unsetSession("PROMOCODE");
        if ($getPaymentSettingGroupDetails['paySetGroupKey'] != 'Paypalexprescheckout') {
            return ('Success|' . $orderID);
        } else {
            return $orderID;
        }
    }
    //PAYMENT SUCCESS GIFT COUPON
    //PAYMENT SUCCESS 
    public function giftPaymentSuccessAction($orderID, $couponID, $_POST) {
        $ObjOrder                       = new OrderModel($this->ObjView->languageIDs);
        $ObjPayment                     = new PaymentModel();
        $ObjUsers                       = new UserModel();
        $ObjCountry                     = new CountryModel();
        $ObjStates                      = new StateModel();
        $ObjCurrency                    = new CurrencyModel();
        $ObjEmailtemplate               = new EmailtemplateModel();
        $mail_settings                  = $this->getSettingValue('mail_settings');
        $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
        $ObjMail                        = new Mail($mail_settings);
        $languageIDs                    = $this->ObjView->languageIDs;
        $ObjPaymentgateway              = new PaymentgatewayModel();
        $ObjGiftcoupon                  = new GiftcouponModel();
        //get order array
        $getOrderDetails                = $ObjOrder->getOrderDetails($orderID);
        if ($_POST['paymentSettingsGroupID'] > 0) {
            $getPaymentSettingGroupDetails = $ObjPaymentgateway->getPaymentSettingGroupDetails($_POST['paymentSettingsGroupID']);
            $ObjPayment->updateOrderStatus($orderID, 'Completed');
        }
        $userID = $this->ObjSession->getSession("REGISTERED_USER_ID");
        if ($userID == '') {
            $userID = $getOrderDetails['userID'];
            $this->ObjSession->setSession("REGISTERED_USER_ID", $userID);
        }
        $userCreditAmount = $ObjUsers->getAvailableBalance($userID);
        $userCreditAmount = ($userCreditAmount <= 0) ? 0 : $userCreditAmount;
        $orderNumber      = $ObjOrder->updateCouponOrderNumber($orderID);
        $ObjGiftcoupon->updateCouponStatus($couponID, $orderID);
        $creditAmount        = ($_POST['paymentSettingsGroupID'] <= 0) ? 0 : $userCreditAmount;
        $totalDiscountAmount = $getOrderDetails['orderPromoDiscountAmount'] + $creditAmount;
        $payAmount           = $getOrderDetails['orderSubtotal'];
        $payAmount           = ($totalDiscountAmount > $payAmount) ? ($totalDiscountAmount - $payAmount) : ($payAmount - $totalDiscountAmount);
        //update user transaction
        $transactionAmt      = ($_POST['paymentSettingsGroupID'] <= 0) ? $payAmount : $userCreditAmount;
        if ($transactionAmt > 0) {
            $transactionDesc     = 'lbl_amount_debited_coupon' . ":" . $orderNumber;
            $saveUserTransaction = $ObjOrder->saveUserTransaction($userID, $transactionAmt, $transactionDesc);
        }
        if ($_POST['paymentSettingsGroupID'] > 0) {
            $orderStatus = $getOrderDetails['orderStatus'];
        }
        // sent mail to user
        //email body
        $resCouponDetails = $ObjGiftcoupon->getGiftCouponDetails($orderID, $couponID);
        $emailbody        = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'gift-coupon-confirm-user');
        $from             = $emailbody['giftFromName'] . "<" . $emailbody['emailFromEmail'] . ">";
        $subject          = $emailbody['emailSubject'];
        $emailTop         = $ObjEmailtemplate->getEmailTempSection($languageIDs, 'email_top');
        $emailBottom      = $ObjEmailtemplate->getEmailTempSection($languageIDs, 'email_bottom');
        $siteName         = $this->getSettingValue('site_settings', 'site_name');
        $siteUrl          = $this->getSettingValue('site_settings', 'global_http_url');
        $companyLogo      = $this->getSettingValue('company_information', 'company_logo');
        $this->ObjView->assign('companyLogo', $companyLogo);
        $resOrderDetails = $ObjOrder->getOrderDetails($orderID, 'statename');
        $resUserDetails  = $ObjUsers->getUserDetails($userID);
        $billingAdressFormat .= $ObjCountry->getFormattedAddress($resOrderDetails['billingAddress'], $this->ObjView->languageIDs);
        if ($billingAdressFormat != '')
            $billingInformation = $billingAdressFormat;
        $personalMessage  = ($resCouponDetails['couponMessage'] != '') ? str_replace(chr(13), "<br>", stripslashes($resCouponDetails['couponMessage'])) : '';
        $userEmailAddress = ($resUserDetails['userEmail'] != '') ? $resUserDetails['userEmail'] : '';
        if ($resCouponDetails['sendMode'] == 'Email') {
            $recipientEmail = (trim($resCouponDetails['couponEmailTo']) != '') ? trim($resCouponDetails['couponEmailTo']) : $userEmailAddress;
        } else {
            $recipientEmail = '';
        }
        $commonTemplate    = $this->getTemplateDetails('COMMON INFORMATION', $emailbody['emailHtml']);
        $commonInformation = $commonTemplate;
        $otherTemplate     = $this->getTemplateDetails('PROMO CODE', $commonTemplate);
        if ($resOrderDetails['orderPromoCode'] != '') {
            $commonTemplate = str_replace('{$orderPromoDiscountAmount}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderPromoDiscountAmount']) . ' (' . $resOrderDetails['orderPromoCode'] . ')', $commonTemplate);
        } else {
            $commonTemplate = str_replace($otherTemplate, '', $commonTemplate);
        }
        $otherTemplate = $this->getTemplateDetails('USER CREDIT AMOUNT', $commonTemplate);
        if ($resOrderDetails['orderUserCreditAmount'] > 0) {
            $commonTemplate = str_replace('{$orderUserCreditAmount}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderUserCreditAmount']), $commonTemplate);
        } else {
            $commonTemplate = str_replace($otherTemplate, '', $commonTemplate);
        }
        //email body
        $subject            = $this->getSettingValue('site_settings', 'site_name') . ":-" . stripslashes($subject);
        $userName           = stripslashes($resUserDetails['userFirstName']) . " " . stripslashes($resUserDetails['userLastName']);
        $emailBottomContent = str_replace('{$siteName}', $siteName, $emailBottom['sectionContent']);
        $emailTopContent    = str_replace('{$fromEmail}', $from, $emailTop['sectionContent']);
        $bodytag            = $emailbody['emailHtml'];
        $bodytag            = str_replace('{$billingName}', '', $bodytag);
        $bodytag            = str_replace('{$billingAddress}', $billingInformation, $bodytag);
        $bodytag            = str_replace('{$billingCityInfo}', '', $bodytag);
        $bodytag            = str_replace('{$giftFromName}', stripslashes($resCouponDetails['giftFromName']), $bodytag);
        $bodytag            = str_replace('{$orderNumber}', $resOrderDetails['orderNumber'], $bodytag);
        $bodytag            = str_replace('{$couponTo}', trim($resCouponDetails['couponTo']), $bodytag);
        $bodytag            = str_replace('{$recipientEmail}', $recipientEmail, $bodytag);
        $bodytag            = str_replace('{$personalMessage}', $personalMessage, $bodytag);
        $bodytag            = str_replace('{$orderSubtotal}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderSubtotal']), $bodytag);
        $bodytag            = str_replace('{$orderTotalAmount}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderTotalAmount']), $bodytag);
        $bodytag            = ($resOrderDetails['billingPhone'] != '') ? str_replace('{$billingPhone}', stripslashes($resOrderDetails['billingPhone']), $bodytag) : preg_replace('#(' . preg_quote('<!--PHONE NUMBER BEGIN-->') . ')(.*)(' . preg_quote('<!--PHONE NUMBER END-->') . ')#si', '', $bodytag);
        $bodytag            = ($resOrderDetails['billingEmail'] != '') ? str_replace('{$billingEmail}', stripslashes($resOrderDetails['billingEmail']), $bodytag) : preg_replace('#(' . preg_quote('<!--EMAILID BEGIN-->') . ')(.*)(' . preg_quote('<!--EMAILID END-->') . ')#si', '', $bodytag);
        $bodytag            = str_replace($commonInformation, $commonTemplate, $bodytag);
        $bodytag            = str_replace('{$userName}', $userName, $bodytag);
        $bodytag            = str_replace('{$siteUrl}', $siteUrl, $bodytag);
        $bodytag            = str_replace('{$companyLogo}', $companyLogo, $bodytag);
        $orderURL           = rtrim($siteUrl, "/") . "/user/invoice/id/" . $orderID . "/type/coupon/";
        $bodytag            = str_replace('{$orderURL}', $orderURL, $bodytag);
        $mailContent        = $bodytag;
        $emailID            = stripslashes($resUserDetails['userEmail']);
        $this->ObjView->assign('emailbody', $mailContent);
        $this->ObjView->assign('siteUrl', $siteUrl);
        $this->ObjView->assign('siteName', $siteName);
        $this->ObjView->assign('userName', $userName);
        $this->ObjView->assign('emailBottom', $emailBottomContent);
        $this->ObjView->assign('emailTop', $emailTopContent);
        $this->ObjView->assign('unsubscribeID', $emailID);
        $body = $this->ObjCommon->fetch($this->ObjView, "email_templates/OrderMail.tpl");
        //send mail
        $ObjMail->IsHTML();
        try {
            $send = $ObjMail->sendMail(array(
                $emailbody['emailFromEmail'],
                $emailbody['emailFromName']
            ), $emailID, $subject, $body);
            $ObjMail->Send();
        }
        catch (Exception $e) {
            $error = $e->getMessage();
        }
        $adminMail = $this->getSettingValue('site_settings', 'global_notification_email');
        if ($adminMail != '') {
            $languageIDsCustom            = $languageIDs;
            $languageIDsCustom['Current'] = $languageIDs['Default'];
            $emailbody                    = $ObjEmailtemplate->getEmailTemlateDetails($languageIDsCustom, '', 'gift-coupon-confirm-admin');
            $bodytag                      = $emailbody['emailHtml'];
            if ($resOrderDetails['paymentSettingsGroupID'] > 0) {
                $paymentType     = ($resOrderDetails['paymentType'] == 'AUTH_ONLY') ? 'AUTH ONLY' : 'AUTH CAPTURE';
                $paymentTemplate = $this->getTemplateDetails('PAYMENT', $bodytag);
                $bodytag         = str_replace('{$orderPaymentMethodName}', stripslashes($resOrderDetails['orderPaymentMethodName']), $bodytag);
                if ($getPaymentSettingGroupDetails['paySetGroupKey'] != 'Free_Checkout') {
                    $bodytag = str_replace('{$paymentType}', $paymentType, $bodytag);
                } else {
                    $paymentTypeTemplate = $this->getTemplateDetails('PAYMENT TYPE', $bodytag);
                    $bodytag             = str_replace($paymentTypeTemplate, '', $bodytag);
                }
            }
            $Body = str_replace('{$outOpt}', $outOpt, $Body);
            $this->ObjView->assign('emailbody', $Body);
            $this->ObjView->assign('companyLogo', $companyLogo);
            $this->ObjView->assign('siteName', $siteName);
            $this->ObjView->assign('siteUrl', $siteUrl);
            $email_body = $this->ObjCommon->fetch($this->ObjView, 'email_templates/Layout.tpl', $languageIDsCustomAdmin);
            try {
                $ObjMail->IsHTML();
                $ObjMail->sendMail(array(
                    $fromEmailId,
                    $fromEmailName
                ), $toEmailID, $Subject, $email_body);
                $ObjMail->Send();
            }
            catch (Exception $e) {
                $e->getMessage();
            }
        }
        ///////////////////////////////////////////////////////////////////////
        $this->ObjSession->unsetSession("ORDERID");
        $this->ObjSession->unsetSession("PROMOCODE");
        if ($getPaymentSettingGroupDetails['paySetGroupKey'] != 'Paypalexprescheckout') {
            return ('Success|' . $orderID);
        } else {
            return $orderID;
        }
    }
    //PAYMENT SUCCESS GIFT COUPON
    //PAYMENT SUCCESS 
    public function giftPaymentSuccessAction($orderID, $couponID, $_POST) {
        $ObjOrder                       = new OrderModel($this->ObjView->languageIDs);
        $ObjPayment                     = new PaymentModel();
        $ObjUsers                       = new UserModel();
        $ObjCountry                     = new CountryModel();
        $ObjStates                      = new StateModel();
        $ObjCurrency                    = new CurrencyModel();
        $ObjEmailtemplate               = new EmailtemplateModel();
        $mail_settings                  = $this->getSettingValue('mail_settings');
        $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
        $ObjMail                        = new Mail($mail_settings);
        $languageIDs                    = $this->ObjView->languageIDs;
        $ObjPaymentgateway              = new PaymentgatewayModel();
        $ObjGiftcoupon                  = new GiftcouponModel();
        //get order array
        $getOrderDetails                = $ObjOrder->getOrderDetails($orderID);
        if ($_POST['paymentSettingsGroupID'] > 0) {
            $getPaymentSettingGroupDetails = $ObjPaymentgateway->getPaymentSettingGroupDetails($_POST['paymentSettingsGroupID']);
            $ObjPayment->updateOrderStatus($orderID, 'Completed');
        }
        $userID = $this->ObjSession->getSession("REGISTERED_USER_ID");
        if ($userID == '') {
            $userID = $getOrderDetails['userID'];
            $this->ObjSession->setSession("REGISTERED_USER_ID", $userID);
        }
        $userCreditAmount = $ObjUsers->getAvailableBalance($userID);
        $userCreditAmount = ($userCreditAmount <= 0) ? 0 : $userCreditAmount;
        $orderNumber      = $ObjOrder->updateCouponOrderNumber($orderID);
        $ObjGiftcoupon->updateCouponStatus($couponID, $orderID);
        $creditAmount        = ($_POST['paymentSettingsGroupID'] <= 0) ? 0 : $userCreditAmount;
        $totalDiscountAmount = $getOrderDetails['orderPromoDiscountAmount'] + $creditAmount;
        $payAmount           = $getOrderDetails['orderSubtotal'];
        $payAmount           = ($totalDiscountAmount > $payAmount) ? ($totalDiscountAmount - $payAmount) : ($payAmount - $totalDiscountAmount);
        //update user transaction
        $transactionAmt      = ($_POST['paymentSettingsGroupID'] <= 0) ? $payAmount : $userCreditAmount;
        if ($transactionAmt > 0) {
            //	$transactionDesc			=	'Amount debited for coupon purchase. Coupon ID : '.$orderNumber;
            $transactionDesc     = 'lbl_amount_debited_coupon' . ":" . $orderNumber;
            $saveUserTransaction = $ObjOrder->saveUserTransaction($userID, $transactionAmt, $transactionDesc);
        }
        if ($_POST['paymentSettingsGroupID'] > 0) {
            $orderStatus = $getOrderDetails['orderStatus'];
        }
        // sent mail to user
        //email body
        $resCouponDetails = $ObjGiftcoupon->getGiftCouponDetails($orderID, $couponID);
        $emailbody        = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'gift-coupon-confirm-user');
        $from             = $emailbody['giftFromName'] . "<" . $emailbody['emailFromEmail'] . ">";
        $subject          = $emailbody['emailSubject'];
        $emailTop         = $ObjEmailtemplate->getEmailTempSection($languageIDs, 'email_top');
        $emailBottom      = $ObjEmailtemplate->getEmailTempSection($languageIDs, 'email_bottom');
        $siteName         = $this->getSettingValue('site_settings', 'site_name');
        $siteUrl          = $this->getSettingValue('site_settings', 'global_http_url');
        $companyLogo      = $this->getSettingValue('company_information', 'company_logo');
        $this->ObjView->assign('companyLogo', $companyLogo);
        $resOrderDetails     = $ObjOrder->getOrderDetails($orderID, 'statename');
        $resUserDetails      = $ObjUsers->getUserDetails($userID);
        // formating billing informatio in gift coupon order confirmation mails
        $addressFormat       = explode(",", $resOrderDetails['addressFormat']);
        $billingAdressFormat = '<br />';
        foreach ($addressFormat as $format) {
            $format = trim($format);
            if ($billingAdressFormat != '' && ($format != "Lname" && $format != "Fname")) {
                $billingAdressFormat .= ', <br /> ';
            }
            if ($format == "Fname") {
                $billingAdressFormat .= '<b>' . $resOrderDetails['billingFirstName'] . '</b>  ';
            } else if ($format == "Lname") {
                $billingAdressFormat .= '<b>' . $resOrderDetails['billingLastName'] . '</b>  ';
            } else if ($format == "Address") {
                $billingAdressFormat .= $resOrderDetails['billingAddress1'];
                if ($resOrderDetails['billingAddress2'] != '') {
                    $billingAdressFormat .= ", " . $resOrderDetails['billingAddress2'];
                }
            } else if ($format == "City") {
                $billingAdressFormat .= $resOrderDetails['billingCity'];
            } else if ($format == "County/State") {
                $billingAdressFormat .= $resOrderDetails['stateName'] . $resOrderDetails['billingProvince'];
            } else if ($format == "Postcode/Zip Code") {
                $billingAdressFormat .= $resOrderDetails['billingZip'];
            } else if ($format == "Country") {
                $billingAdressFormat .= $resOrderDetails['countryName'];
            }
        }
        $personalMessage  = ($resCouponDetails['couponMessage'] != '') ? str_replace(chr(13), "<br>", stripslashes($resCouponDetails['couponMessage'])) : '';
        $userEmailAddress = ($resUserDetails['userEmail'] != '') ? $resUserDetails['userEmail'] : '';
        if ($resCouponDetails['sendMode'] == 'Email') {
            $recipientEmail = (trim($resCouponDetails['couponEmailTo']) != '') ? trim($resCouponDetails['couponEmailTo']) : $userEmailAddress;
        } else {
            $recipientEmail = '';
        }
        $commonTemplate    = $this->getTemplateDetails('COMMON INFORMATION', $emailbody['emailHtml']);
        $commonInformation = $commonTemplate;
        $otherTemplate     = $this->getTemplateDetails('PROMO CODE', $commonTemplate);
        if ($resOrderDetails['orderPromoCode'] != '') {
            $commonTemplate = str_replace('{$orderPromoDiscountAmount}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderPromoDiscountAmount']) . ' (' . $resOrderDetails['orderPromoCode'] . ')', $commonTemplate);
        } else {
            $commonTemplate = str_replace($otherTemplate, '', $commonTemplate);
        }
        $otherTemplate = $this->getTemplateDetails('USER CREDIT AMOUNT', $commonTemplate);
        if ($resOrderDetails['orderUserCreditAmount'] > 0) {
            $commonTemplate = str_replace('{$orderUserCreditAmount}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderUserCreditAmount']), $commonTemplate);
        } else {
            $commonTemplate = str_replace($otherTemplate, '', $commonTemplate);
        }
        //email body
        $subject            = $this->getSettingValue('site_settings', 'site_name') . ":-" . stripslashes($subject);
        $userName           = stripslashes($resUserDetails['userFirstName']) . " " . stripslashes($resUserDetails['userLastName']);
        $emailBottomContent = str_replace('{$siteName}', $siteName, $emailBottom['sectionContent']);
        $emailTopContent    = str_replace('{$fromEmail}', $from, $emailTop['sectionContent']);
        $bodytag            = $emailbody['emailHtml'];
        //$bodytag 						= 	str_replace('{$billingInformation}',$billingInformation,$bodytag);	
        $bodytag            = str_replace('{$billingName}', '', $bodytag);
        $bodytag            = str_replace('{$billingAddress}', $billingInformation, $bodytag);
        $bodytag            = str_replace('{$billingCityInfo}', '', $bodytag);
        $bodytag            = str_replace('{$giftFromName}', stripslashes($resCouponDetails['giftFromName']), $bodytag);
        $bodytag            = str_replace('{$couponTo}', trim($resCouponDetails['couponTo']), $bodytag);
        $bodytag            = str_replace('{$recipientEmail}', $recipientEmail, $bodytag);
        $bodytag            = str_replace('{$personalMessage}', $personalMessage, $bodytag);
        $bodytag            = str_replace('{$orderSubtotal}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderSubtotal']), $bodytag);
        $bodytag            = str_replace('{$orderTotalAmount}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderTotalAmount']), $bodytag);
        $bodytag            = ($resOrderDetails['billingPhone'] != '') ? str_replace('{$billingPhone}', stripslashes($resOrderDetails['billingPhone']), $bodytag) : preg_replace('#(' . preg_quote('<!--PHONE NUMBER BEGIN-->') . ')(.*)(' . preg_quote('<!--PHONE NUMBER END-->') . ')#si', '', $bodytag);
        $bodytag            = ($resOrderDetails['billingEmail'] != '') ? str_replace('{$billingEmail}', stripslashes($resOrderDetails['billingEmail']), $bodytag) : preg_replace('#(' . preg_quote('<!--EMAILID BEGIN-->') . ')(.*)(' . preg_quote('<!--EMAILID END-->') . ')#si', '', $bodytag);
        $bodytag            = str_replace($commonInformation, $commonTemplate, $bodytag);
        //$bodytag 						=  	str_replace('{$orderInformation}',$orderInformation,$bodytag);
        $bodytag            = str_replace('{$userName}', $userName, $bodytag);
        $bodytag            = str_replace('{$siteUrl}', $siteUrl, $bodytag);
        $bodytag            = str_replace('{$companyLogo}', $companyLogo, $bodytag);
        $orderURL           = rtrim($siteUrl, "/") . "/user/invoice/id/" . $orderID . "/type/coupon/";
        $bodytag            = str_replace('{$orderURL}', $orderURL, $bodytag);
        $mailContent        = $bodytag;
        $emailID            = stripslashes($resUserDetails['userEmail']);
        $this->ObjView->assign('emailbody', $mailContent);
        $this->ObjView->assign('siteUrl', $siteUrl);
        $this->ObjView->assign('siteName', $siteName);
        $this->ObjView->assign('userName', $userName);
        $this->ObjView->assign('emailBottom', $emailBottomContent);
        $this->ObjView->assign('emailTop', $emailTopContent);
        $this->ObjView->assign('unsubscribeID', $emailID);
        $body = $this->ObjCommon->fetch($this->ObjView, "email_templates/OrderMail.tpl");
        //send mail
        $ObjMail->IsHTML();
        try {
            $send = $ObjMail->sendMail(array(
                $emailbody['emailFromEmail'],
                $emailbody['emailFromName']
            ), $emailID, $subject, $body);
            $ObjMail->Send();
        }
        catch (Exception $e) {
            $error = $e->getMessage();
        }
        $adminMail = $this->getSettingValue('site_settings', 'global_notification_email');
        if ($adminMail != '') {
            $languageIDsCustom            = $languageIDs;
            $languageIDsCustom['Current'] = $languageIDs['Default'];
            $emailbody                    = $ObjEmailtemplate->getEmailTemlateDetails($languageIDsCustom, '', 'gift-coupon-confirm-admin');
            $bodytag                      = $emailbody['emailHtml'];
            if ($resOrderDetails['paymentSettingsGroupID'] > 0) {
                $paymentType     = ($resOrderDetails['paymentType'] == 'AUTH_ONLY') ? 'AUTH ONLY' : 'AUTH CAPTURE';
                $paymentTemplate = $this->getTemplateDetails('PAYMENT', $bodytag);
                $bodytag         = str_replace('{$orderPaymentMethodName}', stripslashes($resOrderDetails['orderPaymentMethodName']), $bodytag);
                if ($getPaymentSettingGroupDetails['paySetGroupKey'] != 'Free_Checkout') {
                    $bodytag = str_replace('{$paymentType}', $paymentType, $bodytag);
                } else {
                    $paymentTypeTemplate = $this->getTemplateDetails('PAYMENT TYPE', $bodytag);
                    $bodytag             = str_replace($paymentTypeTemplate, '', $bodytag);
                }
            } else {
                $bodytag = str_replace($paymentTemplate, '', $bodytag);
            }
            $from    = $emailbody['emailFromName'] . "<" . $emailbody['emailFromEmail'] . ">";
            $subject = $this->getSettingValue('site_settings', 'site_name') . ":-" . $emailbody['emailSubject'];
            $ObjMail->ClearAddresses();
            //$bodytag 				= 	str_replace('{$billingInformation}',$billingInformation,$emailbody['emailHtml']);	
            $bodytag         = str_replace('{$billingName}', '', $bodytag);
            $bodytag         = str_replace('{$billingAddress}', $billingInformation, $bodytag);
            $bodytag         = str_replace('{$billingCityInfo}', '', $bodytag);
            //$bodytag 				=  	str_replace('{$orderInformation}',$orderInformation,$bodytag);
            $bodytag         = str_replace('{$giftFromName}', stripslashes($resCouponDetails['giftFromName']), $bodytag);
            $bodytag         = str_replace('{$couponTo}', trim($resCouponDetails['couponTo']), $bodytag);
            $bodytag         = str_replace('{$recipientEmail}', $recipientEmail, $bodytag);
            $bodytag         = str_replace('{$personalMessage}', $personalMessage, $bodytag);
            $bodytag         = str_replace('{$orderSubtotal}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderSubtotal']), $bodytag);
            $bodytag         = str_replace('{$orderNumber}', $resOrderDetails['orderNumber'], $bodytag);
            $bodytag         = str_replace('{$orderTotalAmount}', $ObjCurrency->getFormattedCurrency($resOrderDetails['orderTotalAmount']), $bodytag);
            $bodytag         = ($resOrderDetails['billingPhone'] != '') ? str_replace('{$billingPhone}', stripslashes($resOrderDetails['billingPhone']), $bodytag) : preg_replace('#(' . preg_quote('<!--PHONE NUMBER BEGIN-->') . ')(.*)(' . preg_quote('<!--PHONE NUMBER END-->') . ')#si', '', $bodytag);
            $bodytag         = ($resOrderDetails['billingEmail'] != '') ? str_replace('{$billingEmail}', stripslashes($resOrderDetails['billingEmail']), $bodytag) : preg_replace('#(' . preg_quote('<!--EMAILID BEGIN-->') . ')(.*)(' . preg_quote('<!--EMAILID END-->') . ')#si', '', $bodytag);
            $bodytag         = str_replace($commonInformation, $commonTemplate, $bodytag);
            $bodytag         = str_replace('{$userName}', $emailbody['emailFromName'], $bodytag);
            $bodytag         = str_replace('{$siteUrl}', $siteUrl, $bodytag);
            $bodytag         = str_replace('{$companyLogo}', $companyLogo, $bodytag);
            $mailContent     = $bodytag;
            $emailTop        = $ObjEmailtemplate->getEmailTempSection($languageIDsCustom, 'email_top');
            $emailTopContent = str_replace('{$fromEmail}', $from, $emailTop['sectionContent']);
            $this->ObjView->assign('emailbody', $mailContent);
            $this->ObjView->assign('siteUrl', $siteUrl);
            $this->ObjView->assign('siteName', $siteName);
            $this->ObjView->assign('userName', $userName);
            //$this->ObjView->assign('emailBottom',$adminEmailBottom);
            $this->ObjView->assign('emailTop', $emailTopContent);
            $emailID = stripslashes($resUserDetails['userEmail']);
            $body    = $this->ObjCommon->fetch($this->ObjView, "email_templates/OrderMail.tpl", $languageIDsCustom);
            //send mail
            $ObjMail->IsHTML();
            try {
                $send = $ObjMail->sendMail(array(
                    $emailbody['emailFromEmail'],
                    $emailbody['emailFromName']
                ), $adminMail, $subject, $body);
                $ObjMail->Send();
            }
            catch (Exception $e) {
                $error = $e->getMessage();
            }
        }
        //end of mail sending 
        ///////////////////////////////////////////////////////////////////////
        if ($getPaymentSettingGroupDetails['paySetGroupKey'] != 'Paypalexprescheckout') {
            return ('Success');
        } else {
            return $orderID;
        }
    }
    //END OF FUNCTION	 
    //to get template details between given comments
    function getTemplateDetails($comments, $string) {
        $beginTxt      = '<!--' . $comments . ' BEGIN-->';
        $endTxt        = '<!--' . $comments . ' END-->';
        $otherTemplate = substr($string, strlen($beginTxt) + strpos($string, $beginTxt), (strlen($string) - strpos($string, $endTxt)) * (-1));
        return $otherTemplate;
    }
}