You are here

public function PaymentOffsiteForm::buildConfigurationForm in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/payment/src/PluginForm/PaymentOffsiteForm.php \Drupal\commerce_payment\PluginForm\PaymentOffsiteForm::buildConfigurationForm()
  2. 8.2 modules/payment_example/src/PluginForm/OffsiteRedirect/PaymentOffsiteForm.php \Drupal\commerce_payment_example\PluginForm\OffsiteRedirect\PaymentOffsiteForm::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PaymentOffsiteForm::buildConfigurationForm

File

modules/payment_example/src/PluginForm/OffsiteRedirect/PaymentOffsiteForm.php, line 15

Class

PaymentOffsiteForm

Namespace

Drupal\commerce_payment_example\PluginForm\OffsiteRedirect

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);

  /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
  $payment = $this->entity;

  /** @var \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface $payment_gateway_plugin */
  $payment_gateway_plugin = $payment
    ->getPaymentGateway()
    ->getPlugin();
  $redirect_method = $payment_gateway_plugin
    ->getConfiguration()['redirect_method'];
  $remove_js = $redirect_method == 'post_manual';
  if (in_array($redirect_method, [
    'post',
    'post_manual',
  ])) {
    $redirect_url = Url::fromRoute('commerce_payment_example.dummy_redirect_post')
      ->toString();
    $redirect_method = 'post';
  }
  else {

    // Gateways that use the GET redirect method usually perform an API call
    // that prepares the remote payment and provides the actual url to
    // redirect to. Any params received from that API call that need to be
    // persisted until later payment creation can be saved in $order->data.
    // Example: $order->setData('my_gateway', ['test' => '123']), followed
    // by an $order->save().
    $order = $payment
      ->getOrder();

    // Simulate an API call failing and throwing an exception, for test purposes.
    // See PaymentCheckoutTest::testFailedCheckoutWithOffsiteRedirectGet().
    if ($order
      ->getBillingProfile()
      ->get('address')->family_name == 'FAIL') {
      throw new PaymentGatewayException('Could not get the redirect URL.');
    }
    $redirect_url = Url::fromRoute('commerce_payment_example.dummy_redirect_302', [], [
      'absolute' => TRUE,
    ])
      ->toString();
  }
  $data = [
    'return' => $form['#return_url'],
    'cancel' => $form['#cancel_url'],
    'total' => $payment
      ->getAmount()
      ->getNumber(),
  ];
  $form = $this
    ->buildRedirectForm($form, $form_state, $redirect_url, $data, $redirect_method);
  if ($remove_js) {

    // Disable the javascript that auto-clicks the Submit button.
    unset($form['#attached']['library']);
  }
  return $form;
}