You are here

class MolliePayment in Mollie Payment 8.2

Same name in this branch
  1. 8.2 src/Plugin/Payment/MethodConfiguration/MolliePayment.php \Drupal\mollie_payment\Plugin\Payment\MethodConfiguration\MolliePayment
  2. 8.2 src/Plugin/Payment/Method/MolliePayment.php \Drupal\mollie_payment\Plugin\Payment\Method\MolliePayment

Provides the configuration for the mollie_payment payment method plugin.

Plugins extending this class should provide a configuration schema that extends plugin.plugin_configuration.payment_method_configuration.payment_basic.

Plugin annotation


@PaymentMethodConfiguration(
  description = @Translation("A payment method enabling payment through Mollie."),
  id = "mollie_payment",
  label = @Translation("Mollie")
)

Hierarchy

Expanded class hierarchy of MolliePayment

File

src/Plugin/Payment/MethodConfiguration/MolliePayment.php, line 30

Namespace

Drupal\mollie_payment\Plugin\Payment\MethodConfiguration
View source
class MolliePayment extends PaymentMethodConfigurationBase implements ContainerFactoryPluginInterface {

  /**
   * The payment status plugin type.
   *
   * @var \Drupal\plugin\PluginTypeInterface
   */
  protected $paymentStatusType;

  /**
   * The plugin selector manager.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface
   */
  protected $pluginSelectorManager;

  /**
   * Constructs a new instance.
   *
   * @param mixed[] $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed[] $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translator.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface
   *   The plugin selector manager.
   * @param \Drupal\plugin\PluginType\PluginType $payment_status_type
   *   The payment status plugin type.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, PluginSelectorManagerInterface $plugin_selector_manager, PluginType $payment_status_type) {
    $configuration += $this
      ->defaultConfiguration();
    parent::__construct($configuration, $plugin_id, $plugin_definition, $string_translation, $module_handler);
    $this->paymentStatusType = $payment_status_type;
    $this->pluginSelectorManager = $plugin_selector_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {

    /** @var \Drupal\plugin\PluginTypeManagerInterface $plugin_type_manager */
    $plugin_type_manager = $container
      ->get('plugin.plugin_type_manager');
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('string_translation'), $container
      ->get('module_handler'), $container
      ->get('plugin.manager.plugin.plugin_selector'), $plugin_type_manager
      ->getPluginType('payment_status'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'brand_label' => '',
      'execute_status_id' => 'payment_pending',
      'capture' => FALSE,
      'capture_status_id' => 'payment_success',
      'refund' => FALSE,
      'refund_status_id' => 'payment_refunded',
    ];
  }

  /**
   * Sets the status to set on payment execution.
   *
   * @param string $status
   *   The plugin ID of the payment status to set.
   *
   * @return $this
   */
  public function setExecuteStatusId($status) {
    $this->configuration['execute_status_id'] = $status;
    return $this;
  }

  /**
   * Gets the status to set on payment execution.
   *
   * @return string
   *   The plugin ID of the payment status to set.
   */
  public function getExecuteStatusId() {
    return $this->configuration['execute_status_id'];
  }

  /**
   * Sets the status to set on payment capture.
   *
   * @param string $status
   *   The plugin ID of the payment status to set.
   *
   * @return $this
   */
  public function setCaptureStatusId($status) {
    $this->configuration['capture_status_id'] = $status;
    return $this;
  }

  /**
   * Gets the status to set on payment capture.
   *
   * @return string
   *   The plugin ID of the payment status to set.
   */
  public function getCaptureStatusId() {
    return $this->configuration['capture_status_id'];
  }

  /**
   * Sets whether or not capture is supported.
   *
   * @param bool $capture
   *   Whether or not to support capture.
   *
   * @return $this
   */
  public function setCapture($capture) {
    $this->configuration['capture'] = $capture;
    return $this;
  }

  /**
   * Gets whether or not capture is supported.
   *
   * @param bool
   *   Whether or not to support capture.
   */
  public function getCapture() {
    return $this->configuration['capture'];
  }

  /**
   * Sets the status to set on payment refund.
   *
   * @param string $status
   *   The plugin ID of the payment status to set.
   *
   * @return $this
   */
  public function setRefundStatusId($status) {
    $this->configuration['refund_status_id'] = $status;
    return $this;
  }

  /**
   * Gets the status to set on payment refund.
   *
   * @return string
   *   The plugin ID of the payment status to set.
   */
  public function getRefundStatusId() {
    return $this->configuration['refund_status_id'];
  }

  /**
   * Sets whether or not refunds are supported.
   *
   * @param bool $refund
   *   Whether or not to support refunds.
   *
   * @return $this
   */
  public function setRefund($refund) {
    $this->configuration['refund'] = $refund;
    return $this;
  }

  /**
   * Gets whether or not refunds are supported.
   *
   * @param bool
   *   Whether or not to support refunds.
   */
  public function getRefund() {
    return $this->configuration['refund'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['plugin_form'] = [
      '#process' => [
        [
          $this,
          'processBuildConfigurationForm',
        ],
      ],
      '#type' => 'container',
    ];
    return $form;
  }

  /**
   * Implements a form API #process callback.
   */
  public function processBuildConfigurationForm(array &$element, FormStateInterface $form_state, array &$form) {
    $element['brand_label'] = [
      '#default_value' => $this
        ->getBrandLabel(),
      '#description' => $this
        ->t('The label that payers will see when choosing a payment method. Defaults to the payment method label.'),
      '#title' => $this
        ->t('Brand label'),
      '#type' => 'textfield',
    ];
    $mollie_profiles = MollieProfile::loadMultiple();
    $options = [
      '' => $this
        ->t('- Select a profile -'),
    ];
    foreach ($mollie_profiles as $id => $mollie_profile) {
      $options[$id] = $mollie_profile
        ->label();
    }
    $element['profile'] = [
      '#default_value' => $this
        ->getProfile(),
      '#description' => $this
        ->t('The Mollie profile that will be used to connect to Mollie.'),
      '#title' => $this
        ->t('Mollie profile'),
      '#type' => 'select',
      '#options' => $options,
    ];
    $workflow_group = implode('][', array_merge($element['#parents'], [
      'workflow',
    ]));
    $element['workflow'] = [
      '#type' => 'vertical_tabs',
    ];
    $element['execute'] = [
      '#group' => $workflow_group,
      '#open' => TRUE,
      '#type' => 'details',
      '#title' => $this
        ->t('Execution'),
    ];
    $element['execute']['execute_status'] = $this
      ->getExecutePaymentStatusSelector($form_state)
      ->buildSelectorForm([], $form_state);
    $element['capture'] = [
      '#group' => $workflow_group,
      '#open' => TRUE,
      '#type' => 'details',
      '#title' => $this
        ->t('Capture'),
    ];
    $capture_id = Html::getUniqueId('capture');
    $element['capture']['capture'] = [
      '#id' => $capture_id,
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add an additional capture step after payments have been executed.'),
      '#default_value' => $this
        ->getCapture(),
    ];
    $element['capture']['plugin_form'] = [
      '#type' => 'container',
      '#states' => [
        'visible' => [
          '#' . $capture_id => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $element['capture']['plugin_form']['capture_status'] = $this
      ->getCapturePaymentStatusSelector($form_state)
      ->buildSelectorForm([], $form_state);
    $refund_id = Html::getUniqueId('refund');
    $element['refund'] = [
      '#group' => $workflow_group,
      '#open' => TRUE,
      '#type' => 'details',
      '#title' => $this
        ->t('Refund'),
    ];
    $element['refund']['refund'] = [
      '#id' => $refund_id,
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add an additional refund step after payments have been executed.'),
      '#default_value' => $this
        ->getRefund(),
    ];
    $element['refund']['plugin_form'] = [
      '#type' => 'container',
      '#states' => [
        'visible' => [
          '#' . $refund_id => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $element['refund']['plugin_form']['refund_status'] = $this
      ->getRefundPaymentStatusSelector($form_state)
      ->buildSelectorForm([], $form_state);
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this
      ->getExecutePaymentStatusSelector($form_state)
      ->validateSelectorForm($form['plugin_form']['execute']['execute_status'], $form_state);
    $this
      ->getCapturePaymentStatusSelector($form_state)
      ->validateSelectorForm($form['plugin_form']['capture']['plugin_form']['capture_status'], $form_state);
    $this
      ->getRefundPaymentStatusSelector($form_state)
      ->validateSelectorForm($form['plugin_form']['refund']['plugin_form']['refund_status'], $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this
      ->getExecutePaymentStatusSelector($form_state)
      ->submitSelectorForm($form['plugin_form']['execute']['execute_status'], $form_state);
    $this
      ->getCapturePaymentStatusSelector($form_state)
      ->submitSelectorForm($form['plugin_form']['capture']['plugin_form']['capture_status'], $form_state);
    $this
      ->getRefundPaymentStatusSelector($form_state)
      ->submitSelectorForm($form['plugin_form']['refund']['plugin_form']['refund_status'], $form_state);
    $parents = $form['plugin_form']['brand_label']['#parents'];
    array_pop($parents);
    $values = $form_state
      ->getValues();
    $values = NestedArray::getValue($values, $parents);
    $this
      ->setExecuteStatusId($this
      ->getExecutePaymentStatusSelector($form_state)
      ->getSelectedPlugin()
      ->getPluginId());
    $this
      ->setCapture($values['capture']['capture']);
    $this
      ->setCaptureStatusId($this
      ->getCapturePaymentStatusSelector($form_state)
      ->getSelectedPlugin()
      ->getPluginId());
    $this
      ->setRefund($values['refund']['refund']);
    $this
      ->setRefundStatusId($this
      ->getRefundPaymentStatusSelector($form_state)
      ->getSelectedPlugin()
      ->getPluginId());
    $this
      ->setBrandLabel($values['brand_label']);
    $this
      ->setProfile($values['profile']);
  }

  /**
   * Gets the brand label.
   *
   * @return string
   */
  public function getBrandLabel() {
    return $this->configuration['brand_label'];
  }

  /**
   * Sets the brand label.
   *
   * @param string $label
   *
   * @return static
   */
  public function setBrandLabel($label) {
    $this->configuration['brand_label'] = $label;
    return $this;
  }

  /**
   * Gets the Mollie profile.
   *
   * @return string
   */
  public function getProfile() {
    return $this->configuration['profile'];
  }

  /**
   * Sets the Mollie profile.
   *
   * @param string $profile
   *
   * @return static
   */
  public function setProfile($profile) {
    $this->configuration['profile'] = $profile;
    return $this;
  }

  /**
   * Gets the payment status selector for the execute phase.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *
   * @return \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface
   */
  protected function getExecutePaymentStatusSelector(FormStateInterface $form_state) {
    $plugin_selector = $this
      ->getPaymentStatusSelector($form_state, 'execute', $this
      ->getExecuteStatusId());
    $plugin_selector
      ->setLabel($this
      ->t('Payment execution status'));
    $plugin_selector
      ->setDescription($this
      ->t('The status to set payments to after being executed by this payment method.'));
    return $plugin_selector;
  }

  /**
   * Gets the payment status selector for the capture phase.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *
   * @return \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface
   */
  protected function getCapturePaymentStatusSelector(FormStateInterface $form_state) {
    $plugin_selector = $this
      ->getPaymentStatusSelector($form_state, 'capture', $this
      ->getExecuteStatusId());
    $plugin_selector
      ->setLabel($this
      ->t('Payment capture status'));
    $plugin_selector
      ->setDescription($this
      ->t('The status to set payments to after being captured by this payment method.'));
    return $plugin_selector;
  }

  /**
   * Gets the payment status selector for the refund phase.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *
   * @return \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface
   */
  protected function getRefundPaymentStatusSelector(FormStateInterface $form_state) {
    $plugin_selector = $this
      ->getPaymentStatusSelector($form_state, 'refund', $this
      ->getExecuteStatusId());
    $plugin_selector
      ->setLabel($this
      ->t('Payment refund status'));
    $plugin_selector
      ->setDescription($this
      ->t('The status to set payments to after being refunded by this payment method.'));
    return $plugin_selector;
  }

  /**
   * Gets the payment status selector.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   * @param string $type
   * @param string $default_plugin_id
   *
   * @return \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface
   */
  protected function getPaymentStatusSelector(FormStateInterface $form_state, $type, $default_plugin_id) {
    $key = 'payment_status_selector_' . $type;
    if ($form_state
      ->has($key)) {
      $plugin_selector = $form_state
        ->get($key);
    }
    else {
      $plugin_selector = $this->pluginSelectorManager
        ->createInstance('payment_select_list');
      $plugin_selector
        ->setSelectablePluginType($this->paymentStatusType);
      $plugin_selector
        ->setRequired(TRUE);
      $plugin_selector
        ->setCollectPluginConfiguration(FALSE);
      $plugin_selector
        ->setSelectedPlugin($this->paymentStatusType
        ->getPluginManager()
        ->createInstance($default_plugin_id));
      $form_state
        ->set($key, $plugin_selector);
    }
    return $plugin_selector;
  }

}

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
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
MolliePayment::$paymentStatusType protected property The payment status plugin type.
MolliePayment::$pluginSelectorManager protected property The plugin selector manager.
MolliePayment::buildConfigurationForm public function Form constructor. Overrides PaymentMethodConfigurationBase::buildConfigurationForm
MolliePayment::create public static function Creates an instance of the plugin. Overrides PaymentMethodConfigurationBase::create
MolliePayment::defaultConfiguration public function Gets default configuration for this plugin. Overrides PaymentMethodConfigurationBase::defaultConfiguration
MolliePayment::getBrandLabel public function Gets the brand label.
MolliePayment::getCapture public function Gets whether or not capture is supported.
MolliePayment::getCapturePaymentStatusSelector protected function Gets the payment status selector for the capture phase.
MolliePayment::getCaptureStatusId public function Gets the status to set on payment capture.
MolliePayment::getExecutePaymentStatusSelector protected function Gets the payment status selector for the execute phase.
MolliePayment::getExecuteStatusId public function Gets the status to set on payment execution.
MolliePayment::getPaymentStatusSelector protected function Gets the payment status selector.
MolliePayment::getProfile public function Gets the Mollie profile.
MolliePayment::getRefund public function Gets whether or not refunds are supported.
MolliePayment::getRefundPaymentStatusSelector protected function Gets the payment status selector for the refund phase.
MolliePayment::getRefundStatusId public function Gets the status to set on payment refund.
MolliePayment::processBuildConfigurationForm public function Implements a form API #process callback.
MolliePayment::setBrandLabel public function Sets the brand label.
MolliePayment::setCapture public function Sets whether or not capture is supported.
MolliePayment::setCaptureStatusId public function Sets the status to set on payment capture.
MolliePayment::setExecuteStatusId public function Sets the status to set on payment execution.
MolliePayment::setProfile public function Sets the Mollie profile.
MolliePayment::setRefund public function Sets whether or not refunds are supported.
MolliePayment::setRefundStatusId public function Sets the status to set on payment refund.
MolliePayment::submitConfigurationForm public function Form submission handler. Overrides PaymentMethodConfigurationBase::submitConfigurationForm
MolliePayment::validateConfigurationForm public function Form validation handler. Overrides PaymentMethodConfigurationBase::validateConfigurationForm
MolliePayment::__construct public function Constructs a new instance. Overrides PaymentMethodConfigurationBase::__construct
PaymentMethodConfigurationBase::$moduleHandler protected property The module handler.
PaymentMethodConfigurationBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
PaymentMethodConfigurationBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
PaymentMethodConfigurationBase::getMessageText public function Gets the payer message text.
PaymentMethodConfigurationBase::getMessageTextFormat public function Gets the payer message text format.
PaymentMethodConfigurationBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
PaymentMethodConfigurationBase::setMessageText public function Sets payer message text.
PaymentMethodConfigurationBase::setMessageTextFormat public function Sets payer message text format.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
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.