예제 #1
0
function _wpsc_copy_billing_details()
{
    $form = WPSC_Checkout_Form::get();
    $fields = $form->get_fields();
    $fields_to_copy = array('firstname', 'lastname', 'address', 'city', 'state', 'country', 'postcode');
    $field_ids = array('shipping' => array(), 'billing' => array());
    foreach ($fields as $field) {
        if (!empty($field->unique_name) && preg_match('/^(billing|shipping)(.+)/', $field->unique_name, $matches) && in_array($matches[2], $fields_to_copy)) {
            $field_ids[$matches[1]][$matches[2]] = $field->id;
        }
    }
    $post_data =& $_POST['wpsc_checkout_details'];
    foreach ($field_ids['shipping'] as $name => $id) {
        $billing_field_id = $field_ids['billing'][$name];
        $post_data[$id] = $post_data[$billing_field_id];
    }
}
예제 #2
0
    /**
     * Display the payment gateway settings form as seen in WP eCommerce Settings area.
     * This method must be overridden by subclasses.
     *
     * @abstract
     * @access public
     * @since 3.9
     *
     * @return void
     */
    public function setup_form()
    {
        $checkout_field_types = array('billing' => __('Billing Fields', 'wp-e-commerce'), 'shipping' => __('Shipping Fields', 'wp-e-commerce'));
        $fields = array('firstname' => __('First Name', 'wp-e-commerce'), 'lastname' => __('Last Name', 'wp-e-commerce'), 'address' => __('Address', 'wp-e-commerce'), 'city' => __('City', 'wp-e-commerce'), 'state' => __('State', 'wp-e-commerce'), 'country' => __('Country', 'wp-e-commerce'), 'postcode' => __('Postal Code', 'wp-e-commerce'));
        $checkout_form = WPSC_Checkout_Form::get();
        foreach ($checkout_field_types as $field_type => $title) {
            ?>
			<tr>
				<td colspan="2">
					<h4><?php 
            echo esc_html($title);
            ?>
</h4>
				</td>
			</tr>
			<?php 
            foreach ($fields as $field_name => $field_title) {
                $unique_name = $field_type . $field_name;
                $selected_id = $this->setting->get("checkout_field_{$unique_name}", $checkout_form->get_field_id_by_unique_name($unique_name));
                ?>
				<tr>
					<td>
						<label for="manual-form-<?php 
                echo esc_attr($unique_name);
                ?>
"><?php 
                echo esc_html($field_title);
                ?>
</label>
					</td>
					<td>
						<select name="<?php 
                echo $this->setting->get_field_name("checkout_field_{$unique_name}");
                ?>
" id="manual-form-<?php 
                echo esc_attr($unique_name);
                ?>
">
							<?php 
                $checkout_form->field_drop_down_options($selected_id);
                ?>
						</select>
					</td>
				</tr>
			<?php 
            }
        }
    }
 /**
  * Pull and Record PayPal Details
  *
  * @return void
  */
 public function pull_paypal_details()
 {
     $this->set_purchase_log_for_callbacks();
     // Pull the User Details from PayPal
     $this->paypal_data = $paypal = $this->gateway->get_details_for($_GET['token']);
     $payer = $paypal->get('payer');
     $address = $paypal->get('shipping_address');
     // PurchaseLog Update
     if (isset($address['country_code'])) {
         $this->purchase_log->set('billing_country', $address['country_code']);
         $this->purchase_log->set('shipping_country', $address['country_code']);
     }
     if (isset($address['state'])) {
         $this->purchase_log->set('billing_region', $address['state']);
         $this->purchase_log->set('shipping_region', $address['state']);
     }
     // Save Checkout Form Fields
     $form = WPSC_Checkout_Form::get();
     $fields = $form->get_fields();
     $_POST['wpsc_checkout_details'] = array();
     foreach ($fields as $field) {
         $this->set_post_var($field, $payer, $address);
     }
     // Save details to the Forms Table
     WPSC_Checkout_Form_Data::save_form($this->purchase_log, $fields);
 }
예제 #4
0
 public function save_shipping_and_billing_info()
 {
     global $wpsc_cart;
     $purchase_log = $this->get_purchase_log();
     $sessionid = mt_rand(100, 999) . time();
     wpsc_update_customer_meta('checkout_session_id', $sessionid);
     $purchase_log->set(array('user_ID' => get_current_user_id(), 'date' => time(), 'plugin_version' => WPSC_VERSION, 'statusno' => '0', 'sessionid' => $sessionid));
     $form = WPSC_Checkout_Form::get();
     $fields = $form->get_fields();
     foreach ($fields as $field) {
         if (!array_key_exists($field->id, $_POST['wpsc_checkout_details'])) {
             continue;
         }
         $value = $_POST['wpsc_checkout_details'][$field->id];
         switch ($field->unique_name) {
             case 'billingstate':
                 wpsc_update_customer_meta('billing_region', $value);
                 $purchase_log->set('billing_region', $value);
                 break;
             case 'shippingstate':
                 wpsc_update_customer_meta('shipping_region', $value);
                 $purchase_log->set('shipping_region', $value);
                 break;
             case 'billingcountry':
                 wpsc_update_customer_meta('billing_country', $value);
                 $purchase_log->set('billing_country', $value);
                 break;
             case 'shippingcountry':
                 wpsc_update_customer_meta('shipping_country', $value);
                 $purchase_log->set('shipping_region', $value);
                 break;
             case 'shippingpostcode':
                 wpsc_update_customer_meta('shipping_zip', $value);
                 break;
         }
     }
     _wpsc_update_location();
     //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;
     }
     $purchase_log->set(array('wpec_taxes_total' => $tax, 'wpec_taxes_rate' => $tax_percentage));
     $purchase_log->save();
     //Check to ensure purchase log row was inserted successfully
     if (is_null($purchase_log->get('id'))) {
         $this->message_collection->add(__('A database error occured while processing your request.', 'wp-e-commerce'), 'error');
         return;
     }
     $wpsc_cart->log_id = $purchase_log->get('id');
     wpsc_update_customer_meta('current_purchase_log_id', $purchase_log->get('id'));
     WPSC_Checkout_Form_Data::save_form($purchase_log, $fields);
     $this->init_shipping_calculator();
     if (wpsc_uses_shipping() && !$this->shipping_calculator->has_quotes) {
         $this->message_collection->add(__('Sorry, but we cannot ship products to your submitted address. Please either provide another shipping address or contact the store administrator about product availability to your location.', 'wp-e-commerce'), 'error');
         return;
     }
     $this->wizard->completed_step('shipping-and-billing');
     $url = add_query_arg($_GET, wpsc_get_checkout_url($this->wizard->pending_step));
     wp_redirect($url);
     exit;
 }
예제 #5
0
 private function save_shipping_and_billing_info()
 {
     global $wpsc_cart;
     // see if an existing purchase log has been set for this user
     // otherwise create one
     $purchase_log_id = (int) wpsc_get_customer_meta('current_purchase_log_id');
     if ($purchase_log_id) {
         $purchase_log = new WPSC_Purchase_Log($purchase_log_id);
     } else {
         $purchase_log = new WPSC_Purchase_Log();
     }
     $sessionid = mt_rand(100, 999) . time();
     wpsc_update_customer_meta('checkout_session_id', $sessionid);
     $purchase_log->set(array('user_ID' => wpsc_get_current_customer_id(), 'date' => time(), 'plugin_version' => WPSC_VERSION, 'statusno' => '0', 'sessionid' => $sessionid));
     $form = WPSC_Checkout_Form::get();
     $fields = $form->get_fields();
     foreach ($fields as $field) {
         if (!array_key_exists($field->id, $_POST['wpsc_checkout_details'])) {
             continue;
         }
         $value = $_POST['wpsc_checkout_details'][$field->id];
         switch ($field->unique_name) {
             case 'billingstate':
                 wpsc_update_customer_meta('billing_region', $value);
                 $purchase_log->set('billing_region', $value);
                 break;
             case 'shippingstate':
                 wpsc_update_customer_meta('shipping_region', $value);
                 $purchase_log->set('shipping_region', $value);
                 break;
             case 'billingcountry':
                 wpsc_update_customer_meta('billing_country', $value);
                 $purchase_log->set('billing_country', $value);
                 break;
             case 'shippingcountry':
                 wpsc_update_customer_meta('shipping_country', $value);
                 $purchase_log->set('shipping_region', $value);
                 break;
             case 'shippingpostcode':
                 wpsc_update_customer_meta('shipping_zip', $value);
                 break;
         }
     }
     _wpsc_update_location();
     if (wpsc_is_tax_included()) {
         $tax = $wpsc_cart->calculate_total_tax();
         $tax_percentage = $wpsc_cart->tax_percentage;
     } else {
         $tax = 0;
         $tax_percentage = 0;
     }
     $purchase_log->set(array('wpec_taxes_total' => $tax, 'wpec_taxes_rate' => $tax_percentage));
     $purchase_log->save();
     $wpsc_cart->log_id = $purchase_log->get('id');
     wpsc_update_customer_meta('current_purchase_log_id', $purchase_log->get('id'));
     $this->save_form($purchase_log, $fields);
     $this->init_shipping_calculator();
     if (wpsc_uses_shipping() && !$this->shipping_calculator->has_quotes) {
         $this->message_collection->add(__('Sorry but we cannot ship products to your submitted address. Please either provide another shipping address or contact the store administrator about product availability to your location.', 'wpsc'), 'error');
         return;
     }
     $this->wizard->completed_step('shipping-and-billing');
     wp_redirect(wpsc_get_checkout_url($this->wizard->pending_step));
     exit;
 }
예제 #6
0
 private function save_customer_settings()
 {
     $form = WPSC_Checkout_Form::get();
     $fields = $form->get_fields();
     $customer_details = wpsc_get_customer_meta('checkout_details');
     if (!is_array($customer_details)) {
         $customer_details = array();
     }
     foreach ($fields as $field) {
         if (!array_key_exists($field->id, $_POST['wpsc_checkout_details'])) {
             continue;
         }
         $value = $_POST['wpsc_checkout_details'][$field->id];
         $customer_details[$field->id] = $value;
         switch ($field->unique_name) {
             case 'billingstate':
                 wpsc_update_customer_meta('billing_region', $value);
                 break;
             case 'shippingstate':
                 wpsc_update_customer_meta('shipping_region', $value);
                 break;
             case 'billingcountry':
                 wpsc_update_customer_meta('billing_country', $value);
                 break;
             case 'shippingcountry':
                 wpsc_update_customer_meta('shipping_country', $value);
                 break;
             case 'shippingpostcode':
                 wpsc_update_customer_meta('shipping_zip', $value);
                 break;
         }
     }
     _wpsc_update_location();
     wpsc_save_customer_details($customer_details);
 }
 /**
  * Used in conjunction with set() method, saves individual checkout form fields to database.
  *
  * @since  4.0
  * @return void
  */
 public function save()
 {
     $log = new WPSC_Purchase_Log($this->log_id);
     $form = WPSC_Checkout_Form::get();
     $fields = $form->get_fields();
     $original_data = wp_list_pluck($this->get_raw_data(), 'value', 'id');
     $this->submitted_data = array_replace($original_data, $this->submitted_data);
     return self::save_form($log, $fields, $this->submitted_data);
 }
예제 #8
0
 public function set_customer_details()
 {
     $_POST['wpsc_checkout_details'] = array();
     $_GET['amazon_reference_id'] = sanitize_text_field($_POST['amazon_reference_id']);
     try {
         if (!$this->reference_id) {
             throw new Exception(__('An Amazon payment method was not chosen.', 'wpsc'));
         }
         if (is_null($this->purchase_log)) {
             $log = _wpsc_get_current_controller()->get_purchase_log();
             wpsc_update_customer_meta('current_purchase_log_id', $log->get('id'));
             $this->set_purchase_log($log);
         }
         global $wpsc_cart;
         // Update order reference with amounts
         $response = $this->api_request(array('Action' => 'SetOrderReferenceDetails', 'AmazonOrderReferenceId' => $this->reference_id, 'OrderReferenceAttributes.OrderTotal.Amount' => $wpsc_cart->calculate_total_price(), 'OrderReferenceAttributes.OrderTotal.CurrencyCode' => strtoupper($this->get_currency_code()), 'OrderReferenceAttributes.SellerNote' => sprintf(__('Order %s from %s.', 'wpsc'), $this->purchase_log->get('id'), urlencode(remove_accents(wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES)))), 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId' => $this->purchase_log->get('id'), 'OrderReferenceAttributes.SellerOrderAttributes.StoreName' => remove_accents(wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES)), 'OrderReferenceAttributes.PlatformId' => 'A2Z8DY3R4G08IM'));
         if (is_wp_error($response)) {
             throw new Exception($response->get_error_message());
         }
         if (isset($response['Error']['Message'])) {
             throw new Exception($response['Error']['Message']);
         }
         $response = $this->api_request(array('Action' => 'GetOrderReferenceDetails', 'AmazonOrderReferenceId' => $this->reference_id));
         if (is_wp_error($response)) {
             throw new Exception($response->get_error_message());
         }
         if (!isset($response['GetOrderReferenceDetailsResult']['OrderReferenceDetails']['Destination']['PhysicalDestination'])) {
             return;
         }
         $address = $response['GetOrderReferenceDetailsResult']['OrderReferenceDetails']['Destination']['PhysicalDestination'];
         remove_action('wpsc_checkout_get_fields', '__return_empty_array');
         add_filter('wpsc_validate_form', '__return_true');
         $form = WPSC_Checkout_Form::get();
         $fields = $form->get_fields();
         foreach ($fields as $field) {
             switch ($field->unique_name) {
                 case 'shippingstate':
                     $_POST['wpsc_checkout_details'][$field->id] = WPSC_Countries::get_region_id($address['CountryCode'], $address['StateOrRegion']);
                     break;
                 case 'shippingcountry':
                     $_POST['wpsc_checkout_details'][$field->id] = $address['CountryCode'];
                     break;
                 case 'shippingpostcode':
                     $_POST['wpsc_checkout_details'][$field->id] = $address['PostalCode'];
                     break;
                 case 'shippingcity':
                     $_POST['wpsc_checkout_details'][$field->id] = $address['City'];
                     break;
             }
         }
     } catch (Exception $e) {
         WPSC_Message_Collection::get_instance()->add($e->getMessage(), 'error', 'main', 'flash');
         return;
     }
 }
예제 #9
0
function _wpsc_convert_checkout_form_fields($customer_settings = false)
{
    $form = WPSC_Checkout_Form::get();
    $fields = $form->get_fields();
    $args = array();
    $purchase_log_exists = false;
    if (!$customer_settings) {
        $purchase_log_id = wpsc_get_customer_meta('current_purchase_log_id');
        $purchase_log_exists = (bool) $purchase_log_id;
        if ($purchase_log_exists) {
            $form_data_obj = new WPSC_Checkout_Form_Data($purchase_log_id);
            $form_raw_data = $form_data_obj->get_raw_data();
            $form_data = array();
            foreach ($form_raw_data as $data) {
                $form_data[$data->id] = $data;
            }
        }
    }
    $i = 0;
    $state_country_pairs = array('billing_state' => array(), 'shipping_state' => array());
    $customer_details = wpsc_get_customer_meta('checkout_details');
    if (!is_array($customer_details)) {
        $customer_details = array();
    }
    foreach ($fields as $field) {
        $id = empty($field->unique_name) ? $field->id : $field->unique_name;
        $default_value = array_key_exists($field->id, $customer_details) ? $customer_details[$field->id] : '';
        if ($purchase_log_exists && $field->type != 'heading' && isset($form_data[$field->id])) {
            $default_value = $form_data[$field->id]->value;
        }
        $field_arr = array('type' => $field->type, 'id' => "wpsc-checkout-field-{$id}", 'title' => esc_html($field->name), 'name' => 'wpsc_checkout_details[' . $field->id . ']', 'value' => wpsc_submitted_value($field->id, $default_value, $_POST['wpsc_checkout_details']));
        $validation_rules = array('trim');
        if ($field->mandatory) {
            $validation_rules[] = 'required';
        }
        $optional_state_field = false;
        if (in_array($field->unique_name, array('billingstate', 'shippingstate'))) {
            $field_arr['type'] = 'select_region';
            /* output states for all countries just in case Javascript doesn't work */
            $field_arr['country'] = 'all';
            if ($field->unique_name == 'billingstate') {
                $state_country_pairs['billing_state']['key'] = $i;
            } else {
                $state_country_pairs['shipping_state']['key'] = $i;
            }
            // optional text field in case the country they select do not have states
            // and JS is disabled either by preferences or on error
            $optional_state_field = true;
            // convert state values in text into proper ID
            $validation_rules[] = '_wpsc_convert_state';
        } elseif (in_array($field->unique_name, array('billingcountry', 'shippingcountry')) || $field->type == 'delivery_country') {
            $field_arr['type'] = 'select_country';
            $validation_rules[] = 'country';
            if ($field->unique_name == 'billingcountry') {
                $state_country_pairs['billing_state']['country_field_id'] = $field->id;
            } else {
                $state_country_pairs['shipping_state']['country_field_id'] = $field->id;
            }
        } elseif ($field->type == 'text') {
            $field_arr['type'] = 'textfield';
        } elseif ($field->type == 'select') {
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif ($field->type == 'radio') {
            $field_arr['type'] = 'radios';
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif ($field->type == 'checkbox') {
            $field_arr['type'] = 'checkboxes';
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif (in_array($field->type, array('address', 'city', 'email'))) {
            $field_arr['type'] = 'textfield';
            if ($field->type == 'email') {
                $validation_rules[] = 'email';
            }
        } elseif ($field->type == 'heading' && $field->unique_name == 'delivertoafriend') {
            $field_arr['shipping_heading'] = true;
        }
        $field_arr['rules'] = implode('|', $validation_rules);
        $args[$i] = $field_arr;
        $i++;
        if ($optional_state_field) {
            $args[$i] = $args[$i - 1];
            $args[$i]['type'] = 'textfield';
            $args[$i]['id'] = 'wpsc-checkout-field-' . $id . '-text';
            $i++;
        }
    }
    if (wpsc_has_tnc() && !$customer_settings) {
        $args[] = array('type' => 'checkbox', 'id' => 'wpsc-terms-and-conditions', 'title' => sprintf(__("I agree to the <a class='thickbox' target='_blank' href='%s' class='termsandconds'>Terms and Conditions</a>", "wpsc"), esc_url(site_url("?termsandconds=true&amp;width=360&amp;height=400"))), 'value' => 1, 'name' => 'wpsc_terms_conditions', 'rules' => 'required', 'checked' => wpsc_submitted_value('wpsc_terms_conditions', 0) == 1);
    }
    foreach ($state_country_pairs as $field) {
        $args[$field['key']]['rules'] .= '|state_of[' . $field['country_field_id'] . ']';
        $args[$field['key']]['rules'] = ltrim($args[$field['key']]['rules'], '|');
    }
    return $args;
}
예제 #10
0
function _wpsc_convert_checkout_form_fields($customer_settings = false)
{
    $form = WPSC_Checkout_Form::get();
    $fields = $form->get_fields();
    if (empty($fields)) {
        return array();
    }
    $args = array();
    $purchase_log_exists = false;
    $fieldsets = array('billing' => array('type' => 'fieldset', 'title' => apply_filters('wpsc_checkout_billing_header_label', __('<h2>Billing &amp; Shipping Details</h2>', 'wp-e-commerce')), 'id' => 'wpsc-checkout-form-billing', 'fields' => array()), 'shipping' => array('type' => 'fieldset', 'title' => apply_filters('wpsc_checkout_shipping_header_label', __('<h2>Shipping Details</h2>', 'wp-e-commerce')), 'id' => 'wpsc-checkout-form-shipping', 'fields' => array()));
    if (!$customer_settings) {
        $purchase_log_id = wpsc_get_customer_meta('current_purchase_log_id');
        $purchase_log_exists = (bool) $purchase_log_id;
        if ($purchase_log_exists) {
            $form_data_obj = new WPSC_Checkout_Form_Data($purchase_log_id);
            $form_raw_data = $form_data_obj->get_raw_data();
            $form_data = array();
            foreach ($form_raw_data as $data) {
                $form_data[$data->id] = $data;
            }
        }
    }
    $i = 0;
    $state_country_pairs = array('billing_state' => array(), 'shipping_state' => array());
    $customer_details = wpsc_get_customer_meta('checkout_details');
    if (!is_array($customer_details)) {
        $customer_details = array();
    }
    foreach ($fields as $field) {
        $id = empty($field->unique_name) ? $field->id : $field->unique_name;
        $is_shipping = false !== strpos($field->unique_name, 'shipping');
        $is_billing = false !== strpos($field->unique_name, 'billing');
        $default_value = array_key_exists($field->id, $customer_details) ? $customer_details[$field->id] : '';
        /* Doing our college-best to check for one of the two original headings */
        if ('heading' == $field->type && ('delivertoafriend' == $field->unique_name || '1' === $field->id)) {
            continue;
        }
        if ($purchase_log_exists && isset($form_data[$field->id])) {
            $default_value = $form_data[$field->id]->value;
        }
        if (isset($_POST['wpsc_checkout_details'])) {
            $_POST['wpsc_checkout_details'] = wp_unslash($_POST['wpsc_checkout_details']);
        }
        $field_arr = array('type' => $field->type, 'id' => "wpsc-checkout-field-{$id}", 'title' => esc_html($field->name), 'name' => 'wpsc_checkout_details[' . $field->id . ']', 'value' => wpsc_submitted_value($field->id, wp_unslash($default_value), $_POST['wpsc_checkout_details']));
        $validation_rules = array('trim');
        if ($field->mandatory) {
            $validation_rules[] = 'required';
        }
        $optional_state_field = false;
        if (in_array($field->unique_name, array('billingstate', 'shippingstate'))) {
            $field_arr['type'] = 'select_region';
            /* output states for all countries just in case Javascript doesn't work */
            $field_arr['country'] = 'all';
            if ($field->unique_name == 'billingstate') {
                $state_country_pairs['billing_state']['key'] = $i;
            } else {
                $state_country_pairs['shipping_state']['key'] = $i;
            }
            // optional text field in case the country they select do not have states
            // and JS is disabled either by preferences or on error
            $optional_state_field = true;
            // convert state values in text into proper ID
            $validation_rules[] = '_wpsc_convert_state';
        } elseif (in_array($field->unique_name, array('billingcountry', 'shippingcountry')) || $field->type == 'delivery_country') {
            $field_arr['type'] = 'select_country';
            $validation_rules[] = 'country';
            if ($field->unique_name == 'billingcountry') {
                $state_country_pairs['billing_state']['country_field_id'] = $field->id;
            } else {
                $state_country_pairs['shipping_state']['country_field_id'] = $field->id;
            }
        } elseif ($field->type == 'text') {
            $field_arr['type'] = 'textfield';
        } elseif ($field->type == 'select') {
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif ($field->type == 'radio') {
            $field_arr['type'] = 'radios';
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif ($field->type == 'checkbox') {
            $field_arr['type'] = 'checkboxes';
            $field_arr['options'] = array_flip(unserialize($field->options));
        } elseif (in_array($field->type, array('address', 'city', 'email'))) {
            $field_arr['type'] = 'textfield';
            if ($field->type == 'email') {
                $validation_rules[] = 'email';
            }
        } elseif ($field->type == 'heading' && $field->unique_name == 'delivertoafriend') {
            $field_arr['shipping_heading'] = true;
        }
        $field_arr['rules'] = implode('|', $validation_rules);
        if ($is_shipping) {
            $fieldsets['shipping']['fields'][$i] = $field_arr;
        } else {
            if ($is_billing) {
                $fieldsets['billing']['fields'][$i] = $field_arr;
            } else {
                $args[$i] = $field_arr;
            }
        }
        $i++;
        if ($optional_state_field && $is_billing) {
            $fieldsets['billing']['fields'][$i] = $fieldsets['billing']['fields'][$i - 1];
            $fieldsets['billing']['fields'][$i]['type'] = 'textfield';
            $fieldsets['billing']['fields'][$i]['id'] = 'wpsc-checkout-field-' . $id . '-text';
            $i++;
        } else {
            if ($optional_state_field && $is_shipping) {
                $fieldsets['shipping']['fields'][$i] = $fieldsets['shipping']['fields'][$i - 1];
                $fieldsets['shipping']['fields'][$i]['type'] = 'textfield';
                $fieldsets['shipping']['fields'][$i]['id'] = 'wpsc-checkout-field-' . $id . '-text';
            }
        }
    }
    if (wpsc_has_tnc() && !$customer_settings) {
        $args[] = array('type' => 'checkbox', 'id' => 'wpsc-terms-and-conditions', 'title' => sprintf(__("I agree to the <a class='thickbox' target='_blank' href='%s' class='termsandconds'>Terms and Conditions</a>", 'wp-e-commerce'), esc_url(add_query_arg(array('termsandconds' => 'true', 'width' => 360, 'height' => 400)))), 'value' => 1, 'name' => 'wpsc_terms_conditions', 'rules' => 'required', 'checked' => wpsc_submitted_value('wpsc_terms_conditions', 0) == 1);
    }
    foreach ($state_country_pairs as $state => $field) {
        $is_shipping = 'shipping_state' == $state;
        if (isset($field['key']) && $is_shipping) {
            $fieldsets['shipping']['fields'][$field['key']]['rules'] .= '|state_of[' . $field['country_field_id'] . ']';
            $fieldsets['shipping']['fields'][$field['key']]['rules'] = ltrim($fieldsets['shipping']['fields'][$field['key']]['rules'], '|');
        } else {
            if (isset($field['key'])) {
                $fieldsets['billing']['fields'][$field['key']]['rules'] .= '|state_of[' . $field['country_field_id'] . ']';
                $fieldsets['billing']['fields'][$field['key']]['rules'] = ltrim($fieldsets['billing']['fields'][$field['key']]['rules'], '|');
            }
        }
    }
    /* Add 'shipping same as billing' box to end of billing, rather than shipping header. */
    if (!empty($fieldsets['billing']['fields']) && !empty($fieldsets['shipping']['fields'])) {
        $checked = wpsc_get_customer_meta('wpsc_copy_billing_details');
        $fieldsets['billing']['fields'][$i++] = array('type' => 'checkbox', 'id' => 'wpsc-terms-and-conditions', 'title' => apply_filters('wpsc_shipping_same_as_billing', __('Shipping address is same as billing', 'wp-e-commerce')), 'value' => 1, 'name' => 'wpsc_copy_billing_details', 'checked' => empty($checked) || '1' == $checked);
    }
    if (empty($fieldsets['billing']['fields'])) {
        unset($fieldsets['billing']);
    }
    if (empty($fieldsets['shipping']['fields'])) {
        unset($fieldsets['shipping']);
    }
    return $fieldsets + $args;
}