You are here

class PaymentReferenceForm in Commerce PayPal 8

Class PaymentReferenceForm.

@package Drupal\commerce_paypal\PluginForm

Hierarchy

Expanded class hierarchy of PaymentReferenceForm

File

src/PluginForm/PaymentReferenceForm.php, line 17

Namespace

Drupal\commerce_paypal\PluginForm
View source
class PaymentReferenceForm extends PaymentGatewayFormBase implements ContainerInjectionInterface {

  /**
   * The currency formatter.
   *
   * @var \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface
   */
  protected $currencyFormatter;

  /**
   * Constructs a new PaymentReferenceForm object.
   *
   * @param \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface $currency_formatter
   *   Currency formatter service.
   */
  public function __construct(CurrencyFormatterInterface $currency_formatter) {
    $this->currencyFormatter = $currency_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('commerce_price.currency_formatter'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
    $payment = $this->entity;
    $form['#success_message'] = $this
      ->t('Reference transaction processed successfully.');

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $payment
      ->getOrder();
    $balance = $order
      ->getBalance();
    $formatted_balance = $this->currencyFormatter
      ->format($balance
      ->getNumber(), $balance
      ->getCurrencyCode());
    $description = $this
      ->t('Order balance: @balance', [
      '@balance' => $formatted_balance,
    ]);
    $form['amount'] = [
      '#type' => 'commerce_price',
      '#title' => $this
        ->t('Capture amount'),
      '#default_value' => $payment
        ->getBalance()
        ->toArray(),
      '#required' => TRUE,
      '#available_currencies' => [
        $payment
          ->getAmount()
          ->getCurrencyCode(),
      ],
      '#description' => $description,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
    $payment = $this->entity;

    // If the transaction has expired, display an error message and redirect.
    if ($payment
      ->getCompletedTime() && $payment
      ->getCompletedTime() < strtotime('-365 days')) {
      $form_state
        ->setError($form, $this
        ->t('This transaction has passed its 365 day limit and can no longer be used for reference transactions.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValue($form['#parents']);
    $amount = Price::fromArray($values['amount']);

    /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
    $payment = $this->entity;

    /** @var \Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway\PayflowLinkInterface $payment_gateway_plugin */
    $payment_gateway_plugin = $this->plugin;
    $payment_gateway_plugin
      ->referencePayment($payment, $amount);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
PaymentGatewayFormBase::$entity protected property The form entity.
PaymentGatewayFormBase::getEntity public function Gets the form entity. Overrides PaymentGatewayFormInterface::getEntity
PaymentGatewayFormBase::getErrorElement public function Gets the form element to which errors should be assigned. Overrides PaymentGatewayFormInterface::getErrorElement 1
PaymentGatewayFormBase::setEntity public function Sets the form entity. Overrides PaymentGatewayFormInterface::setEntity
PaymentReferenceForm::$currencyFormatter protected property The currency formatter.
PaymentReferenceForm::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
PaymentReferenceForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
PaymentReferenceForm::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
PaymentReferenceForm::validateConfigurationForm public function Form validation handler. Overrides PluginFormBase::validateConfigurationForm
PaymentReferenceForm::__construct public function Constructs a new PaymentReferenceForm object.
PluginFormBase::$plugin protected property The plugin this form is for. 3
PluginFormBase::setPlugin public function Sets the plugin for this object. Overrides PluginAwareInterface::setPlugin 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.