Skip to content

Cancel order and restore quote methods increase stocks twice #9969

Closed
@simpleadm

Description

@simpleadm

Some payment methods in cancel payment action try to cancel current order and restore quote and as the result product qty is increased by 2.

Affected payment methods

  1. Paypal Hosted Pro (CE, EE)
  2. Paypal Payflow (CE, EE)
  3. Worldpay (EE)
  4. Eway (EE)
  5. PayEx (Third party payment gateway)

Preconditions

  1. Magento 2.1.* CE and EE
  2. Configured payment methods

Steps to reproduce

  1. Suppose you have product with stock quantity: 1
  2. Add product to cart and Checkout
  3. Select affected payment method
  4. The order is placed and stock is reduced to 0
  5. Click cancel on Payment gateway (e.g. press "Cancel and return to store" button on PayPal page)

Expected result

  1. Order was canceled
  2. Product stock quantity: 1

Actual result

  1. Order was canceled
  2. Product stock quantity: 2 (increase to 2)

Additional information

For example Paypal Hosted Pro Cancel action
\Magento\Paypal\Controller\Hostedpro\Cancel

   /**
     * Customer canceled payment on gateway side.
     *
     * @return void
     */
    public function execute()
    {
        $this->checkoutHelper->cancelCurrentOrder('');
        $this->checkoutHelper->restoreQuote();

        $this->_redirect('checkout', ['_fragment' => 'payment']);
    }

Method cancelCurrentOrder will execute $order->registerCancellation($comment)->save(); that will cancel all order items and dispatch 'sales_order_item_cancel' event for each one. \Magento\CatalogInventory\Observer\CancelOrderItemObserver observer listens this event and increases product qty.

Next \Magento\Checkout\Model\Session::restoreQuote() will be executed to restore customer quote. That method will update and save a quote and dispatch restore_quote event.
\Magento\CatalogInventory\Observer\RevertQuoteInventoryObserver listens to this event and product qty is increased the second time.

The problem is that both statements increase quantity of product.

The same issue in Payflow cancel action \Magento\Paypal\Controller\Payflow:

     /**
     * Cancel order, return quote to customer
     *
     * @param string $errorMsg
     * @return false|string
     */
    protected function _cancelPayment($errorMsg = '')
    {
        $errorMsg = trim(strip_tags($errorMsg));

        $gotoSection = false;
        $this->_checkoutHelper->cancelCurrentOrder($errorMsg);
        if ($this->_checkoutSession->restoreQuote()) {
            //Redirect to payment step
            $gotoSection = 'paymentMethod';
        }

        return $gotoSection;
    }

Metadata

Metadata

Labels

Component: PaymentFixed in 2.1.xThe issue has been fixed in 2.1 release lineFixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releasebug report

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions