You are here

public function PaymentAddForm::submitForm in Commerce Core 8.2

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormInterface::submitForm

File

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

Class

PaymentAddForm
Provides the payment add form.

Namespace

Drupal\commerce_payment\Form

Code

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(),
    ]);
  }
}