Repository: svthalia/concrexit

Issue:
Thalia Pay: Allow API access to Thalia Pay status of members
### Is your feature request related to a problem? Please describe.
It is currently not possible to find out if a user is allowed to use Thalia Pay.

### Describe the solution you'd like
There needs to be a field in the payable object. So events and pizzas for example.

### Motivation
To be able to implement Thalia Pay in the app we really need this.



Discussion:
This is blocked by #1397 I think 
Why exactly? It is just the tpay allowed value that is currently used to determine if you can actually use it in the website as well.
> Why exactly? It is just the tpay allowed value that is currently used to determine if you can actually use it in the website as well.

Hmm yeah okay you're right. I meant to say that with #1397 we are going to refactor the `PaymentUser` model once again from a proxy model to a real model. So that will change some things for the implementation
I think we should rebrand this issue `Payments in API v2`
That is part of #1428 

Resolving patch:
diff --git a/website/payments/api/v2/serializers/payment_user.py b/website/payments/api/v2/serializers/payment_user.py
new file mode 100644
--- /dev/null
+++ b/website/payments/api/v2/serializers/payment_user.py
@@ -0,0 +1,10 @@
+from rest_framework.fields import DecimalField, BooleanField
+from rest_framework.serializers import Serializer
+
+
+class PaymentUserSerializer(Serializer):
+    """Serializer to show payment users."""
+
+    tpay_balance = DecimalField(decimal_places=2, max_digits=1000)
+    tpay_allowed = BooleanField()
+    tpay_enabled = BooleanField()
diff --git a/website/payments/api/v2/urls.py b/website/payments/api/v2/urls.py
--- a/website/payments/api/v2/urls.py
+++ b/website/payments/api/v2/urls.py
@@ -5,12 +5,18 @@
     PaymentListView,
     PaymentDetailView,
     PayableDetailView,
+    PaymentUserCurrentView,
 )
 
 app_name = "payments"
 
 urlpatterns = [
     path("payments/", PaymentListView.as_view(), name="payments-list"),
+    path(
+        "payments/users/me/",
+        PaymentUserCurrentView.as_view(),
+        name="payments-tpay-status",
+    ),
     path("payments/<uuid:pk>/", PaymentDetailView.as_view(), name="payment-detail"),
     path(
         "payments/payables/<str:app_label>/<str:model_name>/<str:payable_pk>/",
diff --git a/website/payments/api/v2/views.py b/website/payments/api/v2/views.py
--- a/website/payments/api/v2/views.py
+++ b/website/payments/api/v2/views.py
@@ -1,12 +1,10 @@
 import rest_framework.filters as framework_filters
 from django.apps import apps
-from django.http import Http404
 from django.utils.translation import gettext_lazy as _
 from rest_framework import status, serializers
 from rest_framework.exceptions import PermissionDenied, ValidationError
 from rest_framework.generics import (
     ListAPIView,
-    CreateAPIView,
     RetrieveAPIView,
     get_object_or_404,
 )
@@ -17,6 +15,7 @@
 from payments.api.v2 import filters
 from payments.api.v2.serializers import PaymentSerializer
 from payments.api.v2.serializers.payable_detail import PayableSerializer
+from payments.api.v2.serializers.payment_user import PaymentUserSerializer
 from payments.exceptions import PaymentError
 from payments.models import Payment, PaymentUser
 from thaliawebsite.api.v2.permissions import IsAuthenticatedOrTokenHasScopeForMethod
@@ -127,3 +126,18 @@ def patch(self, request, *args, **kwargs):
             PayableSerializer(payable, context=self.get_serializer_context()).data,
             status=status.HTTP_201_CREATED,
         )
+
+
+class PaymentUserCurrentView(RetrieveAPIView):
+    """Returns details of the authenticated member."""
+
+    queryset = PaymentUser
+    serializer_class = PaymentUserSerializer
+    permission_classes = [
+        IsAuthenticatedOrTokenHasScopeForMethod,
+    ]
+
+    required_scopes_per_method = {"GET": ["payments:read"]}
+
+    def get_object(self):
+        return get_object_or_404(PaymentUser, pk=self.request.user.pk)


Test patch: