protected function PaymentAddForm::buildPaymentForm in Commerce Core 8.2
Builds the form for adding a payment.
Parameters
array $form: The parent form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the complete form.
Return value
array The built form.
1 call to PaymentAddForm::buildPaymentForm()
- PaymentAddForm::buildForm in modules/
payment/ src/ Form/ PaymentAddForm.php - Form constructor.
File
- modules/
payment/ src/ Form/ PaymentAddForm.php, line 283
Class
- PaymentAddForm
- Provides the payment add form.
Namespace
Drupal\commerce_payment\FormCode
protected function buildPaymentForm(array $form, FormStateInterface $form_state) {
$values = [
'order_id' => $this->order
->id(),
];
if ($form_state
->has('new_payment_method')) {
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $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');
/** @var \Drupal\commerce_payment\PaymentOption $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;
}