Skip to content

Create the payment in payment_changed if payment.created hasn't arrived - #4

Merged
saevarom merged 1 commit into
mainfrom
payment-changed-before-created
Oct 14, 2026
Merged

saevarom merged 1 commit into
mainfrom
payment-changed-before-created

Conversation

@saevarom

Copy link
Copy Markdown
Member

Problem

When a stored card is charged it settles about a second after the payment is created. The payment.changed webhook can then be handled before payment.created has saved the local Payment. When that happens, the default payment_changed handler raised Payment.DoesNotExist and the webhook returned a 500.

Handlers are run in order, so the exception also stopped any handlers registered after it. In godi_hirdirinn this meant carts were never marked paid even though the customer had been charged.

Fix

payment_changed now uses get_or_create, the same way payment_created already does. Whichever webhook arrives first creates the row. A late payment.created finds the existing row and leaves it alone, so a settled payment isn't reset to pending.

Version bumped to 0.1.26 in setup.py and release.sh.

Testing

  • New test test_webhook_payment_changed_before_payment_created: sends a signed payment.changed (state settled) for an unknown payment, then the late payment.created. It failed with Payment.DoesNotExist before the fix.
  • All 9 tests pass locally (SQLite, Django 4.2), and ruff is clean.

🤖 Generated with Claude Code

When a stored card is charged it settles about a second after it is
created, so the payment.changed webhook can be handled before
payment.created has saved the local Payment. payment_changed then raised
Payment.DoesNotExist, which also stopped any handlers registered after it.

Use get_or_create like payment_created does, so whichever webhook comes
first creates the row. Version 0.1.26.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@saevarom
saevarom merged commit 3371b0f into main Oct 14, 2026
2 checks passed
@saevarom
saevarom deleted the payment-changed-before-created branch October 14, 2026 14:23
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