mergecal icon indicating copy to clipboard operation
mergecal copied to clipboard

Update Stripe customer metadata when attaching orphaned customers to users

Open Copilot opened this issue 4 months ago • 0 comments

Problem

When an orphaned Stripe customer (a customer without a subscriber) is attached to a user during login, the local djstripe database is updated but the metadata on Stripe's side is not synchronized. This can lead to inconsistencies between our local database and Stripe's records, making it difficult to identify which user a customer belongs to when looking at Stripe dashboard or using Stripe's API directly.

Solution

This PR adds a call to stripe.Customer.modify() in the create_stripe_customer signal handler to update the customer metadata on Stripe's API with the key djstripe_subscriber containing the user's ID.

Changes

mergecalweb/billing/signals.py

  • Added stripe.Customer.modify() call after attaching an orphaned customer to a user (lines 90-94)
  • The metadata update includes {"djstripe_subscriber": str(user.pk)} to maintain consistency between local and remote data

mergecalweb/billing/tests.py

  • Added comprehensive test coverage for the signal handler:
    • Verifies metadata is updated when orphaned customers are attached
    • Ensures new customer creation flow is not affected
    • Confirms customers with existing subscribers are not modified

Implementation Details

Following the existing pattern in the codebase (e.g., stripe.Subscription.create on line 94), this implementation uses the Stripe Python SDK directly rather than relying on djstripe to automatically sync changes. Research confirmed that djstripe does not automatically push local model changes back to Stripe, so direct API calls are necessary.

The user ID is converted to a string as Stripe metadata values must be strings.

Testing

  • ✅ All new tests pass (3/3)
  • ✅ All existing tests pass (17/17 user tests)
  • ✅ Linting passes (ruff)
  • ✅ Security scan passes (CodeQL - 0 alerts)
  • ✅ No breaking changes to existing functionality
Original prompt

On signals.py:79-79, I need to update the stripes customer metadata to include the user id under the key: djstripe_subscriber. I would assume that djstripe would update teh customer object on stripe but it doesn't seem to be the case. I would research if djstripe provides a means to make changes on stripe if not just do it with the stripe api. Checkout a new branch


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Oct 22 '25 22:10 Copilot