diff --git a/modules/mollie_webform/src/EventSubscriber/MollieTransactionEventSubscriber.php b/modules/mollie_webform/src/EventSubscriber/MollieTransactionEventSubscriber.php index 511511c8ba61a9d8c157f84213525ab63f25e1b4..8628a68bc95277b87ca466fb5f239bf8eec1f39e 100644 --- a/modules/mollie_webform/src/EventSubscriber/MollieTransactionEventSubscriber.php +++ b/modules/mollie_webform/src/EventSubscriber/MollieTransactionEventSubscriber.php @@ -9,11 +9,10 @@ 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; /** - * Class MollieTransactionEventSubscriber. + * Provides an event subscriber to react on a Mollie payment status change. */ class MollieTransactionEventSubscriber implements EventSubscriberInterface { @@ -99,7 +98,9 @@ class MollieTransactionEventSubscriber implements EventSubscriberInterface { */ public static function getSubscribedEvents(): array { return [ - MollieTransactionStatusChangeEvent::EVENT_NAME => 'updateOrderStatus', + // Our priority should be lower than 100 to be sure the payment status is + // updated before the order status. + MollieTransactionStatusChangeEvent::EVENT_NAME => ['updateOrderStatus', 50], ]; } diff --git a/modules/mollie_webform/src/Plugin/WebformHandler/MolliePaymentHandler.php b/modules/mollie_webform/src/Plugin/WebformHandler/MolliePaymentHandler.php index 8c5141a4cf4ab129feb734e5f950db0dbaeb98e7..3000eeba4e3123d86dda501c1557f4d0861f9541 100644 --- a/modules/mollie_webform/src/Plugin/WebformHandler/MolliePaymentHandler.php +++ b/modules/mollie_webform/src/Plugin/WebformHandler/MolliePaymentHandler.php @@ -5,6 +5,8 @@ namespace Drupal\mollie_webform\Plugin\WebformHandler; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\TrustedRedirectResponse; +use Drupal\Core\TypedData\Exception\MissingDataException; +use Drupal\mollie\Entity\Payment; use Drupal\webform\Plugin\WebformHandlerBase; use Drupal\webform\WebformSubmissionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -21,13 +23,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class MolliePaymentHandler extends WebformHandlerBase { - /** - * The entity type manager. - * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface - */ - protected $entityTypeManager; - /** * The Mollie webform helper. * @@ -39,10 +34,9 @@ class MolliePaymentHandler extends WebformHandlerBase { * {@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; + $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); + $instance->mollieWebformHelper = $container->get('mollie_webform.helper'); + return $instance; } /** @@ -150,25 +144,25 @@ class MolliePaymentHandler extends WebformHandlerBase { $description = !empty($submittedDescription) ? $submittedDescription : $description; } - $transaction = $this->entityTypeManager->getStorage('mollie_payment')->create( - [ - 'amount' => (float) $webform_submission->getElementData($this->getConfiguration()['settings']['amount_element']), - 'currency' => $this->getConfiguration()['settings']['currency'], - 'description' => $description, - 'context' => 'mollie_webform', - 'context_id' => $webform_submission->id(), - 'method' => $webform_submission->getElementData($this->getConfiguration()['settings']['method_element']) ?? [], - ] - ); try { // Create the Mollie payment. + $transaction = Payment::createFromPaymentData( + [ + 'amount' => number_format($webform_submission->getElementData($this->getConfiguration()['settings']['amount_element']), 2, '.', ''), + 'currency' => $this->getConfiguration()['settings']['currency'], + 'description' => $description, + 'context' => 'mollie_webform', + 'context_id' => $webform_submission->id(), + 'method' => $webform_submission->getElementData($this->getConfiguration()['settings']['method_element']) ?? [], + ] + ); $transaction->save(); // Redirect to Mollie. $response = new TrustedRedirectResponse($transaction->getCheckoutUrl(), '303'); $form_state->setResponse($response); } - catch (EntityStorageException $e) { + catch (MissingDataException | EntityStorageException $e) { watchdog_exception('mollie', $e); } }