PaymentForm.php in Payment 8.2
File
modules/payment_form/src/Entity/Payment/PaymentForm.php
View source
<?php
namespace Drupal\payment_form\Entity\Payment;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\payment\Plugin\Payment\Method\PaymentExecutionPaymentMethodManager;
use Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface;
use Drupal\plugin\PluginDiscovery\LimitedPluginDiscoveryDecorator;
use Drupal\plugin\PluginType\PluginTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentForm extends ContentEntityForm {
protected $currentUser;
protected $paymentMethodType;
protected $pluginSelectorManager;
public static function create(ContainerInterface $container) {
$plugin_type_manager = $container
->get('plugin.plugin_type_manager');
$form = parent::create($container);
$form->currentUser = $container
->get('current_user');
$form->paymentMethodType = $plugin_type_manager
->getPluginType('payment_method');
$form->pluginSelectorManager = $container
->get('plugin.manager.plugin.plugin_selector');
$form->urlGenerator = $container
->get('url_generator');
$form->stringTranslation = $container
->get('string_translation');
return $form;
}
public function setPluginSelectorManager(PluginSelectorManagerInterface $plugin_selector_manager) {
$this->pluginSelectorManager = $plugin_selector_manager;
}
public function setPaymentMethodPluginType(PluginTypeInterface $payment_method_type) {
$this->paymentMethodType = $payment_method_type;
}
public function setCurrentUser(AccountInterface $current_user) {
$this->currentUser = $current_user;
}
protected function currentUser() {
return $this->currentUser;
}
public function form(array $form, FormStateInterface $form_state) {
$payment = $this
->getEntity();
$form['line_items'] = [
'#payment_line_items' => $payment,
'#type' => 'payment_line_items_display',
];
$form['payment_method'] = $this
->getPluginSelector($form_state)
->buildSelectorForm([], $form_state);
return parent::form($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this
->getPluginSelector($form_state)
->validateSelectorForm($form['payment_method'], $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$payment = $this
->getEntity();
$plugin_selector = $this
->getPluginSelector($form_state);
$plugin_selector
->submitSelectorForm($form['payment_method'], $form_state);
$payment
->setPaymentMethod($plugin_selector
->getSelectedPlugin());
$payment
->save();
$result = $payment
->execute();
if (!$result
->isCompleted()) {
$form_state
->setResponse($result
->getCompletionResponse()
->getResponse());
}
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions = [
'submit' => $actions['submit'],
];
$actions['submit']['#value'] = $this
->t('Pay');
$actions['submit']['#disabled'] = count($this
->getPaymentMethodManager()
->getDefinitions()) == 0;
return $actions;
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
$this
->getFormDisplay($form_state)
->extractFormValues($entity, $form, $form_state);
}
protected function getPluginSelector(FormStateInterface $form_state) {
if ($form_state
->has('plugin_selector')) {
$plugin_selector = $form_state
->get('plugin_selector');
}
else {
$config = $this
->config('payment_form.payment_type');
$plugin_selector_id = $config
->get('plugin_selector_id');
$plugin_selector = $this->pluginSelectorManager
->createInstance($plugin_selector_id);
$plugin_selector
->setSelectablePluginType($this->paymentMethodType);
$plugin_selector
->setSelectablePluginDiscovery($this
->getPaymentMethodManager());
$plugin_selector
->setSelectablePluginFactory($this
->getPaymentMethodManager());
$plugin_selector
->setRequired();
$plugin_selector
->setLabel($this
->t('Payment method'));
$form_state
->set('plugin_selector', $plugin_selector);
}
return $plugin_selector;
}
protected function getPaymentMethodManager() {
$config = $this
->config('payment_form.payment_type');
$limit_allowed_plugins = $config
->get('limit_allowed_plugins');
$payment_method_discovery = $this->paymentMethodType
->getPluginManager();
if ($limit_allowed_plugins) {
$allowed_plugin_ids = $config
->get('allowed_plugin_ids');
$payment_method_discovery = new LimitedPluginDiscoveryDecorator($payment_method_discovery);
$payment_method_discovery
->setDiscoveryLimit($allowed_plugin_ids);
}
$payment_method_manager = new PaymentExecutionPaymentMethodManager($this
->getEntity(), $this->currentUser, $this->paymentMethodType
->getPluginManager(), $payment_method_discovery);
return $payment_method_manager;
}
}