Add (broken) charge method to payment method endpoint
This commit is contained in:
parent
c0512e54b0
commit
1cb1de4876
2 changed files with 15 additions and 1 deletions
|
@ -21,7 +21,7 @@ class PaymentSerializer(serializers.ModelSerializer):
|
||||||
class PaymentMethodSerializer(serializers.ModelSerializer):
|
class PaymentMethodSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = PaymentMethod
|
model = PaymentMethod
|
||||||
fields = ['owner', 'primary', 'source', 'description']
|
fields = '__all__'
|
||||||
|
|
||||||
class ProductSerializer(serializers.Serializer):
|
class ProductSerializer(serializers.Serializer):
|
||||||
vms = VMProductSerializer(many=True, read_only=True)
|
vms = VMProductSerializer(many=True, read_only=True)
|
||||||
|
|
|
@ -71,6 +71,20 @@ class PaymentMethodViewSet(viewsets.ModelViewSet):
|
||||||
headers = self.get_success_headers(serializer.data)
|
headers = self.get_success_headers(serializer.data)
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
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.
|
# Admin views.
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue