mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-06-28 22:03:59 +00:00
Add has_a_pending_payment field to Device model and update related views for payment handling
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 2m38s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 2m38s
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
# Create your views here.
|
||||
# billing/views.py
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
import requests
|
||||
@ -64,6 +64,9 @@ class ListCreatePaymentView(StaffEditorPermissionMixin, generics.ListCreateAPIVi
|
||||
|
||||
# Connect devices to payment
|
||||
devices = Device.objects.filter(id__in=device_ids, user=user)
|
||||
for device in devices:
|
||||
device.has_a_pending_payment = True
|
||||
device.save()
|
||||
payment.devices.set(devices)
|
||||
|
||||
serializer = PaymentSerializer(payment)
|
||||
@ -82,20 +85,9 @@ class UpdatePaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
lookup_field = "pk"
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
number_of_months = request.data.get("number_of_months")
|
||||
|
||||
if not number_of_months:
|
||||
return Response(
|
||||
{"message": "number_of_months is required."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
if not isinstance(number_of_months, int):
|
||||
return Response(
|
||||
{"message": "number_of_months must be an integer."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
device_expire_date = timezone.now() + timedelta(days=30 * number_of_months)
|
||||
instance = self.get_object()
|
||||
number_of_months = instance.number_of_months
|
||||
device_expire_date = timezone.now() + timedelta(days=30 * number_of_months)
|
||||
devices = instance.devices.all()
|
||||
serializer = self.get_serializer(instance, data=request.data, partial=False)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
@ -142,8 +134,10 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
self.verify_external_payment(data, payment)
|
||||
|
||||
# Update devices
|
||||
expiry_date = datetime.now() + timedelta(days=30 * payment.number_of_months)
|
||||
devices.update(is_active=True, expiry_date=expiry_date)
|
||||
expiry_date = timezone.now() + timedelta(days=30 * payment.number_of_months)
|
||||
devices.update(
|
||||
is_active=True, expiry_date=expiry_date, has_a_pending_payment=False
|
||||
)
|
||||
|
||||
return Response({"message": "Payment verified successfully."})
|
||||
|
||||
@ -171,7 +165,7 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
)
|
||||
|
||||
payment.paid = True
|
||||
payment.paid_at = datetime.now()
|
||||
payment.paid_at = timezone.now()
|
||||
payment.method = "WALLET"
|
||||
payment.save()
|
||||
|
||||
|
Reference in New Issue
Block a user