Beispiel #1
0
 /**
  * This method occurs after payment is attempted,
  * and fires the onPostPayment plugin event
  *
  * @return unknown_type
  */
 function confirmPayment()
 {
     $app = JFactory::getApplication();
     $orderpayment_type = $app->input->getString('orderpayment_type');
     // Get post values
     $values = $app->input->getArray($_POST);
     //backward compatibility for payment plugins
     foreach ($values as $name => $value) {
         $app->input->set($name, $value);
     }
     //set the guest mail to null if it is present
     //check if it was a guest checkout
     $account = $this->session->get('account', 'register', 'k2store');
     // get the order_id from the session set by the prePayment
     $orderpayment_id = (int) $app->getUserState('k2store.orderpayment_id');
     if ($account != 'guest') {
         $order_link = 'index.php?option=com_k2store&view=orders&task=view&id=' . $orderpayment_id;
     } else {
         $guest_token = $app->getUserState('k2store.order_token');
         $order_link = 'index.php?option=com_k2store&view=orders&task=view';
         //assign to another session variable, for security reasons
         if ($this->session->has('guest', 'k2store')) {
             $guest = $this->session->get('guest', array(), 'k2store');
             $this->session->set('guest_order_email', $guest['billing']['email']);
             $this->session->set('guest_order_token', $guest_token);
         }
     }
     $dispatcher = JDispatcher::getInstance();
     JPluginHelper::importPlugin('k2store');
     $html = "";
     $order = $this->_order;
     $order->load(array('id' => $orderpayment_id));
     // free product? set the state to confirmed and save the order.
     if (!empty($orderpayment_id) && (double) $order->order_total == (double) '0.00') {
         $order->order_state = trim(JText::_('CONFIRMED'));
         $order->order_state_id = '1';
         // PAYMENT RECEIVED.
         if ($order->save()) {
             // remove items from cart
             K2StoreHelperCart::removeOrderItems($order->id);
         }
         //send email
         require_once JPATH_SITE . '/components/com_k2store/helpers/orders.php';
         K2StoreOrdersHelper::sendUserEmail($order->user_id, $order->order_id, $order->transaction_status, $order->order_state, $order->order_state_id);
     } else {
         // get the payment results from the payment plugin
         $results = $dispatcher->trigger("onK2StorePostPayment", array($orderpayment_type, $values));
         // Display whatever comes back from Payment Plugin for the onPrePayment
         for ($i = 0; $i < count($results); $i++) {
             $html .= $results[$i];
         }
         // re-load the order in case the payment plugin updated it
         $order->load(array('id' => $orderpayment_id));
     }
     // $order_id would be empty on posts back from Paypal, for example
     if (isset($orderpayment_id)) {
         //unset a few things from the session.
         $this->session->clear('shipping_method', 'k2store');
         $this->session->clear('shipping_methods', 'k2store');
         $this->session->clear('shipping_values', 'k2store');
         $this->session->clear('payment_method', 'k2store');
         $this->session->clear('payment_methods', 'k2store');
         $this->session->clear('payment_values', 'k2store');
         $this->session->clear('guest', 'k2store');
         $this->session->clear('customer_note', 'k2store');
         //save the coupon to the order_coupons table for tracking and unset session.
         if ($this->session->has('coupon', 'k2store')) {
             $coupon_info = K2StoreHelperCart::getCoupon($this->session->get('coupon', '', 'k2store'));
             if ($coupon_info) {
                 $order_coupons = JTable::getInstance('OrderCoupons', 'Table');
                 $order_coupons->set('coupon_id', $coupon_info->coupon_id);
                 $order_coupons->set('orderpayment_id', $orderpayment_id);
                 $order_coupons->set('customer_id', JFactory::getUser()->id);
                 $order_coupons->set('amount', $order->order_discount);
                 $order_coupons->set('created_date', JFactory::getDate()->toSql());
                 $order_coupons->store();
             }
         }
         //clear the session
         $this->session->clear('coupon', 'k2store');
         //trigger onAfterOrder plugin event
         $results = $dispatcher->trigger("onK2StoreAfterPayment", array($order));
         foreach ($results as $result) {
             $html .= $result;
         }
         // Set display
         $view = $this->getView('checkout', 'html');
         $view->setLayout('postpayment');
         $view->set('_doTask', true);
         $params = $params = JComponentHelper::getParams('com_k2store');
         if ($params->get('show_postpayment_orderlink', 1)) {
             $view->assign('order_link', JRoute::_($order_link));
         }
         $view->assign('plugin_html', $html);
         // Get and Set Model
         $model = $this->getModel('checkout');
         $view->setModel($model, true);
         $view->display();
     }
     return;
 }
Beispiel #2
0
 function getTotals()
 {
     $app = JFactory::getApplication();
     $session = JFactory::getSession();
     $tax = new K2StoreTax();
     $products = $this->getDataNew();
     $total_data = array();
     $total = 0;
     //products
     $total_data['products'] = $products;
     //sub total
     $total_data['subtotal'] = K2StoreHelperCart::getSubtotal();
     $total += $total_data['subtotal'];
     //taxes
     $tax_data = array();
     $taxes = K2StoreHelperCart::getTaxes();
     //coupon
     if ($session->has('coupon', 'k2store')) {
         $coupon_info = K2StoreHelperCart::getCoupon($session->get('coupon', '', 'k2store'));
         if ($coupon_info) {
             $discount_total = 0;
             if (!$coupon_info->product) {
                 $sub_total = K2StoreHelperCart::getSubTotal();
             } else {
                 $sub_total = 0;
                 foreach ($products as $product) {
                     if (in_array($product['product_id'], $coupon_info->product)) {
                         $sub_total += $product['total'];
                     }
                 }
             }
             if ($coupon_info->value_type == 'F') {
                 $coupon_info->value = min($coupon_info->value, $sub_total);
             }
             foreach ($products as $product) {
                 $discount = 0;
                 if (!$coupon_info->product) {
                     $status = true;
                 } else {
                     if (in_array($product['product_id'], $coupon_info->product)) {
                         $status = true;
                     } else {
                         $status = false;
                     }
                 }
                 if ($status) {
                     if ($coupon_info->value_type == 'F') {
                         $discount = $coupon_info->value * ($product['total'] / $sub_total);
                     } elseif ($coupon_info->value_type == 'P') {
                         $discount = $product['total'] / 100 * $coupon_info->value;
                     }
                     if ($product['tax_profile_id']) {
                         $tax_rates = $this->tax->getRateArray($product['total'] - ($product['total'] - $discount), $product['tax_profile_id']);
                         foreach ($tax_rates as $tax_rate) {
                             //	if ($tax_rate['value_type'] == 'P') {
                             $taxes[$tax_rate['taxrate_id']] -= $tax_rate['amount'];
                             //	}
                         }
                     }
                 }
                 $discount_total += $discount;
             }
             $total_data['coupon'] = array('title' => JText::sprintf('K2STORE_COUPON_TITLE', $session->get('coupon', '', 'k2store')), 'value' => -$discount_total);
             //$total_data['coupon'] = $coupon_data;
             //less the coupon discount in the total
             $total -= $discount_total;
         }
     }
     if ($session->has('shipping_values', 'k2store')) {
         $shipping = $session->get('shipping_values', array(), 'k2store');
         if (count($shipping) && isset($shipping['shipping_name'])) {
             $total_data['shipping_amount'] = $shipping['shipping_price'] + $shipping['shipping_extra'];
             $total_data['shipping_tax'] = $shipping['shipping_tax'];
             $total_data['shipping_total'] = $shipping['shipping_price'] + $shipping['shipping_extra'] + $shipping['shipping_tax'];
             $total_data['shipping_name'] = $shipping['shipping_name'];
             $total += $total_data['shipping_total'];
         }
     }
     $total_data['total_without_tax'] = $total;
     //taxes
     foreach ($taxes as $key => $value) {
         if ($value > 0) {
             $tax_data[] = array('title' => $this->tax->getRateName($key), 'percent' => $tax->getPercent($key), 'value' => $value);
             $total += $value;
         }
     }
     $total_data['taxes'] = $tax_data;
     $total_data['total'] = $total;
     return $total_data;
 }
Beispiel #3
0
 /**
  * Calculates the per_order coupon discount for the order
  * and the total post-tax/shipping discount
  * and sets order->order_discount
  *
  * @return unknown_type
  */
 function calculateDiscountTotals()
 {
     $this->_taxes = K2StoreHelperCart::getTaxes();
     $session = JFactory::getSession();
     $tax = new K2StoreTax();
     JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_k2store/models');
     $model = JModelLegacy::getInstance('MyCart', 'K2StoreModel');
     $products = $model->getDataNew();
     if ($session->has('coupon', 'k2store')) {
         $coupon_info = K2StoreHelperCart::getCoupon($session->get('coupon', '', 'k2store'));
         if ($coupon_info) {
             $discount_total = 0;
             if (!$coupon_info->product) {
                 $sub_total = K2StoreHelperCart::getSubTotal();
             } else {
                 $sub_total = 0;
                 foreach ($products as $product) {
                     if (in_array($product['product_id'], $coupon_info->product)) {
                         $sub_total += $product['total'];
                     }
                 }
             }
             if ($coupon_info->value_type == 'F') {
                 $coupon_info->value = min($coupon_info->value, $sub_total);
             }
             foreach ($products as $product) {
                 $discount = 0;
                 if (!$coupon_info->product) {
                     $status = true;
                 } else {
                     if (in_array($product['product_id'], $coupon_info->product)) {
                         $status = true;
                     } else {
                         $status = false;
                     }
                 }
                 if ($status) {
                     if ($coupon_info->value_type == 'F') {
                         $discount = $coupon_info->value * ($product['total'] / $sub_total);
                     } elseif ($coupon_info->value_type == 'P') {
                         $discount = $product['total'] / 100 * $coupon_info->value;
                     }
                     if ($product['tax_profile_id']) {
                         $tax_rates = $tax->getRateArray($product['total'] - ($product['total'] - $discount), $product['tax_profile_id']);
                         foreach ($tax_rates as $tax_rate) {
                             //	if ($tax_rate['value_type'] == 'P') {
                             $this->_taxes[$tax_rate['taxrate_id']] -= $tax_rate['amount'];
                             //	}
                         }
                     }
                 }
                 $discount_total += $discount;
             }
         }
     }
     // store the total amount of the discount
     //set the total as equal to the order_subtotal + order_tax if its greater than the sum of the two
     $this->order_discount = $discount_total > $this->order_subtotal + $this->order_tax ? $this->order_subtotal + $this->order_tax : $discount_total;
 }
Beispiel #4
0
 function validateCoupon()
 {
     $app = JFactory::getApplication();
     $coupon_info = K2StoreHelperCart::getCoupon($app->input->getString('coupon', ''));
     if ($coupon_info) {
         return true;
     } else {
         throw new Exception(JText::_('K2STORE_COUPON_INVALID'));
         return false;
     }
 }