Example #1
0
 /**
  * Changes the status of an order
  * @author pablo
  * @author soeren
  * @author Uli
  * 
  *
  * @param array $d
  * @return boolean
  */
 function order_status_update(&$d)
 {
     global $mosConfig_offset;
     $db = new ps_DB();
     $timestamp = time() + $mosConfig_offset * 60 * 60;
     $mysqlDatetime = date("Y-m-d G:i:s", $timestamp);
     if (empty($_REQUEST['include_comment'])) {
         $include_comment = "N";
     }
     // get the current order status
     $curr_order_status = @$d["current_order_status"];
     $notify_customer = empty($d['notify_customer']) ? "N" : $d['notify_customer'];
     if ($notify_customer == "Y") {
         $notify_customer = 1;
     } else {
         $notify_customer = 0;
     }
     $d['order_comment'] = empty($d['order_comment']) ? "" : $d['order_comment'];
     if (empty($d['order_item_id'])) {
         // When the order is set to "confirmed", we can capture
         // the Payment with authorize.net
         if ($curr_order_status == "P" && $d["order_status"] == "C") {
             $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
             $q .= "#__{vm}_order_payment.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_orders.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
             $db->query($q);
             $db->next_record();
             $payment_class = $db->f("payment_class");
             if ($payment_class == "ps_authorize") {
                 require_once CLASSPATH . "payment/ps_authorize.cfg.php";
                 if (AN_TYPE == 'AUTH_ONLY') {
                     require_once CLASSPATH . "payment/ps_authorize.php";
                     $authorize = new ps_authorize();
                     $d["order_number"] = $db->f("order_number");
                     if (!$authorize->capture_payment($d)) {
                         return false;
                     }
                 }
             }
         }
         /*
          * This is like the test above for delayed capture only
          * we (well, I - durian) don't think the credit card
          * should be captured until the item(s) are shipped.
          * In fact, VeriSign says not to capture the cards until
          * the item ships.  Maybe this behavior should be a
          * configurable item?
          *
          * When the order changes from Confirmed or Pending to
          * Shipped, perform the delayed capture.
          *
          * Restricted to PayFlow Pro for now.
          */
         if (($curr_order_status == "P" || $curr_order_status == "C") && $d["order_status"] == "S") {
             $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
             $q .= "#__{vm}_order_payment.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_orders.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
             $db->query($q);
             $db->next_record();
             $payment_class = $db->f("payment_class");
             if ($payment_class == "ps_pfp") {
                 require_once CLASSPATH . "payment/ps_pfp.cfg.php";
                 if (PFP_TYPE == 'A') {
                     require_once CLASSPATH . "payment/ps_pfp.php";
                     $pfp = new ps_pfp();
                     $d["order_number"] = $db->f("order_number");
                     if (!$pfp->capture_payment($d)) {
                         return false;
                     }
                 }
             }
         }
         /*
          * If a pending order gets cancelled, void the authorization.
          *
          * It might work on captured cards too, if we want to
          * void shipped orders.
          *
          * Restricted to PayFlow Pro for now.
          */
         if ($curr_order_status == "P" && $d["order_status"] == "X") {
             $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
             $q .= "#__{vm}_order_payment.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_orders.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
             $db->query($q);
             $db->next_record();
             $payment_class = $db->f("payment_class");
             if ($payment_class == "ps_pfp") {
                 require_once CLASSPATH . "payment/ps_pfp.cfg.php";
                 if (PFP_TYPE == 'A') {
                     require_once CLASSPATH . "payment/ps_pfp.php";
                     $pfp = new ps_pfp();
                     $d["order_number"] = $db->f("order_number");
                     if (!$pfp->void_authorization($d)) {
                         return false;
                     }
                 }
             }
         }
         $fields = array('order_status' => $d["order_status"], 'mdate' => $timestamp);
         $db->buildQuery('UPDATE', '#__{vm}_orders', $fields, "WHERE order_id='" . $db->getEscaped($d["order_id"]) . "'");
         $db->query();
         // Update the Order History.
         $fields = array('order_id' => $d["order_id"], 'order_status_code' => $d["order_status"], 'date_added' => $mysqlDatetime, 'customer_notified' => $notify_customer, 'comments' => $d['order_comment']);
         $db->buildQuery('INSERT', '#__{vm}_order_history', $fields);
         $db->query();
         // Do we need to re-update the Stock Level?
         if ((strtoupper($d["order_status"]) == "X" || strtoupper($d["order_status"]) == "R") && $curr_order_status != $d["order_status"]) {
             // Get the order items and update the stock level
             // to the number before the order was placed
             $q = "SELECT product_id, product_quantity FROM #__{vm}_order_item WHERE order_id='" . $db->getEscaped($d["order_id"]) . "'";
             $db->query($q);
             $dbu = new ps_DB();
             // Now update each ordered product
             while ($db->next_record()) {
                 $q = "UPDATE #__{vm}_product \r\n\t\t\t\t\t\t\tSET product_in_stock=product_in_stock+" . $db->f("product_quantity") . ",\r\n\t\t\t\t\t\t\t\tproduct_sales=product_sales-" . $db->f("product_quantity") . " \r\n\t\t\t\t\t\t\tWHERE product_id='" . $db->f("product_id") . "'";
                 $dbu->query($q);
             }
         }
         // Update the Order Items' status
         $q = "SELECT order_item_id FROM #__{vm}_order_item WHERE order_id=" . $db->getEscaped($d['order_id']);
         $db->query($q);
         $dbu = new ps_DB();
         while ($db->next_record()) {
             $item_id = $db->f("order_item_id");
             $fields = array('order_status' => $d["order_status"], 'mdate' => $timestamp);
             $dbu->buildQuery('UPDATE', '#__{vm}_order_item', $fields, "WHERE order_item_id='" . (int) $item_id . "'");
             $dbu->query();
         }
         if (ENABLE_DOWNLOADS == '1') {
             ##################
             ## DOWNLOAD MOD
             $this->mail_download_id($d);
         }
         if (!empty($notify_customer)) {
             $this->notify_customer($d);
         }
     } elseif (!empty($d['order_item_id'])) {
         $fields = array('order_status' => $d["order_status"], 'mdate' => $timestamp);
         $db->buildQuery('UPDATE', '#__{vm}_order_item', $fields, 'WHERE order_item_id=' . intval($d['order_item_id']));
         return $db->query() !== false;
     }
     return true;
 }
Example #2
0
 /**
  * Changes the status of an order
  * @author pablo
  * @author soeren
  * @author Uli
  * 
  *
  * @param array $d
  * @return boolean
  */
 function order_status_update(&$d)
 {
     global $mosConfig_offset;
     global $sess, $VM_LANG, $vmLogger;
     $db = new ps_DB();
     //$timestamp = time() + ($mosConfig_offset*60*60);  //Original
     $timestamp = time();
     //Custom
     //$mysqlDatetime = date("Y-m-d G:i:s",$timestamp);  //Original
     $mysqlDatetime = date("Y-m-d G:i:s", $timestamp + $mosConfig_offset * 60 * 60);
     //Custom
     if (empty($_REQUEST['include_comment'])) {
         $include_comment = "N";
     }
     // get the current order status
     $curr_order_status = @$d["current_order_status"];
     $notify_customer = empty($d['notify_customer']) ? "N" : $d['notify_customer'];
     if ($notify_customer == "Y") {
         $notify_customer = 1;
     } else {
         $notify_customer = 0;
     }
     $d['order_comment'] = empty($d['order_comment']) ? "" : $d['order_comment'];
     if (empty($d['order_item_id'])) {
         // When the order is set to "confirmed", we can capture
         // the Payment with authorize.net
         if ($curr_order_status == "P" && $d["order_status"] == "C") {
             $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
             $q .= "#__{vm}_order_payment.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_orders.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
             $db->query($q);
             $db->next_record();
             $payment_class = $db->f("payment_class");
             $d["order_number"] = $db->f("order_number");
             switch ($payment_class) {
                 case "ps_authorize":
                     require_once CLASSPATH . "payment/ps_authorize.cfg.php";
                     if (AN_TYPE == 'AUTH_ONLY') {
                         require_once CLASSPATH . "payment/ps_authorize.php";
                         $authorize = new ps_authorize();
                         if (!$authorize->capture_payment($d)) {
                             return false;
                         }
                     }
                     break;
                 default:
                     // default case for payment methods that allow to "capture" the payment
                     if (is_file(CLASSPATH . 'payment/' . basename($payment_class) . '.php')) {
                         require_once CLASSPATH . 'payment/' . basename($payment_class) . '.php';
                         if (!class_exists($payment_class)) {
                             break;
                         }
                         $paymentObj = new $payment_class();
                         if (!method_exists($paymentObj, 'capture_payment')) {
                             break;
                         }
                         if (!$paymentObj->capture_payment($d)) {
                             return false;
                         }
                     }
                     break;
             }
         }
         /*
          * This is like the test above for delayed capture only
          * we (well, I - durian) don't think the credit card
          * should be captured until the item(s) are shipped.
          * In fact, VeriSign says not to capture the cards until
          * the item ships.  Maybe this behavior should be a
          * configurable item?
          *
          * When the order changes from Confirmed or Pending to
          * Shipped, perform the delayed capture.
          *
          * Restricted to PayFlow Pro for now.
          */
         if (($curr_order_status == "P" || $curr_order_status == "C") && $d["order_status"] == "S") {
             $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
             $q .= "#__{vm}_order_payment.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_orders.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
             $db->query($q);
             $db->next_record();
             $payment_class = $db->f("payment_class");
             if ($payment_class == "payflow_pro") {
                 require_once CLASSPATH . "payment/payflow_pro.cfg.php";
                 if (PFP_TYPE == 'A') {
                     require_once CLASSPATH . "payment/payflow_pro.php";
                     $pfp = new ps_pfp();
                     $d["order_number"] = $db->f("order_number");
                     if (!$pfp->capture_payment($d)) {
                         return false;
                     }
                 }
             }
         }
         /**
          * Do capture when product is shipped
          */
         /*
         			 if(($curr_order_status == "P" || $curr_order_status == "C") && $d["order_status"]=="S")
         			 {
         				$q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
         				$q .= "#__{vm}_orders.order_id='".$db->getEscaped($d['order_id'])."' ";
         				$q .= "AND #__{vm}_orders.order_id=#__{vm}_order_payment.order_id ";
         				$q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
         				$db->query( $q );
         				$db->next_record();
         				$payment_class = strtolower(basename($db->f("payment_class")));
         				if( file_exists( CLASSPATH.'payment/'.$payment_class.'.php' )) {
         					require_once( CLASSPATH."payment/$payment_class.php");
         					$payment = new $payment_class();
         					$d["order_number"] = $db->f("order_number");
         					if( is_callable( array( $payment, 'capture_payment' ))) {
         						if( !$payment->capture_payment( $d )) {
         							return false;
         						}
         					}
         				}			 
         			 }*/
         /*
          * If a pending order gets cancelled, void the authorization.
          *
          * It might work on captured cards too, if we want to
          * void shipped orders.
          *
          */
         if ($curr_order_status == "P" && $d["order_status"] == "X") {
             $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
             $q .= "#__{vm}_order_payment.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_orders.order_id='" . $db->getEscaped($d['order_id']) . "' ";
             $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
             $db->query($q);
             $db->next_record();
             $payment_class = strtolower(basename($db->f("payment_class")));
             if (file_exists(CLASSPATH . 'payment/' . $payment_class . '.php')) {
                 require_once CLASSPATH . "payment/{$payment_class}.php";
                 $payment = new $payment_class();
                 $d["order_number"] = $db->f("order_number");
                 if (is_callable(array($payment, 'void_authorization'))) {
                     if (!$payment->void_authorization($d)) {
                         return false;
                     }
                 }
             }
         }
         // Do a Refund
         if ($d['order_status'] == 'R' && $curr_order_status != 'R') {
             $vmLogger->debug("Initiating Refund");
             $q = 'SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ';
             $q .= '#__{vm}_orders.order_id=\'' . $db->getEscaped($d['order_id']) . '\' ';
             $q .= 'AND #__{vm}_orders.order_id=#__{vm}_order_payment.order_id ';
             $q .= 'AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id';
             $db->query($q);
             $db->next_record();
             $payment_class = strtolower(basename($db->f("payment_class")));
             $vmLogger->debug('Payment Class: ' . $payment_class);
             if (file_exists(CLASSPATH . 'payment/' . $payment_class . '.php')) {
                 $vmLogger->debug('Found Payment Module');
                 require_once CLASSPATH . "payment/{$payment_class}.php";
                 $payment = new $payment_class();
                 $d["order_number"] = $db->f("order_number");
                 if (is_callable(array($payment, 'do_refund'))) {
                     $vmLogger->debug('Can call do_refund');
                     if (!$payment->do_refund($d)) {
                         $vmLogger->debug('failed to do refund');
                         return false;
                     }
                 }
             }
         }
         $fields = array('order_status' => $d["order_status"], 'mdate' => $timestamp);
         $db->buildQuery('UPDATE', '#__{vm}_orders', $fields, "WHERE order_id='" . $db->getEscaped($d["order_id"]) . "'");
         $db->query();
         // Update the Order History.
         $fields = array('order_id' => $d["order_id"], 'order_status_code' => $d["order_status"], 'date_added' => $mysqlDatetime, 'customer_notified' => $notify_customer, 'comments' => $d['order_comment']);
         $db->buildQuery('INSERT', '#__{vm}_order_history', $fields);
         $db->query();
         // Do we need to re-update the Stock Level?
         if ((strtoupper($d["order_status"]) == "X" || strtoupper($d["order_status"]) == "R") && $curr_order_status != $d["order_status"]) {
             // Get the order items and update the stock level
             // to the number before the order was placed
             $q = "SELECT product_id, product_quantity FROM #__{vm}_order_item WHERE order_id='" . $db->getEscaped($d["order_id"]) . "'";
             $db->query($q);
             $dbu = new ps_DB();
             require_once CLASSPATH . 'ps_product.php';
             // Now update each ordered product
             while ($db->next_record()) {
                 if (ENABLE_DOWNLOADS == '1' && ps_product::is_downloadable($db->f("product_id")) && VM_DOWNLOADABLE_PRODUCTS_KEEP_STOCKLEVEL == '1') {
                     $q = "UPDATE #__{vm}_product  \n\t\t\t\t\t\t\t\tSET product_sales=product_sales-" . $db->f("product_quantity") . " \n\t\t\t\t\t\t\tWHERE product_id=" . $db->f("product_id");
                     $dbu->query($q);
                 } else {
                     $q = "UPDATE #__{vm}_product \n\t\t\t\t\t\t\tSET product_in_stock=product_in_stock+" . $db->f("product_quantity") . ",\n\t\t\t\t\t\t\t\tproduct_sales=product_sales-" . $db->f("product_quantity") . " \n\t\t\t\t\t\t\tWHERE product_id=" . $db->f("product_id");
                     $dbu->query($q);
                 }
             }
         }
         // Update the Order Items' status
         $q = "SELECT order_item_id FROM #__{vm}_order_item WHERE order_id=" . $db->getEscaped($d['order_id']);
         $db->query($q);
         $dbu = new ps_DB();
         while ($db->next_record()) {
             $item_id = $db->f("order_item_id");
             $fields = array('order_status' => $d["order_status"], 'mdate' => $timestamp);
             $dbu->buildQuery('UPDATE', '#__{vm}_order_item', $fields, "WHERE order_item_id='" . (int) $item_id . "'");
             $dbu->query();
         }
         if (ENABLE_DOWNLOADS == '1') {
             ##################
             ## DOWNLOAD MOD
             $this->mail_download_id($d);
         }
         if (!empty($notify_customer)) {
             $this->notify_customer($d);
         }
     } elseif (!empty($d['order_item_id'])) {
         // Update the Order Items' status
         $q = "SELECT order_item_id, product_id, product_quantity FROM #__{vm}_order_item \n\t\t\t\t\t\t\tWHERE order_id=" . $db->getEscaped($d['order_id']) . ' AND order_item_id=' . intval($d['order_item_id']);
         $db->query($q);
         $item_product_id = $db->f('product_id');
         $item_product_quantity = $db->f('product_quantity');
         require_once CLASSPATH . 'ps_product.php';
         if (ENABLE_DOWNLOADS == '1' && ps_product::is_downloadable($item_product_id) && VM_DOWNLOADABLE_PRODUCTS_KEEP_STOCKLEVEL == '1') {
             $q = "UPDATE #__{vm}_product  \n\t\t\t\t\t\t\t\tSET product_sales=product_sales-" . $item_product_quantity . " \n\t\t\t\t\t\t\tWHERE product_id=" . $item_product_id;
             $db->query($q);
         } else {
             $q = "UPDATE #__{vm}_product \n\t\t\t\t\t\t\tSET product_in_stock=product_in_stock+" . $item_product_quantity . ",\n\t\t\t\t\t\t\t\tproduct_sales=product_sales-" . $item_product_quantity . " \n\t\t\t\t\t\t\tWHERE product_id=" . $item_product_id;
             $db->query($q);
         }
         $fields = array('order_status' => $d["order_status"], 'mdate' => $timestamp);
         $db->buildQuery('UPDATE', '#__{vm}_order_item', $fields, 'WHERE order_item_id=' . intval($d['order_item_id']));
         return $db->query() !== false;
     }
     return true;
 }
Example #3
0
	/**
     * Changes the status of an order
     * @author pablo
     * @author soeren
     * @author Uli
     *
     *
     * @param array $d
     * @return boolean
    */
	function order_status_update(&$d) {
		global $mosConfig_offset;
			global  $sess, $VM_LANG, $vmLogger;

		$db = new ps_DB;
		//$timestamp = time() + ($mosConfig_offset*60*60);  //Original
		$timestamp = time();  //Custom
		//$mysqlDatetime = date("Y-m-d G:i:s",$timestamp);  //Original
		$mysqlDatetime = date("Y-m-d G:i:s", $timestamp + ($mosConfig_offset*60*60));  //Custom

		if( empty($_REQUEST['include_comment'])) {
			$include_comment="N";
		}

		// get the current order status
		$curr_order_status = @$d["current_order_status"];
		$notify_customer = empty($d['notify_customer']) ? "N" : $d['notify_customer'];
		if( $notify_customer=="Y" ) {
			$notify_customer=1;
		}
		else {
			$notify_customer=0;
		}

		$d['order_comment'] = empty($d['order_comment']) ? "" : $d['order_comment'];
		if( empty($d['order_item_id']) ) {
			// When the order is set to "confirmed", we can capture
			// the Payment with authorize.net
			if( $curr_order_status=="P" && $d["order_status"]=="C") {
				$q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
				$q .= "#__{vm}_order_payment.order_id='".$db->getEscaped($d['order_id'])."' ";
				$q .= "AND #__{vm}_orders.order_id='".$db->getEscaped($d['order_id'])."' ";
				$q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
				$db->query( $q );
				$db->next_record();
				$payment_class = $db->f("payment_class");
				$d["order_number"] = $db->f("order_number");

				switch( $payment_class ) {
					case "ps_authorize":

						require_once( CLASSPATH."payment/ps_authorize.cfg.php");
						if( AN_TYPE == 'AUTH_ONLY' ) {
							require_once( CLASSPATH."payment/ps_authorize.php");
							$authorize = new ps_authorize();
							if( !$authorize->capture_payment( $d )) {
								return false;
							}
						}
						break;
					default:
							// default case for payment methods that allow to "capture" the payment
							if( is_file( CLASSPATH.'payment/'.basename($payment_class).'.php' ) ) {
								require_once( CLASSPATH.'payment/'.basename($payment_class).'.php' );
								if( !class_exists($payment_class)) break;
								$paymentObj = new $payment_class();

								if( !method_exists($paymentObj,'capture_payment')) break;

								if( !$paymentObj->capture_payment( $d )) {
									return false;
								}
							}
							break;
				}
			}
			/*
			 * This is like the test above for delayed capture only
			 * we (well, I - durian) don't think the credit card
			 * should be captured until the item(s) are shipped.
			 * In fact, VeriSign says not to capture the cards until
			 * the item ships.  Maybe this behavior should be a
			 * configurable item?
			 *
			 * When the order changes from Confirmed or Pending to
			 * Shipped, perform the delayed capture.
			 *
			 * Restricted to PayFlow Pro for now.
			 */
			if( ($curr_order_status=="P" || $curr_order_status=="C") && $d["order_status"]=="S") {
				$q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
				$q .= "#__{vm}_order_payment.order_id='".$db->getEscaped($d['order_id'])."' ";
				$q .= "AND #__{vm}_orders.order_id='".$db->getEscaped($d['order_id'])."' ";
				$q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
				$db->query( $q );
				$db->next_record();
				$payment_class = $db->f("payment_class");
				if( $payment_class=="payflow_pro" ) {
					require_once( CLASSPATH."payment/payflow_pro.cfg.php");
					if( PFP_TYPE == 'A' ) {
						require_once( CLASSPATH."payment/payflow_pro.php");
						$pfp = new ps_pfp();
						$d["order_number"] = $db->f("order_number");
						if( !$pfp->capture_payment( $d )) {
							return false;
						}
					}
				}
			}

			/**
			 * Do capture when product is shipped
			 */
			 /*
			 if(($curr_order_status == "P" || $curr_order_status == "C") && $d["order_status"]=="S")
			 {
				$q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
				$q .= "#__{vm}_orders.order_id='".$db->getEscaped($d['order_id'])."' ";
				$q .= "AND #__{vm}_orders.order_id=#__{vm}_order_payment.order_id ";
				$q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
				$db->query( $q );
				$db->next_record();
				$payment_class = strtolower(basename($db->f("payment_class")));
				if( file_exists( CLASSPATH.'payment/'.$payment_class.'.php' )) {
					require_once( CLASSPATH."payment/$payment_class.php");
					$payment = new $payment_class();
					$d["order_number"] = $db->f("order_number");
					if( is_callable( array( $payment, 'capture_payment' ))) {
						if( !$payment->capture_payment( $d )) {
							return false;
						}
					}
				}
			 }*/

			/*
			 * If a pending order gets cancelled, void the authorization.
			 *
			 * It might work on captured cards too, if we want to
			 * void shipped orders.
			 *
			 */
			if( $curr_order_status=="P" && $d["order_status"]=="X") {
				$q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
				$q .= "#__{vm}_order_payment.order_id='".$db->getEscaped($d['order_id'])."' ";
				$q .= "AND #__{vm}_orders.order_id='".$db->getEscaped($d['order_id'])."' ";
				$q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
				$db->query( $q );
				$db->next_record();
				$payment_class = strtolower(basename($db->f("payment_class")));
				if( file_exists( CLASSPATH.'payment/'.$payment_class.'.php' )) {
					require_once( CLASSPATH."payment/$payment_class.php");
					$payment = new $payment_class();
					$d["order_number"] = $db->f("order_number");
					if( is_callable( array( $payment, 'void_authorization' ))) {
						if( !$payment->void_authorization( $d )) {
							return false;
						}
					}
				}
			}

			// Do a Refund
			if( $d['order_status']=='R' && $curr_order_status != 'R') {
				$vmLogger->debug("Initiating Refund");
				$q = 'SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ';
				$q .= '#__{vm}_orders.order_id=\''.$db->getEscaped($d['order_id']).'\' ';
				$q .= 'AND #__{vm}_orders.order_id=#__{vm}_order_payment.order_id ';
				$q .= 'AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id';
				$db->query( $q );
				$db->next_record();
				$payment_class = strtolower(basename($db->f("payment_class")));
				$vmLogger->debug('Payment Class: '.$payment_class);
				if( file_exists( CLASSPATH.'payment/'.$payment_class.'.php' )) {
					$vmLogger->debug('Found Payment Module');
					require_once( CLASSPATH."payment/$payment_class.php");
					$payment = new $payment_class();
					$d["order_number"] = $db->f("order_number");
					if( is_callable( array( $payment, 'do_refund' )))
					{
						$vmLogger->debug('Can call do_refund');
						if( !$payment->do_refund( $d )) {
							$vmLogger->debug('failed to do refund');
							return false;
						}
					}
				}
			}

			$fields =array( 'order_status'=> $d["order_status"],
										'mdate'=> $timestamp );
			$db->buildQuery('UPDATE', '#__{vm}_orders', $fields, "WHERE order_id='" . $db->getEscaped($d["order_id"]) . "'");
			$db->query();

			// Update the Order History.
			$fields = array( 'order_id' => $d["order_id"],
										'order_status_code' => $d["order_status"],
										'date_added' => $mysqlDatetime,
										'customer_notified' => $notify_customer,
										'comments' => $d['order_comment']
							);
			$db->buildQuery('INSERT', '#__{vm}_order_history', $fields );
			$db->query();

			// Do we need to re-update the Stock Level?
			if( (strtoupper($d["order_status"]) == "X" || strtoupper($d["order_status"])=="R")
				// && CHECK_STOCK == '1'
				&& $curr_order_status != $d["order_status"]
				) {
				// Get the order items and update the stock level
				// to the number before the order was placed
				$q = "SELECT product_id, product_quantity FROM #__{vm}_order_item WHERE order_id='".$db->getEscaped($d["order_id"])."'";
				$db->query( $q );
				$dbu = new ps_DB;
				require_once( CLASSPATH.'ps_product.php');
				// Now update each ordered product
				while( $db->next_record() ) {
					if( ENABLE_DOWNLOADS == '1' && ps_product::is_downloadable($db->f("product_id")) && VM_DOWNLOADABLE_PRODUCTS_KEEP_STOCKLEVEL == '1') {
						$q = "UPDATE #__{vm}_product
								SET product_sales=product_sales-".$db->f("product_quantity")."
							WHERE product_id=".$db->f("product_id");
						$dbu->query( $q );
					}
					else {
						$q = "UPDATE #__{vm}_product
							SET product_in_stock=product_in_stock+".$db->f("product_quantity").",
								product_sales=product_sales-".$db->f("product_quantity")."
							WHERE product_id=".$db->f("product_id");
						$dbu->query( $q );
					}
				}
			}
			// Update the Order Items' status
			$q = "SELECT order_item_id FROM #__{vm}_order_item WHERE order_id=".$db->getEscaped($d['order_id']);
			$db->query($q);
			$dbu = new ps_DB;
			while ($db->next_record()) {
				$item_id = $db->f("order_item_id");
				$fields =array( 'order_status'=> $d["order_status"],
											'mdate'=> $timestamp );
				$dbu->buildQuery('UPDATE', '#__{vm}_order_item', $fields, "WHERE order_item_id='" .(int)$item_id . "'");
				$dbu->query();
			}
		    if ($d["order_status"] == "C" || $d["order_status"] == "P")
            {
                //////////////////////////////////////////////////////////////////////////
                // OSE  added
                //////////////////////////////////////////////////////////////////////////
                //////////////Joomla Database Class//////////////
               require_once(JPATH_SITE.DS.'components'.DS.'com_osemsc'.DS.'init.php');
               require_once(JPATH_SITE.DS.'components'.DS.'com_osemsc'.DS.'helpers'.DS.'oseMscPublic.php');
                $jdb = &JFactory::getDBO();
                $jquery = "SELECT user_id FROM `#__vm_orders` WHERE `order_id` = '" . $db->getEscaped($d['order_id']) . "'";
                $jdb->setQuery($jquery);
                $user_id = $jdb->loadResult();
                $jquery = "SELECT a.product_id, b.category_id FROM `#__vm_order_item` as a, `#__vm_product_category_xref` as b WHERE a.`order_id` = '" . $db->getEscaped($d['order_id']) . "' AND a.`product_id` = b.`product_id`";
                $jdb->setQuery($jquery);
                $results = $jdb->loadObjectList();
				$cart = oseMscPublic::getCart();
                if (empty($results))
                {
                    $jquery = "SELECT a.product_id, b.category_id, c.product_parent_id FROM `#__vm_order_item` as a, `#__vm_product_category_xref` as b, `#__vm_product` as c WHERE a.`order_id` = '" . $db->getEscaped($d['order_id']) . "' AND a.`product_id` = c.`product_id` AND b.`product_id` = c.`product_parent_id`";
                    $jdb->setQuery($jquery);
                    $results = $jdb->loadObjectList();
                }

                foreach ($results as $result)
                {
                    //////////////////////////////////////////////////
                    $product_id = $result->product_id;
                    $jquery = "SELECT * FROM `#__osemsc_ext` WHERE `type` = 'vm'";
                    $jdb->setQuery($jquery);
                    $jdb->query();
                    $rows = $jdb->loadObjectList();
                    foreach ($rows as $row)
                    {
                        $msc_data = oseJson::decode($row->params);
                        $left_var = "";
                        $right_var = "";
                        if ($msc_data->category_id > 0)
                        {
                            $left_var = $result->category_id;
                            $right_var = $msc_data->category_id;
                        }
                        else
                        {
                            $left_var = $result->product_id;
                            $right_var = $msc_data->product_id;
                        }
                        if ($left_var == $right_var)
                        {
                        	$msc_id = $row->id;
                            $query = "SELECT order_currency FROM `#__vm_orders` WHERE `order_id` = ".(int)$d["order_id"];
                            $db->setQuery($query);
                            $currency = $db->loadResult();

                            if ($d["order_status"] == "C")
                            {
					            $member= oseRegistry :: call('member');
            				    $member->instance($user_id, 'member_id');

            					
            					// get current item
			            		$cart->updateParams('payment_mode','m');
				            	$paymentInfo = oseRegistry::call('msc')->getPaymentMscInfo($msc_id,$currency,0);
								$nItem = array('entry_id'=>$msc_id,'entry_type'=>'msc','msc_option'=>oseObject::getValue($paymentInfo,'msc_option'));
								$cart->addItem($nItem['entry_id'],$nItem['entry_type'],$nItem);

								$cart->update();

								// join msc
				            	oseRegistry::call('msc')->runAddonAction('register.payment.save',array('member_id'=>$user_id,'payment_method'=>'none'), true, false);
								$order_id = JRequest::getInt('order_id',0);

								oseRegistry::call('payment')->getInstance('Order')->confirmOrder($order_id, array());
                            }
                            else
                            {
                            	$msc_id= $row->id;
								$member= oseRegistry :: call('member');
            				    $member->instance($user_id, 'member_id');
								$params = $member->getAddonParams($msc_id,$user_id,0,$params = array());
								$updated= $msc->runAddonAction('member.msc.cancelMsc', $params);
                            }
                        }
                    }
                }
                //////////////////////////////////////////////////////////////////////////
            }
			if (ENABLE_DOWNLOADS == '1') {
				##################
				## DOWNLOAD MOD
				$this->mail_download_id( $d );
			}

			if( !empty($notify_customer) ) {
				$this->notify_customer( $d );
			}
		} elseif( !empty($d['order_item_id'])) {
				// Update the Order Items' status
				$q = "SELECT order_item_id, product_id, product_quantity FROM #__{vm}_order_item
							WHERE order_id=".$db->getEscaped($d['order_id'])
						. ' AND order_item_id='.intval( $d['order_item_id'] );
				$db->query($q);
				$item_product_id = $db->f('product_id');
				$item_product_quantity = $db->f('product_quantity');
				require_once( CLASSPATH. 'ps_product.php' );
				if( ENABLE_DOWNLOADS == '1' && ps_product::is_downloadable($item_product_id) && VM_DOWNLOADABLE_PRODUCTS_KEEP_STOCKLEVEL == '1') {
						$q = "UPDATE #__{vm}_product
								SET product_sales=product_sales-".$item_product_quantity."
							WHERE product_id=".$item_product_id;
						$db->query( $q );
					}
					else {
						$q = "UPDATE #__{vm}_product
							SET product_in_stock=product_in_stock+".$item_product_quantity.",
								product_sales=product_sales-".$item_product_quantity."
							WHERE product_id=".$item_product_id;
						$db->query( $q );
				}

				$fields =array( 'order_status'=> $d["order_status"],
											'mdate'=> $timestamp );
				$db->buildQuery('UPDATE', '#__{vm}_order_item', $fields, 'WHERE order_item_id='.intval( $d['order_item_id'] ));
				return $db->query() !== false;
		}
		return true;
	}