mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-10-05 13:35:23 +00:00
fix(payment): update wallet payment processing to activate devices and set expiry date and add further optimizations 🐛
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m8s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m8s
This commit is contained in:
@@ -152,7 +152,7 @@ class PaymentDetailAPIView(StaffEditorPermissionMixin, generics.RetrieveAPIView)
|
|||||||
|
|
||||||
|
|
||||||
class UpdatePaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
class UpdatePaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||||
queryset = Payment.objects.select_related("user").all()
|
queryset = Payment.objects.select_related("user").prefetch_related("devices").all()
|
||||||
serializer_class = UpdatePaymentSerializer
|
serializer_class = UpdatePaymentSerializer
|
||||||
lookup_field = "pk"
|
lookup_field = "pk"
|
||||||
|
|
||||||
@@ -172,11 +172,12 @@ class UpdatePaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
|||||||
|
|
||||||
class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||||
serializer_class = PaymentSerializer
|
serializer_class = PaymentSerializer
|
||||||
queryset = Payment.objects.select_related("user").all()
|
queryset = Payment.objects.select_related("user").prefetch_related("devices").all()
|
||||||
lookup_field = "pk"
|
lookup_field = "pk"
|
||||||
|
|
||||||
def update(self, request, *args, **kwargs):
|
def update(self, request, *args, **kwargs):
|
||||||
payment = self.get_object()
|
payment = self.get_object()
|
||||||
|
devices = payment.devices.all()
|
||||||
data = request.data
|
data = request.data
|
||||||
user = request.user
|
user = request.user
|
||||||
print("logged in user", user)
|
print("logged in user", user)
|
||||||
@@ -198,7 +199,6 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
|||||||
status=status.HTTP_400_BAD_REQUEST,
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
)
|
)
|
||||||
|
|
||||||
devices = payment.devices.all()
|
|
||||||
if method == "WALLET":
|
if method == "WALLET":
|
||||||
if user.wallet_balance < payment.amount: # type: ignore
|
if user.wallet_balance < payment.amount: # type: ignore
|
||||||
return Response(
|
return Response(
|
||||||
@@ -209,6 +209,7 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
|||||||
self.process_wallet_payment(
|
self.process_wallet_payment(
|
||||||
user, # type: ignore
|
user, # type: ignore
|
||||||
payment,
|
payment,
|
||||||
|
devices,
|
||||||
)
|
)
|
||||||
return Response(
|
return Response(
|
||||||
{
|
{
|
||||||
@@ -271,14 +272,24 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
|||||||
status=status.HTTP_400_BAD_REQUEST,
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
)
|
)
|
||||||
|
|
||||||
def process_wallet_payment(self, user: User, payment: Payment):
|
def process_wallet_payment(self, user: User, payment: Payment, devices=None):
|
||||||
print("processing wallet payment...")
|
print("processing wallet payment...")
|
||||||
print(user, payment.amount)
|
print(user, payment.amount)
|
||||||
|
# Use passed devices or fetch if not provided
|
||||||
|
if devices is None:
|
||||||
|
devices = payment.devices.all()
|
||||||
|
|
||||||
payment.paid = True
|
payment.paid = True
|
||||||
payment.paid_at = timezone.now()
|
payment.paid_at = timezone.now()
|
||||||
payment.method = "WALLET"
|
payment.method = "WALLET"
|
||||||
payment.status = "PAID"
|
payment.status = "PAID"
|
||||||
|
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,
|
||||||
|
registered=True,
|
||||||
|
)
|
||||||
payment.save()
|
payment.save()
|
||||||
|
|
||||||
user.deduct_wallet_funds(
|
user.deduct_wallet_funds(
|
||||||
|
Reference in New Issue
Block a user