Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync cards via webhook #41

Merged
merged 6 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions aa_stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ def update_at_stripe(self, stripe_token, set_default):

return self

def refresh_from_stripe(self):
card = self._retrieve_from_stripe(True)
if card:
self.last4 = card["last4"]
self.exp_month = card["exp_month"]
self.exp_year = card["exp_year"]
self.stripe_response = card
self.is_created_at_stripe = True
return not self.is_deleted

def delete(self, *args, **kwargs):
card = self._retrieve_from_stripe(set_deleted=True)
if card:
Expand Down Expand Up @@ -636,6 +646,38 @@ def _parse_coupon_notification(self, action):
elif action == "deleted":
StripeCoupon.objects.filter(coupon_id=coupon_id, created=created).delete()

def _parse_customer_source_notification(self, action):
if self.raw_data["data"]["object"]["object"] != "card":
return

stripe_card_id = self.raw_data["data"]["object"]["id"]
stripe_customer_id = self.raw_data["data"]["object"]["customer"]
try:
customer = StripeCustomer.objects.get(stripe_customer_id=stripe_customer_id)
except StripeCustomer.DoesNotExist:
raise StripeWebhookParseError(
_("There is no customer with id that is assigned to this card in our database"))

if action == "created":
try:
StripeCard.objects.all_with_deleted().get(stripe_card_id=stripe_card_id, customer=customer)
except StripeCard.DoesNotExist:
card = StripeCard(stripe_card_id=stripe_card_id, customer=customer)
if card.refresh_from_stripe():
card.save()
else:
raise StripeWebhookParseError(_("Card with this id does not exists at Stripe API"))
else:
pass # we already have the card

elif action == "updated":
try:
card = StripeCard.objects.get(stripe_card_id=stripe_card_id, customer=customer)
card.refresh_from_stripe()
card.save()
except StripeCard.DoesNotExist:
pass # do not update if does not exist

def parse(self, save=False):
if self.is_parsed:
raise StripeWebhookAlreadyParsed
Expand All @@ -654,6 +696,8 @@ def parse(self, save=False):
if event_model:
if event_model == "coupon":
self._parse_coupon_notification(event_action)
elif event_model == "customer.source":
self._parse_customer_source_notification(event_action)

self.is_parsed = True
if save:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _create_card(self,
stripe_card_id="",
is_default=True,
set_self=True,
last4=4242,
last4="4242",
exp_month=1,
exp_year=2025):
card = StripeCard.objects.create(
Expand Down