
| Current Path : /var/www/html/store/web/modules/contrib/commerce_funds/src/Form/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/store/web/modules/contrib/commerce_funds/src/Form/ConfirmEscrowRelease.php |
<?php
namespace Drupal\commerce_funds\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Url;
use Drupal\commerce_funds\Entity\Transaction;
use Drupal\commerce_funds\TransactionManagerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a confirmation form to release an escrow payment.
*/
class ConfirmEscrowRelease extends ConfirmFormBase {
/**
* The current account.
*
* @var \Drupal\Core\Session\AccountProxy
*/
protected $currentUser;
/**
* The transaction manager.
*
* @var \Drupal\commerce_funds\TransactionManagerInterface
*/
protected $transactionManager;
/**
* The transaction.
*
* @var \Drupal\commerce_funds\Entity\Transaction
*/
protected $transaction;
/**
* Class constructor.
*/
public function __construct(AccountProxy $current_user, TransactionManagerInterface $transaction_manager) {
$this->currentUser = $current_user;
$this->transactionManager = $transaction_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user'),
$container->get('commerce_funds.transaction_manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() : string {
return "confirm_escrow_release";
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('view.commerce_funds_user_transactions.incoming_escrow_payments');
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to release that escrow payment?');
}
/**
* Check if the user is allowed to perform an escrow operation.
*
* @param \Drupal\commerce_funds\Entity\Transaction $transaction
* The transaction id to check permissions on.
*
* @return bool
* User is allowed or not.
*/
protected function isUserAllowed(Transaction $transaction) {
$uid = $this->currentUser->id();
if ($transaction->getStatus() == $transaction::TRANSACTION_STATUS['pending']) {
if ($uid == $transaction->getIssuerId()) {
return TRUE;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, string $transaction_hash = NULL) {
$transaction = $this->transaction = \Drupal::service('commerce_funds.transaction_manager')->loadTransactionByHash($transaction_hash);
// Check if the user is allowed to perform the operation.
if (!empty($transaction) && $this->isUserAllowed($transaction)) {
return parent::buildForm($form, $form_state);
}
else {
throw new NotFoundHttpException();
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$transaction = $this->transaction;
// Send emails.
$this->transactionManager->sendTransactionMails($transaction);
// Release escrow payment.
$this->transactionManager->addFundsToBalance($transaction, $transaction->getRecipient());
// Update site balance.
$this->transactionManager->updateSiteBalance($transaction);
// Update transaction status.
$transaction->setStatus($transaction::TRANSACTION_STATUS['completed']);
$transaction->save();
// Generate confirmation message.
$this->transactionManager->generateConfirmationMessage($transaction);
// Set redirection.
$form_state->setRedirect('view.commerce_funds_user_transactions.incoming_escrow_payments');
}
}