From 63e315778fe06956f425b13de333cf3ae3822c32 Mon Sep 17 00:00:00 2001 From: Jhony Lucas Date: Fri, 13 Sep 2024 08:47:38 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20add=20duration=20and=20duration=5Fin=5F?= =?UTF-8?q?months=20fields=20to=20StripeCouponValid=E2=80=A6=20(#676)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/apps/account_payment/graphql.py | 49 ++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/backend/apps/account_payment/graphql.py b/backend/apps/account_payment/graphql.py index 198f83c0..f69b9ba7 100644 --- a/backend/apps/account_payment/graphql.py +++ b/backend/apps/account_payment/graphql.py @@ -4,7 +4,18 @@ from djstripe.models import Customer as DJStripeCustomer from djstripe.models import Price as DJStripePrice from djstripe.models import Subscription as DJStripeSubscription -from graphene import ID, Boolean, Field, Float, InputObjectType, List, Mutation, ObjectType, String +from graphene import ( + ID, + Boolean, + Field, + Float, + InputObjectType, + Int, + List, + Mutation, + ObjectType, + String, +) from graphene_django import DjangoObjectType from graphene_django.filter import DjangoFilterConnectionField from graphql_jwt.decorators import login_required @@ -257,6 +268,8 @@ class StripeCouponValidationMutation(Mutation): is_valid = Boolean() discount_amount = Float() + duration = String() + duration_in_months = Int() errors = List(String) class Arguments: @@ -272,12 +285,27 @@ def mutate(cls, root, info, coupon, price_id): if promotion_code_object and promotion_code_object.active: coupon_object = stripe.Coupon.retrieve(promotion_code_object.coupon.id) else: - return cls(is_valid=False, discount_amount=0, errors=["Cupom inválido"]) + return cls( + is_valid=False, + discount_amount=0, + duration_in_months=0, + errors=["Cupom inválido"], + ) except Exception as e: - return cls(is_valid=False, discount_amount=0, errors=["Cupom inválido", str(e)]) + return cls( + is_valid=False, + discount_amount=0, + duration_in_months=0, + errors=["Cupom inválido", str(e)], + ) if not coupon_object.valid: - return cls(is_valid=False, discount_amount=0, errors=["Cupom não está ativo"]) + return cls( + is_valid=False, + discount_amount=0, + duration_in_months=0, + errors=["Cupom não está ativo"], + ) price = DJStripePrice.objects.get(djstripe_id=price_id) price_amount = price.unit_amount / 100.0 @@ -289,7 +317,18 @@ def mutate(cls, root, info, coupon, price_id): elif coupon_object.percent_off: discount_amount = (coupon_object.percent_off / 100.0) * price_amount - return cls(is_valid=True, discount_amount=discount_amount) + duration_in_months = coupon_object.duration_in_months + duration = coupon_object.duration + + if coupon_object.duration != "repeating": + duration_in_months = 0 + + return cls( + is_valid=True, + discount_amount=discount_amount, + duration_in_months=duration_in_months, + duration=duration, + ) except Exception as e: logger.error(e) return cls(is_valid=False, errors=[str(e)])