amzn / amazon-payments-magento-2-plugin

Extension to enable Amazon Pay on Magento 2
https://amzn.github.io/amazon-payments-magento-2-plugin/
Apache License 2.0
109 stars 77 forks source link

Use of Magento\Sales\Model\Service\OrderService for cancel Order so that SalesRule Plugin works #1174

Closed stkrelax closed 1 year ago

stkrelax commented 1 year ago

SalesRule di

    <type name="Magento\Sales\Model\Service\OrderService">
        <plugin name="coupon_uses_decrement_plugin" type="Magento\SalesRule\Plugin\CouponUsagesDecrement" />
    </type>

What I expected

What happened instead

mentioned Plugin has no effect because $order->cancel() is called https://github.com/amzn/amazon-payments-magento-2-plugin/blob/0e9aa6e3fafb5517c3bdb6ca10dcd3e25de4474f/Model/CheckoutSessionManagement.php#L624

Steps to reproduce the issue

Your setup

sgabhart22 commented 1 year ago

Hello @stkrelax ,

This is an interesting find that, somehow, has not been brought to our attention until now. I can reproduce this pretty easily on a local project. A simple fix would seem to be replacing (or adding to) some of our manipulations on the $order object in the cancelOrder function with a call to $order->cancel(), but this seems to have some other implications with wrapping up the transaction processing.

A somewhat simple workaround I found is to make the single call to execute() on a \Magento\SalesRule\Model\Coupon\UpdateCouponUsages object in our cancelOrder function before saving the order, since this is really all the core plugin does . This object is added to the list of injected dependencies of the CheckoutSessionManagement class and requires recompiling, but it's relatively non-invasive. I'm attaching a patch here that makes those changes if you'd like to use it in the meantime before we cut an upcoming release.

gh1174.patch.txt

There very well may be a better way to handle this, and we will discuss this internally, but it is a temporary solution that shouldn't have any side effects. Just for confirmation, you should only run into this when the order is automatically cancelled due to some failure with completeCheckoutSession, and not when manually cancelling from the admin area; is this correct?

Thanks, Spencer

sgabhart22 commented 1 year ago

Hello again @stkrelax ,

The fix for this problem has been included in version 5.14.2 of the module, which is now available on the Marketplace. We'll go ahead and close this issue.

Thanks, Spencer