Skip to content

Commit

Permalink
Merge pull request #685 from basedosdados/fix/cancel-sub
Browse files Browse the repository at this point in the history
fix: cancel subscription with Stripe
  • Loading branch information
jhonylucas74 authored Sep 19, 2024
2 parents 8c514c7 + 1c3aa7e commit 5c7c264
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 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, 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
22 changes: 19 additions & 3 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 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 @@ -159,7 +160,10 @@ def unsubscribe(event: Event, **kwargs):
subscription.is_active = False
subscription.save()
# Remove user from google group if subscription exists or not
remove_user(event.customer.email)
try:
remove_user(event.customer.email)
except Exception as e:
logger.error(e)


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

try:
remove_user(event.customer.email)
except Exception as e:
logger.error(e)


@webhooks.handler("customer.subscription.resumed")
Expand All @@ -179,7 +187,11 @@ def resume_subscription(event: Event, **kwargs):
logger.info(f"Resumindo a inscrição do cliente {event.customer.email}")
subscription.is_active = True
subscription.save()
add_user(event.customer.email)

try:
add_user(event.customer.email)
except Exception as e:
logger.error(e)


@webhooks.handler("setup_intent.succeeded")
Expand All @@ -192,6 +204,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 5c7c264

Please sign in to comment.