Skip to content

Do not mark a canceled order as paid on Momo callbacks - #1155

Open
blurman-ai wants to merge 1 commit into
simplcommerce:masterfrom
blurman-ai:fix/momo-payment-after-auto-cancel
Open

blurman-ai wants to merge 1 commit into
simplcommerce:masterfrom
blurman-ai:fix/momo-payment-after-auto-cancel

Conversation

@blurman-ai

Copy link
Copy Markdown

What happens to a buyer

OrderCancellationBackgroundService cancels orders that stay in PendingPayment or PaymentFailed for more than 5 minutes and puts the items back into stock. With Momo the order is created before the buyer is redirected to the Momo page, so a buyer who pays after that window is charged for an order that is already canceled. momo/result then sets the order to PaymentReceived without checking its current status (momo/notify does check it, so the outcome depends on which callback arrives first). The shop sees a paid order whose stock may already have been sold to someone else.

The fix

  • IOrderService.IsAwaitingPayment(order): true for PendingPayment and PaymentFailed.
  • MomoPaymentController.UpdatePaymentStatus (used by result and notify) only changes the order status when the order is awaiting payment. A canceled order stays canceled; if Momo reports success, a Payment with status Succeeded and FailureMessage "Payment received after the order was canceled. Refund required." is saved once per transaction id, so an operator can see that a refund is needed.
  • momo/result tells the buyer that the payment arrived after the order was canceled and to contact the shop.
  • momo/notify also handles canceled orders, so the payment is recorded even if the buyer never returns to the result page.

Test

New test project SimplCommerce.Module.Orders.Tests with a theory over IsAwaitingPayment for the relevant statuses. It fails (4 of 6 cases) when the check returns true for every status and passes with this change. The controller itself is not covered: the repository has no controller tests.

Open question

There is no refund-needed state: Payment has only Succeeded/Failed. I used Succeeded plus a note in FailureMessage. Would you prefer a dedicated PaymentStatus or OrderStatus value (needs a migration)?

Not changed

NganLuong rejects such callbacks with "Invalid order state" and does not record the payment, so the operator cannot see that money was taken. The same check could be applied there in a follow-up.

Record the payment as succeeded with a refund-required note instead.
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant