You are here

public function PayPalPaymentsStandard::buildRedirectForm in Ubercart 8.4

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

\Drupal\uc_order\OrderInterface $order: The order that is being processed.

Return value

array The form structure.

Overrides OffsitePaymentMethodPluginInterface::buildRedirectForm

File

payment/uc_paypal/src/Plugin/Ubercart/PaymentMethod/PayPalPaymentsStandard.php, line 196

Class

PayPalPaymentsStandard
Defines the PayPal Payments Standard payment method.

Namespace

Drupal\uc_paypal\Plugin\Ubercart\PaymentMethod

Code

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
    ->getCountry();
  $full_phone = trim($address
    ->getPhone());
  $phone = '';
  for ($i = 0; $i < strlen($full_phone); $i++) {
    if (is_numeric($full_phone[$i])) {
      $phone .= $full_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 = [
    // PayPal command variable.
    'cmd' => '_cart',
    // Set the correct codepage.
    'charset' => 'utf-8',
    // IPN control notify URL.
    'notify_url' => Url::fromRoute('uc_paypal.ipn', [], [
      'absolute' => TRUE,
    ])
      ->toString(),
    // Display information.
    '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,
    // Transaction information.
    '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, '.'),
    // Shopping cart specific variables.
    'business' => $this->configuration['wps_email'],
    'upload' => 1,
    'lc' => $this->configuration['wps_language'],
    // Prepopulating forms/address overriding.
    'address1' => substr($address
      ->getStreet1(), 0, 100),
    'address2' => substr($address
      ->getStreet2(), 0, 100),
    'city' => substr($address
      ->getCity(), 0, 40),
    'country' => $country,
    'email' => $order
      ->getEmail(),
    'first_name' => substr($address
      ->getFirstName(), 0, 32),
    'last_name' => substr($address
      ->getLastName(), 0, 64),
    'state' => $address
      ->getZone(),
    'zip' => $address
      ->getPostalCode(),
    '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->value, FALSE, FALSE, '.');
      $data['item_name_' . $i] = $item->title->value;
      $data['item_number_' . $i] = $item->model->value;
      $data['quantity_' . $i] = $item->qty->value;

      // PayPal will only display the first two...
      if (!empty($item->data->attributes)) {
        $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 discount amount must be positive.
        // The minus sign is not an error!
        $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] = [
        '#type' => 'hidden',
        '#value' => $value,
      ];
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit order'),
  ];
  return $form;
}