question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PaymentMethod.detach() does not set default_payment_method to None

See original GitHub issue

Describe the bug

I found a bug today when a customer is detaching his PaymentMethod and he only had one.

Detaching works fine because PaymentMethod.customer is set to None, however Customer.default_payment_method still references the now detached PaymentMethod.

To Reproduce

  1. Create customer
  2. Attach a payment method
  3. Detach payment method

Expected behavior

If no other payment methods are available, set default_payment_method back to None. If any other payment method is available, we could pick one of them optionally.

As a workaround I am using the payment_method.detached webhook:


def payment_method_detached(event):
    """ Not sure if it's a bug in djstripe's PaymentMethod.detach() but default_payment_method is not set to None """

    customer_id = event.data["previous_attributes"]["customer"]
    customer = Customer.objects.get(id=customer_id)

    if not customer.payment_methods.all().exists():
        customer.default_payment_method = None
        customer.save()

Environment

  • dj-stripe version: 2.3.0
  • Your Stripe account’s default API version: 2019-11-05
  • Database: Postgres
  • Python version: 3.6.9
  • Django version: 2.2.11

Can you reproduce the issue with the latest version of master?

[Yes / No]

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
arnav13081994commented, Dec 9, 2021

@dbartenstein I was able to replicate this issue. This happens when the customer has atleast 1 or more subscriptions. This does not happen for one time payments for some reason. This is Stripe’s default behaviour. Also this is a more generic issue with nulled FKs not being synced. The current logic essentially skips their sync which is why sync fails when the last PM is removed. Or more generally when any FK is set to null as returned by the Stripe API.

I will think of a way to fix this generic issue.

0reactions
arnav13081994commented, Dec 10, 2021

@dbartenstein No I don’t think that would be possible. Sorry.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Detach a PaymentMethod from a Customer - Stripe
Detaches a PaymentMethod object from a Customer. After a PaymentMethod is detached, it can no longer be used for a payment or re-attached...
Read more >
How to remove the default payment method in Stripe Checkout?
Following the official guide, I've built a working implementation but I have a question: how can users delete their credit card data after ......
Read more >
Set existing Payment Method as default · Issue #1111 - GitHub
customer.add_payment_method(payment_method) sets a new card as the default payment method, but is there a way to set an existing payment ...
Read more >
Default Payment Method - Payments: Features
Automatically set the latest default payment method on all related "Open" Transactions. For example: If a customer has updated their payment ...
Read more >
PaymentMethod - Knowledge Center - Zuora
The SOAP API does not support the following payment method types. ... but is required for all other calls. including the create() call....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found