-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
249 lines (213 loc) · 10 KB
/
views.py
File metadata and controls
249 lines (213 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from dataclasses import asdict
from datetime import datetime, timedelta
from typing import Literal
from django.shortcuts import get_object_or_404
from django.utils import timezone
from drf_spectacular.utils import extend_schema
from rest_framework.exceptions import PermissionDenied
from rest_framework.generics import CreateAPIView, ListAPIView
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
# from procollab_skills.decorators import exclude_auth_perm # , exclude_sub_check_perm
from yookassa import Payment, Refund
from progress.models import UserProfile, CustomUser
from subscription.typing import (
CreatePaymentData,
AmountData,
ConfirmationRequestData,
CreatePaymentViewRequestData,
CreatePaymentResponseData,
WebHookRequest,
ReceiptData,
ItemData,
SettlementData,
)
from subscription.models import SubscriptionType
from subscription.serializers import (
CreatePaymentResponseSerializer,
SubscriptionSerializer,
RenewSubDateSerializer,
BuySubSerializer,
)
from subscription.utils.create_payment import create_payment
import logging
@extend_schema(
summary="Создаёт объект оплаты в ЮКассе",
description="""Выводит только тот уровень, который юзер может пройти. Остальные для прохождения закрыты""",
tags=["Подписка"],
request=BuySubSerializer,
parameters=[],
responses={201: CreatePaymentResponseSerializer},
)
class CreatePayment(CreateAPIView):
permission_classes = [AllowAny]
@staticmethod
def check_subscription(user_sub_date):
try:
thirty_days_ago = datetime.now().date() - timedelta(days=30)
if user_sub_date and user_sub_date >= thirty_days_ago:
raise PermissionDenied("Подписка уже оформлена.")
except PermissionDenied as e:
return Response({"error": str(e)}, status=403)
def get_request_data(self, user_profile_id: int) -> CreatePaymentViewRequestData:
self.subscription_id = self.request.data.get("subscription_id")
return CreatePaymentViewRequestData(
subscription_id=self.subscription_id,
confirmation=ConfirmationRequestData(
type="redirect", return_url=self.request.data.get("redirect_url")
), # на будущее, при добавлении новых способов оплаты
user_profile_id=user_profile_id,
)
def create(self, request, *args, **kwargs) -> Response:
try:
self.check_subscription(self.user_profile.last_subscription_date)
request_data: CreatePaymentViewRequestData = self.get_request_data(self.profile_id)
subscription = get_object_or_404(SubscriptionType, id=self.subscription_id)
amount = AmountData(value=subscription.price)
type: Literal["payment"] = "payment"
payload = CreatePaymentData(
amount=amount,
confirmation=request_data.confirmation,
metadata={
"user_profile_id": self.profile_id,
"subscription_id": subscription.id,
},
receipt=ReceiptData(
customer={"email": self.user.email},
items=[ItemData(description=subscription.name, amount=amount)],
settlements=[SettlementData(type=type, amount=amount)],
type=type,
),
)
payment: CreatePaymentResponseData = create_payment(payload)
return Response(asdict(payment), status=200)
except Exception as e:
return Response({"error": str(e)}, status=400)
@extend_schema(
summary="Вывод доступных подписок",
tags=["Подписка"],
)
class ViewSubscriptions(ListAPIView):
queryset = SubscriptionType.objects.all()
permission_classes = [AllowAny]
serializer_class = SubscriptionSerializer
def list(self, request, *args, **kwargs) -> Response:
is_logged_in = isinstance(self.user, CustomUser)
profile: UserProfile = self.user_profile if hasattr(self, "user_profile") else None
if (
profile
and profile.last_subscription_date
and profile.last_subscription_date > timezone.now() - timedelta(days=31)
):
return Response("subscription is active", status=200)
if (
(not is_logged_in)
or (not profile.bought_trial_subscription)
or (profile and not profile.last_subscription_date)
):
queryset, created = SubscriptionType.objects.get_or_create(
name="Пробная",
price=1,
features=(
"Задания от практикующих специалистов, Нативные задания, "
"Карьерные знания дешевле стакана кофе, Общество единомышленников"
),
)
else:
queryset, created = SubscriptionType.objects.get_or_create(
name="Оптимум",
price=120,
features=(
"Задания от практикующих специалистов, Нативные задания, "
"Карьерные знания дешевле стакана кофе, Общество единомышленников"
),
)
serializer = self.serializer_class(queryset)
return Response(serializer.data, status=200)
@extend_schema(summary="НЕ ДЛЯ ФРОНТА. Обновление дат подписки для юзеров.", tags=["Подписка"])
class NotificationWebHook(CreateAPIView):
serializer_class = RenewSubDateSerializer
permission_classes = [AllowAny]
authentication_classes = []
def get_request_data(self) -> WebHookRequest:
return WebHookRequest(event=self.request.data["event"], object=self.request.data["object"])
def create(self, request, *args, **kwargs) -> Response:
# try:
notification_data = self.get_request_data()
profile_to_update = UserProfile.objects.select_related("user", "last_subscription_type").filter(
id=notification_data.object["metadata"]["user_profile_id"]
)
if notification_data.event == "payment.succeeded" and notification_data.object["status"] == "succeeded":
params_to_update = {"last_subscription_date": timezone.now()}
if sub_id := notification_data.object["metadata"].get("subscription_id"):
params_to_update["last_subscription_type_id"] = sub_id
params_to_update["bought_trial_subscription"] = True
profile_to_update.update(**params_to_update)
logging.info(
f"subscription date renewed for {profile_to_update[0].user.first_name} "
f"{profile_to_update[0].user.last_name}"
)
return Response(
f"subscription date renewed for {profile_to_update[0].user.first_name} "
f"{profile_to_update[0].user.last_name}"
)
elif notification_data.event == "refund.succeeded" and notification_data.object["status"] == "succeeded":
(
profile_to_update.update(
last_subscription_date=None,
is_autopay_allowed=False,
last_subscription_type=None,
)
)
logging.info(
f"subscription canceled for {profile_to_update[0].user.first_name} "
f"{profile_to_update[0].user.last_name}"
)
return Response(
f"subscription canceled for {profile_to_update[0].user.first_name} "
f"{profile_to_update[0].user.last_name}"
)
return Response({"error": "event is not yet succeeded"}, status=400)
# except Exception as e:
# return Response({"error": str(e)}, status=400)
@extend_schema(summary="Запрос возврата", tags=["Подписка"])
class CreateRefund(CreateAPIView):
def create(self, request, *args, **kwargs):
try:
payments = Payment.list(
{
"metadata": {"user_profile_id": self.profile_id},
"status": "succeeded",
}
)
last_payment = payments.items[0]
if self.user_profile.last_subscription_date >= timezone.now().date() - timedelta(days=15):
response = Refund.create(
{
"payment_id": last_payment.id,
"amount": {
"value": last_payment.amount.value,
"currency": "RUB",
},
}
)
if response.status == "succeeded":
self.user_profile.last_subscription_date = None
self.user_profile.is_autoplay_allowed = False
self.user_profile.save()
logging.info(
f"subscription canceled for {self.user_profile.user.first_name} "
f"{self.user_profile.user.last_name}"
)
return Response(
f"subscription canceled for {self.user_profile.user.first_name} "
f"{self.user_profile.user.last_name}"
)
else:
return Response(
{"error": "Вы не можете отменить подписку после 15 дней пользования, извините"},
status=400,
)
return Response(status=202)
except Exception as e:
return Response({"error": str(e)}, status=400)