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

#3274375 - Updated Mollie for Drupal Webform to work with the new architecture

parent 4d6e9a9e
Branches
No related tags found
No related merge requests found
......@@ -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],
];
}
......
......@@ -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);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment