PaymentMethodFormBase.php in Commerce Core 8.2
File
modules/payment/src/PluginForm/PaymentMethodFormBase.php
View source
<?php
namespace Drupal\commerce_payment\PluginForm;
use Drupal\commerce\InlineFormManager;
use Drupal\commerce_store\CurrentStoreInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentMethodFormBase extends PaymentGatewayFormBase implements ContainerInjectionInterface {
protected $currentStore;
protected $entityTypeManager;
protected $inlineFormManager;
protected $logger;
public function __construct(CurrentStoreInterface $current_store, EntityTypeManagerInterface $entity_type_manager, InlineFormManager $inline_form_manager, LoggerInterface $logger) {
$this->currentStore = $current_store;
$this->entityTypeManager = $entity_type_manager;
$this->inlineFormManager = $inline_form_manager;
$this->logger = $logger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('commerce_store.current_store'), $container
->get('entity_type.manager'), $container
->get('plugin.manager.commerce_inline_form'), $container
->get('logger.channel.commerce_payment'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$payment_gateway_plugin = $this->plugin;
$payment_method = $this->entity;
$form['#attached']['library'][] = 'commerce_payment/payment_method_form';
$form['#tree'] = TRUE;
if ($payment_gateway_plugin
->collectsBillingInformation()) {
$billing_profile = $payment_method
->getBillingProfile();
if (!$billing_profile) {
$profile_storage = $this->entityTypeManager
->getStorage('profile');
$billing_profile = $profile_storage
->create([
'type' => 'customer',
'uid' => 0,
]);
}
$store = $this->currentStore
->getStore();
$inline_form = $this->inlineFormManager
->createInstance('customer_profile', [
'profile_scope' => 'billing',
'available_countries' => $store ? $store
->getBillingCountries() : [],
'address_book_uid' => $payment_method
->getOwnerId(),
], $billing_profile);
$form['billing_information'] = [
'#parents' => array_merge($form['#parents'], [
'billing_information',
]),
'#inline_form' => $inline_form,
];
$form['billing_information'] = $inline_form
->buildInlineForm($form['billing_information'], $form_state);
}
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$payment_gateway_plugin = $this->plugin;
$payment_method = $this->entity;
if ($payment_gateway_plugin
->collectsBillingInformation()) {
$inline_form = $form['billing_information']['#inline_form'];
$billing_profile = $inline_form
->getEntity();
$payment_method
->setBillingProfile($billing_profile);
}
}
}