public function prepare_fields()
 {
     $fields = array();
     $invoice_id = false;
     if (isset($this->data['invoice'])) {
         $invoice = $this->data['invoice'];
         $invoice_id = $invoice->id;
     }
     $profile = MS_Addon_Taxamo_Api::get_tax_profile();
     $countries = MS_Addon_Taxamo_Api::get_country_codes();
     $action = MS_Addon_Taxamo::AJAX_SAVE_USERPROFILE;
     $nonce = wp_create_nonce($action);
     $country_options = array('auto' => sprintf(__('The detected country %s is correct.', MS_TEXT_DOMAIN), '<strong>' . $profile->detected_country->name . '</strong>'), 'vat' => __('I have an EU VAT number and want to use it for tax declaration.', MS_TEXT_DOMAIN), 'declared' => __('Manually declare my country of residence.', MS_TEXT_DOMAIN));
     $vat_details = '';
     if (!empty($profile->vat_number) && $profile->vat_valid) {
         $vat_details = sprintf(__('This is a valid VAT number of %s. By using this you are are now exempt of VAT.', MS_TEXT_DOMAIN), '<strong>' . $profile->vat_country->name . '</strong>');
     } else {
         $vat_details = __('VAT Number is invalid.', MS_TEXT_DOMAIN);
     }
     if ($profile->use_vat_number) {
         $tax_message = __('Valid EU VAT Number provided: You are exempt of VAT', MS_TEXT_DOMAIN);
     } else {
         $tax_message = __('The country used for tax calculation is %s', MS_TEXT_DOMAIN);
     }
     $fields['tax_country_label'] = array('type' => MS_Helper_Html::TYPE_HTML_TEXT, 'title' => sprintf($tax_message, '<strong>' . $profile->tax_country->name . '</strong>'), 'wrapper_class' => 'effective_tax_country');
     $fields['detected_country_label'] = array('type' => MS_Helper_Html::TYPE_HTML_TEXT, 'title' => sprintf(__('We have detected that your computer is located in %s', MS_TEXT_DOMAIN), '<strong>' . $profile->detected_country->name . '</strong>'));
     $fields['detected_country'] = array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'detected_country', 'value' => $profile->detected_country->code);
     $fields['country_choice'] = array('type' => MS_Helper_Html::INPUT_TYPE_RADIO, 'id' => 'country_choice', 'class' => 'country_choice', 'value' => $profile->country_choice, 'field_options' => $country_options);
     $fields['declared_country_code'] = array('type' => MS_Helper_Html::INPUT_TYPE_SELECT, 'id' => 'declared_country', 'title' => __('My country of residence', MS_TEXT_DOMAIN), 'desc' => __('I confirm that I am established, have my permanent address, or usually reside in the following country', MS_TEXT_DOMAIN), 'value' => $profile->declared_country->code, 'field_options' => $countries, 'wrapper_class' => 'manual_country_field');
     $fields['vat_number'] = array('type' => MS_Helper_Html::INPUT_TYPE_TEXT, 'id' => 'vat_number', 'title' => __('EU VAT Number', MS_TEXT_DOMAIN), 'desc' => __('Fill this field if you are representing EU VAT payer', MS_TEXT_DOMAIN), 'wrapper_class' => 'vat_number_field', 'value' => $profile->vat_number, 'valid_country' => $profile->vat_country->vat_valid, 'after' => $vat_details);
     $fields['invoice_id'] = array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'invoice_id', 'value' => $invoice_id);
     $fields['action'] = array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => 'action', 'value' => $action);
     $fields['_wpnonce'] = array('type' => MS_Helper_Html::INPUT_TYPE_HIDDEN, 'id' => '_wpnonce', 'value' => $nonce);
     /*
      * 1. Checkbox "I confirm that the country of my main residence is in <country>" (in the payment table!)
      * 4. When VAT is entered the checkbox is disabled and VAT country is used. Checkbox 1 is hidden.
      */
     return apply_filters('ms_addon_taxamo_userprofile_fields', $fields);
 }
 /**
  * Returns a list of all taxamo relevant EU countries.
  *
  * @since  1.0.0
  * @api
  *
  * @param  string $type [prefix|name|vat]
  *         name ..   code => "name"
  *         prefix .. code => "prefix - name"
  *         vat ..    vat-prefix => code
  * @return array
  */
 public static function get_country_codes($type = 'prefix')
 {
     if (null === self::$Countries) {
         $country_names = MS_Gateway::get_country_codes();
         // Country names in current language.
         $list = get_site_transient('ms_taxamo_countries');
         $list = false;
         if (!$list || !is_array($list)) {
             $resp = self::taxamo()->getCountriesDict('true');
             $list = array();
             foreach ($resp->dictionary as $item) {
                 $list[$item->code] = array('name' => $item->name, 'vat' => $item->tax_number_country_code);
             }
             set_site_transient('ms_taxamo_countries', $list, WEEK_IN_SECONDS);
         }
         self::$Countries = array();
         self::$Countries_Prefix = array();
         self::$Countries_Vat = array();
         foreach ($list as $code => $item) {
             if (isset($country_names[$code])) {
                 $item['name'] = $country_names[$code];
             }
             self::$Countries[$code] = $item['name'];
             self::$Countries_Prefix[$code] = $code . ' - ' . $item['name'];
             self::$Countries_Vat[$item['vat']] = $code;
         }
         self::$Countries['XX'] = '- ' . __('Outside the EU', 'membership2') . ' -';
         self::$Countries_Prefix['XX'] = '- ' . __('Outside the EU', 'membership2') . ' -';
     }
     switch ($type) {
         case 'prefix':
             return self::$Countries_Prefix;
         case 'vat':
             return self::$Countries_Vat;
         case 'name':
         default:
             return self::$Countries;
     }
 }
 /**
  * Handle Ajax action to update a user tax-profile field.
  *
  * @since  1.0.0
  */
 public function ajax_save_userprofile()
 {
     $response = '';
     $isset = array('country_choice', 'declared_country', 'vat_number');
     if ($this->verify_nonce() && self::validate_required($isset, 'POST', false)) {
         $invoice_id = intval($_POST['invoice_id']);
         $invoice = MS_Factory::load('MS_Model_Invoice', $invoice_id);
         $data = $_POST;
         unset($data['invoice_id']);
         unset($data['action']);
         unset($data['_wpnonce']);
         $data['declared_country'] = (object) array('code' => $data['declared_country']);
         foreach ($data as $field => $value) {
             $value = apply_filters('ms_addon_taxamo_userprofile_value', $value, $field, $invoice_id, $this);
             MS_Addon_Taxamo_Api::set_tax_profile($field, $value);
         }
         // User profile updated. Now update the tax-rate in the invoice.
         if ($invoice->is_valid()) {
             $profile = MS_Addon_Taxamo_Api::get_tax_profile();
             $invoice->set_custom_data('tax_profile', $profile);
             $invoice->total_amount_changed();
             $invoice->save();
         }
         $view = MS_Factory::load('MS_Addon_Taxamo_Userprofile');
         $view->data = apply_filters('ms_addon_taxamo_editor_data', array('invoice' => $invoice), $this);
         $response = trim($view->to_html());
     }
     wp_die($response);
 }