PaymentReferenceForm.php in Commerce PayPal 8
File
src/PluginForm/PaymentReferenceForm.php
View source
<?php
namespace Drupal\commerce_paypal\PluginForm;
use CommerceGuys\Intl\Formatter\CurrencyFormatterInterface;
use Drupal\commerce_payment\PluginForm\PaymentGatewayFormBase;
use Drupal\commerce_price\Price;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PaymentReferenceForm extends PaymentGatewayFormBase implements ContainerInjectionInterface {
protected $currencyFormatter;
public function __construct(CurrencyFormatterInterface $currency_formatter) {
$this->currencyFormatter = $currency_formatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('commerce_price.currency_formatter'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$payment = $this->entity;
$form['#success_message'] = $this
->t('Reference transaction processed successfully.');
$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;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$payment = $this->entity;
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.'));
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValue($form['#parents']);
$amount = Price::fromArray($values['amount']);
$payment = $this->entity;
$payment_gateway_plugin = $this->plugin;
$payment_gateway_plugin
->referencePayment($payment, $amount);
}
}