You are here

class PaymentMethodAddForm in Commerce Purchase Order 8

PaymentMethodAddForm for Purchase Order.

Hierarchy

Expanded class hierarchy of PaymentMethodAddForm

File

src/PluginForm/PurchaseOrder/PaymentMethodAddForm.php, line 14

Namespace

Drupal\commerce_purchase_order\PluginForm\PurchaseOrder
View source
class PaymentMethodAddForm extends PaymentMethodFormBase {

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

    /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
    $form['payment_details'] = [
      '#parents' => array_merge($form['#parents'], [
        'payment_details',
      ]),
      '#type' => 'container',
      '#payment_method_type' => 'purchase_order',
    ];
    $form['payment_details'] = $this
      ->buildPurchaseOrderForm($form['payment_details'], $form_state);

    // Move the billing information below the payment details.
    if (isset($form['billing_information'])) {
      $form['billing_information']['#weight'] = 10;
    }
    return $form;
  }

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

    /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
    $payment_method = $this->entity;
    $values = $form_state
      ->getValue($form['#parents']);

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

    // The payment method form is customer facing. For security reasons
    // the returned errors need to be more generic.
    // The Purchase Order Gateway will handle processing the form values.
    try {
      $payment_gateway_plugin
        ->createPaymentMethod($payment_method, $values['payment_details']);
    } catch (DeclineException $e) {
      \Drupal::logger('commerce_payment')
        ->warning($e
        ->getMessage());
      throw new DeclineException('We encountered an error processing your payment method. Please verify your details and try again.');
    } catch (PaymentGatewayException $e) {
      \Drupal::logger('commerce_payment')
        ->error($e
        ->getMessage());
      throw new PaymentGatewayException('We encountered an unexpected error processing your payment method. Please try again later.');
    }
  }

  /**
   * Builds the purchase order form.
   *
   * @param array $element
   *   The target element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the complete form.
   *
   * @return array
   *   The built form.
   */
  protected function buildPurchaseOrderForm(array $element, FormStateInterface $form_state) {
    $element['#attributes']['class'][] = 'purchase-order-form';
    $element['number'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Purchase Order number'),
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#required' => TRUE,
      '#maxlength' => 19,
      '#size' => 20,
    ];
    return $element;
  }

  /**
   * Handles the submission of the billing profile form.
   *
   * @param array $element
   *   The billing profile form element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the complete form.
   */
  protected function submitBillingProfileForm(array $element, FormStateInterface $form_state) {
    $billing_profile = $element['#entity'];
    $form_display = EntityFormDisplay::collectRenderDisplay($billing_profile, 'default');
    $form_display
      ->extractFormValues($billing_profile, $element, $form_state);

    /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
    $payment_method = $this->entity;
    $payment_method
      ->setBillingProfile($billing_profile);
  }

}

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::getErrorElement public function Gets the form element to which errors should be assigned. Overrides PaymentGatewayFormInterface::getErrorElement 1
PaymentGatewayFormBase::setEntity public function Sets the form entity. Overrides PaymentGatewayFormInterface::setEntity
PaymentMethodAddForm::buildConfigurationForm public function Form constructor. Overrides PaymentMethodFormBase::buildConfigurationForm
PaymentMethodAddForm::buildPurchaseOrderForm protected function Builds the purchase order form.
PaymentMethodAddForm::submitBillingProfileForm protected function Handles the submission of the billing profile form.
PaymentMethodAddForm::submitConfigurationForm public function Form submission handler. Overrides PaymentMethodFormBase::submitConfigurationForm
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.
PaymentMethodFormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
PaymentMethodFormBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormBase::validateConfigurationForm 2
PaymentMethodFormBase::__construct public function Constructs a new PaymentMethodFormBase.
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.