magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.5k stars 9.31k forks source link

Sales Rule Coupons usage is incremented asynchronously but decremented Synchronously #34627

Closed tommi-martin closed 2 years ago

tommi-martin commented 2 years ago

In Magento 2.4.3 a commit introduced asynchronous incrementation to the sales rule coupon usage system in task MC-29335. However this implementation did not modify the decrement logic, which can lead to database error when coupon usage is being decremented during order cancellation. I would expect that this error is quite rare on actual production servers, however it makes testing and developing payment methods a headache.

Preconditions (*)

  1. Magento 2.4.3 or higher
  2. Message queue consumers are inactive or delayed.
  3. Active Cart sales rule that gives free shipping for purchases over 50$

Steps to reproduce (*)

  1. Add products to cart with combined value exceeding 50$
  2. Proceed to checkout
  3. Fill checkout details and submit order
  4. Cancel order using \Magento\Sales\Api\OrderManagementInterface::cancel before message queue has been processed. This can be achieved from admin order cancel controller

Expected result (*)

  1. Order is cancelled

Actual result (*)

  1. Cancelling order fails to the following exception.
    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento-clean`.`salesrule_customer`, CONSTRAINT `SALESRULE_CUSTOMER_RULE_ID_SEQUENCE_SALESRULE_SEQUENCE_VALUE` FOREIGN KEY (`rule_id`) REFERENCES `sequence_salesrule` (`sequence_value`) ON DE), query was: INSERT INTO `salesrule_customer` () VALUES ()

    Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 2 years ago

Hi @tommi-martin. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] commented 2 years ago

Hi @engcom-Delta. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Delta commented 2 years ago

Hi @tommi-martin, Tried to reproduce it on Magento 2.4 Develop latest branch , order has cancelled successfully, while cancelling getting error "You have not cancelled the item" Please find the attached screenshots & video for reference. OrderCanceled

https://user-images.githubusercontent.com/51681379/142020176-cd00bf2f-f784-4f22-9fa4-9198e4c539fa.mp4

https://user-images.githubusercontent.com/51681379/142020226-058068f7-8974-4b90-a16b-2f7a71de3267.mp4

tommi-martin commented 2 years ago

Hello @engcom-Delta Thank you for your attention towards this matter. In the recording did you perhaps check the exception that caused the error message to be printed? It originates from the cancel controller's try catch logic and if I gave you my instructions right, the actual exception should be the mysql query error in the original description, while the message shown to user is inaccurate.

\Magento\Sales\Controller\Adminhtml\Order\Cancel::execute

try {
    $this->orderManagement->cancel($order->getEntityId());
    $this->messageManager->addSuccessMessage(__('You canceled the order.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
    $this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
    $this->messageManager->addErrorMessage(__('You have not canceled the item.'));
    $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
}

I suspect the exception behind that particular error message is the same as I had in the development of my payment module on the frontend side of things. Which I will cover up with a similar try catch, however shouldn't the original issue be addressed?

m2-assistant[bot] commented 2 years ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Hotel commented 2 years ago

Hello @tommi-martin,

We have tried to reproduce the issue in the Magento 2.4-develop branch and we are unable to reproduce the issue. It may be possible the issue has been fixed in this branch. We are able to cancel the order without any issue. For us the message queue is inactive.

Please have a look at the below screenshot for reference: image

We request you to try to reproduce the issue in the Magento 2.4-develop branch and let us know if it is still reproducible for you.

Thanks

braders commented 2 years ago

I manage a high-traffic Magento site using the Cybersource payment gateway, and am seeing this issue frequently in relation to 3DSecure. If the 3DSecure authentication fails, cybersource is throwing an error which causes the sales coupon to be cancelled.

In addition to the SQL error, we are having significant issues with customers re-trying the payment and finding their coupon has reportedly been used (becuase the coupon was decremented, but the increment was still in queue). Our customer services team are having to manually refund several missed coupons per day due to the customer confusion this is causing.

Any progress here would be much appreciated.

engcom-Hotel commented 2 years ago

Hello @tommi-martin @braders,

Have you tried to reproduce the issue in Magento 2.4-develop branch? It seems that the is already fixed in this branch.

Thanks

engcom-Hotel commented 2 years ago

Dear @tommi-martin,

We have noticed that this issue has not been updated for a period of 14 Days. Hence we assume that this issue is fixed now, so we are closing it. Please raise a fresh ticket or reopen this ticket if you need more assistance on this.

Regards