You are here

public function PaymentMethodAddForm::buildCreditCardForm in Commerce Stripe 8

Builds the credit card form.

Parameters

array $element: The target element.

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

Return value

array The built credit card form.

Overrides PaymentMethodAddForm::buildCreditCardForm

File

src/PluginForm/Stripe/PaymentMethodAddForm.php, line 20

Class

PaymentMethodAddForm
Provides payment form for Stripe.

Namespace

Drupal\commerce_stripe\PluginForm\Stripe

Code

public function buildCreditCardForm(array $element, FormStateInterface $form_state) {

  // Alter the form with Stripe specific needs.
  $element['#attributes']['class'][] = 'stripe-form';

  // Set our key to settings array.

  /** @var \Drupal\commerce_stripe\Plugin\Commerce\PaymentGateway\StripeInterface $plugin */
  $plugin = $this->plugin;

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
  $payment_method = $this->entity;
  $payment_method_owner = $payment_method
    ->getOwner();

  // @todo Replace with setting check from https://www.drupal.org/project/commerce/issues/2871483.
  // @todo Simplify check after https://www.drupal.org/project/commerce/issues/3073942.
  $client_secret = NULL;
  if ($payment_method_owner instanceof UserInterface && $payment_method_owner
    ->isAuthenticated()) {
    $route_match = \Drupal::routeMatch();

    // @todo Use context passed by parent element after https://www.drupal.org/project/commerce/issues/3077783.
    if ($route_match
      ->getRouteName() === 'entity.commerce_payment_method.add_form') {

      // A SetupIntent is required if this is being created for off-session
      // usage (for instance, outside of checkout where there is no payment
      // intent that will be authenticated.)
      $setup_intent = SetupIntent::create([
        'usage' => 'off_session',
      ]);
      $client_secret = $setup_intent->client_secret;
    }
  }
  $element['#attached']['library'][] = 'commerce_stripe/stripe';
  $element['#attached']['library'][] = 'commerce_stripe/form';
  $element['#attached']['drupalSettings']['commerceStripe'] = [
    'publishableKey' => $plugin
      ->getPublishableKey(),
    'clientSecret' => $client_secret,
  ];

  // Populated by the JS library.
  $element['stripe_payment_method_id'] = [
    '#type' => 'hidden',
    '#attributes' => [
      'id' => 'stripe-payment-method-id',
    ],
  ];
  $element['card_number'] = [
    '#type' => 'item',
    '#title' => t('Card number'),
    '#required' => TRUE,
    '#validated' => TRUE,
    '#markup' => '<div id="card-number-element" class="form-text"></div>',
  ];
  $element['expiration'] = [
    '#type' => 'item',
    '#title' => t('Expiration date'),
    '#required' => TRUE,
    '#validated' => TRUE,
    '#markup' => '<div id="expiration-element"></div>',
  ];
  $element['security_code'] = [
    '#type' => 'item',
    '#title' => t('CVC'),
    '#required' => TRUE,
    '#validated' => TRUE,
    '#markup' => '<div id="security-code-element"></div>',
  ];

  // To display validation errors.
  $element['payment_errors'] = [
    '#type' => 'markup',
    '#markup' => '<div id="payment-errors"></div>',
    '#weight' => -200,
  ];
  $cacheability = new CacheableMetadata();
  $cacheability
    ->addCacheableDependency($this->entity);
  $cacheability
    ->setCacheMaxAge(0);
  $cacheability
    ->applyTo($element);
  return $element;
}