forked from Sylius/Sylius
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Sylius#7426 from lchrusciel/shipping-method-checko…
…ut-api [API] [Checkout] Get available shipping and payment methods
- Loading branch information
Showing
20 changed files
with
756 additions
and
120 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
src/Sylius/Bundle/ApiBundle/Controller/ShowAvailablePaymentMethodsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\ApiBundle\Controller; | ||
|
||
use FOS\RestBundle\View\View; | ||
use FOS\RestBundle\View\ViewHandlerInterface; | ||
use SM\Factory\FactoryInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Sylius\Component\Core\OrderCheckoutTransitions; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
/** | ||
* @author Łukasz Chruściel <lukasz.chrusciel@lakion.com> | ||
*/ | ||
final class ShowAvailablePaymentMethodsController | ||
{ | ||
/** | ||
* @var FactoryInterface | ||
*/ | ||
private $stateMachineFactory; | ||
|
||
/** | ||
* @var OrderRepositoryInterface | ||
*/ | ||
private $orderRepository; | ||
|
||
/** | ||
* @var PaymentMethodsResolverInterface | ||
*/ | ||
private $paymentMethodResolver; | ||
|
||
/** | ||
* @var ViewHandlerInterface | ||
*/ | ||
private $restViewHandler; | ||
|
||
/** | ||
* @param FactoryInterface $stateMachineFactory | ||
* @param OrderRepositoryInterface $orderRepository | ||
* @param PaymentMethodsResolverInterface $paymentMethodResolver | ||
* @param ViewHandlerInterface $restViewHandler | ||
*/ | ||
public function __construct( | ||
FactoryInterface $stateMachineFactory, | ||
OrderRepositoryInterface $orderRepository, | ||
PaymentMethodsResolverInterface $paymentMethodResolver, | ||
ViewHandlerInterface $restViewHandler | ||
) { | ||
$this->stateMachineFactory = $stateMachineFactory; | ||
$this->orderRepository = $orderRepository; | ||
$this->paymentMethodResolver = $paymentMethodResolver; | ||
$this->restViewHandler = $restViewHandler; | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* | ||
* @return Response | ||
*/ | ||
public function showAction(Request $request) | ||
{ | ||
/** @var OrderInterface $cart */ | ||
$cart = $this->getCartOr404($request->attributes->get('orderId')); | ||
|
||
if (!$this->isCheckoutTransitionPossible($cart, OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)) { | ||
throw new BadRequestHttpException('The payment methods cannot be resolved in the current state of cart!'); | ||
} | ||
|
||
$payments = []; | ||
|
||
foreach ($cart->getPayments() as $payment) { | ||
$payments['payments'][] = [ | ||
'methods' => $this->getPaymentMethods($payment, $cart->getLocaleCode()), | ||
]; | ||
} | ||
|
||
return $this->restViewHandler->handle(View::create($payments)); | ||
} | ||
|
||
/** | ||
* @param mixed $cartId | ||
* | ||
* @return OrderInterface | ||
*/ | ||
private function getCartOr404($cartId) | ||
{ | ||
$cart = $this->orderRepository->findCartById($cartId); | ||
|
||
if (null === $cart) { | ||
throw new NotFoundHttpException(sprintf("The cart with %s id could not be found!", $cartId)); | ||
} | ||
|
||
return $cart; | ||
} | ||
|
||
/** | ||
* @param OrderInterface $cart | ||
* @param string $transition | ||
* | ||
* @return bool | ||
*/ | ||
private function isCheckoutTransitionPossible(OrderInterface $cart, $transition) | ||
{ | ||
return $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH)->can($transition); | ||
} | ||
|
||
/** | ||
* @param PaymentInterface $payment | ||
* @param string $locale | ||
* | ||
* @return array | ||
*/ | ||
private function getPaymentMethods(PaymentInterface $payment, $locale) | ||
{ | ||
$paymentMethods = $this->paymentMethodResolver->getSupportedMethods($payment); | ||
|
||
$rawPaymentMethods = []; | ||
|
||
foreach ($paymentMethods as $paymentMethod) { | ||
$rawPaymentMethods[] = [ | ||
'id' => $paymentMethod->getId(), | ||
'code' => $paymentMethod->getCode(), | ||
'name' => $paymentMethod->getTranslation($locale)->getName(), | ||
'description' => $paymentMethod->getTranslation($locale)->getDescription(), | ||
]; | ||
} | ||
|
||
return $rawPaymentMethods; | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
src/Sylius/Bundle/ApiBundle/Controller/ShowAvailableShippingMethodsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\ApiBundle\Controller; | ||
|
||
use FOS\RestBundle\View\View; | ||
use FOS\RestBundle\View\ViewHandlerInterface; | ||
use SM\Factory\FactoryInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\ShipmentInterface; | ||
use Sylius\Component\Core\OrderCheckoutTransitions; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\Component\Registry\ServiceRegistryInterface; | ||
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
/** | ||
* @author Łukasz Chruściel <lukasz.chrusciel@lakion.com> | ||
*/ | ||
final class ShowAvailableShippingMethodsController | ||
{ | ||
/** | ||
* @var FactoryInterface | ||
*/ | ||
private $stateMachineFactory; | ||
|
||
/** | ||
* @var OrderRepositoryInterface | ||
*/ | ||
private $orderRepository; | ||
|
||
/** | ||
* @var ShippingMethodsResolverInterface | ||
*/ | ||
private $shippingMethodsResolver; | ||
|
||
/** | ||
* @var ViewHandlerInterface | ||
*/ | ||
private $restViewHandler; | ||
|
||
/** | ||
* @var ServiceRegistryInterface | ||
*/ | ||
private $calculators; | ||
|
||
/** | ||
* @param FactoryInterface $stateMachineFactory | ||
* @param OrderRepositoryInterface $orderRepository | ||
* @param ShippingMethodsResolverInterface $shippingMethodsResolver | ||
* @param ViewHandlerInterface $restViewHandler | ||
* @param ServiceRegistryInterface $calculators | ||
*/ | ||
public function __construct( | ||
FactoryInterface $stateMachineFactory, | ||
OrderRepositoryInterface $orderRepository, | ||
ShippingMethodsResolverInterface $shippingMethodsResolver, | ||
ViewHandlerInterface $restViewHandler, | ||
ServiceRegistryInterface $calculators | ||
) { | ||
$this->stateMachineFactory = $stateMachineFactory; | ||
$this->orderRepository = $orderRepository; | ||
$this->shippingMethodsResolver = $shippingMethodsResolver; | ||
$this->restViewHandler = $restViewHandler; | ||
$this->calculators = $calculators; | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* | ||
* @return Response | ||
*/ | ||
public function showAction(Request $request) | ||
{ | ||
/** @var OrderInterface $cart */ | ||
$cart = $this->getCartOr404($request->attributes->get('orderId')); | ||
|
||
if (!$this->isCheckoutTransitionPossible($cart, OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)) { | ||
throw new BadRequestHttpException('The shipment methods cannot be resolved in the current state of cart!'); | ||
} | ||
|
||
$shipments = []; | ||
|
||
foreach ($cart->getShipments() as $shipment) { | ||
$shipments['shipments'][] = [ | ||
'methods' => $this->getCalculatedShippingMethods($shipment, $cart->getLocaleCode()), | ||
]; | ||
} | ||
|
||
return $this->restViewHandler->handle(View::create($shipments)); | ||
} | ||
|
||
/** | ||
* @param mixed $cartId | ||
* | ||
* @return OrderInterface | ||
*/ | ||
private function getCartOr404($cartId) | ||
{ | ||
$cart = $this->orderRepository->findCartById($cartId); | ||
|
||
if (null === $cart) { | ||
throw new NotFoundHttpException(sprintf("The cart with %s id could not be found!", $cartId)); | ||
} | ||
|
||
return $cart; | ||
} | ||
|
||
/** | ||
* @param OrderInterface $cart | ||
* @param string $transition | ||
* | ||
* @return bool | ||
*/ | ||
private function isCheckoutTransitionPossible(OrderInterface $cart, $transition) | ||
{ | ||
return $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH)->can($transition); | ||
} | ||
|
||
/** | ||
* @param ShipmentInterface $shipment | ||
* @param string $locale | ||
* | ||
* @return array | ||
*/ | ||
private function getCalculatedShippingMethods(ShipmentInterface $shipment, $locale) | ||
{ | ||
$shippingMethods = $this->shippingMethodsResolver->getSupportedMethods($shipment); | ||
|
||
$rawShippingMethods = []; | ||
|
||
foreach ($shippingMethods as $shippingMethod) { | ||
$calculator = $this->calculators->get($shippingMethod->getCalculator()); | ||
|
||
$rawShippingMethods[] = [ | ||
'id' => $shippingMethod->getId(), | ||
'code' => $shippingMethod->getCode(), | ||
'name' => $shippingMethod->getTranslation($locale)->getName(), | ||
'description' => $shippingMethod->getTranslation($locale)->getDescription(), | ||
'price' => $calculator->calculate($shipment, $shippingMethod->getConfiguration()), | ||
]; | ||
} | ||
|
||
return $rawShippingMethods; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.