 <?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/ //
//$Id: PaymentModel.php, v 1.0 2013/03/16 18:52:11
class PaymentModel extends Model {
    var $methods = array();
    var $isError = false;
    var $errorMessage;
    var $is_ipn_method = false;
    var $pid;
    var $protocol;
    var $type;
    var $url_to_gateway = "";
    var $name;
    var $title;
    var $id;
    var $class_name;
    var $settingsVars;
    var $logFile = false;
    var $logFileName = "log.txt";
    public function listPaymentSettingsValues($paymentMethodId='',$paySetGroupKey='') {
            if($paySetGroupKey!='' && $paymentMethodId=='') {
                $paymentMethodId = $this->getPaymentSettingGroupByGroupKey($paySetGroupKey);
            }   
            $listPaymentSettings= $this->listPaymentSettings($paymentMethodId);
            foreach($listPaymentSettings as $key=>$paymentSettings) 
            {
                    $paymentValues=array_combine(explode(",",$paymentSettings['paySetKeys']),explode(",",$paymentSettings['paySetValues']));				

            if(isset($groupKey) && $groupKey=='Y') 
            {
                return $paymentSettings['paySetGroupKey'];
            } else {
                foreach ($paymentValues as $key => $value) {
                    $this->settingsVars[$key] = $value;
                }
                $this->type                             = 'cc';
                $this->settingsVars['paySetGroupLabel'] = $paymentSettings['paySetGroupLabel'];
            }
        }
        $paymentCurrency                        = $this->getPaymentSettingByKey($paymentMethodId, 'payment_currency');
        $this->settingsVars['payment_currency'] = $paymentCurrency;
        return $this;
    }
    public function isMethod($type) {
        return array_key_exists($type, $this->methods);
    }
    public function isMethods() {
        return count($this->methods);
    }
    public function listPaymentSettings($paymentMethodId = '') {
        $sql = "	SELECT
								GROUP_CONCAT(paySet.paySetKey) AS paySetKeys,
								GROUP_CONCAT(paySet.paySetValue) AS paySetValues,
								paySetGro.paySetGroupID,
								paySetGro.paySetGroupLabel,
								paySetGro.paySetGroupKey 
							FROM
								" . DB_PREFIX . "payment_settings AS paySet 
							INNER JOIN
								" . DB_PREFIX . "payment_settings_group AS paySetGro
								ON paySetGro.paySetGroupID=paySet.paySetGroupID
								AND  paySetGro.paySetGroupStatus='Active'";
        if ($paymentMethodId > 0) {
            $sql .= " 	AND paySetGro.paySetGroupID='" . $paymentMethodId . "'";
        }
        $sql .= " 		AND paySet.paySetKey!='payment_currency'
							GROUP BY
								paySet.paySetGroupID
							ORDER BY
								paySetGro.paySetGroupSortOrder ";
        return $this->ObjDatabase->readValues($sql);
    }
    public function getPaymentSettingByKey($paymentMethodId, $paySetKey) {
        $sql                     = "	SELECT
						paySetValue
					FROM
						" . DB_PREFIX . "payment_settings
					WHERE
						paySetKey='" . $paySetKey . "'
						AND paySetGroupID='" . $paymentMethodId . "'
						";
        $resSelectPaymentSetting = $this->ObjDatabase->readValue($sql);
        return $resSelectPaymentSetting['paySetValue'];
    }
    public function getPaymentSettingGroupByGroupKey($paySetKey) {
        $sql                          = "	SELECT
								paySetGroupID
							FROM
								" . DB_PREFIX . "payment_settings_group
							WHERE
								paySetGroupKey='" . $paySetKey . "'";
        $resSelectPaymentSettingGroup = $this->ObjDatabase->readValue($sql);
        return $resSelectPaymentSettingGroup['paySetGroupID'];
    }
    public function getPaymentTransactionDetails($orderID = '', $masterOrderID = '') {
        $sql = "			SELECT
								payTra.* 
							FROM
								" . DB_PREFIX . "payment_transactions  payTra ";
        if ($orderID > 0) {
            $sql .= " 	LEFT JOIN
								" . DB_PREFIX . "order_masters ordMas
								ON (payTra.masterOrderID=ordMas.masterOrderID)
							WHERE
								ordMas.masterOrderID='" . $orderID . "' ";
        } else if ($masterOrderID > 0) {
            $sql .= " 	WHERE
								payTra.masterOrderID='" . $masterOrderID . "' ";
        }
        $sql .= " 		ORDER BY
								payTra.paymentTransactionID DESC ";
        return $this->ObjDatabase->readValue($sql);
    }
    public function createTransaction($order, $transactionID = '', $response = '', $extra = '', $authValue = "", $paymentGatewayCharge = '0', $paymentMethodName = '', $paymentCurrencyID = 0, $convertedPaymentAmount = 0) {
        $ObjCart          = new CartModel();
        $ObjCurrency      = new CurrencyModel();
        $orderTotalAmount = 0;
        $gateWayAmount    = 0;
        $itemCCCharge     = 0;
        if (isset($order['orderPaymentMethodName']) && $order['orderPaymentMethodName'] != '') {
            $paymentGateway = $this->escape($order['orderPaymentMethodName']);
        } else {
            $paymentGateway = $this->escape((isset($this->settingsVars['payment_processor_id']) ? $this->settingsVars['payment_processor_id'] : '') . "(" . $this->settingsVars['paySetGroupLabel'] . ")");
        }
        $paymentMethodName = ($paymentMethodName == '') ? $order['orderPaymentMethodName'] : $paymentMethodName;
        $orderSubTotal     = (isset($order['orderSubtotal']) ? $order['orderSubtotal'] : 0);
        $orderTotal        = (isset($order['orderTotalAmount']) ? $order['orderTotalAmount'] : 0);
        $taxAmount         = (isset($order['orderTaxAmount']) ? $order['orderTaxAmount'] : 0);
        if (isset($order['paymentType']) && $order['paymentType']) {
            $paymentType = $order['paymentType'];
        } else {
            $paymentType = $this->type;
        }
        $convertedPaymentAmount = ($paymentCurrencyID > 0) ? $convertedPaymentAmount : 0;
        if (isset($order['transactionID']) && $order['transactionID'] > 0) {
            $allMasterOrders = $ObjCart->getOrderMasterIDByTransactionID($order['transactionID'], 'all');
        } else {
            $allMasterOrders = $ObjCart->getOrderMasterIDByOrderNumber($order['masterOrderID'], 'all');
        }
        foreach ($allMasterOrders as $key => $items) {
            $subOrderID           = $items['masterOrderID'];
            $paymentGatewayCharge = 0;
            $orderTotalAmount     = 0;
            $gateWayAmount        = 0;
            $itemCCCharge         = 0;
            $sql                  = "	INSERT INTO
							" . DB_PREFIX . "payment_transactions
							(
								transactionID,
								masterOrderID,
								userID,
								completedDate,
								extra,
								paymentType,
								paymentGateway,
								paymentResponse,
								orderSubTotal,
								orderTotal,
								taxAmount,
								paymentCurrencyID,
								convertedPaymentAmount
							) 
						VALUES
							(
								'" . $transactionID . "',
								'" . $subOrderID . "',
								'" . (isset($order["userID"]) ? $order["userID"] : 0) . "',
								NOW(),
								'" . $extra . "',
								'" . $this->escape($paymentType) . "',
								'" . $paymentGateway . "',
								'" . $this->escape($response) . "',
								'" . $orderSubTotal . "',
								'" . $orderTotal . "',
								'" . $taxAmount . "',
								'" . $paymentCurrencyID . "',
								'" . $convertedPaymentAmount . "'
							) ";
            $this->ObjDatabase->setQuery($sql);
        }
        if ($order['paymentSettingsGroupID'] > 0) {
            if ($authValue == 'AUTH_ONLY') {
                $paymentStatus        = ($order['orderPaymentMethodName'] == 'Free_Checkout') ? 'Received' : 'Pending';
                $paymentTransactionID = $transactionID;
            } else {
                $paymentStatus        = 'Received';
                $paymentTransactionID = '';
            }
            $sql = "	UPDATE
							" . DB_PREFIX . "order_masters ordMas
						SET  
							ordMas.paymentSettingsGroupID='" . $order['paymentSettingsGroupID'] . "',
							ordMas.paymentType='" . $authValue . "',
							ordMas.paymentStatus='" . $paymentStatus . "',
							ordMas.orderPaymentMethodName='" . $this->escape($paymentMethodName) . "',
							ordMas.transactionID='" . $transactionID . "' 
						WHERE ";
            if (isset($order['transactionID']) && $order['transactionID'] > 0) {
                $sql .= " ordMas.transactionID='" . $order['transactionID'] . "'";
            } else {
                $sql .= " ordMas.masterOrderID='" . $order["masterOrderID"] . "' OR ordMas.parentOrderID='" . $order["masterOrderID"] . "'";
            }
            $resUpdateOrder         = $this->ObjDatabase->setQuery($sql);
            $order["orderNumber"]   = (isset($order["orderNumber"]) ? $order["orderNumber"] : 0);
            $order["transactionID"] = (isset($order["transactionID"]) ? $order["transactionID"] : 0);
            $this->updateStatusChanegHistory(0, $order["orderNumber"], $order['transactionID'], '', $paymentStatus);
            //@Mic starts
            $resOrders         = $ObjCart->getOrderDetails(0, 0, $order["masterOrderID"]);
            $purchasedCurrency = isset($resOrders[0]['purchasedCurrency']) ? $resOrders[0]['purchasedCurrency'] : 0;
            if (isset($this->settingsVars["payment_gateway_charge_percent"]) && $this->settingsVars["payment_gateway_charge_percent"] != '') {
                $paymentGatewayChargePercent = $this->settingsVars["payment_gateway_charge_percent"];
            }
            $paymentGatewaySurCharge = isset($this->settingsVars["payment_gateway_charge"]) ? $this->settingsVars["payment_gateway_charge"] : 0;
            $resDefaultCurrency      = $ObjCurrency->getDefaultCurrencyDetails();
            $defaultCurrencyId       = $resDefaultCurrency['currencyID'];
            //For updating the payment gateway charge calculation for orders 
            //@usage MIC
            if (count($resOrders) > 0) {
                foreach ($resOrders as $key => $items) {
                    $paymentGatewayCharge = 0;
                    $orderTotalAmount     = 0;
                    $gateWayAmount        = 0;
                    $itemCCCharge         = 0;
                    //For skipping auth capture on order status change
                    if ($items['orderPaymentGatewayCharge'] > 0) {
                        continue;
                    }
                    $resOrderItems = $ObjCart->getOrderItemDetails($items["masterOrderID"]);
                    $extraData     = unserialize($items['extraData']);
                    if (count($resOrderItems) > 0) {
                        foreach ($resOrderItems as $key1 => $subItems) {
                            $subItemExtraData = unserialize($subItems['extra']);
                            $shippingSetting  = $subItems['shippingSetting'];
                            $taxSetting       = $subItems['taxSetting'];
                            $orderTotalAmount += $subItemExtraData['orderItemSubtotal'] + $subItemExtraData['orderOtherServiceCharge'];
                            if ($shippingSetting == 'business') {
                                $orderTotalAmount += $subItemExtraData['shippingAmount'];
                            }
                            if ($taxSetting == 'business') {
                                $orderTotalAmount += $subItemExtraData['orderTaxAmount'];
                            }
                            $gateWayAmount += $subItemExtraData['orderItemSubtotal'] + $subItemExtraData['orderOtherServiceCharge'];
                            if ($shippingSetting == 'business') {
                                $gateWayAmount += $subItemExtraData['shippingAmount'];
                            }
                            if ($taxSetting == 'business') {
                                $gateWayAmount += $subItemExtraData['orderTaxAmount'];
                            }
                        }
                    }
                    if (isset($paymentGatewayChargePercent) && $paymentGatewayChargePercent > 0) {
                        $paymentGatewayCharge = $gateWayAmount * ($paymentGatewayChargePercent / 100);
                    }
                    $paymentGatewayCharge += $paymentGatewaySurCharge;
                    $businessPayment                                        = $items['businessPayment'] - $paymentGatewayCharge;
                    $adminGateWatCharge                                     = $ObjCurrency->convertValueCustom($paymentGatewayCharge, $items["purchasedCurrency"], $defaultCurrencyId);
                    $adminBusinessPayment                                   = $ObjCurrency->convertValueCustom($businessPayment, $items["purchasedCurrency"], $defaultCurrencyId);
                    $extraData['amountDetails']['admin']['ccCharge']        = $adminGateWatCharge;
                    $extraData['amountDetails']['admin']['businessPayment'] = $adminBusinessPayment;
                    $sql                                                    = "	UPDATE
									" . DB_PREFIX . "order_masters
								SET  
									orderPaymentGatewayCharge='" . $paymentGatewayCharge . "',
									businessPayment='" . $businessPayment . "',
									convertedBusinessPayment='" . $adminBusinessPayment . "',
									extraData=
									'" . $this->escape(serialize($extraData), FALSE, FALSE) . "'
								WHERE
									masterOrderID=" . (int) $items["masterOrderID"];
                    $resUpdateOrder                                         = $this->ObjDatabase->setQuery($sql);
                    unset($extraData);
                    if (count($resOrderItems) > 0) {
                        foreach ($resOrderItems as $key1 => $subItems) {
                            $extraData       = unserialize($subItems['extra']);
                            $itemTotal       = $extraData['orderItemSubtotal'] + $extraData['orderOtherServiceCharge'];
                            $shippingSetting = $subItems['shippingSetting'];
                            $taxSetting      = $subItems['taxSetting'];
                            if ($shippingSetting == 'business') {
                                $itemTotal += $extraData['shippingAmount'];
                            }
                            if ($taxSetting == 'business') {
                                $itemTotal += $extraData['orderTaxAmount'];
                            }
                            if ($paymentGatewayCharge > 0 && $itemTotal > 0 && $orderTotalAmount > 0) {
                                $itemCCCharge = $paymentGatewayCharge * ($itemTotal / $orderTotalAmount);
                            }
                            $itemBusinessPayment                                    = $extraData['businessPayment'] - $itemCCCharge;
                            $extraData['orderPaymentGatewayCharge']                 = $itemCCCharge;
                            $extraData['businessPayment']                           = $itemBusinessPayment;
                            $adminCCCharge                                          = $ObjCurrency->convertValueCustom($itemCCCharge, $purchasedCurrency, $defaultCurrencyId);
                            $adminBusinessPayment                                   = $ObjCurrency->convertValueCustom($itemBusinessPayment, $purchasedCurrency, $defaultCurrencyId);
                            $extraData['amountDetails']['admin']['ccCharge']        = $adminCCCharge;
                            $extraData['amountDetails']['admin']['businessPayment'] = $adminBusinessPayment;
                            $sql                                                    = "	UPDATE
											" . DB_PREFIX . "order_items
										SET  
											extra='" . $this->escape(serialize($extraData), FALSE, FALSE) . "' 
										WHERE
											orderItemID=" . $subItems["orderItemID"];
                            $resUpdateOrder                                         = $this->ObjDatabase->setQuery($sql);
                            unset($extraData);
                        }
                    }
                }
            }
            //@Mic ends 
        }
        return (isset($resUpdateOrder) ? $resUpdateOrder : array());
    }
    public function createTransactionError($order, $transactionID = '', $response = '', $extra = '', $authValue = "", $paymentGatewayCharge = '0', $paymentMethodName = '') {
        if (isset($order['orderPaymentMethodName']) && $order['orderPaymentMethodName'] != '') {
            $paymentGateway = $this->escape($order['orderPaymentMethodName']);
        } else {
            $paymentGateway = $this->escape((isset($this->settingsVars['payment_processor_id']) ? $this->settingsVars['payment_processor_id'] : '') . " (" . $this->settingsVars['paySetGroupLabel'] . ")");
        }
        $orderSubTotal = $order['orderSubtotal'];
        $orderTotal    = $order['orderTotalAmount'];
        $taxAmount     = $order['orderTaxAmount'];
        if ($order['paymentType']) {
            $paymentType = $order['paymentType'];
        } else {
            $paymentType = $this->type;
        }
        $sql = "	INSERT INTO 
						" . DB_PREFIX . "payment_transactions_errors
						(
							transactionID,
							orderID,
							userID,
							completedDate,
							extra,
							paymentType,
							paymentGateway,
							paymentResponse,
							orderSubTotal,
							orderTotal,
							taxAmount
						)
					VALUES
						(
							'" . $transactionID . "',
							" . $order["masterOrderID"] . ",
							" . $order["userID"] . ",NOW(),
							'" . $extra . "',
							'" . $this->escape($paymentType) . "',
							'" . $paymentGateway . "',
							'" . $this->escape($response) . "',
							'" . $orderSubTotal . "',
							'" . $orderTotal . "',
							'" . $taxAmount . "'
						) ";
        return $this->ObjDatabase->setQuery($sql);
    }
    public function updateOrderStatus($orderID = '', $orderStatus = '', $transactionID = '', $orderPaymentMethodName = '', $paymentStatus = '', $authValue = '', $masterOrderID = '') {
        $sql = "	UPDATE
							" . DB_PREFIX . "order_masters ordMas ";
        if ($orderID > 0)
            $sql .= " SET
							ordMas.masterOrderID='" . $orderID . "' ";
        else if ($masterOrderID > 0)
            $sql .= " SET
							ordMas.masterOrderID='" . $masterOrderID . "' ";
        if ($orderStatus != '') {
            $sql .= " ,ordMas.orderStatus='" . $orderStatus . "'";
        }
        if ($transactionID != '') {
            $sql .= " 	,
							ordMas.transactionID='" . $transactionID . "' ";
        }
        if ($orderPaymentMethodName != '') {
            $sql .= " 	,
							ordMas.orderPaymentMethodName='" . $this->escape($orderPaymentMethodName) . "' ";
        }
        if ($paymentStatus != '') {
            $sql .= " 	,
							ordMas.paymentStatus='" . $paymentStatus . "' ";
        }
        if ($authValue != '') {
            $sql .= " 	,
							ordMas.paymentType='" . $authValue . "' ";
        }
        if ($orderID > 0)
            $sql .= " WHERE
							ordMas.masterOrderID='" . $orderID . "'";
        else
            $sql .= " WHERE
							ordMas.masterOrderID='" . $masterOrderID . "'";
        $this->ObjDatabase->setQuery($sql);
        /* updating master order/payment status*/
        $this->updateMasterOrderStatus($masterOrderID, $paymentStatus, $orderStatus);
        /* updating master order/payment status*/
    }
    public function updateMasterOrderStatus($masterOrderID, $paymentStatus = '', $orderStatus = '') {
        $sql = "	UPDATE
								" . DB_PREFIX . "order_masters 
							SET
								masterOrderID='" . $masterOrderID . "' ";
        if ($paymentStatus)
            $sql .= "	,
								paymentStatus='" . $paymentStatus . "'";
        if ($orderStatus)
            $sql .= " 	,
								orderStatus='" . $orderStatus . "'";
        $sql .= " 	WHERE
								masterOrderID='" . $masterOrderID . "'";
        $this->ObjDatabase->setQuery($sql);
        $this->updateStatusChanegHistory($masterOrderID, 0, 0, $orderStatus, $paymentStatus);
    }
    public function updateOrderTransactionID($masterOrderID = 0, $transactionID = 0) {
        $sql = "	UPDATE
								" . DB_PREFIX . "order_masters 
							SET
								transactionID='" . $transactionID . "' 
							WHERE
								masterOrderID='" . $masterOrderID . "'";
        $this->ObjDatabase->setQuery($sql);
    }
    public function saveCCdata($POST, $userID) {
        $nameonCard           = $this->escape($POST['creditCardFirstName'] . " " . $POST['creditCardLastName']);
        $cardType             = $this->escape($POST['creditCardType']);
        $cardNumber           = $this->encodeString($this->escape($POST['creditCardNumber']));
        $expMonth             = $this->escape($POST['expMonth']);
        $expYear              = $this->escape($POST['expYear']);
        $sql                  = "	SELECT 
						*
					FROM
						" . DB_PREFIX . "users_cc_info
					WHERE
						userID='" . $userID . "'";
        $resSelectUsersCCInfo = $this->ObjDatabase->numberOfRecords($sql);
        if ($resSelectUsersCCInfo > 0) {
            $sql = "	UPDATE
							" . DB_PREFIX . "users_cc_info
						SET
							firstNameonCard='" . $this->escape($POST['creditCardFirstName']) . "',
							lastNameonCard='" . $this->escape($POST['creditCardLastName']) . "',
							cardType='$cardType',cardNumber='$cardNumber',
							expMonth='$expMonth',
							expYear='$expYear'
						WHERE
							userID='" . $userID . "'";
            $this->ObjDatabase->setQuery($sql);
        } else {
            $sql = "	INSERT INTO
							" . DB_PREFIX . "users_cc_info
							(
								userID,
								firstNameonCard,
								lastNameonCard,
								cardType,
								cardNumber,
								expMonth,
								expYear
							)
						VALUES
							(
								'$userID',
								'" . $this->escape($POST['creditCardFirstName']) . "',
								'" . $this->escape($POST['creditCardLastName']) . "',
								'$cardType',
								'$cardNumber',
								'$expMonth',
								'$expYear'
							)";
            $this->ObjDatabase->setQuery($sql);
        }
        return true;
    }
    public function logStart() {
        if (!$this->logFile) {
            $this->logFile = @fopen($this->logFileName, "a+");
        }
    }
    public function logFinish() {
        if ($this->logFile) {
            @fclose($this->logFile);
        }
    }
    public function log($s, $paymentProcessorID) {
        if ($this->logFile) {
            @fputs($this->logFile, date("Y/m/d-H:i:s") . " - " . $paymentProcessorID . " - " . $s . "\r\n\r\n");
            @fputs($this->logFile, "--------------------------------------------------\r\n\r\n");
        }
    }
    public function getSupportingCurrencyDetails($transactionCurrency = 0, $paymentMethodId = 0) {
        $ObjCurrency = new CurrencyModel();
        if ($paymentMethodId > 0) {
            $paymentCurrency = $this->getPaymentSettingByKey($paymentMethodId, 'payment_currency');
            $currencyArr     = '';
            if ($paymentCurrency != '') {
                $paymentCurrency = str_replace(", ", ",", $paymentCurrency);
                $currencyArr     = explode(",", $paymentCurrency);
            }
            if (count($currencyArr) < 1) {
                return array();
            } else {
                $currencyFlag             = false;
                $purchasedCurrencyDetails = $ObjCurrency->getCurrencyByCurrencyID($transactionCurrency);
                $currencyCode             = $purchasedCurrencyDetails['currencyCode'];
                $systemCurrencies         = $ObjCurrency->listCurrencyCodes();
                if (in_array($purchasedCurrencyDetails['currencyCode'], $currencyArr)) {
                    $array['currencyCode'] = $purchasedCurrencyDetails['currencyCode'];
                    $array['currencyID']   = $transactionCurrency;
                    $currencyFlag          = true;
                } else {
                    foreach ($systemCurrencies as $key => $currency) {
                        if (in_array($currency['currencyCode'], $currencyArr)) {
                            $currencyFlag          = true;
                            $array['currencyCode'] = $currency['currencyCode'];
                            $array['currencyID']   = $currency['currencyID'];
                            break;
                        }
                    }
                }
                if ($currencyFlag == true) {
                    return $array;
                } else {
                    return array();
                }
            }
        }
    }
    public function updateStatusChanegHistory($masterOrderID = 0, $orderNumber = 0, $transactionID = 0, $orderStatus = '', $paymentStatus = '') {
        if ($transactionID > 0) {
            $sql = "	SELECT
							extraData,
							NOW() as curtime,
							masterOrderID
						FROM
							" . DB_PREFIX . "order_masters
						WHERE
							transactionID='" . $transactionID . "'";
            $res = $this->ObjDatabase->readValues($sql);
            if (count($res) > 0) {
                foreach ($res as $key => $result) {
                    $extraData                                                 = unserialize($result['extraData']);
                    $count                                                     = (isset($extraData['statusChanegHistory']) ? count($extraData['statusChanegHistory']) : 0);
                    $extraData['statusChanegHistory'][$count]['orderStatus']   = $orderStatus;
                    $extraData['statusChanegHistory'][$count]['paymentStatus'] = $paymentStatus;
                    $extraData['statusChanegHistory'][$count]['date']          = $result['curtime'];
                    $serialize                                                 = $this->escape(serialize($extraData), false, false);
                    $sql                                                       = " UPDATE
										" . DB_PREFIX . "order_masters 
									SET
										extraData='" . $serialize . "' 
									WHERE
										masterOrderID=" . $result['masterOrderID'];
                    $this->ObjDatabase->setQuery($sql);
                }
            }
        } else if ($orderNumber > 0) {
            $sql = "	SELECT
							extraData,
							NOW() as curtime,
							masterOrderID
						FROM
							" . DB_PREFIX . "order_masters
						WHERE
							orderNumber='" . $orderNumber . "'";
            $res = $this->ObjDatabase->readValues($sql);
            if (count($res) > 0) {
                foreach ($res as $key => $result) {
                    $extraData                                                 = unserialize($result['extraData']);
                    $count                                                     = isset($extraData['statusChanegHistory']) ? count($extraData['statusChanegHistory']) : 0;
                    $extraData['statusChanegHistory'][$count]['orderStatus']   = $orderStatus;
                    $extraData['statusChanegHistory'][$count]['paymentStatus'] = $paymentStatus;
                    $extraData['statusChanegHistory'][$count]['date']          = $result['curtime'];
                    $serialize                                                 = $this->escape(serialize($extraData), false, false);
                    $sql                                                       = " UPDATE
										" . DB_PREFIX . "order_masters 
									SET
										extraData='" . $serialize . "' 
									WHERE
										masterOrderID=" . $result['masterOrderID'];
                    $this->ObjDatabase->setQuery($sql);
                }
            }
        } else if ($masterOrderID > 0) {
            $sql                                                       = "	SELECT
							extraData,
							NOW() as curtime,
							masterOrderID
						FROM
							" . DB_PREFIX . "order_masters
						WHERE
							masterOrderID=" . $masterOrderID;
            $result                                                    = $this->ObjDatabase->readValue($sql);
            $extraData                                                 = unserialize($result['extraData']);
            $count                                                     = isset($extraData['statusChanegHistory']) ? count($extraData['statusChanegHistory']) : 0;
            $extraData['statusChanegHistory'][$count]['orderStatus']   = $orderStatus;
            $extraData['statusChanegHistory'][$count]['paymentStatus'] = $paymentStatus;
            $extraData['statusChanegHistory'][$count]['date']          = $result['curtime'];
            $serialize                                                 = $this->escape(serialize($extraData), false, false);
            $sql                                                       = " UPDATE
								" . DB_PREFIX . "order_masters 
							SET
								extraData='" . $serialize . "' 
							WHERE
								masterOrderID=" . $result['masterOrderID'];
            $this->ObjDatabase->setQuery($sql);
        }
    }
    public function updateMerchantRefPayFort($orderID='',$merchant_reference='') {
        if(trim($orderID)!='' && trim($merchant_reference)!='') {
            $sql        = " UPDATE ".DB_PREFIX."order_masters SET payfortRefNo='$merchant_reference' WHERE masterOrderID ='" . $orderID . "'";
            $resUpdate  = $this->ObjDatabase->setQuery($sql);
        }

    }
     public function listPaymentSettingsApp($paymentMethodId = '') {
        $sql = "	SELECT 
                                                                paySet.paySetLabel,
								paySet.paySetKey,
								paySet.paySetValue 
							FROM 
								" . DB_PREFIX . "payment_settings AS paySet ";
        if ($paymentMethodId > 0) {
            $sql .= " 	WHERE paySet.paySetGroupID='" . $paymentMethodId . "'";
        }
        return $this->ObjDatabase->readValues($sql);
    }
    
    public function updateTempOrderID($tempOrderID,$masterOrderID) {
        $sql  = "update " . DB_PREFIX . "order_masters set tempOrderID='".$tempOrderID."' where masterOrderID='".$masterOrderID."'";
        $this->ObjDatabase->setQuery($sql);
        return true;
    }
}