You are here

abstract class PaymentOffsiteForm in Commerce Core 8.2

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

Provides the base class for payment off-site forms.

Payment gateways are expected to inherit from this class and provide a buildConfigurationForm() method which calls buildRedirectForm() with the right parameters.

Hierarchy

Expanded class hierarchy of PaymentOffsiteForm

1 file declares its use of PaymentOffsiteForm
PaymentOffsiteForm.php in modules/payment_example/src/PluginForm/OffsiteRedirect/PaymentOffsiteForm.php

File

modules/payment/src/PluginForm/PaymentOffsiteForm.php, line 17

Namespace

Drupal\commerce_payment\PluginForm
View source
abstract class PaymentOffsiteForm extends PaymentGatewayFormBase {

  // The redirect methods.
  const REDIRECT_GET = 'get';
  const REDIRECT_POST = 'post';

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    if (empty($form['#return_url'])) {
      throw new \InvalidArgumentException('The offsite-payment form requires the #return_url property.');
    }
    if (empty($form['#cancel_url'])) {
      throw new \InvalidArgumentException('The offsite-payment form requires the #cancel_url property.');
    }
    if (!isset($form['#capture'])) {
      $form['#capture'] = TRUE;
    }
    return $form;
  }

  /**
   * Builds the redirect form.
   *
   * @param array $form
   *   The plugin form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param string $redirect_url
   *   The redirect url.
   * @param array $data
   *   Data that should be sent along.
   * @param string $redirect_method
   *   The redirect method (REDIRECT_GET or REDIRECT_POST constant).
   *
   * @return array
   *   The redirect form, if $redirect_method is REDIRECT_POST.
   *
   * @throws \Drupal\commerce\Response\NeedsRedirectException
   *   The redirect exception, if $redirect_method is REDIRECT_GET.
   */
  protected function buildRedirectForm(array $form, FormStateInterface $form_state, $redirect_url, array $data, $redirect_method = self::REDIRECT_GET) {
    if ($redirect_method == self::REDIRECT_POST) {
      $form['#attached']['library'][] = 'commerce_payment/offsite_redirect';
      $form['#process'][] = [
        get_class($this),
        'processRedirectForm',
      ];
      $form['#redirect_url'] = $redirect_url;
      foreach ($data as $key => $value) {
        $form[$key] = [
          '#type' => 'hidden',
          '#value' => $value,
          // Ensure the correct keys by sending values from the form root.
          '#parents' => [
            $key,
          ],
        ];
      }

      // The key is prefixed with 'commerce_' to prevent conflicts with $data.
      $form['commerce_message'] = [
        '#markup' => '<div class="checkout-help">' . t('Please wait while you are redirected to the payment server. If nothing happens within 10 seconds, please click on the button below.') . '</div>',
        '#weight' => -10,
      ];
    }
    else {
      $redirect_url = Url::fromUri($redirect_url, [
        'absolute' => TRUE,
        'query' => $data,
      ])
        ->toString();
      throw new NeedsRedirectException($redirect_url);
    }
    return $form;
  }

  /**
   * Prepares the complete form for a POST redirect.
   *
   * Sets the form #action, adds a class for the JS to target.
   * Workaround for buildConfigurationForm() not receiving $complete_form.
   *
   * @param array $form
   *   The plugin form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $complete_form
   *   The complete form structure.
   *
   * @return array
   *   The processed form element.
   */
  public static function processRedirectForm(array $form, FormStateInterface $form_state, array &$complete_form) {
    $complete_form['#action'] = $form['#redirect_url'];
    $complete_form['#attributes']['class'][] = 'payment-redirect-form';

    // The form actions are hidden by default, but needed in this case.
    $complete_form['actions']['#access'] = TRUE;
    foreach (Element::children($complete_form['actions']) as $element_name) {
      $complete_form['actions'][$element_name]['#access'] = TRUE;
    }
    return $form;
  }

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

    // Nothing. Off-site payment gateways do not submit forms to Drupal.
  }

}

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
PaymentOffsiteForm::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 1
PaymentOffsiteForm::buildRedirectForm protected function Builds the redirect form.
PaymentOffsiteForm::processRedirectForm public static function Prepares the complete form for a POST redirect.
PaymentOffsiteForm::REDIRECT_GET constant
PaymentOffsiteForm::REDIRECT_POST constant
PaymentOffsiteForm::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
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
PluginFormBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
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.