Add (broken) charge method to payment method endpoint

This commit is contained in:
fnux 2020-02-28 11:10:31 +01:00
parent c0512e54b0
commit 1cb1de4876
2 changed files with 15 additions and 1 deletions

View file

@ -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)

View file

@ -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.