From 1cb1de4876953b3db7d3a9c0d29330514c753dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Fri, 28 Feb 2020 11:10:31 +0100 Subject: [PATCH] Add (broken) charge method to payment method endpoint --- uncloud/uncloud_pay/serializers.py | 2 +- uncloud/uncloud_pay/views.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py index a4a1f1b..3b8cc47 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -21,7 +21,7 @@ class PaymentSerializer(serializers.ModelSerializer): class PaymentMethodSerializer(serializers.ModelSerializer): class Meta: model = PaymentMethod - fields = ['owner', 'primary', 'source', 'description'] + fields = '__all__' class ProductSerializer(serializers.Serializer): vms = VMProductSerializer(many=True, read_only=True) diff --git a/uncloud/uncloud_pay/views.py b/uncloud/uncloud_pay/views.py index ea3cca7..9ed57c8 100644 --- a/uncloud/uncloud_pay/views.py +++ b/uncloud/uncloud_pay/views.py @@ -71,6 +71,20 @@ class PaymentMethodViewSet(viewsets.ModelViewSet): headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) + # TODO: find a way to customize serializer for actions. + # drf-action-serializer module seems to do that. + @action(detail=True, methods=['post']) + def charge(self, request, pk=None): + payment_method = self.get_object() + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + amount = serializer.data['amount'] + if payment_method.charge(amount): + return Response({'charged', amount}) + else: + return Response(status=status.HTTP_500_INTERNAL_ERROR) + + ### # Admin views.