Skip to content

Commit

Permalink
Added the deleted key to the Customer data dict
Browse files Browse the repository at this point in the history
This is done so that Customers can be updated locally depending on whether they have been deleted upstream or not.
  • Loading branch information
arnav13081994 authored and jleclanche committed Dec 11, 2021
1 parent f12988d commit 37509b2
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions djstripe/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,17 @@ def __str__(self):

@classmethod
def _manipulate_stripe_object_hook(cls, data):

# stripe adds a deleted attribute if the Customer has been deleted upstream
if data.get("deleted"):
logger.warning(
f"This customer ({data.get('id')}) has been deleted upstream, in Stripe"
)

else:
# set "deleted" key to False (default)
data["deleted"] = False

discount = data.get("discount")
if discount:
data["coupon_start"] = discount["start"]
Expand Down Expand Up @@ -1115,6 +1126,8 @@ def add_payment_method(self, payment_method, set_default=True):
return payment_method

def purge(self):
"""Customers are soft deleted as deleted customers are still accessible by the
Stripe API and sync for all RelatedModels would fail"""
try:
self._api_delete()
except InvalidRequestError as exc:
Expand All @@ -1126,6 +1139,10 @@ def purge(self):
# The exception was raised for another reason, re-raise it
raise

# toggle the deleted flag on Customer to indicate it has been
# deleted upstream in Stripe
self.deleted = True

if self.subscriber:
# Delete the idempotency key used by Customer.create()
# So re-creating a customer for this subscriber before the key expires
Expand Down

0 comments on commit 37509b2

Please sign in to comment.