Example #1
0
/**
 * submit checkout function, used through ajax and in normal page loading.
 * No parameters, returns nothing
 */
function wpsc_submit_checkout()
{
    global $wpdb, $wpsc_cart, $user_ID, $nzshpcrt_gateways, $wpsc_shipping_modules, $wpsc_gateways;
    $num_items = 0;
    $use_shipping = 0;
    $disregard_shipping = 0;
    $_SESSION['wpsc_checkout_misc_error_messages'] = array();
    $wpsc_checkout = new wpsc_checkout();
    $selected_gateways = get_option('custom_gateway_options');
    $submitted_gateway = $_POST['custom_gateway'];
    $options = get_option('custom_shipping_options');
    $form_validity = $wpsc_checkout->validate_forms();
    extract($form_validity);
    // extracts $is_valid and $error_messages
    if ($_POST['agree'] != 'yes') {
        $_SESSION['wpsc_checkout_misc_error_messages'][] = __('Please agree to the terms and conditions, otherwise we cannot process your order.', 'wpsc');
        $is_valid = false;
    }
    $selectedCountry = $wpdb->get_results("SELECT id, country FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE isocode='" . $wpdb->escape($_SESSION['wpsc_delivery_country']) . "'", ARRAY_A);
    foreach ($wpsc_cart->cart_items as $cartitem) {
        if ($cartitem->meta[0]['no_shipping'] == 1) {
            continue;
        }
        $categoriesIDs = $cartitem->category_id_list;
        foreach ((array) $categoriesIDs as $catid) {
            if (is_array($catid)) {
                $countries = wpsc_get_meta($catid[0], 'target_market', 'wpsc_category');
            } else {
                $countries = wpsc_get_meta($catid, 'target_market', 'wpsc_category');
            }
            if (!empty($countries) && !in_array($selectedCountry[0]['id'], (array) $countries)) {
                $errormessage = sprintf(__('%s cannot be shipped to %s. To continue with your transaction please remove this product from the list below.', 'wpsc'), $cartitem->product_name, $selectedCountry[0]['country']);
                $_SESSION['categoryAndShippingCountryConflict'] = $errormessage;
                $is_valid = false;
            }
        }
        //count number of items, and number of items using shipping
        $num_items++;
        if ($cartitem->uses_shipping != 1) {
            $disregard_shipping++;
        } else {
            $use_shipping++;
        }
    }
    if (array_search($submitted_gateway, $selected_gateways) !== false) {
        $_SESSION['wpsc_previous_selected_gateway'] = $submitted_gateway;
    } else {
        $is_valid = false;
    }
    if (get_option('do_not_use_shipping') == 0 && ($wpsc_cart->selected_shipping_method == null || $wpsc_cart->selected_shipping_option == null) && $num_items != $disregard_shipping) {
        $_SESSION['wpsc_checkout_misc_error_messages'][] = __('You must select a shipping method, otherwise we cannot process your order.', 'wpsc');
        $is_valid = false;
    }
    if (get_option('do_not_use_shipping') != 1 && in_array('ups', (array) $options) && $_SESSION['wpsc_zipcode'] == '' && $num_items != $disregard_shipping) {
        $_SESSION['categoryAndShippingCountryConflict'] = __('Please enter a Zipcode and click calculate to proceed', 'wpsc');
        $is_valid = false;
    }
    if ($is_valid == true) {
        $_SESSION['categoryAndShippingCountryConflict'] = '';
        // check that the submitted gateway is in the list of selected ones
        $sessionid = mt_rand(100, 999) . time();
        $_SESSION['wpsc_sessionid'] = $sessionid;
        $subtotal = $wpsc_cart->calculate_subtotal();
        if ($wpsc_cart->has_total_shipping_discount() == false) {
            $base_shipping = $wpsc_cart->calculate_base_shipping();
        } else {
            $base_shipping = 0;
        }
        $delivery_country = $wpsc_cart->delivery_country;
        $delivery_region = $wpsc_cart->delivery_region;
        if (wpsc_uses_shipping()) {
            $shipping_method = $wpsc_cart->selected_shipping_method;
            $shipping_option = $wpsc_cart->selected_shipping_option;
        } else {
            $shipping_method = '';
            $shipping_option = '';
        }
        if (isset($_POST['how_find_us'])) {
            $find_us = $_POST['how_find_us'];
        } else {
            $find_us = '';
        }
        //keep track of tax if taxes are exclusive
        $wpec_taxes_controller = new wpec_taxes_controller();
        if (!$wpec_taxes_controller->wpec_taxes_isincluded()) {
            $tax = $wpsc_cart->calculate_total_tax();
            $tax_percentage = $wpsc_cart->tax_percentage;
        } else {
            $tax = 0.0;
            $tax_percentage = 0.0;
        }
        $total = $wpsc_cart->calculate_total_price();
        $wpdb->insert(WPSC_TABLE_PURCHASE_LOGS, array('totalprice' => $total, 'statusno' => '0', 'sessionid' => $sessionid, 'user_ID' => (int) $user_ID, 'date' => strtotime(current_time('mysql')), 'gateway' => $submitted_gateway, 'billing_country' => $wpsc_cart->selected_country, 'shipping_country' => $delivery_country, 'billing_region' => $wpsc_cart->selected_region, 'shipping_region' => $delivery_region, 'base_shipping' => $base_shipping, 'shipping_method' => $shipping_method, 'shipping_option' => $shipping_option, 'plugin_version' => WPSC_VERSION, 'discount_value' => $wpsc_cart->coupons_amount, 'discount_data' => $wpsc_cart->coupons_name, 'find_us' => $find_us, 'wpec_taxes_total' => $tax, 'wpec_taxes_rate' => $tax_percentage));
        $purchase_log_id = $wpdb->insert_id;
        $wpsc_checkout->save_forms_to_db($purchase_log_id);
        $wpsc_cart->save_to_db($purchase_log_id);
        $wpsc_cart->submit_stock_claims($purchase_log_id);
        if (get_option('wpsc_also_bought') == 1) {
            wpsc_populate_also_bought_list();
        }
        if (!isset($our_user_id) && isset($user_ID)) {
            $our_user_id = $user_ID;
        }
        $wpsc_cart->log_id = $purchase_log_id;
        do_action('wpsc_submit_checkout', array("purchase_log_id" => $purchase_log_id, "our_user_id" => $our_user_id));
        if (get_option('permalink_structure') != '') {
            $separator = "?";
        } else {
            $separator = "&";
        }
        // submit to gateway
        $current_gateway_data =& $wpsc_gateways[$submitted_gateway];
        if ($current_gateway_data['api_version'] >= 2.0) {
            $merchant_instance = new $current_gateway_data['class_name']($purchase_log_id);
            $merchant_instance->construct_value_array();
            $merchant_instance->submit();
        } elseif ($current_gateway_data['internalname'] == $submitted_gateway && $current_gateway_data['internalname'] != 'google') {
            $gateway_used = $current_gateway_data['internalname'];
            $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('gateway' => $gateway_used), array('id' => $log_id));
            $current_gateway_data['function']($separator, $sessionid);
        } elseif ($current_gateway_data['internalname'] == 'google' && $current_gateway_data['internalname'] == $submitted_gateway) {
            $gateway_used = $current_gateway_data['internalname'];
            $wpdb->update(WPSC_TABLE_PURCHASE_LOGS, array('gateway' => $gateway_used), array('id' => $log_id));
            $_SESSION['gateway'] = 'google';
            wp_redirect(get_option('shopping_cart_url'));
        }
    }
}
Example #2
0
/**
	* submit checkout function, used through ajax and in normal page loading.
	* No parameters, returns nothing
*/
function wpsc_submit_checkout()
{
    global $wpdb, $wpsc_cart, $user_ID, $nzshpcrt_gateways, $wpsc_shipping_modules, $wpsc_gateways;
    //echo "break redirect";
    //
    do_action('wpsc_before_submit_checkout');
    $_SESSION['wpsc_checkout_misc_error_messages'] = array();
    $wpsc_checkout = new wpsc_checkout();
    //exit('coupons:'.$wpsc_cart->coupons_name);
    $selected_gateways = get_option('custom_gateway_options');
    $submitted_gateway = $_POST['custom_gateway'];
    $options = get_option('custom_shipping_options');
    $form_validity = $wpsc_checkout->validate_forms();
    extract($form_validity);
    // extracts $is_valid and $error_messages
    if (get_option('do_not_use_shipping') == 0 && ($wpsc_cart->selected_shipping_method == null || $wpsc_cart->selected_shipping_option == null) && $wpsc_cart->uses_shipping) {
        $_SESSION['wpsc_checkout_misc_error_messages'][] = __('You must select a shipping method, otherwise we cannot process your order.', 'wpsc');
        $is_valid = false;
    }
    if ($_POST['agree'] != 'yes') {
        $_SESSION['wpsc_checkout_misc_error_messages'][] = __('Please agree to the terms and conditions, otherwise we cannot process your order.', 'wpsc');
        $is_valid = false;
    }
    $selectedCountry = $wpdb->get_results("SELECT id, country FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE isocode='" . $wpdb->escape($_SESSION['wpsc_delivery_country']) . "'", ARRAY_A);
    foreach ($wpsc_cart->cart_items as $cartitem) {
        //	exit('<pre>'.print_r($cartitem, true).'</pre>');
        $categoriesIDs = $wpdb->get_col("SELECT category_id FROM `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` WHERE product_id=" . $cartitem->product_id);
        foreach ((array) $categoriesIDs as $catid) {
            if (is_array($catid)) {
                $sql = "SELECT `countryid` FROM `" . WPSC_TABLE_CATEGORY_TM . "` WHERE `visible`=0 AND `categoryid`=" . $catid[0];
            } else {
                $sql = "SELECT `countryid` FROM `" . WPSC_TABLE_CATEGORY_TM . "` WHERE `visible`=0 AND `categoryid`=" . $catid;
            }
            $countries = $wpdb->get_col($sql);
            if (in_array($selectedCountry[0]['id'], (array) $countries)) {
                $errormessage = sprintf(__('Oops the product : %s cannot be shipped to %s. To continue with your transaction please remove this product from the list above.', 'wpsc'), $cartitem->product_name, $selectedCountry[0]['country']);
                $_SESSION['categoryAndShippingCountryConflict'] = $errormessage;
                $is_valid = false;
            }
        }
        //count number of items, and number of items using shipping
        $num_items++;
        if ($cartitem->uses_shipping != 1) {
            $disregard_shipping++;
        } else {
            $use_shipping++;
        }
    }
    // exit('valid >'.$is_valid);
    if (array_search($submitted_gateway, $selected_gateways) !== false) {
        $_SESSION['wpsc_previous_selected_gateway'] = $submitted_gateway;
    } else {
        $is_valid = false;
    }
    if (get_option('do_not_use_shipping') != 1 && in_array('ups', (array) $options) && $_SESSION['wpsc_zipcode'] == '') {
        //exit('Not being called');
        if ($num_items != $disregard_shipping) {
            //<-- new line of code
            $_SESSION['categoryAndShippingCountryConflict'] = __('Please enter a Zipcode and click calculate to proceed');
            $is_valid = false;
        }
    }
    if ($is_valid == true || $_GET['gateway'] == 'noca') {
        $_SESSION['categoryAndShippingCountryConflict'] = '';
        // check that the submitted gateway is in the list of selected ones
        $sessionid = mt_rand(100, 999) . time();
        $_SESSION['wpsc_sessionid'] = $sessionid;
        $subtotal = $wpsc_cart->calculate_subtotal();
        if ($wpsc_cart->has_total_shipping_discount() == false) {
            $base_shipping = $wpsc_cart->calculate_base_shipping();
        } else {
            $base_shipping = 0;
        }
        if (isset($_POST['how_find_us'])) {
            $find_us = $_POST['how_find_us'];
        } else {
            $find_us = '';
        }
        $tax = $wpsc_cart->calculate_total_tax();
        $total = $wpsc_cart->calculate_total_price();
        // Make sure delivery and selected region are onlly saved if the country does have regions
        // Im unsure how this would effect countries that HAVE regions, i.e if you select Canada as country,, will your 			// region be alabama if no region was selected?
        $wpsc_cart->update_location();
        if (!wpsc_has_regions($wpsc_cart->selected_country)) {
            $wpsc_cart->selected_region = '';
        }
        if (!wpsc_has_regions($wpsc_cart->delivery_country)) {
            $wpsc_cart->delivery_region = '';
        }
        $sql = "INSERT INTO `" . WPSC_TABLE_PURCHASE_LOGS . "` (`totalprice`,`statusno`, `sessionid`, `user_ID`, `date`, `gateway`, `billing_country`,`shipping_country`, `billing_region`, `shipping_region`, `base_shipping`,`shipping_method`, `shipping_option`, `plugin_version`, `discount_value`, `discount_data`,`find_us`) VALUES ('{$total}' ,'0', '{$sessionid}', '" . (int) $user_ID . "', UNIX_TIMESTAMP(), '{$submitted_gateway}', '{$wpsc_cart->selected_country}', '{$wpsc_cart->delivery_country}','{$wpsc_cart->selected_region}', '{$wpsc_cart->delivery_region}', '{$base_shipping}', '{$wpsc_cart->selected_shipping_method}', '{$wpsc_cart->selected_shipping_option}', '" . WPSC_VERSION . "', '{$wpsc_cart->coupons_amount}','{$wpsc_cart->coupons_name}', '{$find_us}')";
        //exit($sql);
        $wpdb->query($sql);
        $purchase_log_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid` IN('{$sessionid}') LIMIT 1");
        //exit('PurchLog id'.$purchase_log_id);
        $wpsc_checkout->save_forms_to_db($purchase_log_id);
        $wpsc_cart->save_to_db($purchase_log_id);
        $wpsc_cart->submit_stock_claims($purchase_log_id);
        if (get_option('wpsc_also_bought') == 1) {
            wpsc_populate_also_bought_list();
        }
        wp_get_current_user();
        $our_user_id = $user_ID;
        do_action('wpsc_submit_checkout', array("purchase_log_id" => $purchase_log_id, "our_user_id" => $our_user_id));
        if (get_option('permalink_structure') != '') {
            $seperator = "?";
        } else {
            $seperator = "&";
        }
        if ($total <= 0) {
            $transaction_url_with_sessionid = add_query_arg('sessionid', $session_id, get_option('transact_url'));
            wp_redirect($transaction_url_with_sessionid);
        }
        /// submit to gateway
        $current_gateway_data =& $wpsc_gateways[$submitted_gateway];
        if ($current_gateway_data['api_version'] >= 2.0) {
            $merchant_instance = new $current_gateway_data['class_name']($purchase_log_id);
            $merchant_instance->construct_value_array();
            $merchant_instance->submit();
            //print_r($merchant_instance);
        } else {
            if ($current_gateway_data['internalname'] == $submitted_gateway && $current_gateway_data['internalname'] != 'google') {
                $gateway_used = $current_gateway_data['internalname'];
                $wpdb->query("UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `gateway` = '" . $gateway_used . "' WHERE `id` = '" . $log_id . "' LIMIT 1 ;");
                $current_gateway_data['function']($seperator, $sessionid);
                //break;
            } else {
                if ($_POST['custom_gateway'] == 'google') {
                    $gateway_used = $current_gateway_data['internalname'];
                    $wpdb->query("UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `gateway` = '" . $gateway_used . "' WHERE `id` = '" . $log_id . "' LIMIT 1 ;");
                    $_SESSION['gateway'] = 'google';
                    header('Location: ' . get_option('shopping_cart_url'));
                    exit;
                    //break;
                }
            }
        }
        if (isset($_GET['gateway']) && $_GET['gateway'] == 'noca') {
            //exit('HERE2');
            echo transaction_results($sessionid, true);
        } else {
            //exit('HERE');
        }
    } else {
    }
}
Example #3
0
/**
	* submit checkout function, used through ajax and in normal page loading.
	* No parameters, returns nothing
*/
function wpsc_submit_checkout()
{
    global $wpdb, $wpsc_cart, $user_ID, $nzshpcrt_gateways, $wpsc_shipping_modules;
    $_SESSION['wpsc_checkout_misc_error_messages'] = array();
    $wpsc_checkout = new wpsc_checkout();
    //exit('coupons:'.$wpsc_cart->coupons_name);
    $selected_gateways = get_option('custom_gateway_options');
    $submitted_gateway = $_POST['custom_gateway'];
    $options = get_option('custom_shipping_options');
    $form_validity = $wpsc_checkout->validate_forms();
    //exit('<pre>'.print_r($_POST, true).'</pre>');
    //	exit('2<pre>'.print_r($_SESSION['wpsc_zipcode'], true).'</pre>');
    extract($form_validity);
    // extracts $is_valid and $error_messages
    //	exit('<pre>'.print_r($results, true).'</pre>');
    if (get_option('do_not_use_shipping') == 0 && ($wpsc_cart->selected_shipping_method == null || $wpsc_cart->selected_shipping_option == null)) {
        $_SESSION['wpsc_checkout_misc_error_messages'][] = TXT_WPSC_PLEASEASELECTSHIPPINGMETHOD;
        $is_valid = false;
    }
    if ($_POST['agree'] != 'yes') {
        $_SESSION['wpsc_checkout_misc_error_messages'][] = TXT_WPSC_PLEASEAGREETERMSANDCONDITIONS;
        $is_valid = false;
    }
    //exit('<pre>'.print_r($_POST, true).'</pre>');
    $selectedCountry = $wpdb->get_results("SELECT id, country FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE isocode='" . $wpdb->escape($_SESSION['wpsc_delivery_country']) . "'", ARRAY_A);
    //  exit('valid >'.$is_valid.'\r\n'.$_SESSION['wpsc_delivery_country']);
    foreach ($wpsc_cart->cart_items as $cartitem) {
        //	exit('<pre>'.print_r($cartitem, true).'</pre>');
        $categoriesIDs = $wpdb->get_col("SELECT category_id FROM `" . WPSC_TABLE_ITEM_CATEGORY_ASSOC . "` WHERE product_id=" . $cartitem->product_id);
        foreach ((array) $categoriesIDs as $catid) {
            if (is_array($catid)) {
                $sql = "SELECT `countryid` FROM `" . WPSC_TABLE_CATEGORY_TM . "` WHERE `visible`=0 AND `categoryid`=" . $catid[0];
            } else {
                $sql = "SELECT `countryid` FROM `" . WPSC_TABLE_CATEGORY_TM . "` WHERE `visible`=0 AND `categoryid`=" . $catid;
            }
            $countries = $wpdb->get_col($sql);
            if (in_array($selectedCountry[0]['id'], (array) $countries)) {
                $errormessage = sprintf(TXT_WPSC_CATEGORY_TARGETMARKET, $cartitem->product_name, $selectedCountry[0]['country']);
                $_SESSION['categoryAndShippingCountryConflict'] = $errormessage;
                $is_valid = false;
            }
        }
        //count number of items, and number of items using shipping
        $num_items++;
        if ($cartitem->uses_shipping != 1) {
            $disregard_shipping++;
        } else {
            $use_shipping++;
        }
    }
    // exit('valid >'.$is_valid);
    if (array_search($submitted_gateway, $selected_gateways) !== false) {
        $_SESSION['wpsc_previous_selected_gateway'] = $submitted_gateway;
    } else {
        $is_valid = false;
    }
    if (get_option('do_not_use_shipping') != 1 && in_array('ups', (array) $options) && $_SESSION['wpsc_zipcode'] == '') {
        //exit('Not being called');
        if ($num_items != $disregard_shipping) {
            //<-- new line of code
            $_SESSION['categoryAndShippingCountryConflict'] = __('Please enter a Zipcode and click calculate to proceed');
            $is_valid = false;
        }
    }
    if ($is_valid == true || $_GET['gateway'] == 'noca') {
        $_SESSION['categoryAndShippingCountryConflict'] = '';
        // check that the submitted gateway is in the list of selected ones
        $sessionid = mt_rand(100, 999) . time();
        $_SESSION['wpsc_sessionid'] = $sessionid;
        $subtotal = $wpsc_cart->calculate_subtotal();
        if ($wpsc_cart->has_total_shipping_discount() == false) {
            $base_shipping = $wpsc_cart->calculate_base_shipping();
        } else {
            $base_shipping = 0;
        }
        if (isset($_POST['how_find_us'])) {
            $find_us = $_POST['how_find_us'];
        } else {
            $find_us = '';
        }
        $tax = $wpsc_cart->calculate_total_tax();
        $total = $wpsc_cart->calculate_total_price();
        $sql = "INSERT INTO `" . WPSC_TABLE_PURCHASE_LOGS . "` (`totalprice`,`statusno`, `sessionid`, `user_ID`, `date`, `gateway`, `billing_country`,`shipping_country`, `billing_region`, `shipping_region`, `base_shipping`,`shipping_method`, `shipping_option`, `plugin_version`, `discount_value`, `discount_data`,`find_us`) VALUES ('{$total}' ,'0', '{$sessionid}', '" . (int) $user_ID . "', UNIX_TIMESTAMP(), '{$submitted_gateway}', '{$wpsc_cart->delivery_country}', '{$wpsc_cart->selected_country}','{$wpsc_cart->selected_region}', '{$wpsc_cart->delivery_region}', '{$base_shipping}', '{$wpsc_cart->selected_shipping_method}', '{$wpsc_cart->selected_shipping_option}', '" . WPSC_VERSION . "', '{$wpsc_cart->coupons_amount}','{$wpsc_cart->coupons_name}', '{$find_us}')";
        //exit($sql);
        $wpdb->query($sql);
        $purchase_log_id = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid` IN('{$sessionid}') LIMIT 1");
        //exit('PurchLog id'.$purchase_log_id);
        $wpsc_checkout->save_forms_to_db($purchase_log_id);
        $wpsc_cart->save_to_db($purchase_log_id);
        $wpsc_cart->submit_stock_claims($purchase_log_id);
        if (get_option('wpsc_also_bought') == 1) {
            wpsc_populate_also_bought_list();
        }
        do_action('wpsc_submit_checkout', array("purchase_log_id" => $purchase_log_id, "our_user_id" => $our_user_id));
        if (get_option('permalink_structure') != '') {
            $seperator = "?";
        } else {
            $seperator = "&";
        }
        // submit to gateway
        foreach ($nzshpcrt_gateways as $gateway) {
            if ($gateway['internalname'] == $submitted_gateway && $gateway['internalname'] != 'google') {
                $gateway_used = $gateway['internalname'];
                $wpdb->query("UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `gateway` = '" . $gateway_used . "' WHERE `id` = '" . $log_id . "' LIMIT 1 ;");
                $gateway['function']($seperator, $sessionid);
                break;
            } elseif ($gateway['internalname'] == 'google' && $gateway['internalname'] == $submitted_gateway) {
                $gateway_used = $gateway['internalname'];
                $wpdb->query("UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `gateway` = '" . $gateway_used . "' WHERE `id` = '" . $log_id . "' LIMIT 1 ;");
                $_SESSION['gateway'] = 'google';
                header('Location: ' . get_option('shopping_cart_url'));
                break;
            }
        }
        if (isset($_GET['gateway']) && $_GET['gateway'] == 'noca') {
            //exit('HERE2');
            echo transaction_results($sessionid, true);
        } else {
            //exit('HERE');
        }
    } else {
    }
}