function saveShippingMethod() { global $toC_Json, $osC_Language, $osC_Shipping, $osC_ShoppingCart, $osC_Weight, $osC_Tax, $osC_Currencies; $osC_ShoppingCart = new toC_ShoppingCart_Adapter($_REQUEST['orders_id']); $osC_Shipping = new osC_Shipping(); $osC_Tax = new osC_Tax_Admin(); $osC_Weight = new osC_Weight(); $osC_Currencies = new osC_Currencies(); if ($osC_Shipping->hasQuotes()) { if (isset($_REQUEST['code']) && strpos($_REQUEST['code'], '_')) { list($module, $method) = explode('_', $_REQUEST['code']); $module = 'osC_Shipping_' . $module; if (is_object($GLOBALS[$module]) && $GLOBALS[$module]->isEnabled()) { $quote = $osC_Shipping->getQuote($_REQUEST['code']); if (isset($quote['error'])) { $osC_ShoppingCart->resetShippingMethod(); } else { $osC_ShoppingCart->setShippingMethod($quote); } } else { $osC_ShoppingCart->resetShippingMethod(); } } } $osC_ShoppingCart->updateOrderTotal(); $response = array('success' => true, 'feedback' => $osC_Language->get('ms_success_action_performed')); echo $toC_Json->encode($response); }
function _calculate($set_shipping = true) { global $osC_Currencies, $osC_Tax, $osC_Weight, $osC_Shipping, $osC_OrderTotal; $this->_sub_total = 0; $this->_total = 0; $this->_weight = 0; $this->_tax = 0; $this->_tax_groups = array(); $this->_shipping_boxes_weight = 0; $this->_shipping_boxes = 0; $this->_shipping_quotes = array(); $this->_order_totals = array(); $_SESSION['cartID'] = $this->generateCartID(); if ($this->hasContents()) { foreach ($this->_contents as $data) { if ($data['type'] == PRODUCT_TYPE_SIMPLE || $data['type'] == PRODUCT_TYPE_GIFT_CERTIFICATE && $data['gc_data']['type'] == GIFT_CERTIFICATE_TYPE_PHYSICAL) { $products_weight = $osC_Weight->convert($data['weight'], $data['weight_class_id'], SHIPPING_WEIGHT_UNIT); $this->_weight += $products_weight * $data['quantity']; } $tax = $osC_Tax->getTaxRate($data['tax_class_id'], $this->getTaxingAddress('country_id'), $this->getTaxingAddress('zone_id')); $tax_description = $osC_Tax->getTaxRateDescription($data['tax_class_id'], $this->getTaxingAddress('country_id'), $this->getTaxingAddress('zone_id')); $shown_price = $osC_Currencies->addTaxRateToPrice($data['final_price'], $tax, $data['quantity']); $this->_sub_total += $shown_price; $this->_total += $shown_price; if (DISPLAY_PRICE_WITH_TAX == '1') { $tax_amount = $shown_price - $shown_price / ($tax < 10 ? '1.0' . str_replace('.', '', $tax) : '1.' . str_replace('.', '', $tax)); } else { $tax_amount = $tax / 100 * $shown_price; //oscommerce 3 bug, no matter the tax is displayed or not, tax should not be add to total $this->_total += $tax_amount; } $this->_tax += $tax_amount; if (isset($this->_tax_groups[$tax_description])) { $this->_tax_groups[$tax_description] += $tax_amount; } else { $this->_tax_groups[$tax_description] = $tax_amount; } } $this->_shipping_boxes_weight = $this->_weight; $this->_shipping_boxes = 1; if (SHIPPING_BOX_WEIGHT >= $this->_shipping_boxes_weight * SHIPPING_BOX_PADDING / 100) { $this->_shipping_boxes_weight = $this->_shipping_boxes_weight + SHIPPING_BOX_WEIGHT; } else { $this->_shipping_boxes_weight = $this->_shipping_boxes_weight + $this->_shipping_boxes_weight * SHIPPING_BOX_PADDING / 100; } if ($this->_shipping_boxes_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes $this->_shipping_boxes = ceil($this->_shipping_boxes_weight / SHIPPING_MAX_WEIGHT); $this->_shipping_boxes_weight = $this->_shipping_boxes_weight / $this->_shipping_boxes; } if ($set_shipping === true) { if (!class_exists('osC_Shipping')) { include 'includes/classes/shipping.php'; } if (!$this->isVirtualCart()) { if (!$this->hasShippingMethod() || $this->getShippingMethod('is_cheapest') === true) { $osC_Shipping = new osC_Shipping(); $this->setShippingMethod($osC_Shipping->getCheapestQuote(), false); } else { $osC_Shipping = new osC_Shipping($this->getShippingMethod('id')); $this->setShippingMethod($osC_Shipping->getQuote(), false); } } else { //reset shipping address and shipping method $this->_shipping_address = array(); $this->_shipping_method = array(); } } if (!class_exists('osC_OrderTotal')) { include 'includes/classes/order_total.php'; } $osC_OrderTotal = new osC_OrderTotal(); $this->_order_totals = $osC_OrderTotal->getResult(); } }
function saveShippingMethod() { global $osC_Language, $osC_ShoppingCart, $osC_Shipping, $toC_Json, $osC_Customer, $osC_Payment, $osC_Currencies; $errors = array(); // load all enabled shipping modules if (class_exists('osC_Shipping') === false) { require_once 'includes/classes/shipping.php'; } $osC_Shipping = new osC_Shipping(); // if no shipping method has been selected, automatically select the cheapest method. // if ($osC_ShoppingCart->hasShippingMethod() === false) { // $osC_ShoppingCart->setShippingMethod($osC_Shipping->getCheapestQuote()); // } if (!empty($_POST['shipping_comments'])) { $_SESSION['comments'] = osc_sanitize_string($_POST['shipping_comments']); } if ($osC_Shipping->hasQuotes()) { if (isset($_REQUEST['shipping_mod_sel']) && strpos($_REQUEST['shipping_mod_sel'], '_')) { list($module, $method) = explode('_', $_REQUEST['shipping_mod_sel']); $module = 'osC_Shipping_' . $module; if (is_object($GLOBALS[$module]) && $GLOBALS[$module]->isEnabled()) { $quote = $osC_Shipping->getQuote($_REQUEST['shipping_mod_sel']); if (isset($quote['error'])) { $osC_ShoppingCart->resetShippingMethod(); $errors[] = $quote['error']; } else { $osC_ShoppingCart->setShippingMethod($quote); } } else { $osC_ShoppingCart->resetShippingMethod(); } } } else { $osC_ShoppingCart->resetShippingMethod(); } //gift wrapping if (isset($_POST['gift_wrapping']) && $_POST['gift_wrapping'] == 'true') { $osC_ShoppingCart->setGiftWrapping(true); if (!empty($_POST['gift_wrapping_comments'])) { $_SESSION['gift_wrapping_comments'] = osc_sanitize_string($_POST['gift_wrapping_comments']); } } else { $osC_ShoppingCart->setGiftWrapping(false); unset($_SESSION['gift_wrapping_comments']); } if (sizeof($errors) > 0) { $response = array('success' => false, 'errors' => $errors); } else { $form = self::_getPaymentMethodForm(); $response = array('success' => true, 'form' => $form['form'], 'javascript' => $form['javascript']); } echo $toC_Json->encode($response); }
function _calculate($set_shipping = true) { global $osC_Currencies, $osC_Tax, $osC_Weight, $osC_Shipping, $osC_OrderTotal, $osC_Customer; //put the order currency code in the session for order total modules and shipping modules $_SESSION['currency'] = $this->getCurrency(); $this->restoreStoreCredit(); require_once '../includes/classes/customer.php'; $osC_Customer = new osC_Customer(); $osC_Customer->setCustomerData($this->getCustomersID()); $this->_sub_total = 0; $this->_total = 0; $this->_weight = 0; $this->_tax = 0; $this->_tax_groups = array(); $this->_shipping_boxes_weight = 0; $this->_shipping_boxes = 0; $this->_shipping_quotes = array(); $this->_order_totals = array(); if ($this->hasContents()) { foreach ($this->_contents as $products_id => $data) { $products_weight = $osC_Weight->convert($data['weight'], $data['weight_class_id'], SHIPPING_WEIGHT_UNIT); $this->_weight += $products_weight * $data['quantity']; $tax = $osC_Tax->getTaxRate($data['tax_class_id'], $this->getTaxingAddress('country_id'), $this->getTaxingAddress('zone_id')); $tax_description = $osC_Tax->getTaxRateDescription($data['tax_class_id'], $this->getTaxingAddress('country_id'), $this->getTaxingAddress('zone_id')); //update tax to database $this->_contents[$products_id]['tax'] = $tax; $this->_updateProductTax($this->_contents[$products_id]['orders_products_id'], $tax); $shown_price = $osC_Currencies->addTaxRateToPrice($data['final_price'], $tax, $data['quantity']); $this->_sub_total += $shown_price; $this->_total += $shown_price; if (DISPLAY_PRICE_WITH_TAX == '1') { $tax_amount = $shown_price - $shown_price / ($tax < 10 ? '1.0' . str_replace('.', '', $tax) : '1.' . str_replace('.', '', $tax)); } else { $tax_amount = $tax / 100 * $shown_price; //oscommerce 3 bug, no matter the tax is displayed or not, tax should not be add to total $this->_total += $tax_amount; } $this->_tax += $tax_amount; if (isset($this->_tax_groups[$tax_description])) { $this->_tax_groups[$tax_description] += $tax_amount; } else { $this->_tax_groups[$tax_description] = $tax_amount; } } $this->_shipping_boxes_weight = $this->_weight; $this->_shipping_boxes = 1; if (SHIPPING_BOX_WEIGHT >= $this->_shipping_boxes_weight * SHIPPING_BOX_PADDING / 100) { $this->_shipping_boxes_weight = $this->_shipping_boxes_weight + SHIPPING_BOX_WEIGHT; } else { $this->_shipping_boxes_weight = $this->_shipping_boxes_weight + $this->_shipping_boxes_weight * SHIPPING_BOX_PADDING / 100; } if ($this->_shipping_boxes_weight > SHIPPING_MAX_WEIGHT) { // Split into many boxes $this->_shipping_boxes = ceil($this->_shipping_boxes_weight / SHIPPING_MAX_WEIGHT); $this->_shipping_boxes_weight = $this->_shipping_boxes_weight / $this->_shipping_boxes; } if ($set_shipping === true) { unset($_SESSION['osC_ShoppingCart_data']['shipping_quotes']); if (!class_exists('osC_Shipping')) { include 'includes/classes/shipping.php'; } //check if this order already have a delivery method if (!empty($this->_deliver_module)) { $osC_Shipping = new osC_Shipping($this->_deliver_module); $this->setShippingMethod($osC_Shipping->getQuote(), false); } else { if (!$this->hasShippingMethod() || $this->getShippingMethod('is_cheapest') === true) { $osC_Shipping = new osC_Shipping(); $this->setShippingMethod($osC_Shipping->getCheapestQuote(), false); } else { $osC_Shipping = new osC_Shipping($this->getShippingMethod('id')); $this->setShippingMethod($osC_Shipping->getQuote(), false); } } } } if (!class_exists('osC_OrderTotal')) { include 'includes/classes/order_total.php'; } $osC_OrderTotal = new osC_OrderTotal(); $this->_order_totals = $osC_OrderTotal->getResult(); if ($this->isUseStoreCredit()) { $this->insertStoreCredit(); } unset($_SESSION['currency']); unset($_SESSION['osC_Customer_data']); }
function _set_express_checkout($params) { global $osC_ShoppingCart, $osC_Currencies, $osC_Language, $osC_Tax, $messageStack, $osC_Database; // if there is nothing in the customers cart, redirect them to the shopping cart page if (!$osC_ShoppingCart->hasContents()) { osc_redirect(osc_href_link(FILENAME_CHECKOUT, '', 'NONSSL', true, true, true)); } $params['METHOD'] = 'SetExpressCheckout'; $params['PAYMENTACTION'] = MODULE_PAYMENT_PAYPAL_EXPRESS_TRANSACTION_METHOD == 'Sale' || !osc_not_null(MODULE_PAYMENT_PAYPAL_EXPRESS_API_USERNAME) ? 'Sale' : 'Authorization'; $params['RETURNURL'] = HTTPS_SERVER . DIR_WS_HTTPS_CATALOG . FILENAME_CHECKOUT . '?callback&module=paypal_express&express_action=retrieve'; $params['CANCELURL'] = HTTPS_SERVER . DIR_WS_HTTPS_CATALOG . FILENAME_CHECKOUT . '?callback&module=paypal_express&express_action=cancel'; $params['CURRENCYCODE'] = $osC_Currencies->getCode(); //process item total account(not to include tax) $line_item_no = 0; $items_total = 0; if ($osC_ShoppingCart->hasContents()) { foreach ($osC_ShoppingCart->getProducts() as $product) { $product_name = $product['name']; //gift certificate if ($product['type'] == PRODUCT_TYPE_GIFT_CERTIFICATE) { $product_name .= "\n" . ' - ' . $osC_Language->get('senders_name') . ': ' . $product['gc_data']['senders_name']; if ($product['gc_data']['type'] == GIFT_CERTIFICATE_TYPE_EMAIL) { $product_name .= "\n" . ' - ' . $osC_Language->get('senders_email') . ': ' . $product['gc_data']['senders_email']; } $product_name .= "\n" . ' - ' . $osC_Language->get('recipients_name') . ': ' . $product['gc_data']['recipients_name']; if ($product['gc_data']['type'] == GIFT_CERTIFICATE_TYPE_EMAIL) { $product_name .= "\n" . ' - ' . $osC_Language->get('recipients_email') . ': ' . $product['gc_data']['recipients_email']; } $product_name .= "\n" . ' - ' . $osC_Language->get('message') . ': ' . $product['gc_data']['message']; } if ($osC_ShoppingCart->hasVariants($product['id'])) { foreach ($osC_ShoppingCart->getVariants($product['id']) as $variant) { $product_name .= ' - ' . $variant['groups_name'] . ': ' . $variant['values_name']; } } $params['L_NAME' . $line_item_no] = $product_name; $params['L_AMT' . $line_item_no] = $osC_Currencies->formatRaw($product['final_price']); $params['L_NUMBER' . $line_item_no] = $product['id']; $params['L_QTY' . $line_item_no] = $product['quantity']; $items_total += $product['final_price'] * $product['quantity']; $line_item_no++; } } //process shipping (not to include tax) if (!class_exists('osC_Shipping')) { include 'includes/classes/shipping.php'; } $osC_Shipping = new osC_Shipping(); $shipping = $osC_Shipping->getQuote($osC_ShoppingCart->getShippingMethod('id')); $params['SHIPPINGAMT'] = $osC_Currencies->formatRaw($shipping['cost']); /** * process order totals, minus the coupon and gift certificate discount * if coupon type is freefship, we have to pass shipping fee with zero to paypal * the discount should not to include tax. otherwise, it will be rejected by the paypal * First we process gift certificate because we have to calculte the coupon fee based on total, item total, shipping total */ //process gift certificate foreach ($osC_ShoppingCart->getOrderTotals() as $total) { if ($total['code'] == 'gift_certificate') { $params['L_NAME' . $line_item_no] = 'Discount Gift Certificate'; $params['L_AMT' . $line_item_no] = -$osC_Currencies->formatRaw(abs($total['value'])); $params['L_QTY' . $line_item_no] = 1; //minus the gift certificate discount from sub total(not to include the tax) $items_total -= abs($total['value']); $line_item_no++; break; } } //process the coupon foreach ($osC_ShoppingCart->getOrderTotals() as $total) { if ($total['code'] == 'coupon') { $coupon_amount = $osC_ShoppingCart->getTotal() - $osC_ShoppingCart->getTax() - $shipping['cost'] - $items_total; $params['L_NAME' . $line_item_no] = 'Discount Coupon'; $params['L_AMT' . $line_item_no] = -$osC_Currencies->formatRaw(abs($coupon_amount)); $params['L_QTY' . $line_item_no] = 1; //minus the coupon discount from sub total(not to include the tax) $items_total -= abs($coupon_amount); $line_item_no++; break; } } $params['ITEMAMT'] = $osC_Currencies->formatRaw($items_total); //pass total tax and order total $params['TAXAMT'] = $osC_Currencies->formatRaw($osC_ShoppingCart->getTax()); $params['AMT'] = $osC_Currencies->formatRaw($osC_ShoppingCart->getTotal()); //call the setExpressCheckout api $post_string = ''; foreach ($params as $key => $value) { $post_string .= $key . '=' . urlencode(utf8_encode(trim($value))) . '&'; } $post_string = substr($post_string, 0, -1); $response = $this->sendTransactionToGateway($this->api_url, $post_string); $response_array = array(); parse_str($response, $response_array); if ($response_array['ACK'] == 'Success' || $response_array['ACK'] == 'SuccessWithWarning') { osc_redirect($this->paypal_url . '&token=' . $response_array['TOKEN'] . '&useraction=commit'); } else { $messageStack->add_session('checkout', $osC_Language->get('payment_paypal_express_error_title') . ' <strong>' . stripslashes($response_array['L_LONGMESSAGE0']) . '</strong>'); osc_redirect(osc_href_link(FILENAME_CHECKOUT, 'checkout', 'SSL')); } }