/** * Validate Coupon Code * * @return unknown_type */ function validateCouponCode() { JLoader::import('com_tienda.library.json', JPATH_ADMINISTRATOR . '/components'); $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', JRequest::getVar('elements', '', 'post', 'string'))); // convert elements to array that can be binded Tienda::load('TiendaHelperBase', 'helpers._base'); $helper = TiendaHelperBase::getInstance(); $values = $helper->elementsToArray($elements); $coupon_code = JRequest::getVar('coupon_code', ''); $response = array(); $response['msg'] = ''; $response['error'] = ''; // check if coupon code is valid $user_id = JFactory::getUser()->id; Tienda::load('TiendaHelperCoupon', 'helpers.coupon'); $helper_coupon = new TiendaHelperCoupon(); $coupon = $helper_coupon->isValid($coupon_code, 'code', $user_id); if (!$coupon) { $response['error'] = '1'; $response['msg'] = $helper->generateMessage($helper_coupon->getError()); echo json_encode($response); return; } if (!empty($values['coupons']) && in_array($coupon->coupon_id, $values['coupons'])) { $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_TIENDA_THIS_COUPON_ALREADY_ADDED_TO_THE_ORDER')); echo json_encode($response); return; } // TODO Check that the user can add this coupon to the order $can_add = true; if (!$can_add) { $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_TIENDA_CANNOT_ADD_THIS_COUPON_TO_ORDER')); echo json_encode($response); return; } // Check per product coupon code $ids = array(); $items = TiendaHelperCarts::getProductsInfo(); foreach ($items as $item) { $ids[] = $item->product_id; } if ($coupon->coupon_type == '1') { $check = $helper_coupon->checkByProductIds($coupon->coupon_id, $ids); if (!$check) { $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_TIENDA_THIS_COUPON_NOT_RELATED_TO_PRODUCT_IN_YOUR_CART')); echo json_encode($response); return; } } // if valid, return the html for the coupon $response['msg'] = " <input type='hidden' name='coupons[]' value='{$coupon->coupon_id}'>"; echo json_encode($response); return; }
/** * Validates the credit amount and applies it to the order * @return unknown_type */ function validateApplyCredit() { JLoader::import('com_tienda.library.json', JPATH_ADMINISTRATOR . '/components'); $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', JRequest::getVar('elements', '', 'post', 'string'))); // convert elements to array that can be binded Tienda::load('TiendaHelperBase', 'helpers._base'); $helper = TiendaHelperBase::getInstance(); $values = $helper->elementsToArray($elements); $user_id = $this->user->id; $apply_credit_amount = (double) JRequest::getVar('apply_credit_amount', ''); $response = array(); $response['msg'] = ''; $response['error'] = ''; // is the credit amount valid (i.e. greater than 0, if ($apply_credit_amount < (double) '0.00') { $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_TIENDA_PLEASE_SPECIFY_VALID_CREDIT_AMOUNT')); echo json_encode($response); return; } // less than/== their available amount & order amount? $userinfo = JTable::getInstance('UserInfo', 'TiendaTable'); $userinfo->load(array('user_id' => $user_id)); $userinfo->credits_total = (double) $userinfo->credits_total; if ($apply_credit_amount > $userinfo->credits_total) { $apply_credit_amount = $userinfo->credits_total; } // get the order object so we can populate it $order = $this->_order; // a TableOrders object (see constructor) // bind what you can from the post $order->bind($values); // unset the order credit because it may have been set by the bind $order->order_credit = '0'; // set the currency Tienda::load('TiendaHelperCurrency', 'helpers.currency'); $order->currency_id = TiendaHelperCurrency::getCurrentCurrency(); // USD is default if no currency selected // set the shipping method $order->shipping = new JObject(); $order->shipping->shipping_price = @$values['shipping_price']; $order->shipping->shipping_extra = @$values['shipping_extra']; $order->shipping->shipping_name = @$values['shipping_name']; $order->shipping->shipping_tax = @$values['shipping_tax']; // set the addresses $this->setAddresses($values, false, true); // get the items and add them to the order Tienda::load('TiendaHelperCarts', 'helpers.carts'); $items = TiendaHelperCarts::getProductsInfo(); foreach ($items as $item) { $order->addItem($item); } // get all coupons and add them to the order if (!empty($values['coupons'])) { foreach ($values['coupons'] as $coupon_id) { $coupon = JTable::getInstance('Coupons', 'TiendaTable'); $coupon->load(array('coupon_id' => $coupon_id)); $order->addCoupon($coupon); } } $this->addAutomaticCoupons(); // get the order totals $order->calculateTotals(); if ($apply_credit_amount > $order->order_total) { $apply_credit_amount = $order->order_total; } // if valid, return the html for the credit $response['msg'] = "<input type='hidden' name='order_credit' value='{$apply_credit_amount}'>"; echo json_encode($response); return; }
/** * Second prepayment * * @param object $order order to process * @return string html to display */ function _secondPrePayment() { //error_reporting(E_ALL); // Prepare order JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); // Use AJAX to show plugins that are available JLoader::import('com_tienda.library.json', JPATH_ADMINISTRATOR . '/components'); $guest = JRequest::getVar('guest', '0'); if ($guest == '1' && Tienda::getInstance()->get('guest_checkout_enabled')) { $guest = true; } else { $guest = false; } $order = JTable::getInstance('Orders', 'TiendaTable'); // set the currency $order->currency_id = Tienda::getInstance()->get('default_currencyid', '1'); // USD is default if no currency selected // set the shipping method $order->shipping_method_id = Tienda::getInstance()->get('defaultShippingMethod', '2'); if (!$guest) { // set the order's addresses based on the form inputs // set to user defaults Tienda::load('TiendaHelperUser', 'helpers.user'); $order->user_id = JFactory::getUser()->id; $billing_address = TiendaHelperUser::getPrimaryAddress($order->user_id, 'billing'); $shipping_address = TiendaHelperUser::getPrimaryAddress($order->user_id, 'shipping'); $order->setAddress($billing_address, 'billing'); $order->setAddress($shipping_address, 'shipping'); } // get the items and add them to the order Tienda::load('TiendaHelperCarts', 'helpers.carts'); $items = TiendaHelperCarts::getProductsInfo(); $order->ip_address = $_SERVER['REMOTE_ADDR']; $order->order_state_id = '15'; $order->addItem($items[0]); $order->calculateTotals(); $order->save(); $items[0]->order_id = $order->order_id; $items[0]->orderitem_tax = $order->order_tax; $items[0]->save(); // get the order totals JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); $row = JTable::getInstance('OrderInfo', 'TiendaTable'); $row->order_id = $order->order_id; $row->user_email = JFactory::getUser()->get('email'); // Get Addresses $row->billing_first_name = $billing_address->first_name; $row->billing_last_name = $billing_address->last_name; $row->billing_middle_name = $billing_address->middle_name; $row->billing_phone1 = $billing_address->phone1; $row->billing_phone2 = $billing_address->phone2; $row->billing_fax = $billing_address->fax; $row->billing_address_1 = $billing_address->address_1; $row->billing_address_2 = $billing_address->address_2; $row->billing_city = $billing_address->city; $row->billing_zone_name = $billing_address->zone_name; $row->billing_country_name = $billing_address->country_name; $row->shipping_first_name = $shipping_address->first_name; $row->shipping_last_name = $shipping_address->last_name; $row->shipping_middle_name = $shipping_address->middle_name; $row->shipping_phone1 = $shipping_address->phone1; $row->shipping_phone2 = $shipping_address->phone2; $row->shipping_fax = $shipping_address->fax; $row->shipping_address_1 = $shipping_address->address_1; $row->shipping_address_2 = $shipping_address->address_2; $row->shipping_city = $shipping_address->city; $row->shipping_zone_name = $shipping_address->zone_name; $row->shipping_country_name = $shipping_address->country_name; // set zones and countries $row->billing_zone_id = $billing_address->zone_id; $row->billing_country_id = $billing_address->country_id; $row->shipping_zone_id = $shipping_address->zone_id; $row->shipping_country_id = $shipping_address->country_id; if (!$row->save()) { $this->setError($row->getError()); } $order->orderinfo = $row; $order->save(); $orderpayment_type = $this->_element; $transaction_status = JText::_('COM_TIENDA_INCOMPLETE'); // in the case of orders with a value of 0.00, use custom values if ((double) $order->order_total == (double) '0.00') { $orderpayment_type = 'free'; $transaction_status = JText::_('COM_TIENDA_COMPLETE'); } // Save an orderpayment with an Incomplete status JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); $orderpayment = JTable::getInstance('OrderPayments', 'TiendaTable'); $orderpayment->order_id = $order->order_id; $orderpayment->orderpayment_type = $orderpayment_type; // this is the payment plugin selected $orderpayment->transaction_status = $transaction_status; // payment plugin updates this field onPostPayment $orderpayment->orderpayment_amount = $order->order_total; // this is the expected payment amount. payment plugin should verify actual payment amount against expected payment amount $orderpayment->save(); $vars = $this->prepareSecondVars($order, $orderpayment); $html = $this->_getLayout('secondpayment', $vars); $html .= $this->_getLayout('prepayment', $vars); return $html; }
public function prepare($values, $options = array(), &$order = null) { if (empty($order)) { $order = $this->getTable(); } $this->_order =& $order; $this->_values = $values; $this->_options = $options; if (empty($options['skip_adjust_credits'])) { $order->_adjustCredits = true; // this is not a POS order, so adjust the user's credits (if any used) } $order->bind($values); $order->user_id = $values['user_id']; $order->ip_address = $values['ip_address']; //$_SERVER['REMOTE_ADDR']; // set the currency if (empty($values['currency_id'])) { Tienda::load('TiendaHelperCurrency', 'helpers.currency'); $order->currency_id = TiendaHelperCurrency::getCurrentCurrency(); } // Store the text verion of the currency for order integrity Tienda::load('TiendaHelperOrder', 'helpers.order'); $order->order_currency = TiendaHelperOrder::currencyToParameters($order->currency_id); $saveAddressesToDB = empty($options["save_addresses"]) ? false : true; $this->setAddresses($values, $saveAddressesToDB); // set the shipping method if (@$values['shippingrequired'] || !empty($values['shipping_plugin'])) { $order->shipping = new JObject(); $order->shipping->shipping_price = $values['shipping_price']; $order->shipping->shipping_extra = $values['shipping_extra']; $order->shipping->shipping_name = $values['shipping_name']; $order->shipping->shipping_tax = $values['shipping_tax']; } if (empty($options['skip_add_items'])) { //get the items from the current user's cart and add them to the order Tienda::load('TiendaHelperCarts', 'helpers.carts'); $reviewitems = TiendaHelperCarts::getProductsInfo(); foreach ($reviewitems as $reviewitem) { $order->addItem($reviewitem); } } if (empty($options['skip_add_coupons'])) { $this->addCoupons($values); } if (empty($options['skip_add_credit']) && !empty($values['order_credit'])) { $order->addCredit($values['order_credit']); } $order->order_state_id = empty($values['orderstate_id']) ? $this->initial_order_state : $values['orderstate_id']; $order->calculateTotals(); $order->getShippingTotal(); $order->getInvoiceNumber(); return $order; }
} require_once dirname(__FILE__) . '/helper.php'; // include lang files $lang = JFactory::getLanguage(); $lang->load('com_tienda', JPATH_BASE); $lang->load('com_tienda', JPATH_ADMINISTRATOR); $mainframe = JFactory::getApplication(); $document = JFactory::getDocument(); // params $display_null = $params->get('display_null', '1'); $null_text = $params->get('null_text', 'No Items in Your Cart'); $isAjax = $mainframe->getUserState('mod_usercart.isAjax'); $ajax = $isAjax == '1'; // Grab the cart Tienda::load('TiendaHelperCarts', 'helpers.carts'); $items = TiendaHelperCarts::getProductsInfo(); $num = count($items); // Convert the cart to a "fake" order, to show totals and others things JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); $orderTable = JTable::getInstance('Orders', 'TiendaTable'); foreach ($items as $item) { $orderTable->addItem($item); } $items = $orderTable->getItems(); Tienda::load('Tienda', 'defines'); $config = Tienda::getInstance(); $show_tax = $config->get('display_prices_with_tax'); if ($show_tax) { Tienda::load('TiendaHelperUser', 'helpers.user'); $geozones = TiendaHelperUser::getGeoZones(JFactory::getUser()->id); if (empty($geozones)) {