You are here

public function StripeGateway::cartDetails in Ubercart Stripe 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Ubercart/PaymentMethod/StripeGateway.php \Drupal\uc_stripe\Plugin\Ubercart\PaymentMethod\StripeGateway::cartDetails()

Returns the form or render array to be displayed at checkout.

Parameters

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

array $form: The checkout form array.

\Drupal\Core\Form\FormStateInterface $form_state: The checkout form state array.

Return value

array A form or render array.

Overrides CreditCardPaymentMethodBase::cartDetails

File

src/Plugin/Ubercart/PaymentMethod/StripeGateway.php, line 123

Class

StripeGateway
Stripe Ubercart gateway payment method.

Namespace

Drupal\uc_stripe\Plugin\Ubercart\PaymentMethod

Code

public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) {
  $form = parent::cartDetails($order, $form, $form_state);
  $apikey = $this->configuration['testmode'] ? $this->configuration['test_publishable_key'] : $this->configuration['live_publishable_key'];
  $stripe_is_enabled = TRUE;
  $form['#attached']['drupalSettings']['uc_stripe']['publishable_key'] = $apikey;

  // NOTE: This value seems routinely not to get to the JS
  $form['#attached']['drupalSettings']['uc_stripe']['stripe_is_enabled'] = $stripe_is_enabled;

  // Add custom JS and CSS
  $form['#attached']['library'][] = 'uc_stripe/uc_stripe';
  $form['stripe_nojs_warning'] = array(
    '#type' => 'item',
    '#markup' => '<span id="stripe-nojs-warning" class="stripe-warning">' . t('Sorry, for security reasons your card cannot be processed because Javascript is disabled in your browser.') . '</span>',
    '#weight' => -1000,
  );
  $form['stripe_token'] = array(
    '#type' => 'hidden',
    '#default_value' => 'default',
    '#attributes' => array(
      'id' => 'edit-panes-payment-details-stripe-token',
    ),
  );

  // Prevent form Credit card fill and submission if javascript has not removed
  // the "disabled" attributes..
  // If JS happens to be disabled, we don't want user to be able to enter CC data.
  // Note that we can't use '#disabled', as it causes Form API to discard all input,
  // so use the disabled attribute instead.
  $form['cc_number']['#attributes']['disabled'] = 'disabled';
  if (empty($form['actions']['continue']['#attributes'])) {
    $form['actions']['continue']['#attributes'] = array();
  }
  $form['actions']['continue']['#attributes']['disabled'] = 'disabled';

  // Add custom submit which will do saving away of token during submit.
  //    $form['#submit'][] = 'uc_stripe_checkout_form_customsubmit';
  // Add a section for stripe.js error messages (CC validation, etc.)
  $form['messages'] = array(
    '#markup' => "<div class='uc-stripe-messages messages error hidden'></div>",
  );
  if ($this->configuration['testmode']) {
    $form['testmode'] = [
      '#prefix' => "<div class='messages uc-stripe-testmode'>",
      '#markup' => $this
        ->t('Test mode is <strong>ON</strong> for the Ubercart Stripe Payment Gateway. Your  card will not be charged. To change this setting, edit the payment method at @link.', [
        '@link' => Link::createFromRoute(t('payment method settings'), 'entity.uc_payment_method.collection')
          ->toString(),
      ]),
      '#suffix' => "</div>",
    ];
  }
  return $form;
}