Skip to content

Commit

Permalink
Merge branch 'develop' into 'main'
Browse files Browse the repository at this point in the history
chore: Calc subscription public

See merge request locker/api-core!385
  • Loading branch information
khaitranquang committed Feb 27, 2024
2 parents 54f37aa + e2fc249 commit ca3775b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions locker_server/api/v1_0/payments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion locker_server/api/v1_0/payments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from .serializers import CalcSerializer, ListInvoiceSerializer, AdminUpgradePlanSerializer, UpgradeTrialSerializer, \
UpgradeThreePromoSerializer, UpgradeLifetimeSerializer, UpgradeLifetimePublicSerializer, \
UpgradeEducationPublicSerializer, CancelPlanSerializer, UpgradePlanSerializer, DetailInvoiceSerializer, \
CalcLifetimePublicSerializer
CalcLifetimePublicSerializer, UpgradeSubscriptionPublicSerializer


class PaymentPwdViewSet(APIBaseViewSet):
Expand Down Expand Up @@ -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":
Expand Down
33 changes: 33 additions & 0 deletions locker_server/core/services/payment_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion locker_server/shared/constants/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ------------------------------ #
Expand Down

0 comments on commit ca3775b

Please sign in to comment.