custom/plugins/QfEasyCompliance/src/Controller/CheckoutController.php line 68

Open in your IDE?
  1. <?php
  2. namespace Qf\EasyCompliance\Controller;
  3. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  4. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLogoutRoute;
  5. use Shopware\Core\Checkout\Order\SalesChannel\OrderService;
  6. use Shopware\Core\Checkout\Payment\PaymentService;
  7. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  8. use Shopware\Core\Framework\Routing\Annotation\Since;
  9. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Controller\CheckoutController as BaseController;
  14. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoader;
  15. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoader;
  16. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoader;
  17. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoader;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Shopware\Core\System\StateMachine\Transition;
  24. /**
  25.  * @RouteScope(scopes={"storefront"})
  26.  */
  27. class CheckoutController extends BaseController
  28. {
  29.     /**
  30.      * @var StateMachineRegistry
  31.      */
  32.     protected $stateMachineRegistry;
  33.     
  34.     /**
  35.      * @var OrderService
  36.      */
  37.     protected $orderService;
  38.     
  39.     /**
  40.      * @var SystemConfigService
  41.      */
  42.     protected $config;
  43.     
  44.     public function __construct(
  45.         StateMachineRegistry $stateMachineRegistry,
  46.         CartService $cartService,
  47.         CheckoutCartPageLoader $cartPageLoader,
  48.         CheckoutConfirmPageLoader $confirmPageLoader,
  49.         CheckoutFinishPageLoader $finishPageLoader,
  50.         OrderService $orderService,
  51.         PaymentService $paymentService,
  52.         OffcanvasCartPageLoader $offcanvasCartPageLoader,
  53.         SystemConfigService $config,
  54.         AbstractLogoutRoute $logoutRoute
  55.     ) {
  56.         $this->stateMachineRegistry $stateMachineRegistry;
  57.         $this->cartService $cartService;
  58.         $this->cartPageLoader $cartPageLoader;
  59.         $this->confirmPageLoader $confirmPageLoader;
  60.         $this->finishPageLoader $finishPageLoader;
  61.         $this->orderService $orderService;
  62.         $this->paymentService $paymentService;
  63.         $this->offcanvasCartPageLoader $offcanvasCartPageLoader;
  64.         $this->config $config;
  65.         $this->logoutRoute $logoutRoute;
  66.         
  67.         parent::__construct($cartService$cartPageLoader$confirmPageLoader$finishPageLoader$orderService$paymentService$offcanvasCartPageLoader$config$logoutRoute);
  68.     }
  69.     
  70.     /**
  71.      * @Since("6.0.0.0")
  72.      * @Route("/checkout/order", name="frontend.checkout.finish.order", options={"seo"="false"}, methods={"POST"})
  73.      */
  74.     public function order(RequestDataBag $dataSalesChannelContext $contextRequest $request): Response {
  75.         $customer $context->getCustomer();
  76.         
  77.         if(!$customer) {
  78.             return $this->redirectToRoute('frontend.checkout.register.page');
  79.         }
  80.         
  81.         $name $customer->getFirstName() . ' CheckoutController.php' $customer->getLastName();
  82.         
  83.         if($this->checkName($name)) {
  84.             // Easy Compliance API returned no hits. So the customer is okay.
  85.             return parent::order($data$context$request);
  86.         }
  87.         
  88.         try {
  89.             // $this->addAffiliateTracking($data, $request->getSession());
  90.             $orderId $this->orderService->createOrder($data$context);
  91.         } catch (ConstraintViolationException $formViolations) {
  92.             return $this->forwardToRoute('frontend.checkout.confirm.page', ['formViolations' => $formViolations]);
  93.         } catch (InvalidCartException Error EmptyCartException $error) {
  94.             $this->addCartErrors(
  95.                 $this->cartService->getCart($context->getToken(), $context)
  96.             );
  97.             return $this->forwardToRoute('frontend.checkout.confirm.page');
  98.         }
  99.         
  100.         $this->stateMachineRegistry->transition(new Transition(
  101.             \Shopware\Core\Checkout\Order\OrderDefinition::ENTITY_NAME,
  102.             $orderId,
  103.             'cancel',
  104.             'stateId'
  105.         ), $context->getContext());
  106.         
  107.         $this->addFlash(self::DANGER$this->trans('easycompliance.error'));
  108.         
  109.         return $this->forwardToRoute('frontend.account.home.page');
  110.     }
  111.     
  112.     private function checkName(string $name): bool {
  113.         //POST-Parameter definieren
  114.         $query_vals = array(
  115.             'api_key' => $this->config->get('QfEasyCompliance.config.apiKey'),
  116.             'method' => '1',
  117.             'accuracy' => '70',
  118.             'name' => $name
  119.         );
  120.         
  121.         $request '';
  122.         //POST-String Generieren
  123.         foreach($query_vals as $key => $value) {
  124.             $request .= $key.'='.urlencode($value).'&';
  125.         }
  126.         $request rtrim($request'&');
  127.         
  128.         //cURL mit API-URL initialisieren und Anfrage senden
  129.         $ch curl_init($this->config->get('QfEasyCompliance.config.apiUrl')); 
  130.         curl_setopt($chCURLOPT_HEADER0);
  131.         curl_setopt($chCURLOPT_RETURNTRANSFER1);
  132.         curl_setopt($chCURLOPT_CONNECTTIMEOUT4);
  133.         curl_setopt($chCURLOPT_TIMEOUT8);
  134.         curl_setopt($chCURLOPT_POSTFIELDS$request);
  135.         $response curl_exec($ch);
  136.         curl_close($ch);
  137.         
  138.         //Ergebnis ausgeben
  139.         return empty($response);
  140.     }
  141. }
  142. ?>