Cancel payments after errors during orders placing #144
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In some cases, orders may not be placed for various reasons.
Magento 2 relies on compensating actions rather than a database transaction to roll back payment operations. If an error occurs during order placement, the framework (and more specifically, the payment module) catches the exception and triggers the appropriate cancellation logic. This typically involves calling a void, cancel, or refund method on the payment gateway via the payment adapter, ensuring that payments already authorized or captured are reversed externally. This design allows external payment processes—which may be slow or have their own integrity constraints—to be managed independently from the database operations, aligning with Magento 2 best practices.
It seems that your module does not handle errors related to order placement and does not attempt to cancel payments in these cases.
I have prepared a PR for the case when the order placement process was interrupted by an error, but the order (as well as the payment, transaction, invoice) had not yet been saved in the database. The plugin attempts to perform a
refundin this case (we use theauthorize and captureoption for payments). The scenario with payments under theauthorizescheme needs to be handled differently, usingvoidtransactions. This still needs to be implemented since, in its current form, the transaction status is not stored anywhere, and it is necessary either to store the transaction status in additional payment model information or to retrieve the transaction data in thecanVoidandcanRefundmagento methods so that they take into account the current transaction status.