InvoicePaymentForm.php in Commerce Invoice 8.2
File
src/Form/InvoicePaymentForm.php
View source
<?php
namespace Drupal\commerce_invoice\Form;
use Drupal\commerce_invoice\Entity\InvoiceInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class InvoicePaymentForm extends ConfirmFormBase {
protected $invoice;
public function getFormId() {
return 'commerce_invoice_payment_form';
}
public function getDescription() {
return $this
->t('Are you sure you want to pay the invoice %title?', [
'%title' => $this->invoice
->label(),
]);
}
public function getConfirmText() {
return $this
->t('Pay');
}
public function getQuestion() {
return $this
->t('Are you sure you want to pay the invoice %title?', [
'%title' => $this->invoice
->label(),
]);
}
public function getCancelUrl() {
return Url::fromRoute('entity.commerce_invoice.collection');
}
public function buildForm(array $form, FormStateInterface $form_state, InvoiceInterface $commerce_invoice = NULL) {
$this->invoice = $commerce_invoice;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (!$this->invoice
->isPaid() && $this->invoice
->getTotalPrice()) {
$this->invoice
->setTotalPaid($this->invoice
->getTotalPrice());
$this->invoice
->save();
}
if ($this->invoice
->isPaid()) {
$this
->messenger()
->addMessage($this
->t('Invoice %title has been successfully paid.', [
'%title' => $this->invoice
->label(),
]));
}
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}