ShopApiPlugin icon indicating copy to clipboard operation
ShopApiPlugin copied to clipboard

Checkout Api returning nothing

Open alexander-schranz opened this issue 6 years ago • 3 comments

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.

alexander-schranz avatar Aug 21 '19 09:08 alexander-schranz

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

gomcodoctor avatar Dec 27 '19 16:12 gomcodoctor

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.

mamazu avatar Dec 28 '19 12:12 mamazu

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>

gomcodoctor avatar Dec 28 '19 12:12 gomcodoctor