Skip to content

Commit

Permalink
PAYOSWXP-158: make methods more abstract (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy authored Dec 18, 2024
1 parent 569dd27 commit ce81b1f
Showing 1 changed file with 48 additions and 56 deletions.
104 changes: 48 additions & 56 deletions src/Storefront/Controller/GenericExpressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
use PayonePayment\Payone\Client\PayoneClientInterface;
use PayonePayment\Payone\RequestParameter\RequestParameterFactory;
use RuntimeException;
use Shopware\Core\Checkout\Cart\CartException;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Customer\SalesChannel\AbstractRegisterRoute;
use Shopware\Core\Checkout\Customer\SalesChannel\AccountService;
use Shopware\Core\Framework\Validation\DataBag\DataBag;
use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\System\SalesChannel\SalesChannel\SalesChannelContextSwitcher;
Expand Down Expand Up @@ -56,39 +58,14 @@ public function __construct(
)]
public function createSessionAction(SalesChannelContext $context, string $paymentMethodId): Response
{
if (!\array_key_exists($paymentMethodId, PaymentHandlerGroups::GENERIC_EXPRESS)) {
throw $this->createNotFoundException();
}

$cart = $this->cartService->getCart($context->getToken(), $context);

$salesChannelDataBag = new DataBag([
SalesChannelContextService::PAYMENT_METHOD_ID => $paymentMethodId,
]);

$this->salesChannelContextSwitcher->update($salesChannelDataBag, $context);

$setRequest = $this->requestParameterFactory->getRequestParameter(
new CreateExpressCheckoutSessionStruct(
$context,
PaymentHandlerGroups::GENERIC_EXPRESS[$paymentMethodId]
)
);

try {
$response = $this->client->request($setRequest);
} catch (PayoneRequestException) {
throw new RuntimeException($this->trans('PayonePayment.errorMessages.genericError'));
}
$response = $this->createSession($context, $paymentMethodId);

if (!isset($response['addpaydata']['orderId'])) {
if (!isset($response['orderId'])) {
throw new RuntimeException('generic express checkout: No orderId has been given for payment method id ' . $paymentMethodId);
}

$this->cartExtensionService->addCartExtensionForExpressCheckout($cart, $context, $paymentMethodId, $response['workorderid']);

return new JsonResponse([
'orderId' => $response['addpaydata']['orderId'],
'orderId' => $response['orderId'],
]);
}

Expand All @@ -100,43 +77,20 @@ public function createSessionAction(SalesChannelContext $context, string $paymen
)]
public function redirectAction(SalesChannelContext $context, string $paymentMethodId): Response
{
if (!\array_key_exists($paymentMethodId, PaymentHandlerGroups::GENERIC_EXPRESS)) {
throw $this->createNotFoundException();
}

$cart = $this->cartService->getCart($context->getToken(), $context);

try {
$salesChannelDataBag = new DataBag([
SalesChannelContextService::PAYMENT_METHOD_ID => $paymentMethodId,
]);

$this->salesChannelContextSwitcher->update($salesChannelDataBag, $context);
} catch (\Throwable $exception) {
$response = $this->createSession($context, $paymentMethodId);
} catch (ConstraintViolationException $constraintViolationException) {
return $this->forwardToRoute('frontend.checkout.confirm.page', [
'formViolations' => $exception,
'formViolations' => $constraintViolationException,
]);
}

$setRequest = $this->requestParameterFactory->getRequestParameter(
new CreateExpressCheckoutSessionStruct(
$context,
PaymentHandlerGroups::GENERIC_EXPRESS[$paymentMethodId]
)
);

try {
$response = $this->client->request($setRequest);
} catch (PayoneRequestException) {
throw new RuntimeException($this->trans('PayonePayment.errorMessages.genericError'));
} catch (CartException) {
return $this->forwardToRoute('frontend.checkout.confirm.page');
}

if (!\array_key_exists('redirecturl', $response)) {
throw new RuntimeException('generic express checkout: No redirect URL has been given for payment method id ' . $paymentMethodId);
}

$this->cartExtensionService->addCartExtensionForExpressCheckout($cart, $context, $paymentMethodId, $response['workorderid']);

return new RedirectResponse($response['redirecturl']);
}

Expand Down Expand Up @@ -206,4 +160,42 @@ public function returnAction(SalesChannelContext $context, string $paymentMethod

return $this->redirectToRoute('frontend.checkout.confirm.page');
}

/**
* @return array{orderId: string|null, redirectUrl: string|null}
*/
private function createSession(SalesChannelContext $context, string $paymentMethodId): array
{
if (!\array_key_exists($paymentMethodId, PaymentHandlerGroups::GENERIC_EXPRESS)) {
throw $this->createNotFoundException();
}

$cart = $this->cartService->getCart($context->getToken(), $context);

$salesChannelDataBag = new DataBag([
SalesChannelContextService::PAYMENT_METHOD_ID => $paymentMethodId,
]);

$this->salesChannelContextSwitcher->update($salesChannelDataBag, $context);

$setRequest = $this->requestParameterFactory->getRequestParameter(
new CreateExpressCheckoutSessionStruct(
$context,
PaymentHandlerGroups::GENERIC_EXPRESS[$paymentMethodId]
)
);

try {
$response = $this->client->request($setRequest);
} catch (PayoneRequestException) {
throw new RuntimeException($this->trans('PayonePayment.errorMessages.genericError'));
}

$this->cartExtensionService->addCartExtensionForExpressCheckout($cart, $context, $paymentMethodId, $response['workorderid']);

return [
'orderId' => $response['addpaydata']['orderId'] ?? null,
'redirectUrl' => $response['redirecturl'] ?? null,
];
}
}

0 comments on commit ce81b1f

Please sign in to comment.