Checkout Api returning nothing
Currently the checkout apis (address, shipment, ..) are implemented return 204 when putting things. I would prefer being consistence like in the cart api's where the api returning the whole cart again the checkout api's should do the same and return the whole checkout object.
yes, it would be better if it return whole cart,
204 response was kept knowingly??
I can create PR if there is no special purpose for returning 204 response
It definitely makes sense if the cart is modified in more than one way (for example shipping costs change the total) and so on. But this will be a BC break if you change the status code for these endpoints.
Here is temporary solution for anyone looking for whole cart response
<?php
namespace App\EventSubscriber;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\ShopApiPlugin\ViewRepository\Cart\CartViewRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class BeforeActionSubscriber implements EventSubscriberInterface
{
/** @var ViewHandlerInterface */
private $viewHandler;
/** @var CartViewRepositoryInterface */
private $cartQuery;
public function __construct(
ViewHandlerInterface $viewHandler,
CartViewRepositoryInterface $cartQuery
) {
$this->viewHandler = $viewHandler;
$this->cartQuery = $cartQuery;
}
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => ['handleEmptyResponse', 100],
);
}
public function handleEmptyResponse(ResponseEvent $event){
$request = $event->getRequest();
$response = $event->getResponse();
$route = $request->get('_route');
if(
$response->getStatusCode() == Response::HTTP_NO_CONTENT && $request->get('token')
&& $request->isMethod('PUT')
&& (stripos($route , 'checkout' ) !== false or
stripos( $route, 'shipping_method' ) !== false)
&& stripos( $route, 'complete' ) === false
){
try {
$event->setResponse($this->viewHandler->handle(View::create(
$this->cartQuery->getOneByToken($request->get('token')),
Response::HTTP_OK
)));
} catch (\InvalidArgumentException $exception) {
throw new BadRequestHttpException($exception->getMessage());
}
}
}
}
<service id="App\EventSubscriber\BeforeActionSubscriber">
<argument id="fos_rest.view_handler" type="service"/>
<argument id="sylius.shop_api_plugin.view_repository.cart_view_repository" type="service"/>
<tag name="kernel.event_subscriber"/>
</service>