Loading modules/mollie_webform/README.md +8 −1 Original line number Diff line number Diff line Loading @@ -32,6 +32,13 @@ This module is a submodule of Mollie for Drupal and depends on that module. This 2. Configure the currency, the element to capture the amount from and optionally the elements to capture the payment method and transaction description from. ### Sending emails based on transaction status The transaction status is set and updated after the submission has been completed. To send an email based on the transaction status 1. Add an email handler via Settings > Emails / Handlers > Add handler on the webform entity. 2. Set a condition based on the value of the Mollie payment status element. 3. Set the handler to act on submission updates on the Advanced tab on the handlers settings form. ## Usage The module will update the Mollie payment status element in the webform submission every time that Mollie reports back a status for the transaction corresponding to the webform submission. In this way the status of a transaction can be Loading modules/mollie_webform/mollie_webform.services.yml +3 −1 Original line number Diff line number Diff line services: mollie_webform.helper: class: '\Drupal\mollie_webform\MollieWebformHelper' mollie_webform.redirect_event_subscriber: class: '\Drupal\mollie_webform\EventSubscriber\MollieRedirectEventSubscriber' arguments: ['@entity_type.manager', '@webform.message_manager', '@current_route_match', '@request_stack', '@webform.token_manager', '@config.factory', '@path_alias.manager', '@path.validator', '@webform.request'] Loading @@ -6,6 +8,6 @@ services: - { name: 'event_subscriber' } mollie_webform.transaction_event_subscriber.status_change: class: '\Drupal\mollie_webform\EventSubscriber\MollieTransactionEventSubscriber' arguments: ['@entity_type.manager'] arguments: ['@entity_type.manager', '@mollie_webform.helper'] tags: - { name: 'event_subscriber' } modules/mollie_webform/src/EventSubscriber/MollieTransactionEventSubscriber.php +28 −33 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\mollie\Entity\Payment; use Drupal\mollie\Events\MollieTransactionStatusChangeEvent; use Drupal\mollie_webform\MollieWebformHelper; use Drupal\webform\WebformSubmissionInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; Loading @@ -21,16 +22,26 @@ class MollieTransactionEventSubscriber implements EventSubscriberInterface { * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ private $entityTypeManager; protected $entityTypeManager; /** * Mollie webform helper. * * @var \Drupal\mollie_webform\MollieWebformHelper */ protected $mollieWebformHelper; /** * MollieTransactionEventSubscriber constructor. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * Entity type manager. * @param \Drupal\mollie_webform\MollieWebformHelper $mollieWebformHelper * Mollie webform helper. */ public function __construct(EntityTypeManagerInterface $entityTypeManager) { public function __construct(EntityTypeManagerInterface $entityTypeManager, MollieWebformHelper $mollie_webform_helper) { $this->entityTypeManager = $entityTypeManager; $this->mollieWebformHelper = $mollie_webform_helper; } /** Loading @@ -56,15 +67,24 @@ class MollieTransactionEventSubscriber implements EventSubscriberInterface { return; } // Load the webform and save the payment status. // Load the webform. /** @var \Drupal\webform\WebformSubmissionInterface $submission */ $submission = $this->entityTypeManager->getStorage('webform_submission') ->load($event->getContextId()); $submission->setElementData( $this->getMolliePaymentStatusElementNameFromWebformSubmission($submission), $transaction->getStatus() ); $submission->resave(); // Check whether the transaction status has changed. $element_key = $this->mollieWebformHelper ->getMolliePaymentStatusElementNameFromWebformSubmission($submission); $current_status = $submission->getElementData($element_key); if ($transaction->getStatus() !== $current_status) { // Save the new payment status. $submission->setElementData($element_key, $transaction->getStatus()); // We use a regular save() instead of a resave() to be sure hooks are // triggered. This allows e.g. to only trigger an email handler for a // specific payment status. $submission->save(); } } catch (InvalidPluginDefinitionException | PluginNotFoundException | EntityStorageException $e) { watchdog_exception('mollie_webform', $e); $httpStatusCode = 500; Loading @@ -83,29 +103,4 @@ class MollieTransactionEventSubscriber implements EventSubscriberInterface { ]; } /** * Returns the machine name of the status element on a webform by submission. * * @param \Drupal\webform\WebformSubmissionInterface $submission * A submission submitted for the webform. * * @return string|null * The machine name of the Mollie transaction status element. Or null when * na such element was found on the webform. */ protected function getMolliePaymentStatusElementNameFromWebformSubmission(WebformSubmissionInterface $submission): ?string { $elements = $submission->getWebform()->getElementsDecoded(); foreach ($elements as $name => $element) { if ('mollie_payment_status' === $element['#type']) { // We assume that there is only one element for the Mollie payment // status. If there happen to be more the transactions status is stored // in the first one returned by // \Drupal\webform\WebformInterface::getElementsDecoded(). return $name; } } return NULL; } } modules/mollie_webform/src/MollieWebformHelper.php 0 → 100644 +37 −0 Original line number Diff line number Diff line <?php namespace Drupal\mollie_webform; use Drupal\webform\WebformSubmissionInterface; /** * Provides some helper methods for the Mollie for Drupal Webform module. */ class MollieWebformHelper { /** * Returns the machine name of the status element on a webform by submission. * * @param \Drupal\webform\WebformSubmissionInterface $submission * A submission submitted for the webform. * * @return string|null * The machine name of the Mollie transaction status element. Or null when * na such element was found on the webform. */ public function getMolliePaymentStatusElementNameFromWebformSubmission(WebformSubmissionInterface $submission): ?string { $elements = $submission->getWebform()->getElementsDecoded(); foreach ($elements as $name => $element) { if ('mollie_payment_status' === $element['#type']) { // We assume that there is only one element for the Mollie payment // status. If there happen to be more the transactions' status is stored // in the first one returned by // \Drupal\webform\WebformInterface::getElementsDecoded(). return $name; } } return NULL; } } modules/mollie_webform/src/Plugin/WebformHandler/MolliePaymentHandler.php +26 −0 Original line number Diff line number Diff line Loading @@ -28,12 +28,20 @@ class MolliePaymentHandler extends WebformHandlerBase { */ protected $entityTypeManager; /** * The Mollie webform helper. * * @var \Drupal\mollie_webform\MollieWebformHelper */ protected $mollieWebformHelper; /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $class = parent::create($container, $configuration, $plugin_id, $plugin_definition); $class->entityTypeManager = $container->get('entity_type.manager'); $class->mollieWebformHelper = $container->get('mollie_webform.helper'); return $class; } Loading Loading @@ -114,6 +122,24 @@ class MolliePaymentHandler extends WebformHandlerBase { ]; } /** * {@inheritdoc} */ public function checkConditions(WebformSubmissionInterface $webform_submission) { $conditions_are_met = parent::checkConditions($webform_submission); // If not all conditions are met no further checks are need. if (!$conditions_are_met) { return $conditions_are_met; } // If other conditions are met, check that there is no payment already by // validating that there is no payment status set yet. $element_key = $this->mollieWebformHelper ->getMolliePaymentStatusElementNameFromWebformSubmission($webform_submission); return empty($webform_submission->getElementData($element_key)); } /** * {@inheritdoc} */ Loading Loading
modules/mollie_webform/README.md +8 −1 Original line number Diff line number Diff line Loading @@ -32,6 +32,13 @@ This module is a submodule of Mollie for Drupal and depends on that module. This 2. Configure the currency, the element to capture the amount from and optionally the elements to capture the payment method and transaction description from. ### Sending emails based on transaction status The transaction status is set and updated after the submission has been completed. To send an email based on the transaction status 1. Add an email handler via Settings > Emails / Handlers > Add handler on the webform entity. 2. Set a condition based on the value of the Mollie payment status element. 3. Set the handler to act on submission updates on the Advanced tab on the handlers settings form. ## Usage The module will update the Mollie payment status element in the webform submission every time that Mollie reports back a status for the transaction corresponding to the webform submission. In this way the status of a transaction can be Loading
modules/mollie_webform/mollie_webform.services.yml +3 −1 Original line number Diff line number Diff line services: mollie_webform.helper: class: '\Drupal\mollie_webform\MollieWebformHelper' mollie_webform.redirect_event_subscriber: class: '\Drupal\mollie_webform\EventSubscriber\MollieRedirectEventSubscriber' arguments: ['@entity_type.manager', '@webform.message_manager', '@current_route_match', '@request_stack', '@webform.token_manager', '@config.factory', '@path_alias.manager', '@path.validator', '@webform.request'] Loading @@ -6,6 +8,6 @@ services: - { name: 'event_subscriber' } mollie_webform.transaction_event_subscriber.status_change: class: '\Drupal\mollie_webform\EventSubscriber\MollieTransactionEventSubscriber' arguments: ['@entity_type.manager'] arguments: ['@entity_type.manager', '@mollie_webform.helper'] tags: - { name: 'event_subscriber' }
modules/mollie_webform/src/EventSubscriber/MollieTransactionEventSubscriber.php +28 −33 Original line number Diff line number Diff line Loading @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\mollie\Entity\Payment; use Drupal\mollie\Events\MollieTransactionStatusChangeEvent; use Drupal\mollie_webform\MollieWebformHelper; use Drupal\webform\WebformSubmissionInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; Loading @@ -21,16 +22,26 @@ class MollieTransactionEventSubscriber implements EventSubscriberInterface { * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ private $entityTypeManager; protected $entityTypeManager; /** * Mollie webform helper. * * @var \Drupal\mollie_webform\MollieWebformHelper */ protected $mollieWebformHelper; /** * MollieTransactionEventSubscriber constructor. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * Entity type manager. * @param \Drupal\mollie_webform\MollieWebformHelper $mollieWebformHelper * Mollie webform helper. */ public function __construct(EntityTypeManagerInterface $entityTypeManager) { public function __construct(EntityTypeManagerInterface $entityTypeManager, MollieWebformHelper $mollie_webform_helper) { $this->entityTypeManager = $entityTypeManager; $this->mollieWebformHelper = $mollie_webform_helper; } /** Loading @@ -56,15 +67,24 @@ class MollieTransactionEventSubscriber implements EventSubscriberInterface { return; } // Load the webform and save the payment status. // Load the webform. /** @var \Drupal\webform\WebformSubmissionInterface $submission */ $submission = $this->entityTypeManager->getStorage('webform_submission') ->load($event->getContextId()); $submission->setElementData( $this->getMolliePaymentStatusElementNameFromWebformSubmission($submission), $transaction->getStatus() ); $submission->resave(); // Check whether the transaction status has changed. $element_key = $this->mollieWebformHelper ->getMolliePaymentStatusElementNameFromWebformSubmission($submission); $current_status = $submission->getElementData($element_key); if ($transaction->getStatus() !== $current_status) { // Save the new payment status. $submission->setElementData($element_key, $transaction->getStatus()); // We use a regular save() instead of a resave() to be sure hooks are // triggered. This allows e.g. to only trigger an email handler for a // specific payment status. $submission->save(); } } catch (InvalidPluginDefinitionException | PluginNotFoundException | EntityStorageException $e) { watchdog_exception('mollie_webform', $e); $httpStatusCode = 500; Loading @@ -83,29 +103,4 @@ class MollieTransactionEventSubscriber implements EventSubscriberInterface { ]; } /** * Returns the machine name of the status element on a webform by submission. * * @param \Drupal\webform\WebformSubmissionInterface $submission * A submission submitted for the webform. * * @return string|null * The machine name of the Mollie transaction status element. Or null when * na such element was found on the webform. */ protected function getMolliePaymentStatusElementNameFromWebformSubmission(WebformSubmissionInterface $submission): ?string { $elements = $submission->getWebform()->getElementsDecoded(); foreach ($elements as $name => $element) { if ('mollie_payment_status' === $element['#type']) { // We assume that there is only one element for the Mollie payment // status. If there happen to be more the transactions status is stored // in the first one returned by // \Drupal\webform\WebformInterface::getElementsDecoded(). return $name; } } return NULL; } }
modules/mollie_webform/src/MollieWebformHelper.php 0 → 100644 +37 −0 Original line number Diff line number Diff line <?php namespace Drupal\mollie_webform; use Drupal\webform\WebformSubmissionInterface; /** * Provides some helper methods for the Mollie for Drupal Webform module. */ class MollieWebformHelper { /** * Returns the machine name of the status element on a webform by submission. * * @param \Drupal\webform\WebformSubmissionInterface $submission * A submission submitted for the webform. * * @return string|null * The machine name of the Mollie transaction status element. Or null when * na such element was found on the webform. */ public function getMolliePaymentStatusElementNameFromWebformSubmission(WebformSubmissionInterface $submission): ?string { $elements = $submission->getWebform()->getElementsDecoded(); foreach ($elements as $name => $element) { if ('mollie_payment_status' === $element['#type']) { // We assume that there is only one element for the Mollie payment // status. If there happen to be more the transactions' status is stored // in the first one returned by // \Drupal\webform\WebformInterface::getElementsDecoded(). return $name; } } return NULL; } }
modules/mollie_webform/src/Plugin/WebformHandler/MolliePaymentHandler.php +26 −0 Original line number Diff line number Diff line Loading @@ -28,12 +28,20 @@ class MolliePaymentHandler extends WebformHandlerBase { */ protected $entityTypeManager; /** * The Mollie webform helper. * * @var \Drupal\mollie_webform\MollieWebformHelper */ protected $mollieWebformHelper; /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $class = parent::create($container, $configuration, $plugin_id, $plugin_definition); $class->entityTypeManager = $container->get('entity_type.manager'); $class->mollieWebformHelper = $container->get('mollie_webform.helper'); return $class; } Loading Loading @@ -114,6 +122,24 @@ class MolliePaymentHandler extends WebformHandlerBase { ]; } /** * {@inheritdoc} */ public function checkConditions(WebformSubmissionInterface $webform_submission) { $conditions_are_met = parent::checkConditions($webform_submission); // If not all conditions are met no further checks are need. if (!$conditions_are_met) { return $conditions_are_met; } // If other conditions are met, check that there is no payment already by // validating that there is no payment status set yet. $element_key = $this->mollieWebformHelper ->getMolliePaymentStatusElementNameFromWebformSubmission($webform_submission); return empty($webform_submission->getElementData($element_key)); } /** * {@inheritdoc} */ Loading