Skip to content

fix: recover overlapping Stripe webhook order creation - #559

Open
bseverino wants to merge 2 commits into
mainfrom
EDUN-15347-UPI-idempotent-finalization-paid-cart-protection
Open

fix: recover overlapping Stripe webhook order creation#559
bseverino wants to merge 2 commits into
mainfrom
EDUN-15347-UPI-idempotent-finalization-paid-cart-protection

Conversation

@bseverino

Copy link
Copy Markdown
Member

Summary

  • When the Stripe webhook finalizer fails to create a CommerceTools order because the browser path already converted the cart, look the order up again and finish metadata/fulfillment instead of retrying create or treating it as a hard failure (EDUN-15347).
  • Do not emit a second Order Completed event when that existing order is found. If the lookup itself fails in CommerceTools, let Celery retry that error instead of hiding it behind the original create error.
  • Implements the commerce-coordinator half of production control 5 from Enable Payment Methods (UPI) - Schema & High-Level Technical Documentation (PRD appendix B.5). Pair with the customer-twou PR before UPI production traffic.

What Was Built

Component Details
finalize_ct_order_from_stripe_pi After create_order_from_cart raises CommercetoolsError, query get_order_by_payment_id again
Existing-order heal If an order is found, run the existing heal path (PENDING_FULFILMENT + PaymentIntent metadata) and return already_existed=True with no Segment event
Empty lookup ValueError (no order) re-raises the original create error so bounded retries / recovery logging still apply
Lookup outage Re-query CommercetoolsError propagates so Celery retries the lookup failure

Test Plan

  • Create fails and an order exists → heal, no track, already_existed=True
  • Create fails and lookup returns not-found → original create error is re-raised
  • Create fails and lookup raises CommercetoolsError → lookup error propagates
  • Existing charge-skip / find-before-create / recovery-log coverage still passes
  • Stage: overlapping /payment-return + payment_intent.succeeded → one CT order, one Charge txn, one Segment event

NFR Compliance

  • Not an EDU NestJS public API; existing Stripe webhook → Celery finalize path only.
  • No new endpoints, events, or service-discovery registration.
  • Recovery logging contract is unchanged (quarantine, pi_id, ct_payment_id, ct_cart_id, reason, source).

bseverino and others added 2 commits August 20, 2026 13:34
Co-authored-by: Cursor <cursoragent@cursor.com>
Let authoritative CommerceTools lookup failures drive Celery retries instead of masking them with the original create error.

Co-authored-by: Cursor <cursoragent@cursor.com>
@bseverino
bseverino requested a lite review from Copilot August 20, 2026 18:56
@github-actions

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  commerce_coordinator/apps/commercetools
  stripe_payment_finalize.py
  commerce_coordinator/apps/commercetools/tests
  test_stripe_payment_finalize.py
Project Total  

This report was generated by python-coverage-comment-action

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates Stripe payment finalization to recover from overlapping order creation by re-querying CommerceTools and healing existing orders.

Changes:

  • Re-query and recover existing orders after creation failures.
  • Prevent duplicate fulfillment metadata and Segment events.
  • Add coverage for recovery and error propagation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
commerce_coordinator/apps/commercetools/tests/test_stripe_payment_finalize.py Tests recovery and error propagation scenarios.
commerce_coordinator/apps/commercetools/stripe_payment_finalize.py Implements existing-order recovery and shared healing.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@bseverino
bseverino marked this pull request as ready for review August 20, 2026 19:51
@bseverino
bseverino requested a review from a team as a code owner August 20, 2026 19:51
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.

2 participants