You are here

class PaymentMethodAddForm in Commerce PayPal 8

Hierarchy

Expanded class hierarchy of PaymentMethodAddForm

File

src/PluginForm/Checkout/PaymentMethodAddForm.php, line 17

Namespace

Drupal\commerce_paypal\PluginForm\Checkout
View source
class PaymentMethodAddForm extends BasePaymentMethodAddForm {

  /**
   * The custom card fields builder.
   *
   * @var \Drupal\commerce_paypal\CustomCardFieldsBuilderInterface
   */
  protected $cardFieldsBuilder;

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a new PaymentMethodAddForm object.
   *
   * @param \Drupal\commerce_store\CurrentStoreInterface $current_store
   *   The current store.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce\InlineFormManager $inline_form_manager
   *   The inline form manager.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger.
   * @param \Drupal\commerce_paypal\CustomCardFieldsBuilderInterface $card_fields_builder
   *   The custom card fields builder.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(CurrentStoreInterface $current_store, EntityTypeManagerInterface $entity_type_manager, InlineFormManager $inline_form_manager, LoggerInterface $logger, CustomCardFieldsBuilderInterface $card_fields_builder, RouteMatchInterface $route_match) {
    parent::__construct($current_store, $entity_type_manager, $inline_form_manager, $logger);
    $this->cardFieldsBuilder = $card_fields_builder;
    $this->routeMatch = $route_match;
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

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

    // We need to inject the custom card fields, only when this is the solution
    // configured.
    if (!$this
      ->shouldInjectForm($payment_method
      ->getPaymentGateway()
      ->getPlugin())) {
      return $form;
    }

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $this->routeMatch
      ->getParameter('commerce_order');
    $form['payment_details'] += $this->cardFieldsBuilder
      ->build($order, $payment_method
      ->getPaymentGateway());
    return $form;
  }

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

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

    // When the gateway is configured to display "Smart payment buttons", the
    // buttons are not injected in the payment information pane but in the
    // "review" step, which means the payment method creation should be skipped.
    if ($this
      ->shouldInjectForm($payment_method
      ->getPaymentGateway()
      ->getPlugin())) {
      parent::submitConfigurationForm($form, $form_state);
    }
    else {

      // Since we're not calling the parent submitConfigurationForm() method
      // we need to duplicate the logic for setting the billing profile

      /** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayInterface $payment_gateway_plugin */
      $payment_gateway_plugin = $this->plugin;

      /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
      $payment_method = $this->entity;
      if ($payment_gateway_plugin
        ->collectsBillingInformation()) {

        /** @var \Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormInterface $inline_form */
        $inline_form = $form['billing_information']['#inline_form'];

        /** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */
        $billing_profile = $inline_form
          ->getEntity();
        $payment_method
          ->setBillingProfile($billing_profile);
      }
    }
  }

  /**
   * Determines whether the card fields form should be injected.
   */
  protected function shouldInjectForm(PaymentGatewayInterface $plugin) {
    return $plugin instanceof CheckoutInterface && $plugin
      ->getPaymentSolution() === 'custom_card_fields';
  }

}

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
PaymentGatewayFormBase::$entity protected property The form entity.
PaymentGatewayFormBase::getEntity public function Gets the form entity. Overrides PaymentGatewayFormInterface::getEntity
PaymentGatewayFormBase::setEntity public function Sets the form entity. Overrides PaymentGatewayFormInterface::setEntity
PaymentMethodAddForm::$cardFieldsBuilder protected property The custom card fields builder.
PaymentMethodAddForm::$routeMatch protected property The route match.
PaymentMethodAddForm::buildConfigurationForm public function Form constructor. Overrides PaymentMethodAddForm::buildConfigurationForm
PaymentMethodAddForm::buildCreditCardForm protected function Builds the credit card form. 1
PaymentMethodAddForm::buildPayPalForm protected function Builds the PayPal form.
PaymentMethodAddForm::create public static function Instantiates a new instance of this class. Overrides PaymentMethodFormBase::create
PaymentMethodAddForm::getErrorElement public function Gets the form element to which errors should be assigned. Overrides PaymentGatewayFormBase::getErrorElement
PaymentMethodAddForm::shouldInjectForm protected function Determines whether the card fields form should be injected.
PaymentMethodAddForm::submitConfigurationForm public function Form submission handler. Overrides PaymentMethodAddForm::submitConfigurationForm
PaymentMethodAddForm::submitCreditCardForm protected function Handles the submission of the credit card form.
PaymentMethodAddForm::submitPayPalForm protected function Handles the submission of the PayPal form.
PaymentMethodAddForm::validateConfigurationForm public function Form validation handler. Overrides PaymentMethodFormBase::validateConfigurationForm
PaymentMethodAddForm::validateCreditCardForm protected function Validates the credit card form.
PaymentMethodAddForm::validatePayPalForm protected function Validates the PayPal form.
PaymentMethodAddForm::__construct public function Constructs a new PaymentMethodAddForm object. Overrides PaymentMethodFormBase::__construct
PaymentMethodFormBase::$currentStore protected property The current store.
PaymentMethodFormBase::$entityTypeManager protected property The entity type manager.
PaymentMethodFormBase::$inlineFormManager protected property The inline form manager.
PaymentMethodFormBase::$logger protected property The logger.
PluginFormBase::$plugin protected property The plugin this form is for. 3
PluginFormBase::setPlugin public function Sets the plugin for this object. Overrides PluginAwareInterface::setPlugin 1
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.