MollieReturnController.php in Commerce Mollie 8
File
src/Controller/MollieReturnController.php
View source
<?php
namespace Drupal\commerce_mollie\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Mollie\Api\Types\PaymentStatus as MolliePaymentStatus;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class MollieReturnController extends ControllerBase {
public function returnFromMollieMiddleware(Request $request, RouteMatchInterface $route_match) {
$order = $route_match
->getParameter('commerce_order');
$payment_storage = \Drupal::entityTypeManager()
->getStorage('commerce_payment');
$order_payments = $payment_storage
->loadMultipleByOrder($order);
$last_payment = end($order_payments);
$messenger = \Drupal::messenger();
if ($last_payment
->getRemoteState() === MolliePaymentStatus::STATUS_FAILED) {
$messenger
->addWarning($this
->t('Your payment at Mollie has failed. You may resume the checkout process here when you are ready.'));
}
if ($last_payment
->getRemoteState() === MolliePaymentStatus::STATUS_EXPIRED) {
$messenger
->addWarning($this
->t('Your payment at Mollie has expired. You may resume the checkout process here when you are ready.'));
}
$cancel_route_states = [
MolliePaymentStatus::STATUS_CANCELED,
MolliePaymentStatus::STATUS_FAILED,
MolliePaymentStatus::STATUS_EXPIRED,
];
if (in_array($last_payment
->getRemoteState(), $cancel_route_states, TRUE)) {
$cancel_url = Url::fromRoute('commerce_payment.checkout.cancel', [
'commerce_order' => $order
->id(),
'step' => 'payment',
], [
'absolute' => TRUE,
])
->toString();
return new RedirectResponse($cancel_url);
}
if ($order
->isPaid() || $last_payment
->getRemoteState() !== MolliePaymentStatus::STATUS_OPEN) {
$return_url = Url::fromRoute('commerce_payment.checkout.return', [
'commerce_order' => $order
->id(),
'step' => 'payment',
], [
'absolute' => TRUE,
])
->toString();
return new RedirectResponse($return_url);
}
\Drupal::service('page_cache_kill_switch')
->trigger();
$reload_link = Link::createFromRoute($this
->t('Please reload this page.'), 'commerce_mollie.checkout.mollie_return', [
'commerce_order' => $order
->id(),
])
->toString();
return [
'#theme' => 'mollie_return',
'#reload_link' => (string) $reload_link,
'#cache' => [
'max-age' => 0,
],
];
}
}