You are here

public function PaymentProcess::buildPaneForm in Commerce Core 8.2

Builds the pane form.

Parameters

array $pane_form: The pane form, containing the following basic properties:

  • #parents: Identifies the position of the pane form in the overall parent form, and identifies the location where the field values are placed within $form_state->getValues().

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

array $complete_form: The complete form structure.

Overrides CheckoutPaneInterface::buildPaneForm

File

modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php, line 161

Class

PaymentProcess
Provides the payment process pane.

Namespace

Drupal\commerce_payment\Plugin\Commerce\CheckoutPane

Code

public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
  $error_step_id = $this
    ->getErrorStepId();

  // The payment gateway is currently always required to be set.
  if ($this->order
    ->get('payment_gateway')
    ->isEmpty()) {
    $this
      ->messenger()
      ->addError($this
      ->t('No payment gateway selected.'));
    $this->checkoutFlow
      ->redirectToStep($error_step_id);
  }

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
  $payment_gateway = $this->order->payment_gateway->entity;
  $payment_gateway_plugin = $payment_gateway
    ->getPlugin();
  $payment = $this
    ->createPayment($payment_gateway);
  $next_step_id = $this->checkoutFlow
    ->getNextStepId($this
    ->getStepId());
  if ($payment_gateway_plugin instanceof SupportsStoredPaymentMethodsInterface && !$this->order
    ->get('payment_method')
    ->isEmpty()) {
    try {
      $payment->payment_method = $this->order
        ->get('payment_method')->entity;
      $payment_gateway_plugin
        ->createPayment($payment, $this->configuration['capture']);
      $this->checkoutFlow
        ->redirectToStep($next_step_id);
    } catch (DeclineException $e) {
      $message = $this
        ->t('We encountered an error processing your payment method. Please verify your details and try again.');
      $this
        ->messenger()
        ->addError($message);
      $this->checkoutFlow
        ->redirectToStep($error_step_id);
    } catch (PaymentGatewayException $e) {
      $this->logger
        ->error($e
        ->getMessage());
      $message = $this
        ->t('We encountered an unexpected error processing your payment method. Please try again later.');
      $this
        ->messenger()
        ->addError($message);
      $this->checkoutFlow
        ->redirectToStep($error_step_id);
    }
  }
  elseif ($payment_gateway_plugin instanceof OffsitePaymentGatewayInterface) {
    $complete_form['actions']['next']['#value'] = $this
      ->t('Proceed to @gateway', [
      '@gateway' => $payment_gateway_plugin
        ->getDisplayLabel(),
    ]);

    // Make sure that the payment gateway's onCancel() method is invoked,
    // by pointing the "Go back" link to the cancel URL.
    $complete_form['actions']['next']['#suffix'] = Link::fromTextAndUrl($this
      ->t('Go back'), $this
      ->buildCancelUrl())
      ->toString();

    // Actions are not needed by gateways that embed iframes or redirect
    // via GET. The inline form can show them when needed (redirect via POST).
    $complete_form['actions']['#access'] = FALSE;
    $inline_form = $this->inlineFormManager
      ->createInstance('payment_gateway_form', [
      'operation' => 'offsite-payment',
      'catch_build_exceptions' => FALSE,
    ], $payment);
    $pane_form['offsite_payment'] = [
      '#parents' => array_merge($pane_form['#parents'], [
        'offsite_payment',
      ]),
      '#inline_form' => $inline_form,
      '#return_url' => $this
        ->buildReturnUrl()
        ->toString(),
      '#cancel_url' => $this
        ->buildCancelUrl()
        ->toString(),
      '#capture' => $this->configuration['capture'],
    ];
    try {
      $pane_form['offsite_payment'] = $inline_form
        ->buildInlineForm($pane_form['offsite_payment'], $form_state);
    } catch (PaymentGatewayException $e) {
      $this->logger
        ->error($e
        ->getMessage());
      $message = $this
        ->t('We encountered an unexpected error processing your payment. Please try again later.');
      $this
        ->messenger()
        ->addError($message);
      $this->checkoutFlow
        ->redirectToStep($error_step_id);
    }
    return $pane_form;
  }
  elseif ($payment_gateway_plugin instanceof ManualPaymentGatewayInterface) {
    try {
      $payment_gateway_plugin
        ->createPayment($payment);
      $this->checkoutFlow
        ->redirectToStep($next_step_id);
    } catch (PaymentGatewayException $e) {
      $this->logger
        ->error($e
        ->getMessage());
      $message = $this
        ->t('We encountered an unexpected error processing your payment. Please try again later.');
      $this
        ->messenger()
        ->addError($message);
      $this->checkoutFlow
        ->redirectToStep($error_step_id);
    }
  }
  else {
    $this->logger
      ->error('Unable process payment with :plugin_id', [
      ':plugin_id' => $payment_gateway_plugin
        ->getPluginId(),
    ]);
    $message = $this
      ->t('We encountered an unexpected error processing your payment. Please try again later.');
    $this
      ->messenger()
      ->addError($message);
    $this->checkoutFlow
      ->redirectToStep($error_step_id);
  }
}