Skip to content

Handle Stripe payment_method.detached webhook events#11171

Open
pieterbeulque wants to merge 1 commit intomainfrom
claude/review-issue-6100-qKvJW
Open

Handle Stripe payment_method.detached webhook events#11171
pieterbeulque wants to merge 1 commit intomainfrom
claude/review-issue-6100-qKvJW

Conversation

@pieterbeulque
Copy link
Copy Markdown
Contributor

@pieterbeulque pieterbeulque commented Apr 22, 2026

I threw #6100 straight into Claude Code , feel free to dismiss the PR.

Summary

Adds support for handling Stripe payment_method.detached webhook events to soft-delete payment methods when they are detached from a customer in Stripe.

What

  • Added payment_method_detached task handler in polar/integrations/stripe/tasks.py to process Stripe webhook events when a payment method is detached
  • Added get_by_processor_id method to PaymentMethodRepository to look up payment methods by processor and processor ID
  • Registered payment_method.detached event in Stripe webhook endpoints configuration
  • Added comprehensive test coverage for the new webhook handler with two test cases:
    • Soft-deletes matching payment method and unlinks it from customer's default payment method
    • Gracefully handles unknown payment methods (no-op)

Why

When a customer detaches a payment method from their Stripe account, we need to synchronize that change in our system by soft-deleting the corresponding payment method record and clearing it from the customer's default payment method if applicable. This ensures data consistency between Stripe and our database.

How

The implementation:

  1. Listens for payment_method.detached Stripe webhook events
  2. Extracts the payment method ID from the webhook payload
  3. Looks up the payment method in our database using the new get_by_processor_id repository method
  4. If found, soft-deletes it via the payment method service (with force=True to handle cases where Stripe has already detached it)
  5. Gracefully handles the case where we don't have the payment method on file (no-op)

Checklist

  • This PR addresses a single concern (webhook handler for payment method detachment)
  • The diff is reasonably sized and easy to review
  • New functionality is covered by tests (2 test cases added)
  • No unrelated changes or drive-by fixes are included

https://claude.ai/code/session_01NK91AwkotB5cuFJDMi7gkr

Listen to Stripe's payment_method.detached webhook and soft-delete the
corresponding PaymentMethod so the customer portal no longer displays
stale last4/brand metadata after the card is removed on Stripe.

Fixes #6100
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
polar Ready Ready Preview, Comment Apr 22, 2026 6:24pm
polar-sandbox Ready Ready Preview, Comment Apr 22, 2026 6:24pm

Request Review

@github-actions
Copy link
Copy Markdown
Contributor

OpenAPI Changes

No changes detected in the OpenAPI schema.

@github-actions
Copy link
Copy Markdown
Contributor

Preview Environment
URL: https://polar-preview-vm.taildbff7b.ts.net/pr-11171
API: https://polar-preview-vm.taildbff7b.ts.net/pr-11171/v1/
Logs: backend
SHA: 2043689a703fb50c86e4f05be3941bd86930990b

Copy link
Copy Markdown
Member

@frankie567 frankie567 left a comment

Choose a reason for hiding this comment

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

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.

3 participants