class PaymentGatewayForm in Commerce Core 8.2
Same name in this branch
- 8.2 modules/payment/src/Form/PaymentGatewayForm.php \Drupal\commerce_payment\Form\PaymentGatewayForm
- 8.2 modules/payment/src/Element/PaymentGatewayForm.php \Drupal\commerce_payment\Element\PaymentGatewayForm
- 8.2 modules/payment/src/Plugin/Commerce/InlineForm/PaymentGatewayForm.php \Drupal\commerce_payment\Plugin\Commerce\InlineForm\PaymentGatewayForm
Provides a form element for embedding payment gateway forms.
Each payment gateway plugin defines its own plugin forms, keyed by operation. The plugin forms operate on a payment or a payment method entity. When the plugin form is submitted, an API call is usually performed, and the updated entity is saved.
This inline form takes a payment or a payment method entity, initializes the appropriate plugin form, then lets it do its thing, while ensuring that any thrown exception is correctly handled.
Plugin annotation
@CommerceInlineForm(
id = "payment_gateway_form",
label = @Translation("Payment gateway form"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce\Plugin\Commerce\InlineForm\InlineFormBase implements InlineFormInterface, ContainerFactoryPluginInterface uses AjaxFormTrait
- class \Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormBase implements EntityInlineFormInterface
- class \Drupal\commerce_payment\Plugin\Commerce\InlineForm\PaymentGatewayForm
- class \Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormBase implements EntityInlineFormInterface
- class \Drupal\commerce\Plugin\Commerce\InlineForm\InlineFormBase implements InlineFormInterface, ContainerFactoryPluginInterface uses AjaxFormTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of PaymentGatewayForm
File
- modules/
payment/ src/ Plugin/ Commerce/ InlineForm/ PaymentGatewayForm.php, line 30
Namespace
Drupal\commerce_payment\Plugin\Commerce\InlineFormView source
class PaymentGatewayForm extends EntityInlineFormBase {
/**
* The plugin form factory.
*
* @var \Drupal\Core\Plugin\PluginFormFactoryInterface
*/
protected $pluginFormFactory;
/**
* The plugin form.
*
* @var \Drupal\commerce_payment\PluginForm\PaymentGatewayFormInterface
*/
protected $pluginForm;
/**
* Constructs a new PaymentGatewayForm object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_factory
* The plugin form factory.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, PluginFormFactoryInterface $plugin_form_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->pluginFormFactory = $plugin_form_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin_form.factory'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'operation' => NULL,
// Allows parent forms to handle exceptions themselves (in order to
// perform a redirect, or some other logic).
'catch_build_exceptions' => TRUE,
];
}
/**
* {@inheritdoc}
*/
protected function requiredConfiguration() {
return [
'operation',
];
}
/**
* {@inheritdoc}
*/
public function buildInlineForm(array $inline_form, FormStateInterface $form_state) {
$inline_form = parent::buildInlineForm($inline_form, $form_state);
assert($this->entity instanceof EntityWithPaymentGatewayInterface);
$plugin = $this->entity
->getPaymentGateway()
->getPlugin();
$this->pluginForm = $this->pluginFormFactory
->createInstance($plugin, $this->configuration['operation']);
assert($this->pluginForm instanceof PaymentGatewayFormInterface);
$this->pluginForm
->setEntity($this->entity);
try {
$inline_form = $this->pluginForm
->buildConfigurationForm($inline_form, $form_state);
} catch (PaymentGatewayException $e) {
if (empty($this->configuration['catch_build_exceptions'])) {
throw $e;
}
$inline_form['error'] = [
'#markup' => $this
->t('An error occurred while contacting the gateway. Please try again later.'),
];
$inline_form['#process'][] = [
get_class($this),
'preventSubmit',
];
}
return $inline_form;
}
/**
* Prevents the form from being submitted, by removing the actions element.
*
* Done in a #process callback because buildInlineForm() doesn't have access
* to the complete form (since it's called while the initial form structure
* is still being built).
*
* @param array $element
* The form element being processed.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The form element.
*/
public static function preventSubmit(array &$element, FormStateInterface $form_state, array &$complete_form) {
$complete_form['actions']['#access'] = FALSE;
return $element;
}
/**
* {@inheritdoc}
*/
public function validateInlineForm(array &$inline_form, FormStateInterface $form_state) {
parent::validateInlineForm($inline_form, $form_state);
try {
$this->pluginForm
->validateConfigurationForm($inline_form, $form_state);
$this->entity = $this->pluginForm
->getEntity();
} catch (PaymentGatewayException $e) {
$error_element = $this->pluginForm
->getErrorElement($inline_form, $form_state);
$form_state
->setError($error_element, $e
->getMessage());
}
}
/**
* {@inheritdoc}
*/
public function submitInlineForm(array &$inline_form, FormStateInterface $form_state) {
parent::submitInlineForm($inline_form, $form_state);
try {
$this->pluginForm
->submitConfigurationForm($inline_form, $form_state);
$this->entity = $this->pluginForm
->getEntity();
} catch (PaymentGatewayException $e) {
$error_element = $this->pluginForm
->getErrorElement($inline_form, $form_state);
$form_state
->setError($error_element, $e
->getMessage());
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxFormTrait:: |
public static | function | Ajax handler for refreshing an entire form. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityInlineFormBase:: |
protected | property | The entity. | 1 |
EntityInlineFormBase:: |
public | function |
Gets the entity. Overrides EntityInlineFormInterface:: |
|
EntityInlineFormBase:: |
public | function |
Sets the entity. Overrides EntityInlineFormInterface:: |
|
InlineFormBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
InlineFormBase:: |
public | function |
Gets the inline form label. Overrides InlineFormInterface:: |
|
InlineFormBase:: |
public static | function | Runs the inline form submission. | |
InlineFormBase:: |
public static | function | Runs the inline form validation. | |
InlineFormBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
InlineFormBase:: |
public static | function | Updates the page title based on the inline form's #page_title property. | |
InlineFormBase:: |
protected | function | Validates configuration. | 1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PaymentGatewayForm:: |
protected | property | The plugin form. | |
PaymentGatewayForm:: |
protected | property | The plugin form factory. | |
PaymentGatewayForm:: |
public | function |
Builds the inline form. Overrides InlineFormBase:: |
|
PaymentGatewayForm:: |
public static | function |
Creates an instance of the plugin. Overrides InlineFormBase:: |
|
PaymentGatewayForm:: |
public | function |
Gets default configuration for this plugin. Overrides InlineFormBase:: |
|
PaymentGatewayForm:: |
public static | function | Prevents the form from being submitted, by removing the actions element. | |
PaymentGatewayForm:: |
protected | function |
Gets the required configuration for this plugin. Overrides InlineFormBase:: |
|
PaymentGatewayForm:: |
public | function |
Submits the inline form. Overrides InlineFormBase:: |
|
PaymentGatewayForm:: |
public | function |
Validates the inline form. Overrides InlineFormBase:: |
|
PaymentGatewayForm:: |
public | function |
Constructs a new PaymentGatewayForm object. Overrides InlineFormBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |