PaymentOperationForm.php in Commerce Core 8.2
File
modules/payment/src/Form/PaymentOperationForm.php
View source
<?php
namespace Drupal\commerce_payment\Form;
use Drupal\commerce\InlineFormManager;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentOperationForm extends EntityForm implements ContainerInjectionInterface {
protected $inlineFormManager;
public function __construct(InlineFormManager $inline_form_manager) {
$this->inlineFormManager = $inline_form_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.commerce_inline_form'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$payment = $this->entity;
$payment_gateway_plugin = $payment
->getPaymentGateway()
->getPlugin();
$operations = $payment_gateway_plugin
->buildPaymentOperations($payment);
$operation_id = $this
->getRouteMatch()
->getParameter('operation');
$operation = $operations[$operation_id];
$inline_form = $this->inlineFormManager
->createInstance('payment_gateway_form', [
'operation' => $operation['plugin_form'],
], $this->entity);
$form['#title'] = $operation['page_title'];
$form['#tree'] = TRUE;
$form['payment'] = [
'#parents' => [
'payment',
],
'#inline_form' => $inline_form,
];
$form['payment'] = $inline_form
->buildInlineForm($form['payment'], $form_state);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $operation['title'],
'#button_type' => 'primary',
];
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'button',
],
],
'#url' => $this->entity
->toUrl('collection'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$inline_form = $form['payment']['#inline_form'];
$payment = $inline_form
->getEntity();
if (!empty($form['payment']['#success_message'])) {
$this
->messenger()
->addMessage($form['payment']['#success_message']);
}
$form_state
->setRedirect('entity.commerce_payment.collection', [
'commerce_order' => $payment
->getOrderId(),
]);
}
}