Exemplo n.º 1
0
/**
 * Process a <merchant-calculation-callback>.
 */
function process_merchant_calculation_callback_single($google_response)
{
    global $google_checkout, $order, $cart, $total_weight, $total_count;
    list($root, $gc_data) = $google_response->GetParsedXML();
    $currencies = new currencies();
    // Get a hash array with the description of each shipping method
    $shipping_methods = $google_checkout->getMethods();
    require DIR_WS_CLASSES . 'order.php';
    $order = new order();
    $items = gc_get_arr_result($gc_data[$root]['shopping-cart']['items']['item']);
    // Get Coustoms OT
    $custom_order_totals_total = 0;
    $custom_order_totals = array();
    $order->products = array();
    foreach ($items as $item) {
        if (isset($item['merchant-private-item-data']['item']['VALUE'])) {
            $order->products[] = unserialize(base64_decode($item['merchant-private-item-data']['item']['VALUE']));
        } else {
            if ($item['merchant-private-item-data']['order_total']['VALUE']) {
                $ot = unserialize(base64_decode($item['merchant-private-item-data']['order_total']['VALUE']));
                $custom_order_totals[] = $ot;
                $order_total_value = $ot['value'] * (strrpos($ot['text'], '-') === false ? 1 : -1);
                $custom_order_totals_total += $currencies->get_value($gc_data[$root]['order-total']['currency']) * $order_total_value;
            } else {
                // For invoices.
                $order->products[] = array('qty' => $item['quantity']['VALUE'], 'name' => $item['item-name']['VALUE'], 'model' => $item['item-description']['VALUE'], 'tax' => 0, 'tax_description' => @$item['tax-table-selector']['VALUE'], 'price' => $item['unit-price']['VALUE'], 'final_price' => $item['unit-price']['VALUE'], 'onetime_charges' => 0, 'weight' => 0, 'products_priced_by_attribute' => 0, 'product_is_free' => 0, 'products_discount_type' => 0, 'products_discount_type_from' => 0, 'id' => @$item['merchant-item-id']['VALUE']);
            }
        }
    }
    $ex_cart = $cart;
    $cart = new shoppingCart();
    $prod_attr = gc_get_prattr($order->products);
    foreach ($prod_attr as $product_id => $item_data) {
        //$products_id, $qty = '1', $attributes = '
        $cart->add_cart($product_id, $item_data['qty'], $item_data['attr']);
    }
    // Register a random ID in the session to check throughout the checkout procedure
    // against alterations in the shopping cart contents.
    // TODO(eddavisson): Why is this commented out?
    //if (!tep_session_is_registered('cartID')) {
    //  tep_session_register('cartID');
    //}
    //$cartID = $cart->cartID;
    $total_weight = $cart->show_weight();
    $total_count = $cart->count_contents();
    // Create the results and send it
    $merchant_calc = new GoogleMerchantCalculations(DEFAULT_CURRENCY);
    // Loop through the list of address ids from the callback.
    $addresses = gc_get_arr_result($gc_data[$root]['calculate']['addresses']['anonymous-address']);
    // Get all the enabled shipping methods.
    require_once DIR_WS_CLASSES . 'shipping.php';
    // Required for some shipping methods (ie. USPS).
    require_once 'includes/classes/http_client.php';
    foreach ($addresses as $curr_address) {
        // Set up the order address.
        $curr_id = $curr_address['id'];
        $country = $curr_address['country-code']['VALUE'];
        $city = $curr_address['city']['VALUE'];
        $region = $curr_address['region']['VALUE'];
        $postal_code = $curr_address['postal-code']['VALUE'];
        $row = tep_db_fetch_array(tep_db_query("select * from " . TABLE_COUNTRIES . " where countries_iso_code_2 = '" . gc_make_sql_string($country) . "'"));
        $order->delivery['country'] = array('id' => $row['countries_id'], 'title' => $row['countries_name'], 'iso_code_2' => $country, 'iso_code_3' => $row['countries_iso_code_3']);
        $order->delivery['country_id'] = $row['countries_id'];
        $order->delivery['format_id'] = $row['address_format_id'];
        $row = tep_db_fetch_array(tep_db_query("select * from " . TABLE_ZONES . " where zone_code = '" . gc_make_sql_string($region) . "'"));
        $order->delivery['zone_id'] = $row['zone_id'];
        $order->delivery['state'] = $row['zone_name'];
        $order->delivery['city'] = $city;
        $order->delivery['postcode'] = $postal_code;
        //print_r($order);
        $shipping_modules = new shipping();
        // Loop through each shipping method if merchant-calculated shipping
        // support is to be provided
        //print_r($gc_data[$root]['calculate']['shipping']['method']);
        if (isset($gc_data[$root]['calculate']['shipping'])) {
            $shipping = gc_get_arr_result($gc_data[$root]['calculate']['shipping']['method']);
            // TODO(eddavisson): If we reactivate this, need to move to new configuration system.
            if (MODULE_PAYMENT_GOOGLECHECKOUT_MULTISOCKET == 'True') {
                // Single.
                // Get all the enabled shipping methods.
                $name = $shipping[0]['name'];
                // Compute the price for this shipping method and address id
                list($a, $method_name) = explode(': ', $name);
                if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY && $shipping_methods[$method_name][1] == 'domestic_types' || $order->delivery['country']['id'] != SHIPPING_ORIGIN_COUNTRY && $shipping_methods[$method_name][1] == 'international_types') {
                    // Reset the shipping class to set the new address
                    if (class_exists($shipping_methods[$method_name][2])) {
                        $GLOBALS[$shipping_methods[$method_name][2]] = new $shipping_methods[$method_name][2]();
                    }
                }
                $quotes = $shipping_modules->quote('', $shipping_methods[$method_name][2]);
            } else {
                // Standard
                foreach ($shipping as $curr_ship) {
                    $name = $curr_ship['name'];
                    // Compute the price for this shipping method and address id
                    list($a, $method_name) = explode(': ', $name);
                    if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY && $shipping_methods[$method_name][1] == 'domestic_types' || $order->delivery['country']['id'] != SHIPPING_ORIGIN_COUNTRY && $shipping_methods[$method_name][1] == 'international_types') {
                        // Reset the shipping class to set the new address.
                        if (class_exists($shipping_methods[$method_name][2])) {
                            $GLOBALS[$shipping_methods[$method_name][2]] = new $shipping_methods[$method_name][2]();
                        }
                    }
                }
                $quotes = $shipping_modules->quote();
            }
            reset($shipping);
            foreach ($shipping as $curr_ship) {
                $name = $curr_ship['name'];
                // Compute the price for this shipping method and address id
                list($a, $method_name) = explode(': ', $name);
                unset($quote_provider);
                unset($quote_method);
                if ($order->delivery['country']['id'] == SHIPPING_ORIGIN_COUNTRY && $shipping_methods[$method_name][1] == 'domestic_types' || $order->delivery['country']['id'] != SHIPPING_ORIGIN_COUNTRY && $shipping_methods[$method_name][1] == 'international_types') {
                    foreach ($quotes as $key_provider => $shipping_provider) {
                        // privider name (class)
                        if ($shipping_provider['id'] == $shipping_methods[$method_name][2]) {
                            // method name
                            $quote_provider = $key_provider;
                            if (is_array($shipping_provider['methods'])) {
                                foreach ($shipping_provider['methods'] as $key_method => $shipping_method) {
                                    if ($shipping_method['id'] == $shipping_methods[$method_name][0]) {
                                        $quote_method = $key_method;
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                //if there is a problem with the method, i mark it as non-shippable
                if (isset($quotes[$quote_provider]['error']) || !isset($quotes[$quote_provider]['methods'][$quote_method]['cost'])) {
                    $price = "9999.09";
                    $shippable = "false";
                } else {
                    $price = $quotes[$quote_provider]['methods'][$quote_method]['cost'];
                    $shippable = "true";
                }
                $merchant_result = new GoogleResult($curr_id);
                $merchant_result->SetShippingDetails($name, $currencies->get_value(DEFAULT_CURRENCY) * $price, $shippable);
                if ($gc_data[$root]['calculate']['tax']['VALUE'] == "true") {
                    // Compute tax for this address id and shipping type
                    $amount = 15;
                    // Modify this to the actual tax value
                    $merchant_result->SetTaxDetails($currencies->get_value(DEFAULT_CURRENCY) * $amount);
                }
                if (isset($gc_data[$root]['calculate']['merchant-code-strings']['merchant-code-string'])) {
                    $codes = gc_get_arr_result($gc_data[$root]['calculate']['merchant-code-strings']['merchant-code-string']);
                    foreach ($codes as $curr_code) {
                        // Update this data as required to set whether the coupon is valid, the code and the amount
                        $coupons = new GoogleCoupons("true", $curr_code['code'], $currencies->get_value(DEFAULT_CURRENCY) * 5, "test2");
                        $merchant_result->AddCoupons($coupons);
                    }
                }
                $merchant_calc->AddResult($merchant_result);
            }
        } else {
            $merchant_result = new GoogleResult($curr_id);
            if ($gc_data[$root]['calculate']['tax']['VALUE'] == "true") {
                // Compute tax for this address id and shipping type
                $amount = 15;
                // Modify this to the actual tax value
                $merchant_result->SetTaxDetails($currencies->get_value(DEFAULT_CURRENCY) * $amount);
            }
            //calculate_coupons($google_response, $merchant_result);
            $merchant_calc->AddResult($merchant_result);
        }
    }
    $cart = $ex_cart;
    $google_response->ProcessMerchantCalculations($merchant_calc);
}