PaymentCheckoutController.php in Commerce Core 8.2
File
modules/payment/src/Controller/PaymentCheckoutController.php
View source
<?php
namespace Drupal\commerce_payment\Controller;
use Drupal\commerce\Response\NeedsRedirectException;
use Drupal\commerce_checkout\CheckoutOrderManagerInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\Exception\PaymentGatewayException;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface;
use Drupal\Core\Access\AccessException;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class PaymentCheckoutController implements ContainerInjectionInterface {
protected $checkoutOrderManager;
protected $messenger;
protected $logger;
public function __construct(CheckoutOrderManagerInterface $checkout_order_manager, MessengerInterface $messenger, LoggerInterface $logger) {
$this->checkoutOrderManager = $checkout_order_manager;
$this->messenger = $messenger;
$this->logger = $logger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('commerce_checkout.checkout_order_manager'), $container
->get('messenger'), $container
->get('logger.channel.commerce_payment'));
}
public function returnPage(Request $request, RouteMatchInterface $route_match) {
$order = $route_match
->getParameter('commerce_order');
$step_id = $route_match
->getParameter('step');
$this
->validateStepId($step_id, $order);
$payment_gateway = $order
->get('payment_gateway')->entity;
$payment_gateway_plugin = $payment_gateway
->getPlugin();
if (!$payment_gateway_plugin instanceof OffsitePaymentGatewayInterface) {
throw new AccessException('The payment gateway for the order does not implement ' . OffsitePaymentGatewayInterface::class);
}
$checkout_flow = $order
->get('checkout_flow')->entity;
$checkout_flow_plugin = $checkout_flow
->getPlugin();
try {
$payment_gateway_plugin
->onReturn($order, $request);
$redirect_step_id = $checkout_flow_plugin
->getNextStepId($step_id);
} catch (PaymentGatewayException $e) {
$this->logger
->error($e
->getMessage());
$this->messenger
->addError(t('Payment failed at the payment server. Please review your information and try again.'));
$redirect_step_id = $checkout_flow_plugin
->getPreviousStepId($step_id);
}
$checkout_flow_plugin
->redirectToStep($redirect_step_id);
}
public function cancelPage(Request $request, RouteMatchInterface $route_match) {
$order = $route_match
->getParameter('commerce_order');
$step_id = $route_match
->getParameter('step');
$this
->validateStepId($step_id, $order);
$payment_gateway = $order
->get('payment_gateway')->entity;
$payment_gateway_plugin = $payment_gateway
->getPlugin();
if (!$payment_gateway_plugin instanceof OffsitePaymentGatewayInterface) {
throw new AccessException('The payment gateway for the order does not implement ' . OffsitePaymentGatewayInterface::class);
}
$checkout_flow = $order
->get('checkout_flow')->entity;
$checkout_flow_plugin = $checkout_flow
->getPlugin();
$payment_gateway_plugin
->onCancel($order, $request);
$previous_step_id = $checkout_flow_plugin
->getPreviousStepId($step_id);
$checkout_flow_plugin
->redirectToStep($previous_step_id);
}
protected function validateStepId($requested_step_id, OrderInterface $order) {
$step_id = $this->checkoutOrderManager
->getCheckoutStepId($order);
if ($requested_step_id != $step_id) {
throw new NeedsRedirectException(Url::fromRoute('commerce_checkout.form', [
'commerce_order' => $order
->id(),
'step' => $step_id,
])
->toString());
}
}
}