Skip to content

Commit

Permalink
Added cancel payment view and payment add for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDyakonov committed Jun 15, 2024
1 parent 0d34532 commit 3428ad5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
20 changes: 20 additions & 0 deletions backend/apps/payments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ class PaymentStatusSerializer(serializers.Serializer):
payment_id = serializers.CharField(required=True)


class PaymentIdSerializer(serializers.Serializer):
payment_id = serializers.CharField(help_text="Unique identifier for a payment.")

def validate_payment_id(self, value):
"""
Check that the payment_id exists in the database and is associated with an order.
You might want to add more complex validations depending on your business logic.
"""
if not Order.objects.filter(payment_id=value).exists():
raise serializers.ValidationError(
"No order found with the given payment ID."
)
return value


class PaymentListOutputSerializer(serializers.ModelSerializer):
buyer = BuyerPaymentSerializer(source="cart.buyer", read_only=True)

Expand All @@ -32,3 +47,8 @@ class Meta:
"payment_status",
"ticket_file",
]


class PaymentItemInputSerializer(serializers.Serializer):
cart_id = serializers.IntegerField(required=True)
total = serializers.FloatField(required=True)
8 changes: 7 additions & 1 deletion backend/apps/payments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from .views import (
BookingsByOrderIdAPIView,
PaymentCancelView,
PaymentItemInputView,
PaymentListView,
PaymentProcessingView,
PaymentStatusView,
Expand All @@ -17,5 +19,9 @@
BookingsByOrderIdAPIView.as_view(),
name="bookings-by-order",
),
path("", PaymentListView.as_view(), name="payment-list"),
path("list/", PaymentListView.as_view(), name="payment-list"),
path("add-item/", PaymentItemInputView.as_view(), name="payment-add-item"),
path(
"cancel/<str:payment_id>/", PaymentCancelView.as_view(), name="payment-cancel"
),
]
69 changes: 69 additions & 0 deletions backend/apps/payments/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import secrets

from apps.amo.views import post_orders
from apps.booking.models import Booking, Buyer, Cart
Expand All @@ -15,6 +16,8 @@

from .models import Order
from .serializers import (
PaymentIdSerializer,
PaymentItemInputSerializer,
PaymentListOutputSerializer,
PaymentProcessingSerializer,
PaymentStatusSerializer,
Expand All @@ -30,6 +33,72 @@ def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)


class PaymentItemInputView(generics.CreateAPIView):
serializer_class = PaymentItemInputSerializer

def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

cart_id = request.data.get("cart_id")
total = request.data.get("total")

try:
cart = Cart.objects.get(id=cart_id)
except Cart.DoesNotExist:
return Response(
{"error": "Cart not found"}, status=status.HTTP_404_NOT_FOUND
)

payment_id = secrets.token_hex(10)
confirmation_token = secrets.token_hex(10)

order = Order.objects.create(
cart=cart,
total=total,
payment_id=payment_id,
confirmation_token=confirmation_token,
ticket_file=request.data.get(
"ticket_file", "https://kolomnago.ru/ticket_file.pdf"
),
qr_code=request.data.get("qr_code", "https://kolomnago.ru/qr_code"),
payment_status=request.data.get("payment_status", "pending"),
)

return Response(
{
"payment_id": payment_id,
"confirmation_token": confirmation_token,
"message": "Order created successfully",
},
status=status.HTTP_201_CREATED,
)


class PaymentCancelView(generics.GenericAPIView):
def patch(self, request, *args, **kwargs):
payment_id = kwargs.get("payment_id")
if payment_id is None:
return Response(
{"error": "Missing payment ID"}, status=status.HTTP_400_BAD_REQUEST
)

order = Order.objects.filter(payment_id=payment_id).first()
if order is None:
return Response(
{"error": "Order not found"}, status=status.HTTP_404_NOT_FOUND
)
if order.payment_status == "canceled":
return Response(
{"error": "Order already canceled"}, status=status.HTTP_409_CONFLICT
)

order.payment_status = "canceled"
order.save()
return Response({"success": "Payment canceled"}, status=status.HTTP_200_OK)


class PaymentStatusView(generics.RetrieveAPIView):
serializer_class = PaymentStatusSerializer

Expand Down

0 comments on commit 3428ad5

Please sign in to comment.