PaymentMethod.detach() does not set default_payment_method to None
See original GitHub issueDescribe 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
- Create customer
- Attach a payment method
- 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:
- Created 3 years ago
- Comments:22 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 lastPM
is removed. Or more generally when anyFK
is set tonull
as returned by theStripe API
.I will think of a way to fix this generic issue.
@dbartenstein No I don’t think that would be possible. Sorry.