/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $config = $this->config('uc_store.settings');
     $form['store'] = array('#type' => 'vertical_tabs');
     $form['basic'] = array('#type' => 'details', '#title' => $this->t('Basic information'), '#group' => 'store');
     $form['basic']['uc_store_name'] = array('#type' => 'textfield', '#title' => $this->t('Store name'), '#default_value' => uc_store_name());
     $form['basic']['uc_store_email'] = array('#type' => 'email', '#title' => $this->t('E-mail address'), '#size' => 32, '#required' => TRUE, '#default_value' => uc_store_email());
     $form['basic']['uc_store_email_include_name'] = array('#type' => 'checkbox', '#title' => $this->t('Include the store name in the "From" line of store e-mails.'), '#description' => $this->t('May not be available on all server configurations. Turn off if this causes problems.'), '#default_value' => $config->get('mail_include_name'));
     $form['basic']['uc_store_phone'] = array('#type' => 'tel', '#title' => $this->t('Phone number'), '#default_value' => $config->get('phone'));
     $form['basic']['uc_store_fax'] = array('#type' => 'tel', '#title' => $this->t('Fax number'), '#default_value' => $config->get('fax'));
     $form['basic']['uc_store_help_page'] = array('#type' => 'textfield', '#title' => $this->t('Store help page'), '#description' => $this->t('The Drupal page for the store help link.'), '#default_value' => $config->get('help_page'), '#size' => 32, '#field_prefix' => $this->url('<front>', [], ['absolute' => TRUE]));
     $form['address'] = array('#type' => 'details', '#title' => $this->t('Store address'), '#group' => 'store');
     $form['address']['address'] = array('#type' => 'uc_address', '#default_value' => array('uc_store_street1' => $config->get('address.street1'), 'uc_store_street2' => $config->get('address.street2'), 'uc_store_city' => $config->get('address.city'), 'uc_store_zone' => $config->get('address.zone'), 'uc_store_country' => $form_state->hasValue('uc_store_country') ? $form_state->getValue('uc_store_country') : $config->get('address.country'), 'uc_store_postal_code' => $config->get('address.postal_code')), '#required' => FALSE, '#key_prefix' => 'uc_store');
     $form['currency'] = array('#type' => 'details', '#title' => $this->t('Currency format'), '#group' => 'store');
     $form['currency']['uc_currency_code'] = array('#type' => 'textfield', '#title' => $this->t('Currency code'), '#description' => $this->t('While not used directly in formatting, the currency code is used by other modules as the primary currency for your site.  Enter here your three character <a href=":url">ISO 4217</a> currency code.', [':url' => Url::fromUri('http://en.wikipedia.org/wiki/ISO_4217#Active_codes')->toString()]), '#default_value' => $config->get('currency.code'), '#maxlength' => 3, '#size' => 5);
     $form['currency']['example'] = array('#type' => 'textfield', '#title' => $this->t('Current format'), '#value' => uc_currency_format(1000.1234), '#disabled' => TRUE, '#size' => 10);
     $form['currency']['uc_currency_sign'] = array('#type' => 'textfield', '#title' => $this->t('Currency sign'), '#default_value' => $config->get('currency.symbol'), '#size' => 10, '#maxlength' => 10);
     $form['currency']['uc_sign_after_amount'] = array('#type' => 'checkbox', '#title' => $this->t('Display currency sign after amount.'), '#default_value' => $config->get('currency.symbol_after'));
     $form['currency']['uc_currency_thou'] = array('#type' => 'textfield', '#title' => $this->t('Thousands marker'), '#default_value' => $config->get('currency.thousands_marker'), '#size' => 10, '#maxlength' => 10);
     $form['currency']['uc_currency_dec'] = array('#type' => 'textfield', '#title' => $this->t('Decimal marker'), '#default_value' => $config->get('currency.decimal_marker'), '#size' => 10, '#maxlength' => 10);
     $form['currency']['uc_currency_prec'] = array('#type' => 'select', '#title' => $this->t('Number of decimal places'), '#options' => array(0 => 0, 1 => 1, 2 => 2), '#default_value' => $config->get('currency.precision'));
     $form['weight'] = array('#type' => 'details', '#title' => $this->t('Weight format'), '#group' => 'store');
     $form['weight']['uc_weight_unit'] = array('#type' => 'select', '#title' => $this->t('Default weight units'), '#default_value' => $config->get('weight.units'), '#options' => array('lb' => $this->t('Pounds'), 'oz' => $this->t('Ounces'), 'kg' => $this->t('Kilograms'), 'g' => $this->t('Grams')));
     $form['weight']['uc_weight_thou'] = array('#type' => 'textfield', '#title' => $this->t('Thousands marker'), '#default_value' => $config->get('weight.thousands_marker'), '#size' => 10, '#maxlength' => 10);
     $form['weight']['uc_weight_dec'] = array('#type' => 'textfield', '#title' => $this->t('Decimal marker'), '#default_value' => $config->get('weight.decimal_marker'), '#size' => 10, '#maxlength' => 10);
     $form['weight']['uc_weight_prec'] = array('#type' => 'select', '#title' => $this->t('Number of decimal places'), '#options' => array(0 => 0, 1 => 1, 2 => 2), '#default_value' => $config->get('weight.precision'));
     $form['length'] = array('#type' => 'details', '#title' => $this->t('Length format'), '#group' => 'store');
     $form['length']['uc_length_unit'] = array('#type' => 'select', '#title' => $this->t('Default length units'), '#default_value' => $config->get('length.units'), '#options' => array('in' => $this->t('Inches'), 'ft' => $this->t('Feet'), 'cm' => $this->t('Centimeters'), 'mm' => $this->t('Millimeters')));
     $form['length']['uc_length_thou'] = array('#type' => 'textfield', '#title' => $this->t('Thousands marker'), '#default_value' => $config->get('length.thousands_marker'), '#size' => 10, '#maxlength' => 10);
     $form['length']['uc_length_dec'] = array('#type' => 'textfield', '#title' => $this->t('Decimal marker'), '#default_value' => $config->get('length.decimal_marker'), '#size' => 10, '#maxlength' => 10);
     $form['length']['uc_length_prec'] = array('#type' => 'select', '#title' => $this->t('Number of decimal places'), '#options' => array(0 => 0, 1 => 1, 2 => 2), '#default_value' => $config->get('length.precision'));
     $form['display'] = array('#type' => 'details', '#title' => $this->t('Display settings'), '#group' => 'store');
     $form['display']['uc_customer_list_address'] = array('#type' => 'radios', '#title' => $this->t('Primary customer address'), '#description' => $this->t('Select the address to be used on customer lists and summaries.'), '#options' => array('billing' => $this->t('Billing address'), 'shipping' => $this->t('Shipping address')), '#default_value' => $config->get('customer_address'));
     $form['display']['uc_order_capitalize_addresses'] = array('#type' => 'checkbox', '#title' => $this->t('Capitalize address on order screens'), '#default_value' => $config->get('capitalize_address'));
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 public function buildRedirectForm(array $form, FormStateInterface $form_state, OrderInterface $order = NULL)
 {
     $shipping = 0;
     foreach ($order->line_items as $item) {
         if ($item['type'] == 'shipping') {
             $shipping += $item['amount'];
         }
     }
     $tax = 0;
     if (\Drupal::moduleHandler()->moduleExists('uc_tax')) {
         foreach (uc_tax_calculate($order) as $tax_item) {
             $tax += $tax_item->amount;
         }
     }
     $address = $order->getAddress($this->configuration['wps_address_selection']);
     $country = $address->country;
     $phone = '';
     for ($i = 0; $i < strlen($address->phone); $i++) {
         if (is_numeric($address->phone[$i])) {
             $phone .= $address->phone[$i];
         }
     }
     /**
      * night_phone_a: The area code for U.S. phone numbers, or the country code
      *                for phone numbers outside the U.S.
      * night_phone_b: The three-digit prefix for U.S. phone numbers, or the
      *                entire phone number for phone numbers outside the U.S.,
      *                excluding country code.
      * night_phone_c: The four-digit phone number for U.S. phone numbers.
      *                (Not Used for UK numbers)
      */
     if ($country == 'US' || $country == 'CA') {
         $phone = substr($phone, -10);
         $phone_a = substr($phone, 0, 3);
         $phone_b = substr($phone, 3, 3);
         $phone_c = substr($phone, 6, 4);
     } else {
         $phone_a = $phone_b = $phone_c = '';
     }
     $data = array('cmd' => '_cart', 'charset' => 'utf-8', 'notify_url' => Url::fromRoute('uc_paypal.ipn', [], ['absolute' => TRUE])->toString(), 'cancel_return' => Url::fromRoute('uc_cart.checkout_review', [], ['absolute' => TRUE])->toString(), 'no_note' => 1, 'no_shipping' => $this->configuration['wps_no_shipping'], 'return' => Url::fromRoute('uc_paypal.wps_complete', ['uc_order' => $order->id()], ['absolute' => TRUE])->toString(), 'rm' => 1, 'currency_code' => $order->getCurrency(), 'handling_cart' => uc_currency_format($shipping, FALSE, FALSE, '.'), 'invoice' => $order->id() . '-' . \Drupal::service('uc_cart.manager')->get()->getId(), 'tax_cart' => uc_currency_format($tax, FALSE, FALSE, '.'), 'business' => $this->configuration['wps_email'], 'upload' => 1, 'lc' => $this->configuration['wps_language'], 'address1' => substr($address->street1, 0, 100), 'address2' => substr($address->street2, 0, 100), 'city' => substr($address->city, 0, 40), 'country' => $country, 'email' => $order->getEmail(), 'first_name' => substr($address->first_name, 0, 32), 'last_name' => substr($address->last_name, 0, 64), 'state' => $address->zone, 'zip' => $address->postal_code, 'night_phone_a' => $phone_a, 'night_phone_b' => $phone_b, 'night_phone_c' => $phone_c);
     if ($this->configuration['wps_address_override']) {
         $data['address_override'] = 1;
     }
     // Account for stores that just want to authorize funds instead of capture.
     if ($this->configuration['wps_payment_action'] == 'Authorization') {
         $data['paymentaction'] = 'authorization';
     }
     if ($this->configuration['wps_submit_method'] == 'itemized') {
         // List individual items.
         $i = 0;
         foreach ($order->products as $item) {
             $i++;
             $data['amount_' . $i] = uc_currency_format($item->price, FALSE, FALSE, '.');
             $data['item_name_' . $i] = $item->title;
             $data['item_number_' . $i] = $item->model;
             $data['quantity_' . $i] = $item->qty;
             // PayPal will only display the first two...
             if (!empty($item->data['attributes']) && count($item->data['attributes']) > 0) {
                 $o = 0;
                 foreach ($item->data['attributes'] as $name => $setting) {
                     $data['on' . $o . '_' . $i] = $name;
                     $data['os' . $o . '_' . $i] = implode(', ', (array) $setting);
                     $o++;
                 }
             }
         }
         // Apply discounts (negative amount line items). For example, this handles
         // line items created by uc_coupon.
         $discount = 0;
         foreach ($order->line_items as $item) {
             if ($item['amount'] < 0) {
                 // The minus sign is not an error! The discount amount must be positive.
                 $discount -= $item['amount'];
             }
         }
         if ($discount != 0) {
             $data['discount_amount_cart'] = $discount;
         }
     } else {
         // List the whole cart as a single item to account for fees/discounts.
         $data['amount_1'] = uc_currency_format($order->getTotal() - $shipping - $tax, FALSE, FALSE, '.');
         $data['item_name_1'] = $this->t('Order @order_id at @store', ['@order_id' => $order->id(), '@store' => uc_store_name()]);
     }
     $form['#action'] = $this->configuration['wps_server'];
     foreach ($data as $name => $value) {
         if (!empty($value)) {
             $form[$name] = array('#type' => 'hidden', '#value' => $value);
         }
     }
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Submit order'));
     return $form;
 }