Skip to content
Snippets Groups Projects
Commit 96d5aa22 authored by Rico Van de Vin's avatar Rico Van de Vin
Browse files

Issue #3206912 by corneboele, menwithbeards, ricovandevin, Marty2081: Mollie...

Issue #3206912 by corneboele, menwithbeards, ricovandevin, Marty2081: Mollie Webform: Mails only when paid
parent c2766ca0
No related branches found
No related tags found
No related merge requests found
......@@ -31,7 +31,14 @@ This module is a submodule of Mollie for Drupal and depends on that module. This
1. Add a Mollie payment handler via Settings > Emails / Handlers > Add handler on the webform entity.
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
......
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']
......@@ -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' }
......@@ -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;
......@@ -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;
}
/**
......@@ -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;
......@@ -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;
}
}
<?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;
}
}
......@@ -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;
}
......@@ -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}
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment