$address['customer_id'] = $user;
         // Main and current addresses are saved in session only (not in DB)
         if (in_array($address['address_id'], array('main', 'current'))) {
             $user_address[$address['address_id'] . '_address'] = $address;
         }
         // If main is updated and current is defined as "same", then copy address to current
         if ($address['address_id'] == 'main' && $user_address['main_address']['address_id'] == $user_address['current_address']['address_id']) {
             $user_address['current_address'] = $user_address['main_address'];
         }
         if ($address['as_new']) {
             $address['address_id'] = 0;
             unset($address['as_new']);
         }
         if (!in_array($address['address_id'], array('main', 'current'), true)) {
             $address_id = intval($address['address_id']);
             $naid = cw_user_update_address($user, $address_id, $address);
             $address_id = $naid;
             // For correct shipping cost calculate after address changing
             cw_load('user');
             $user_address =& cw_session_register('user_address');
             $user_address = array();
             global $userinfo;
             $userinfo['current_address'] = cw_user_get_address_by_type('current');
         }
         cw_add_top_message('Address has been updated');
     }
 }
 cw_user_check_addresses($user);
 $action = 'load';
 if ($is_checkout && empty($fill_error)) {
     $action = 'load_checkout';
function cw_user_update($userinfo, $customer_id, $by_customer_id)
{
    global $tables, $addons;
    foreach (array('email', 'status', 'membership_id', 'language', 'change_password') as $fld) {
        if (isset($userinfo[$fld])) {
            $customer[$fld] = $userinfo[$fld];
        }
    }
    if (AREA_TYPE == 'A' && $userinfo['usertype']) {
        $customer['usertype'] = $userinfo['usertype'];
    }
    if (AREA_TYPE == 'A' && $userinfo['customer_id']) {
        $customer['customer_id'] = $userinfo['customer_id'];
    }
    $current = cw_user_get_info($customer_id, 0);
    //allow password update only by the profile owner or by admin
    if (isset($userinfo['password'])) {
        if (!empty($userinfo['password']) && ($customer_id == $by_customer_id || AREA_TYPE == 'A')) {
            $customer['password'] = cw_call('cw_user_get_hashed_password', array($userinfo['password']));
        }
    }
    cw_event('on_user_update', array($customer_id, $by_customer_id, $customer, $userinfo));
    cw_array2update('customers', $customer, "customer_id='{$customer_id}'");
    $additional_info = $userinfo['additional_info'];
    $customer_info_fields = array('ssn', 'tax_number', 'birthday', 'birthday_place', 'sex', 'married', 'nationality', 'company', 'employees', 'foundation', 'foundation_place', 'company_type', 'company_id', 'can_change_company_id', 'contact_date', 'tax_id', 'payment_id', 'payment_note', 'tax_exempt', 'separate_invoices', 'shipping_operated', 'shipment_paid', 'shipping_company_to_carrier_id', 'shipping_company_from_carrier_id', 'cod_delivery_type_id', 'leaving_type', 'department_id', 'division_id', 'doc_prefix', 'order_entering_format', 'status_note');
    if (!cw_query_first_cell("select count(*) from {$tables['customers_customer_info']} where customer_id='{$customer_id}'")) {
        cw_array2insert('customers_customer_info', array('customer_id' => $customer_id));
    }
    cw_array2update('customers_customer_info', $additional_info, "customer_id='{$customer_id}'", $customer_info_fields);
    /*
          db_query("update $tables[customers_addresses] set main=0, current=0 where customer_id = '$customer_id'");
        foreach(array('main_address', 'current_address') as $addr)  {
            if (!$userinfo[$addr]) continue;
    
            $userinfo[$addr]['main'] = $addr == 'main_address';
            $userinfo[$addr]['current'] = $addr == 'current_address';
    
            $address_id = $userinfo[$addr]['address_id'];
            if (!$address_id) $address_id = cw_array2insert('customers_addresses', array('customer_id' => $customer_id, 'main' => $userinfo[$addr]['main'], 'current' => $userinfo[$addr]['current']));
    
            cw_user_update_address($customer_id, $address_id, $userinfo[$addr]);
        }
    */
    foreach (cw_user_address_array($userinfo['addresses']) as $address_id => $address) {
        cw_user_update_address($customer_id, $address['address_id'], $address);
    }
    cw_user_check_addresses($customer_id);
    $relations = $userinfo['relations'];
    $relations_info = array('salesman_customer_id' => $relations['salesman_customer_id'], 'employee_customer_id' => $relations['employee_customer_id'], 'warehouse_customer_id' => $relations['warehouse_customer_id']);
    if (!cw_query_first_cell("select count(*) from {$tables['customers_relations']} where customer_id='{$customer_id}'")) {
        db_query("insert into {$tables['customers_relations']}(customer_id) values('{$customer_id}')");
    }
    cw_array2update('customers_relations', $relations_info, "customer_id='{$customer_id}'");
    if ($userinfo['chamber_certificate_uploaded']['file_path']) {
        db_query("delete from {$tables['customers_chamber_certificates']} where customer_id='{$customer_id}'");
        cw_file_area_save('customers_chamber_certificates', $customer_id, $userinfo['chamber_certificate_uploaded']);
    }
    if (is_array($userinfo['custom_fields'])) {
        foreach ($userinfo['custom_fields'] as $field_id => $value) {
            db_query("delete from {$tables['register_fields_values']} where customer_id='{$customer_id}' AND field_id='{$field_id}'");
            cw_array2insert('register_fields_values', array('field_id' => $field_id, 'value' => $value, 'customer_id' => $customer_id));
        }
    }
    $customer_system_info = cw_query_first("select * from {$tables['customers_system_info']} where customer_id='{$customer_id}'");
    $customer_system_info['customer_id'] = $customer_id;
    if (!$customer_system_info['creation_customer_id']) {
        $customer_system_info['creation_customer_id'] = $by_customer_id;
        $customer_system_info['creation_date'] = cw_core_get_time();
    }
    $customer_system_info['modification_customer_id'] = $by_customer_id;
    $customer_system_info['modification_date'] = cw_core_get_time();
    cw_array2insert('customers_system_info', $customer_system_info, true);
    $salesman_info = cw_query_first("select * from {$tables['customers_salesman_info']} where customer_id = '{$customer_id}'");
    $salesman_info['parent_customer_id'] = $userinfo['salesman_info']['parent_customer_id'];
    cw_array2insert('customers_salesman_info', $salesman_info, true);
}
      </GetExpressCheckoutDetailsRequest>
    </GetExpressCheckoutDetailsReq>
  </soap:Body>
</soap:Envelope>
EOT;
    $result = cw_func_call('cw_paypal_express_request', array('request' => $request));
    $state_err = 0;
    $address = array('firstname' => empty($result['address']['FirstName']) ? $result['FirstName'] : $result['address']['FirstName'], 'lastname' => empty($result['address']['LastName']) ? $result['LastName'] : $result['address']['LastName'], 'address' => preg_replace('![\\s\\n\\r]+!s', ' ', $result['address']['Street1']) . "\n" . preg_replace('![\\s\\n\\r]+!s', ' ', @$result['address']['Street2']), 'city' => $result['address']['CityName'], 'country' => $result['address']['Country'], 'zipcode' => $result['address']['PostalCode'], 'phone' => empty($result['address']['Phone']) ? $result['ContactPhone'] : $result['address']['Phone'], 'state' => cw_paypal_express_detect_state($result['address']['Country'], $result['address']['StateOrProvince'], $state_err));
    if ($config["General"]["use_counties"] == "Y") {
        $default_county = cw_default_county($address['state'], $address['country']);
        $address['county'] = empty($default_county) ? $result['address']['StateOrProvince'] : $default_county;
    }
    $customer_id =& cw_session_register('customer_id');
    if ($customer_id) {
        $address_id = cw_query_first_cell("select address_id from {$tables['customers_addresses']} where customer_id='{$customer_id}' and current=1");
        cw_user_update_address($customer_id, $address_id, cw_addslashes($address));
    } else {
        $profile_values = array('email' => $result['Payer'], 'current_address' => $address);
        $customer_id = cw_user_create_profile(array('usertype' => 'C'));
        $profile_values['status'] = 'Y';
        cw_user_update($profile_values, $customer_id, $customer_id);
        $identifiers =& cw_session_register("identifiers", array());
        $identifiers['C'] = array('customer_id' => $customer_id);
    }
    $paypal_express_details = $result;
    switch ($state_err) {
        case 1:
            $top_message = array("type" => "W", "content" => cw_get_langvar_by_name("lbl_paypal_wrong_country_note"));
            break;
        case 2:
            $top_message = array("type" => "W", "content" => cw_get_langvar_by_name("lbl_paypal_wrong_state_note"));