PaymentMethodAddForm.php in Commerce PayPal 8
File
src/PluginForm/Checkout/PaymentMethodAddForm.php
View source
<?php
namespace Drupal\commerce_paypal\PluginForm\Checkout;
use Drupal\commerce\InlineFormManager;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\PaymentGatewayInterface;
use Drupal\commerce_payment\PluginForm\PaymentMethodAddForm as BasePaymentMethodAddForm;
use Drupal\commerce_paypal\CustomCardFieldsBuilderInterface;
use Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway\CheckoutInterface;
use Drupal\commerce_store\CurrentStoreInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentMethodAddForm extends BasePaymentMethodAddForm {
protected $cardFieldsBuilder;
protected $routeMatch;
public function __construct(CurrentStoreInterface $current_store, EntityTypeManagerInterface $entity_type_manager, InlineFormManager $inline_form_manager, LoggerInterface $logger, CustomCardFieldsBuilderInterface $card_fields_builder, RouteMatchInterface $route_match) {
parent::__construct($current_store, $entity_type_manager, $inline_form_manager, $logger);
$this->cardFieldsBuilder = $card_fields_builder;
$this->routeMatch = $route_match;
}
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'), $container
->get('commerce_paypal.custom_card_fields_builder'), $container
->get('current_route_match'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$payment_method = $this->entity;
if (!$this
->shouldInjectForm($payment_method
->getPaymentGateway()
->getPlugin())) {
return $form;
}
$order = $this->routeMatch
->getParameter('commerce_order');
$form['payment_details'] += $this->cardFieldsBuilder
->build($order, $payment_method
->getPaymentGateway());
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$payment_method = $this->entity;
$payment_method
->setReusable(FALSE);
if ($this
->shouldInjectForm($payment_method
->getPaymentGateway()
->getPlugin())) {
parent::submitConfigurationForm($form, $form_state);
}
else {
$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);
}
}
}
protected function shouldInjectForm(PaymentGatewayInterface $plugin) {
return $plugin instanceof CheckoutInterface && $plugin
->getPaymentSolution() === 'custom_card_fields';
}
}