You are here

public function TwoCheckout::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_2checkout/src/Plugin/Ubercart/PaymentMethod/TwoCheckout.php, line 176

Class

TwoCheckout
Defines the 2Checkout payment method.

Namespace

Drupal\uc_2checkout\Plugin\Ubercart\PaymentMethod

Code

public function buildRedirectForm(array $form, FormStateInterface $form_state, OrderInterface $order = NULL) {
  $address = $order
    ->getAddress('billing');
  if ($address
    ->getCountry()) {
    $country = \Drupal::service('country_manager')
      ->getCountry($address
      ->getCountry())
      ->getAlpha3();
  }
  else {
    $country = '';
  }
  $data = [
    'sid' => $this->configuration['sid'],
    'mode' => '2CO',
    'card_holder_name' => mb_substr($address
      ->getFirstName() . ' ' . $address
      ->getLastName(), 0, 128),
    'street_address' => mb_substr($address
      ->getStreet1(), 0, 64),
    'street_address2' => mb_substr($address
      ->getStreet2(), 0, 64),
    'city' => mb_substr($address
      ->getCity(), 0, 64),
    'state' => $address
      ->getZone(),
    'zip' => mb_substr($address
      ->getPostalCode(), 0, 16),
    'country' => $country,
    'email' => mb_substr($order
      ->getEmail(), 0, 64),
    'phone' => mb_substr($address
      ->getPhone(), 0, 16),
    'purchase_step' => 'payment-method',
    'demo' => $this->configuration['demo'] ? 'Y' : 'N',
    'lang' => $this->configuration['language'],
    'merchant_order_id' => $order
      ->id(),
    'pay_method' => 'CC',
    'x_receipt_link_url' => Url::fromRoute('uc_2checkout.complete', [
      'cart_id' => \Drupal::service('uc_cart.manager')
        ->get()
        ->getId(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'total' => uc_currency_format($order
      ->getTotal(), FALSE, FALSE, '.'),
    'currency_code' => $order
      ->getCurrency(),
    'cart_order_id' => $order
      ->id(),
  ];
  $i = 0;
  foreach ($order->products as $product) {
    $i++;
    $data['li_' . $i . '_type'] = 'product';
    $data['li_' . $i . '_name'] = $product->title->value;

    // @todo HTML escape and limit to 128 chars.
    $data['li_' . $i . '_quantity'] = $product->qty->value;
    $data['li_' . $i . '_product_id'] = $product->model->value;
    $data['li_' . $i . '_price'] = uc_currency_format($product->price->value, FALSE, FALSE, '.');
  }
  if ('direct' == $this->configuration['checkout_type']) {
    $form['#attached']['library'][] = 'uc_2checkout/2checkout.direct';
  }
  $host = $this->configuration['demo'] ? 'sandbox' : 'www';
  $form['#action'] = "https://{$host}.2checkout.com/checkout/purchase";
  foreach ($data as $name => $value) {
    $form[$name] = [
      '#type' => 'hidden',
      '#value' => $value,
    ];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit order'),
  ];
  return $form;
}