from django.conf import settings
from rest_framework import permissions,response,status,viewsets
from rest_framework.decorators import action
from .models import Contribution
from .serializers import ContributionSerializer
from .services import initialize_payment,verify_payment
class ContributionViewSet(viewsets.ModelViewSet):
 permission_classes=[permissions.IsAuthenticated]
 serializer_class=ContributionSerializer;http_method_names=['get','post','head','options'];filterset_fields=['purpose','status']
 def get_queryset(self):return Contribution.objects.filter(member=self.request.user)
 def perform_create(self,s):
  tx=s.save(member=self.request.user,campus=self.request.user.campus,provider=settings.PAYMENT_PROVIDER);initialize_payment(tx,settings.PAYMENT_CALLBACK_URL)
 @action(detail=False,methods=['post'])
 def verify(self,request):
  tx=self.get_queryset().get(reference=request.data.get('reference'));return response.Response(self.get_serializer(verify_payment(tx)).data)
