You are here

class PaymentAddForm in Commerce Core 8.2

Provides the payment add form.

Hierarchy

Expanded class hierarchy of PaymentAddForm

1 string reference to 'PaymentAddForm'
commerce_payment.routing.yml in modules/payment/commerce_payment.routing.yml
modules/payment/commerce_payment.routing.yml

File

modules/payment/src/Form/PaymentAddForm.php, line 23

Namespace

Drupal\commerce_payment\Form
View source
class PaymentAddForm extends FormBase implements ContainerInjectionInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The inline form manager.
   *
   * @var \Drupal\commerce\InlineFormManager
   */
  protected $inlineFormManager;

  /**
   * The payment options builder.
   *
   * @var \Drupal\commerce_payment\PaymentOptionsBuilderInterface
   */
  protected $paymentOptionsBuilder;

  /**
   * The current order.
   *
   * @var \Drupal\commerce_order\Entity\OrderInterface
   */
  protected $order;

  /**
   * The payment options.
   *
   * @var \Drupal\commerce_payment\PaymentOption[]
   */
  protected $paymentOptions;

  /**
   * Constructs a new PaymentAddForm object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce\InlineFormManager $inline_form_manager
   *   The inline form manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   * @param \Drupal\commerce_payment\PaymentOptionsBuilderInterface $payment_options_builder
   *   The payment options builder.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, InlineFormManager $inline_form_manager, RouteMatchInterface $route_match, PaymentOptionsBuilderInterface $payment_options_builder) {
    $this->entityTypeManager = $entity_type_manager;
    $this->inlineFormManager = $inline_form_manager;
    $this->paymentOptionsBuilder = $payment_options_builder;
    $this->order = $route_match
      ->getParameter('commerce_order');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('plugin.manager.commerce_inline_form'), $container
      ->get('current_route_match'), $container
      ->get('commerce_payment.options_builder'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'commerce_payment_add_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Prepare the form for ajax.
    $form['#wrapper_id'] = Html::getUniqueId('payment-add-form-wrapper');
    $form['#prefix'] = '<div id="' . $form['#wrapper_id'] . '">';
    $form['#suffix'] = '</div>';
    $form['#tree'] = TRUE;
    $step = $form_state
      ->get('step');
    $step = $step ?: 'payment_gateway';
    $form_state
      ->set('step', $step);
    if ($step == 'payment_gateway') {
      $form = $this
        ->buildPaymentGatewayForm($form, $form_state);
    }
    elseif ($step == 'payment') {
      $form = $this
        ->buildPaymentForm($form, $form_state);
    }
    return $form;
  }

  /**
   * Builds the form for selecting a payment gateway.
   *
   * @param array $form
   *   The parent form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the complete form.
   *
   * @return array
   *   The built form.
   */
  protected function buildPaymentGatewayForm(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\commerce_payment\PaymentGatewayStorageInterface $payment_gateway_storage */
    $payment_gateway_storage = $this->entityTypeManager
      ->getStorage('commerce_payment_gateway');
    $payment_gateways = $payment_gateway_storage
      ->loadMultipleForOrder($this->order);

    // Allow on-site and manual payment gateways.
    $payment_gateways = array_filter($payment_gateways, function ($payment_gateway) {

      /** @var \Drupal\commerce_payment\Entity\PaymentGateway $payment_gateway */
      return !$payment_gateway
        ->getPlugin() instanceof OffsitePaymentGatewayInterface;
    });

    // @todo Move this check to the access handler.
    if (count($payment_gateways) < 1) {
      throw new AccessDeniedHttpException();
    }

    // Core bug #1988968 doesn't allow the payment method add form JS to depend
    // on an external library, so the libraries need to be preloaded here.
    foreach ($payment_gateways as $payment_gateway) {
      if ($js_library = $payment_gateway
        ->getPlugin()
        ->getJsLibrary()) {
        $form['#attached']['library'][] = $js_library;
      }
    }
    $payment_options = $this->paymentOptionsBuilder
      ->buildOptions($this->order, $payment_gateways);

    // Do not allow admins to add payments to non-reusable payment methods
    // through this form.
    $this->paymentOptions = array_filter($payment_options, function (PaymentOption $payment_option) {
      $order_payment_method = $this->order
        ->get('payment_method')->entity;
      if (!$order_payment_method) {
        return TRUE;
      }

      /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $order_payment_method */
      if ($order_payment_method
        ->id() === $payment_option
        ->getPaymentMethodId()) {
        return $order_payment_method
          ->isReusable();
      }
      return TRUE;
    });
    $option_labels = array_map(function (PaymentOption $option) {
      return $option
        ->getLabel();
    }, $this->paymentOptions);
    $default_option_id = NestedArray::getValue($form_state
      ->getUserInput(), [
      'payment_option',
    ]);
    if ($default_option_id && isset($this->paymentOptions[$default_option_id])) {
      $default_option = $this->paymentOptions[$default_option_id];
    }
    else {
      $default_option = $this->paymentOptionsBuilder
        ->selectDefaultOption($this->order, $this->paymentOptions);
    }
    $form['payment_option'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Payment option'),
      '#options' => $option_labels,
      '#default_value' => $default_option
        ->getId(),
      '#ajax' => [
        'callback' => [
          get_class($this),
          'ajaxRefresh',
        ],
        'wrapper' => $form['#wrapper_id'],
      ],
    ];

    // Add a class to each individual radio, to help themers.
    foreach ($this->paymentOptions as $option) {
      $class_name = $option
        ->getPaymentMethodId() ? 'stored' : 'new';
      $form['payment_option'][$option
        ->getId()]['#attributes']['class'][] = "payment-method--{$class_name}";
    }
    $default_payment_gateway_id = $default_option
      ->getPaymentGatewayId();
    $payment_gateway = $payment_gateways[$default_payment_gateway_id];
    if ($payment_gateway
      ->getPlugin() instanceof SupportsStoredPaymentMethodsInterface) {
      $form = $this
        ->buildPaymentMethodForm($form, $form_state, $default_option);
    }
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Continue'),
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * Clears the payment method value when the payment gateway changes.
   *
   * Changing the payment gateway results in a new set of payment methods,
   * causing the submitted value to trigger an "Illegal choice" error, cause
   * it's no longer allowed. Clearing the value causes the element to fallback
   * to the default value, avoiding the error.
   */
  public static function clearValue(array $element, FormStateInterface $form_state) {
    $value = $element['#value'];
    if (!isset($element['#options'][$value])) {
      $element['#value'] = NULL;
      $user_input =& $form_state
        ->getUserInput();
      unset($user_input['payment_option']);
    }
    return $element;
  }

  /**
   * Builds the payment method form for the selected payment option.
   *
   * @param array $form
   *   The form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state of the form.
   * @param \Drupal\commerce_payment\PaymentOption $payment_option
   *   The payment option.
   *
   * @return array
   *   The modified form.
   */
  protected function buildPaymentMethodForm(array $form, FormStateInterface $form_state, PaymentOption $payment_option) {
    if ($payment_option
      ->getPaymentMethodId() && !$payment_option
      ->getPaymentMethodTypeId()) {

      // Editing payment methods at checkout is not supported.
      return $form;
    }

    /** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */
    $payment_method_storage = $this->entityTypeManager
      ->getStorage('commerce_payment_method');
    $payment_method = $payment_method_storage
      ->create([
      'type' => $payment_option
        ->getPaymentMethodTypeId(),
      'payment_gateway' => $payment_option
        ->getPaymentGatewayId(),
      'uid' => $this->order
        ->getCustomerId(),
      'billing_profile' => $this->order
        ->getBillingProfile(),
    ]);
    $inline_form = $this->inlineFormManager
      ->createInstance('payment_gateway_form', [
      'operation' => 'add-payment-method',
    ], $payment_method);
    $form['add_payment_method'] = [
      '#parents' => [
        'add_payment_method',
      ],
    ];
    $form['add_payment_method'] = $inline_form
      ->buildInlineForm($form['add_payment_method'], $form_state);
    return $form;
  }

  /**
   * Builds the form for adding a payment.
   *
   * @param array $form
   *   The parent form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the complete form.
   *
   * @return array
   *   The built form.
   */
  protected function buildPaymentForm(array $form, FormStateInterface $form_state) {
    $values = [
      'order_id' => $this->order
        ->id(),
    ];
    if ($form_state
      ->has('new_payment_method')) {

      /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $new_payment_method */
      $new_payment_method = $form_state
        ->get('new_payment_method');
      $values['payment_method'] = $new_payment_method
        ->id();
      $values['payment_gateway'] = $new_payment_method
        ->getPaymentGatewayId();
    }
    else {
      $selected_payment_option = $form_state
        ->getValue('payment_option');

      /** @var \Drupal\commerce_payment\PaymentOption $payment_option */
      $payment_option = $this->paymentOptions[$selected_payment_option];
      $values['payment_method'] = $payment_option
        ->getPaymentMethodId();
      $values['payment_gateway'] = $payment_option
        ->getPaymentGatewayId();
    }
    $payment_storage = $this->entityTypeManager
      ->getStorage('commerce_payment');
    $payment = $payment_storage
      ->create($values);
    $inline_form = $this->inlineFormManager
      ->createInstance('payment_gateway_form', [
      'operation' => 'add-payment',
    ], $payment);
    $form['payment'] = [
      '#parents' => [
        'payment',
      ],
    ];
    $form['payment'] = $inline_form
      ->buildInlineForm($form['payment'], $form_state);
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add payment'),
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $step = $form_state
      ->get('step');
    if ($step == 'payment_gateway') {

      // Check if a new payment method was created.
      if (isset($form['add_payment_method']['#inline_form'])) {

        /** @var \Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormInterface $inline_form */
        $inline_form = $form['add_payment_method']['#inline_form'];
        $payment_method = $inline_form
          ->getEntity();
        $form_state
          ->set('new_payment_method', $payment_method);
      }
      $form_state
        ->set('step', 'payment');
      $form_state
        ->setRebuild(TRUE);
    }
    elseif ($step == 'payment') {

      // Save payment gateway and method references on order entity.

      /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
      $payment = $form['payment']['#inline_form']
        ->getEntity();
      $order = $payment
        ->getOrder();
      $order
        ->set('payment_gateway', $payment
        ->getPaymentGateway());
      if ($payment
        ->getPaymentMethod()) {
        $order
          ->set('payment_method', $payment
          ->getPaymentMethod());
      }
      $this->entityTypeManager
        ->getStorage('commerce_order')
        ->save($order);
      $this
        ->messenger()
        ->addMessage($this
        ->t('Payment saved.'));
      $form_state
        ->setRedirect('entity.commerce_payment.collection', [
        'commerce_order' => $this->order
          ->id(),
      ]);
    }
  }

  /**
   * Ajax callback.
   */
  public static function ajaxRefresh(array $form, FormStateInterface $form_state) {
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PaymentAddForm::$entityTypeManager protected property The entity type manager.
PaymentAddForm::$inlineFormManager protected property The inline form manager.
PaymentAddForm::$order protected property The current order.
PaymentAddForm::$paymentOptions protected property The payment options.
PaymentAddForm::$paymentOptionsBuilder protected property The payment options builder.
PaymentAddForm::ajaxRefresh public static function Ajax callback.
PaymentAddForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
PaymentAddForm::buildPaymentForm protected function Builds the form for adding a payment.
PaymentAddForm::buildPaymentGatewayForm protected function Builds the form for selecting a payment gateway.
PaymentAddForm::buildPaymentMethodForm protected function Builds the payment method form for the selected payment option.
PaymentAddForm::clearValue public static function Clears the payment method value when the payment gateway changes.
PaymentAddForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
PaymentAddForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PaymentAddForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
PaymentAddForm::__construct public function Constructs a new PaymentAddForm object.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.