View source
<?php
namespace Drupal\mollie_payment\Plugin\Payment\MethodConfiguration;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface;
use Drupal\plugin\PluginType\PluginType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\payment\Plugin\Payment\MethodConfiguration\PaymentMethodConfigurationBase;
use Drupal\mollie_payment\Entity\MollieProfile;
class MolliePayment extends PaymentMethodConfigurationBase implements ContainerFactoryPluginInterface {
protected $paymentStatusType;
protected $pluginSelectorManager;
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;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$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'));
}
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',
];
}
public function setExecuteStatusId($status) {
$this->configuration['execute_status_id'] = $status;
return $this;
}
public function getExecuteStatusId() {
return $this->configuration['execute_status_id'];
}
public function setCaptureStatusId($status) {
$this->configuration['capture_status_id'] = $status;
return $this;
}
public function getCaptureStatusId() {
return $this->configuration['capture_status_id'];
}
public function setCapture($capture) {
$this->configuration['capture'] = $capture;
return $this;
}
public function getCapture() {
return $this->configuration['capture'];
}
public function setRefundStatusId($status) {
$this->configuration['refund_status_id'] = $status;
return $this;
}
public function getRefundStatusId() {
return $this->configuration['refund_status_id'];
}
public function setRefund($refund) {
$this->configuration['refund'] = $refund;
return $this;
}
public function getRefund() {
return $this->configuration['refund'];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['plugin_form'] = [
'#process' => [
[
$this,
'processBuildConfigurationForm',
],
],
'#type' => 'container',
];
return $form;
}
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;
}
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);
}
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']);
}
public function getBrandLabel() {
return $this->configuration['brand_label'];
}
public function setBrandLabel($label) {
$this->configuration['brand_label'] = $label;
return $this;
}
public function getProfile() {
return $this->configuration['profile'];
}
public function setProfile($profile) {
$this->configuration['profile'] = $profile;
return $this;
}
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;
}
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;
}
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;
}
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;
}
}