# Path: payment/views.py
# class PaymentView:
#     """
#     View responsible for payment of orders
#     """
# 
#     @classmethod
#     @permission_classes([IsAuthenticated])
#     def pay(cls, request, pk):
#         """
#         Pay an order
# 
#         Steps:
#             1. Retrieve Order
#             2. Verify Order
#             3. Create Transaction
#             4. Save Transaction info and redirect
#         """
#         # 1. Retrieve Order
#         order = Order.objects.filter(owner__pk=request.user.pk) \
#                      .filter(status__in=OrderStatus.BUYABLE_STATUS_LIST.value) \
#                      .select_related('sale', 'owner') \
#                      .get(pk=pk)
# 
#         # Lock sensitive part
#         with pay_lock:
# 
#             # 2. Verify Order
#             validator = OrderValidator(order, raise_on_error=True)
#             validator.validate()
# 
#             # 3. Create Transaction
#             pay_service = get_pay_service(order, request)
#             callback_url = request.build_absolute_uri(
#                 reverse('order-status', kwargs={ 'pk': order.pk })
#             )
#             return_url = request.GET['return_url']
#             transaction = pay_service.create_transaction(order, callback_url, return_url)
# 
#             # 4. Save transaction id and redirect
#             order.status = OrderStatus.AWAITING_PAYMENT.value
#             order.tra_id = transaction['tra_id']
#             order.save()
# 
#         # Redirect to transaction url
#         resp = {
#             'status': order.get_status_display(),
#             'redirect_url': transaction['url'],
#         }
#         return Response(resp, status=status.HTTP_200_OK)
# 
#     @classmethod
#     def update_status(cls, request, pk):
#         """
#         Callback after the transaction has been made
#         to validate, cancel or redirect the order
#         """
#         # Update order status
#         order = Order.objects.select_related('sale__association').get(pk=pk)
#         resp = order.update_status()
#         pay_service = get_pay_service(order, request)
# 
#         # Return the response
#         if resp.pop('redirect_to_payment', False):
#             callback_url = request.build_absolute_uri(
#                 reverse('order-status', kwargs={ 'pk': order.pk })
#             )
#             return_url = request.GET.get('return_url', '')
#             resp['redirect_url'] = pay_service.get_redirection_to_payment(order, callback_url, return_url)
# 
#         return Response(resp, status=status.HTTP_200_OK)


from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from .views import PaymentView


urlpatterns = [
    path('orders/<int:pk>/pay',    PaymentView.pay,           name='order-pay'),
    path('orders/<int:pk>/status', PaymentView.update_status, name='order-status'),
]


# Path: payment/views.py
# class PaymentView:
#     """
#     View responsible for payment of orders
#     """
# 
#     @classmethod
#     @permission_classes([IsAuthenticated])
#     def pay(cls, request, pk):
#         """
#         Pay an order
# 
#         Steps:
#             1. Retrieve Order
#             2. Verify Order
#             3. Create Transaction
#             4. Save Transaction info and redirect
#         """
#         # 1. Retrieve Order
#         order = Order.objects.filter(owner__pk=request.user.pk) \
#                      .filter(status__in=OrderStatus.BUYABLE_STATUS_LIST.value) \
#                      .select_related('sale', 'owner') \
#                      .get(pk=pk)
# 
#         # Lock sensitive part
#         with pay_lock:
# 
#             # 2. Verify Order
#             validator = OrderValidator(order, raise_on_error=True)
#             validator.validate()
# 
#             # 3. Create Transaction
#             pay_service = get_pay_service(order, request)
#             callback_url = request.build_absolute_uri(
#                 reverse('order-status', kwargs={ 'pk': order.pk })
#             )
#             return_url = request.GET['return_url']
#             transaction = pay_service.create_transaction(order, callback_url, return_url)
# 
#             # 4. Save transaction id and redirect
#             order.status = OrderStatus.AWAITING_PAYMENT.value
#             order.tra_id = transaction['tra_id']
#             order.save()
# 
#         # Redirect to transaction url
#         resp = {
#             'status': order.get_status_display(),
#             'redirect_url': transaction['url'],
#         }
#         return Response(resp, status=status.HTTP_200_OK)
# 
#     @classmethod
#     def update_status(cls, request, pk):
#         """
#         Callback after the transaction has been made
#         to validate, cancel or redirect the order
#         """
#         # Update order status
#         order = Order.objects.select_related('sale__association').get(pk=pk)
#         resp = order.update_status()
#         pay_service = get_pay_service(order, request)
# 
#         # Return the response
#         if resp.pop('redirect_to_payment', False):
#             callback_url = request.build_absolute_uri(
#                 reverse('order-status', kwargs={ 'pk': order.pk })
#             )
#             return_url = request.GET.get('return_url', '')
#             resp['redirect_url'] = pay_service.get_redirection_to_payment(order, callback_url, return_url)
# 
#         return Response(resp, status=status.HTTP_200_OK)


# Path: payment/urls.py
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from .views import PaymentView



urlpatterns = [
    path('orders/<int:pk>/pay',    PaymentView.pay,           name='order-pay'),
    path('orders/<int:pk>/status', PaymentView.update_status, name='order-status'),
]


urlpatterns = format_suffix_patterns(urlpatterns)