View source
<?php
namespace Drupal\payment\Plugin\Payment\Method;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\Token;
use Drupal\payment\EventDispatcherInterface;
use Drupal\payment\PaymentAwareTrait;
use Drupal\payment\OperationResult;
use Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PaymentMethodBase extends PluginBase implements ContainerFactoryPluginInterface, PaymentMethodInterface, PaymentMethodCapturePaymentInterface, PaymentMethodRefundPaymentInterface, PluginFormInterface, CacheableDependencyInterface, ConfigurableInterface, DependentPluginInterface {
use PaymentAwareTrait;
protected $eventDispatcher;
protected $moduleHandler;
protected $token;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, EventDispatcherInterface $event_dispatcher, Token $token, PaymentStatusManagerInterface $payment_status_manager) {
$configuration += $this
->defaultConfiguration();
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->eventDispatcher = $event_dispatcher;
$this->moduleHandler = $module_handler;
$this->paymentStatusManager = $payment_status_manager;
$this->token = $token;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('payment.event_dispatcher'), $container
->get('token'), $container
->get('plugin.manager.payment.status'));
}
public function getCacheContexts() {
return [];
}
public function getCacheTags() {
return [
'payment_method',
];
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function calculateDependencies() {
return [];
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this
->defaultConfiguration();
}
public function getMessageText() {
return $this->pluginDefinition['message_text'];
}
public function getMessageTextFormat() {
return $this->pluginDefinition['message_text_format'];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$message_text = $this->token
->replace($this
->getMessageText(), array(
'payment' => $this
->getPayment(),
), array(
'clear' => TRUE,
));
if ($this->moduleHandler
->moduleExists('filter')) {
$elements['message'] = array(
'#type' => 'processed_text',
'#text' => $message_text,
'#format' => $this
->getMessageTextFormat(),
);
}
else {
$elements['message'] = array(
'#type' => 'markup',
'#markup' => $message_text,
);
}
return $elements;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function executePaymentAccess(AccountInterface $account) {
if (!$this
->getPayment()) {
throw new \LogicException('Trying to check access for a non-existing payment. A payment must be set trough self::setPayment() first.');
}
return AccessResult::allowedIf($this->pluginDefinition['active'])
->andIf($this
->executePaymentAccessCurrency($account))
->andIf($this->eventDispatcher
->executePaymentAccess($this
->getPayment(), $this, $account))
->andIf($this
->doExecutePaymentAccess($account))
->addCacheableDependency($this
->getPayment())
->addCacheTags([
'payment_method',
]);
}
protected function doExecutePaymentAccess(AccountInterface $account) {
return AccessResult::allowed();
}
public function executePayment() {
if (!$this
->getPayment()) {
throw new \LogicException('Trying to execute a non-existing payment. A payment must be set trough self::setPayment() first.');
}
$this->eventDispatcher
->preExecutePayment($this
->getPayment());
$this->payment
->setPaymentStatus($this->paymentStatusManager
->createInstance('payment_pending'));
$this
->doExecutePayment();
$this
->getPayment()
->save();
return $this
->getPaymentExecutionResult();
}
protected function doExecutePayment() {
}
public function getPaymentExecutionResult() {
return new OperationResult();
}
public function capturePaymentAccess(AccountInterface $account) {
if (!$this
->getPayment()) {
throw new \LogicException('Trying to check access for a non-existing payment. A payment must be set trough self::setPayment() first.');
}
return $this
->doCapturePaymentAccess($account);
}
protected function doCapturePaymentAccess(AccountInterface $account) {
return AccessResult::forbidden();
}
public function capturePayment() {
if (!$this
->getPayment()) {
throw new \LogicException('Trying to capture a non-existing payment. A payment must be set trough self::setPayment() first.');
}
$this->eventDispatcher
->preCapturePayment($this
->getPayment());
$this
->doCapturePayment();
return $this
->getPaymentCaptureResult();
}
public function getPaymentCaptureResult() {
return new OperationResult();
}
protected function doCapturePayment() {
throw new \Exception('Child classes must override this method to support payment capture.');
}
public function refundPaymentAccess(AccountInterface $account) {
if (!$this
->getPayment()) {
throw new \LogicException('Trying to check access for a non-existing payment. A payment must be set trough self::setPayment() first.');
}
return $this
->doRefundPaymentAccess($account);
}
protected function doRefundPaymentAccess(AccountInterface $account) {
return AccessResult::forbidden();
}
public function refundPayment() {
if (!$this
->getPayment()) {
throw new \LogicException('Trying to refund a non-existing payment. A payment must be set trough self::setPayment() first.');
}
$this->eventDispatcher
->preRefundPayment($this
->getPayment());
$this
->doRefundPayment();
return $this
->getPaymentRefundResult();
}
public function getPaymentRefundResult() {
return new OperationResult();
}
protected function doRefundPayment() {
throw new \Exception('Child classes must override this method to support payment refund.');
}
protected function executePaymentAccessCurrency(AccountInterface $account) {
$supported_currencies = $this
->getSupportedCurrencies();
$payment_currency_code = $this
->getPayment()
->getCurrencyCode();
$payment_amount = $this
->getPayment()
->getAmount();
if ($supported_currencies === TRUE) {
return AccessResult::allowed();
}
foreach ($supported_currencies as $supported_currency) {
if ($supported_currency
->getCurrencyCode() != $payment_currency_code) {
continue;
}
elseif ($supported_currency
->getMinimumAmount() && $payment_amount < $supported_currency
->getMinimumAmount()) {
return AccessResult::forbidden();
}
elseif ($supported_currency
->getMaximumAmount() && $payment_amount > $supported_currency
->getMaximumAmount()) {
return AccessResult::forbidden();
}
else {
return AccessResult::allowed();
}
}
return AccessResult::forbidden();
}
protected abstract function getSupportedCurrencies();
}