View source
<?php
namespace Drupal\commerce_payment\Plugin\Commerce\InlineForm;
use Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormBase;
use Drupal\commerce_payment\Entity\EntityWithPaymentGatewayInterface;
use Drupal\commerce_payment\Exception\PaymentGatewayException;
use Drupal\commerce_payment\PluginForm\PaymentGatewayFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentGatewayForm extends EntityInlineFormBase {
protected $pluginFormFactory;
protected $pluginForm;
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;
}
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'));
}
public function defaultConfiguration() {
return [
'operation' => NULL,
'catch_build_exceptions' => TRUE,
];
}
protected function requiredConfiguration() {
return [
'operation',
];
}
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;
}
public static function preventSubmit(array &$element, FormStateInterface $form_state, array &$complete_form) {
$complete_form['actions']['#access'] = FALSE;
return $element;
}
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());
}
}
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());
}
}
}