From 1c3aa7e37e5efb35924bfeefa97e4dd51eebee51 Mon Sep 17 00:00:00 2001 From: Jhony Lucas Date: Thu, 19 Sep 2024 15:19:44 -0300 Subject: [PATCH] fix: cancel subscription with Stripe --- backend/apps/account_payment/graphql.py | 2 ++ backend/apps/account_payment/webhooks.py | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/backend/apps/account_payment/graphql.py b/backend/apps/account_payment/graphql.py index f69b9ba7..98fc67c4 100644 --- a/backend/apps/account_payment/graphql.py +++ b/backend/apps/account_payment/graphql.py @@ -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: @@ -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: diff --git a/backend/apps/account_payment/webhooks.py b/backend/apps/account_payment/webhooks.py index b0f64888..22132072 100644 --- a/backend/apps/account_payment/webhooks.py +++ b/backend/apps/account_payment/webhooks.py @@ -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") @@ -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") @@ -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") @@ -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") @@ -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")}