Skip to content

Commit

Permalink
fix: avoid saving subscription if customer doesn't exist (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Dec 7, 2023
1 parent 081ab8c commit 075fff3
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions bd_api/apps/payment/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ def get_subscription(event: Event) -> Subscription:
if hasattr(subscription, "internal_subscription"):
return subscription.internal_subscription
else:
return Subscription.objects.create(
subscription=subscription,
admin=event.customer.subscriber,
)
if event.customer.subscriber:
return Subscription.objects.create(
subscription=subscription,
admin=event.customer.subscriber,
)


def get_credentials(scopes: list[str] = None, impersonate: str = None):
Expand Down Expand Up @@ -92,10 +93,9 @@ def update_customer(event: Event, **kwargs):
def subscribe(event: Event, **kwargs):
"""Add customer to allowed google groups"""
if event.data["object"]["status"] in ["trialing", "active"]:
subscription = get_subscription(event)
add_user(event.customer.email)
subscription.is_active = True
subscription.save()
if subscription := get_subscription(event):
subscription.is_active = True
subscription.save()


@webhooks.handler("customer.subscription.deleted")
Expand Down

0 comments on commit 075fff3

Please sign in to comment.