From e2fc24991a84c00a44211e1ff6af128c0491d679 Mon Sep 17 00:00:00 2001 From: Khai Tran Date: Tue, 27 Feb 2024 16:55:05 +0700 Subject: [PATCH] chore: Calc subscription public --- .../payment_pwd_permission.py | 2 +- .../api/v1_0/payments/serializers.py | 9 +++++ locker_server/api/v1_0/payments/views.py | 4 ++- .../core/services/payment_service.py | 33 +++++++++++++++++++ .../shared/constants/transactions.py | 2 +- 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/locker_server/api/permissions/locker_permissions/payment_pwd_permission.py b/locker_server/api/permissions/locker_permissions/payment_pwd_permission.py index 83c8865d..9cca1f23 100644 --- a/locker_server/api/permissions/locker_permissions/payment_pwd_permission.py +++ b/locker_server/api/permissions/locker_permissions/payment_pwd_permission.py @@ -5,7 +5,7 @@ class PaymentPwdPermission(APIPermission): def has_permission(self, request, view): if view.action in ["webhook_create", "webhook_set_status", "webhook_unpaid_subscription", "webhook_cancel_subscription", - "upgrade_trial_enterprise_by_code", "calc_lifetime_public"]: + "upgrade_trial_enterprise_by_code", "calc_lifetime_public", "calc_subscription_public"]: return True elif view.action in ["list", "set_invoice_status", "user_invoices", "admin_upgrade_plan"]: return self.is_admin(request) diff --git a/locker_server/api/v1_0/payments/serializers.py b/locker_server/api/v1_0/payments/serializers.py index 8974a35b..57231e86 100644 --- a/locker_server/api/v1_0/payments/serializers.py +++ b/locker_server/api/v1_0/payments/serializers.py @@ -127,6 +127,15 @@ class UpgradeLifetimePublicSerializer(serializers.Serializer): ) +class UpgradeSubscriptionPublicSerializer(serializers.Serializer): + promo_code = serializers.CharField(required=False, allow_null=True, allow_blank=True) + plan_alias = serializers.ChoiceField( + choices=[PLAN_TYPE_PM_PREMIUM, PLAN_TYPE_PM_FAMILY], default=PLAN_TYPE_PM_PREMIUM, + required=False + ) + duration = serializers.ChoiceField(choices=LIST_DURATION, default=DURATION_MONTHLY, required=False) + + class UpgradeEducationPublicSerializer(serializers.Serializer): email = serializers.EmailField(max_length=128) education_email = serializers.EmailField(max_length=255, required=False, allow_blank=True, allow_null=True) diff --git a/locker_server/api/v1_0/payments/views.py b/locker_server/api/v1_0/payments/views.py index 6bd8e154..4d64c1b3 100644 --- a/locker_server/api/v1_0/payments/views.py +++ b/locker_server/api/v1_0/payments/views.py @@ -23,7 +23,7 @@ from .serializers import CalcSerializer, ListInvoiceSerializer, AdminUpgradePlanSerializer, UpgradeTrialSerializer, \ UpgradeThreePromoSerializer, UpgradeLifetimeSerializer, UpgradeLifetimePublicSerializer, \ UpgradeEducationPublicSerializer, CancelPlanSerializer, UpgradePlanSerializer, DetailInvoiceSerializer, \ - CalcLifetimePublicSerializer + CalcLifetimePublicSerializer, UpgradeSubscriptionPublicSerializer class PaymentPwdViewSet(APIBaseViewSet): @@ -54,6 +54,8 @@ def get_serializer_class(self): self.serializer_class = UpgradeLifetimePublicSerializer elif self.action == "calc_lifetime_public": self.serializer_class = CalcLifetimePublicSerializer + elif self.action == "upgrade_subscription_public": + self.serializer_class = UpgradeSubscriptionPublicSerializer elif self.action == "upgrade_education_public": self.serializer_class = UpgradeEducationPublicSerializer elif self.action == "cancel_plan": diff --git a/locker_server/core/services/payment_service.py b/locker_server/core/services/payment_service.py index 0bb25664..0b99722e 100644 --- a/locker_server/core/services/payment_service.py +++ b/locker_server/core/services/payment_service.py @@ -127,6 +127,39 @@ def calc_lifetime_payment_public(self, plan_alias: str, currency: str = CURRENCY result["plan"] = new_plan.to_json() return result + def calc_subscription_payment_public(self, plan_alias: str, duration: str = DURATION_MONTHLY, + currency: str = CURRENCY_USD, number_members: int = 1, promo_code: str = None, + user=None, allow_trial: bool = True): + new_plan = self.plan_repository.get_plan_by_alias(alias=plan_alias) + if not new_plan: + raise PlanDoesNotExistException + if new_plan.is_team_plan is False: + number_members = 1 + if user is not None: + current_plan = self.user_plan_repository.get_user_plan(user_id=user.user_id) + utm_source = self.user_repository.get_from_cystack_id(user_id=user.user_id).get("utm_source") + result = self.user_plan_repository.calc_update_price( + current_plan=current_plan, + new_plan=new_plan, + new_duration=duration, + new_quantity=number_members, + currency=currency, + promo_code=promo_code, + allow_trial=allow_trial, + utm_source=utm_source, + ) + else: + result = self.user_plan_repository.calc_payment_public( + new_plan=new_plan, + new_duration=duration, + new_quantity=number_members, + currency=currency, + promo_code=promo_code, + allow_trial=allow_trial, + ) + result["plan"] = new_plan.to_json() + return result + def admin_upgrade_plan(self, user_id: int, plan_alias: str, end_period: float = None, scope: str = None): new_plan = self.plan_repository.get_plan_by_alias(alias=plan_alias) if not new_plan: diff --git a/locker_server/shared/constants/transactions.py b/locker_server/shared/constants/transactions.py index a280142e..ceadb381 100644 --- a/locker_server/shared/constants/transactions.py +++ b/locker_server/shared/constants/transactions.py @@ -54,7 +54,7 @@ DURATION_HALF_YEARLY = "half_yearly" DURATION_YEARLY = "yearly" -LIST_DURATION = [DURATION_MONTHLY, DURATION_HALF_YEARLY, DURATION_YEARLY] +LIST_DURATION = [DURATION_MONTHLY, DURATION_YEARLY] # ------------------- Promo code types ------------------------------ #