<?php
namespace Qf\EasyCompliance\Controller;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLogoutRoute;
use Shopware\Core\Checkout\Order\SalesChannel\OrderService;
use Shopware\Core\Checkout\Payment\PaymentService;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Annotation\Since;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\StateMachine\StateMachineRegistry;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Controller\CheckoutController as BaseController;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoader;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoader;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoader;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoader;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Core\System\StateMachine\Transition;
/**
* @RouteScope(scopes={"storefront"})
*/
class CheckoutController extends BaseController
{
/**
* @var StateMachineRegistry
*/
protected $stateMachineRegistry;
/**
* @var OrderService
*/
protected $orderService;
/**
* @var SystemConfigService
*/
protected $config;
public function __construct(
StateMachineRegistry $stateMachineRegistry,
CartService $cartService,
CheckoutCartPageLoader $cartPageLoader,
CheckoutConfirmPageLoader $confirmPageLoader,
CheckoutFinishPageLoader $finishPageLoader,
OrderService $orderService,
PaymentService $paymentService,
OffcanvasCartPageLoader $offcanvasCartPageLoader,
SystemConfigService $config,
AbstractLogoutRoute $logoutRoute
) {
$this->stateMachineRegistry = $stateMachineRegistry;
$this->cartService = $cartService;
$this->cartPageLoader = $cartPageLoader;
$this->confirmPageLoader = $confirmPageLoader;
$this->finishPageLoader = $finishPageLoader;
$this->orderService = $orderService;
$this->paymentService = $paymentService;
$this->offcanvasCartPageLoader = $offcanvasCartPageLoader;
$this->config = $config;
$this->logoutRoute = $logoutRoute;
parent::__construct($cartService, $cartPageLoader, $confirmPageLoader, $finishPageLoader, $orderService, $paymentService, $offcanvasCartPageLoader, $config, $logoutRoute);
}
/**
* @Since("6.0.0.0")
* @Route("/checkout/order", name="frontend.checkout.finish.order", options={"seo"="false"}, methods={"POST"})
*/
public function order(RequestDataBag $data, SalesChannelContext $context, Request $request): Response {
$customer = $context->getCustomer();
if(!$customer) {
return $this->redirectToRoute('frontend.checkout.register.page');
}
$name = $customer->getFirstName() . ' CheckoutController.php' . $customer->getLastName();
if($this->checkName($name)) {
// Easy Compliance API returned no hits. So the customer is okay.
return parent::order($data, $context, $request);
}
try {
// $this->addAffiliateTracking($data, $request->getSession());
$orderId = $this->orderService->createOrder($data, $context);
} catch (ConstraintViolationException $formViolations) {
return $this->forwardToRoute('frontend.checkout.confirm.page', ['formViolations' => $formViolations]);
} catch (InvalidCartException | Error | EmptyCartException $error) {
$this->addCartErrors(
$this->cartService->getCart($context->getToken(), $context)
);
return $this->forwardToRoute('frontend.checkout.confirm.page');
}
$this->stateMachineRegistry->transition(new Transition(
\Shopware\Core\Checkout\Order\OrderDefinition::ENTITY_NAME,
$orderId,
'cancel',
'stateId'
), $context->getContext());
$this->addFlash(self::DANGER, $this->trans('easycompliance.error'));
return $this->forwardToRoute('frontend.account.home.page');
}
private function checkName(string $name): bool {
//POST-Parameter definieren
$query_vals = array(
'api_key' => $this->config->get('QfEasyCompliance.config.apiKey'),
'method' => '1',
'accuracy' => '70',
'name' => $name
);
$request = '';
//POST-String Generieren
foreach($query_vals as $key => $value) {
$request .= $key.'='.urlencode($value).'&';
}
$request = rtrim($request, '&');
//cURL mit API-URL initialisieren und Anfrage senden
$ch = curl_init($this->config->get('QfEasyCompliance.config.apiUrl'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
curl_close($ch);
//Ergebnis ausgeben
return empty($response);
}
}
?>