View source
<?php
namespace Drupal\commerce_payment\Form;
use Drupal\commerce\InlineFormManager;
use Drupal\commerce_payment\PaymentOption;
use Drupal\commerce_payment\PaymentOptionsBuilderInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsStoredPaymentMethodsInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class PaymentAddForm extends FormBase implements ContainerInjectionInterface {
protected $entityTypeManager;
protected $inlineFormManager;
protected $paymentOptionsBuilder;
protected $order;
protected $paymentOptions;
public function __construct(EntityTypeManagerInterface $entity_type_manager, InlineFormManager $inline_form_manager, RouteMatchInterface $route_match, PaymentOptionsBuilderInterface $payment_options_builder) {
$this->entityTypeManager = $entity_type_manager;
$this->inlineFormManager = $inline_form_manager;
$this->paymentOptionsBuilder = $payment_options_builder;
$this->order = $route_match
->getParameter('commerce_order');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('plugin.manager.commerce_inline_form'), $container
->get('current_route_match'), $container
->get('commerce_payment.options_builder'));
}
public function getFormId() {
return 'commerce_payment_add_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#wrapper_id'] = Html::getUniqueId('payment-add-form-wrapper');
$form['#prefix'] = '<div id="' . $form['#wrapper_id'] . '">';
$form['#suffix'] = '</div>';
$form['#tree'] = TRUE;
$step = $form_state
->get('step');
$step = $step ?: 'payment_gateway';
$form_state
->set('step', $step);
if ($step == 'payment_gateway') {
$form = $this
->buildPaymentGatewayForm($form, $form_state);
}
elseif ($step == 'payment') {
$form = $this
->buildPaymentForm($form, $form_state);
}
return $form;
}
protected function buildPaymentGatewayForm(array $form, FormStateInterface $form_state) {
$payment_gateway_storage = $this->entityTypeManager
->getStorage('commerce_payment_gateway');
$payment_gateways = $payment_gateway_storage
->loadMultipleForOrder($this->order);
$payment_gateways = array_filter($payment_gateways, function ($payment_gateway) {
return !$payment_gateway
->getPlugin() instanceof OffsitePaymentGatewayInterface;
});
if (count($payment_gateways) < 1) {
throw new AccessDeniedHttpException();
}
foreach ($payment_gateways as $payment_gateway) {
if ($js_library = $payment_gateway
->getPlugin()
->getJsLibrary()) {
$form['#attached']['library'][] = $js_library;
}
}
$payment_options = $this->paymentOptionsBuilder
->buildOptions($this->order, $payment_gateways);
$this->paymentOptions = array_filter($payment_options, function (PaymentOption $payment_option) {
$order_payment_method = $this->order
->get('payment_method')->entity;
if (!$order_payment_method) {
return TRUE;
}
if ($order_payment_method
->id() === $payment_option
->getPaymentMethodId()) {
return $order_payment_method
->isReusable();
}
return TRUE;
});
$option_labels = array_map(function (PaymentOption $option) {
return $option
->getLabel();
}, $this->paymentOptions);
$default_option_id = NestedArray::getValue($form_state
->getUserInput(), [
'payment_option',
]);
if ($default_option_id && isset($this->paymentOptions[$default_option_id])) {
$default_option = $this->paymentOptions[$default_option_id];
}
else {
$default_option = $this->paymentOptionsBuilder
->selectDefaultOption($this->order, $this->paymentOptions);
}
$form['payment_option'] = [
'#type' => 'radios',
'#title' => $this
->t('Payment option'),
'#options' => $option_labels,
'#default_value' => $default_option
->getId(),
'#ajax' => [
'callback' => [
get_class($this),
'ajaxRefresh',
],
'wrapper' => $form['#wrapper_id'],
],
];
foreach ($this->paymentOptions as $option) {
$class_name = $option
->getPaymentMethodId() ? 'stored' : 'new';
$form['payment_option'][$option
->getId()]['#attributes']['class'][] = "payment-method--{$class_name}";
}
$default_payment_gateway_id = $default_option
->getPaymentGatewayId();
$payment_gateway = $payment_gateways[$default_payment_gateway_id];
if ($payment_gateway
->getPlugin() instanceof SupportsStoredPaymentMethodsInterface) {
$form = $this
->buildPaymentMethodForm($form, $form_state, $default_option);
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Continue'),
'#button_type' => 'primary',
];
return $form;
}
public static function clearValue(array $element, FormStateInterface $form_state) {
$value = $element['#value'];
if (!isset($element['#options'][$value])) {
$element['#value'] = NULL;
$user_input =& $form_state
->getUserInput();
unset($user_input['payment_option']);
}
return $element;
}
protected function buildPaymentMethodForm(array $form, FormStateInterface $form_state, PaymentOption $payment_option) {
if ($payment_option
->getPaymentMethodId() && !$payment_option
->getPaymentMethodTypeId()) {
return $form;
}
$payment_method_storage = $this->entityTypeManager
->getStorage('commerce_payment_method');
$payment_method = $payment_method_storage
->create([
'type' => $payment_option
->getPaymentMethodTypeId(),
'payment_gateway' => $payment_option
->getPaymentGatewayId(),
'uid' => $this->order
->getCustomerId(),
'billing_profile' => $this->order
->getBillingProfile(),
]);
$inline_form = $this->inlineFormManager
->createInstance('payment_gateway_form', [
'operation' => 'add-payment-method',
], $payment_method);
$form['add_payment_method'] = [
'#parents' => [
'add_payment_method',
],
];
$form['add_payment_method'] = $inline_form
->buildInlineForm($form['add_payment_method'], $form_state);
return $form;
}
protected function buildPaymentForm(array $form, FormStateInterface $form_state) {
$values = [
'order_id' => $this->order
->id(),
];
if ($form_state
->has('new_payment_method')) {
$new_payment_method = $form_state
->get('new_payment_method');
$values['payment_method'] = $new_payment_method
->id();
$values['payment_gateway'] = $new_payment_method
->getPaymentGatewayId();
}
else {
$selected_payment_option = $form_state
->getValue('payment_option');
$payment_option = $this->paymentOptions[$selected_payment_option];
$values['payment_method'] = $payment_option
->getPaymentMethodId();
$values['payment_gateway'] = $payment_option
->getPaymentGatewayId();
}
$payment_storage = $this->entityTypeManager
->getStorage('commerce_payment');
$payment = $payment_storage
->create($values);
$inline_form = $this->inlineFormManager
->createInstance('payment_gateway_form', [
'operation' => 'add-payment',
], $payment);
$form['payment'] = [
'#parents' => [
'payment',
],
];
$form['payment'] = $inline_form
->buildInlineForm($form['payment'], $form_state);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Add payment'),
'#button_type' => 'primary',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$step = $form_state
->get('step');
if ($step == 'payment_gateway') {
if (isset($form['add_payment_method']['#inline_form'])) {
$inline_form = $form['add_payment_method']['#inline_form'];
$payment_method = $inline_form
->getEntity();
$form_state
->set('new_payment_method', $payment_method);
}
$form_state
->set('step', 'payment');
$form_state
->setRebuild(TRUE);
}
elseif ($step == 'payment') {
$payment = $form['payment']['#inline_form']
->getEntity();
$order = $payment
->getOrder();
$order
->set('payment_gateway', $payment
->getPaymentGateway());
if ($payment
->getPaymentMethod()) {
$order
->set('payment_method', $payment
->getPaymentMethod());
}
$this->entityTypeManager
->getStorage('commerce_order')
->save($order);
$this
->messenger()
->addMessage($this
->t('Payment saved.'));
$form_state
->setRedirect('entity.commerce_payment.collection', [
'commerce_order' => $this->order
->id(),
]);
}
}
public static function ajaxRefresh(array $form, FormStateInterface $form_state) {
return $form;
}
}