Setono / SyliusGiftCardPlugin

Sell gift cards on your Sylius store and let users use them in the checkout process
MIT License
44 stars 40 forks source link

[Bug]: Every item added to cart via API is treated as a gift card #261

Open TalkMarc opened 10 months ago

TalkMarc commented 10 months ago

Plugin version

0.12.0-alpha.5

Sylius version

1.12.10

What happened?

When a product is added to a cart via API (POST /api/v2/shop/orders/[TOKEN]/items) a new gift card with the value of that product is created, even if the product added is not a gift card.

This also creates follow up issues when an item already existing in a cart is added again. (A new entity was found through the relationship...)

Details

I see that the api platform data persister dispatches a message event with the Message of type Setono\SyliusGiftCardPlugin\Api\Command\AddItemToCart. This message then gets picked up by the plugins AddItemToCartHandler. This handler does not check if the item/product added is a gift card. It only checks if the passed message is of type Setono\SyliusGiftCardPlugin\Api\Command\AddItemToCart, which it always will be, since that is the "input" that is has to be changed in the Order api resource configuration during plugin installation.

API resource config deviation

I find it important to add that, in the installation instructions for the API, the "shop_add_item" operation of the Order has the method "PATCH", while in the Sylius api bundle it's actually configured with method "POST".

Relevant log output

STACK TRACE:
GiftCard.php:57, Setono\SyliusGiftCardPlugin\Model\GiftCard->__construct()
Factory.php:38, Sylius\Component\Resource\Factory\Factory->createNew()
GiftCardFactory.php:50, Setono\SyliusGiftCardPlugin\Factory\GiftCardFactory->createNew()
GiftCardFactory.php:58, Setono\SyliusGiftCardPlugin\Factory\GiftCardFactory->createForChannel()
GiftCardFactory.php:110, Setono\SyliusGiftCardPlugin\Factory\GiftCardFactory->createFromOrderItemUnitAndCart()
AddItemToCartHandler.php:91, Setono\SyliusGiftCardPlugin\Api\CommandHandler\AddItemToCartHandler->__invoke()
HandleMessageMiddleware.php:157, Symfony\Component\Messenger\Middleware\HandleMessageMiddleware->callHandler()
HandleMessageMiddleware.php:96, Symfony\Component\Messenger\Middleware\HandleMessageMiddleware->handle()
SendMessageMiddleware.php:77, Symfony\Component\Messenger\Middleware\SendMessageMiddleware->handle()
DoctrineTransactionMiddleware.php:31, Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware->handleForManager()
AbstractDoctrineMiddleware.php:45, Symfony\Bridge\Doctrine\Messenger\AbstractDoctrineMiddleware->handle()
ValidationMiddleware.php:45, Symfony\Component\Messenger\Middleware\ValidationMiddleware->handle()
FailedMessageProcessingMiddleware.php:34, Symfony\Component\Messenger\Middleware\FailedMessageProcessingMiddleware->handle()
DispatchAfterCurrentBusMiddleware.php:68, Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware->handle()
RejectRedeliveredMessageMiddleware.php:41, Symfony\Component\Messenger\Middleware\RejectRedeliveredMessageMiddleware->handle()
AddBusNameStampMiddleware.php:37, Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware->handle()
TraceableMiddleware.php:40, Symfony\Component\Messenger\Middleware\TraceableMiddleware->handle()
MessageBus.php:70, Symfony\Component\Messenger\MessageBus->dispatch()
TraceableMessageBus.php:38, Symfony\Component\Messenger\TraceableMessageBus->dispatch()
DispatchTrait.php:44, ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister->dispatch()
DataPersister.php:99, ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister->persist()
MessengerDataPersister.php:35, Sylius\Bundle\ApiBundle\DataPersister\MessengerDataPersister->persist()
ChainDataPersister.php:59, ApiPlatform\Core\DataPersister\ChainDataPersister->persist()
TraceableChainDataPersister.php:58, ApiPlatform\Core\Bridge\Symfony\Bundle\DataPersister\TraceableChainDataPersister->persist()
WriteListener.php:96, ApiPlatform\Core\EventListener\WriteListener->onKernelView()
WrappedListener.php:116, Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke()
EventDispatcher.php:220, Symfony\Component\EventDispatcher\EventDispatcher->callListeners()
EventDispatcher.php:56, Symfony\Component\EventDispatcher\EventDispatcher->dispatch()
TraceableEventDispatcher.php:139, Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch()
HttpKernel.php:187, Symfony\Component\HttpKernel\HttpKernel->handleRaw()
HttpKernel.php:76, Symfony\Component\HttpKernel\HttpKernel->handle()
Kernel.php:197, Symfony\Component\HttpKernel\Kernel->handle()
HttpKernelRunner.php:35, Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
autoload_runtime.php:29, require_once()
index.php:5, {main}()
loevgaard commented 5 months ago

I will be working on these issues in the upcoming weeks. The first PR I did today where I dropped support for PHP 7.4 and PHP 8.0 to make it easier for me.

TalkMarc commented 5 months ago

Thank you for the update. You can also check out my simple approach to fix this issue on my fork https://github.com/TalkMarc/SyliusGiftCardPlugin/commit/bfa384b303b48e117060828cec04e6e6f2dab038