Skip to content

Commit

Permalink
Merge branch 'dev' into feat/change-user-gcp-email
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonylucas74 authored Sep 30, 2024
2 parents 1cadc7c + 5c7c264 commit 559610d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions backend/apps/account_payment/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from backend.apps.account.models import Account, Subscription
from backend.apps.account_payment.webhooks import add_user, is_email_in_group, remove_user
from backend.custom.environment import get_backend_url
from backend.custom.graphql_base import CountableConnection, PlainTextNode

if settings.STRIPE_LIVE_MODE:
Expand Down Expand Up @@ -243,6 +244,7 @@ def mutate(cls, root, info, price_id, coupon=None):
metadata={
"price_id": price_id,
"promotion_code": promotion_code,
"backend_url": get_backend_url(),
},
)
else:
Expand Down
23 changes: 19 additions & 4 deletions backend/apps/account_payment/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from backend.apps.account.models import Account, Subscription
from backend.custom.client import send_discord_message as send
from backend.custom.environment import get_backend_url

logger = logger.bind(module="payment")

Expand Down Expand Up @@ -194,10 +195,13 @@ def unsubscribe(event: Event, **kwargs):

account = Account.objects.filter(email=event.customer.email).first()
# Remove user from google group if subscription exists or not
if account:
remove_user(account.gcp_email or account.email)
else:
remove_user(event.customer.email)
try:
if account:
remove_user(account.gcp_email or account.email)
else:
remove_user(event.customer.email)
except Exception as e:
logger.error(e)


@webhooks.handler("customer.subscription.paused")
Expand All @@ -209,10 +213,14 @@ def pause_subscription(event: Event, **kwargs):
logger.info(f"Pausando a inscrição do cliente {event.customer.email}")
subscription.is_active = False
subscription.save()

try:
if account:
remove_user(account.gcp_email or account.email)
else:
remove_user(event.customer.email)
except Exception as e:
logger.error(e)


@webhooks.handler("customer.subscription.resumed")
Expand All @@ -225,10 +233,13 @@ def resume_subscription(event: Event, **kwargs):
subscription.is_active = True
subscription.save()

try:
if account:
add_user(account.gcp_email or account.email)
else:
add_user(event.customer.email)
except Exception as e:
logger.error(e)


@webhooks.handler("setup_intent.succeeded")
Expand All @@ -241,6 +252,10 @@ def setup_intent_succeeded(event: Event, **kwargs):
metadata = setup_intent.get("metadata")
price_id = metadata.get("price_id")
promotion_code = metadata.get("promotion_code")
backend_url = metadata.get("backend_url")

if not backend_url == get_backend_url():
return logger.info(f"Ignore setup intent from {backend_url}")

StripeCustomer.modify(
customer.id, invoice_settings={"default_payment_method": setup_intent.get("payment_method")}
Expand Down

0 comments on commit 559610d

Please sign in to comment.