From 3fa1d5753ef030d6e89565bb4a05472863a49010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Wed, 15 Apr 2020 16:01:31 +0200 Subject: [PATCH] Minimal VAT validation on billing address registration --- uncloud_django_based/uncloud/requirements.txt | 4 ++++ .../uncloud/uncloud_pay/serializers.py | 7 +----- .../uncloud/uncloud_pay/views.py | 24 +++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/uncloud_django_based/uncloud/requirements.txt b/uncloud_django_based/uncloud/requirements.txt index 90c9882..a7fc9f2 100644 --- a/uncloud_django_based/uncloud/requirements.txt +++ b/uncloud_django_based/uncloud/requirements.txt @@ -18,3 +18,7 @@ django-hardcopy # schema support pyyaml uritemplate + +# Comprehensive interface to validate VAT numbers, making use of the VIES +# service for European countries. +vat-validator diff --git a/uncloud_django_based/uncloud/uncloud_pay/serializers.py b/uncloud_django_based/uncloud/uncloud_pay/serializers.py index 664e19b..5579b14 100644 --- a/uncloud_django_based/uncloud/uncloud_pay/serializers.py +++ b/uncloud_django_based/uncloud/uncloud_pay/serializers.py @@ -56,15 +56,10 @@ class BillRecordSerializer(serializers.Serializer): order = serializers.HyperlinkedRelatedField( view_name='order-detail', read_only=True) - description = serializers.CharField() - recurring_period = serializers.CharField() - recurring_price = serializers.DecimalField(max_digits=10, decimal_places=2) - recurring_count = serializers.DecimalField(max_digits=10, decimal_places=2) - one_time_price = serializers.DecimalField(max_digits=10, decimal_places=2) - amount = serializers.DecimalField(max_digits=10, decimal_places=2) class BillSerializer(serializers.ModelSerializer): records = BillRecordSerializer(many=True, read_only=True) + class Meta: model = Bill fields = ['reference', 'owner', 'total', 'due_date', 'creation_date', diff --git a/uncloud_django_based/uncloud/uncloud_pay/views.py b/uncloud_django_based/uncloud/uncloud_pay/views.py index 36a291a..5bd1ae6 100644 --- a/uncloud_django_based/uncloud/uncloud_pay/views.py +++ b/uncloud_django_based/uncloud/uncloud_pay/views.py @@ -7,12 +7,15 @@ from rest_framework.response import Response from rest_framework.decorators import action from rest_framework.reverse import reverse from rest_framework.decorators import renderer_classes +from vat_validator import validate_vat, vies +from vat_validator.countries import EU_COUNTRY_CODES import json from .models import * from .serializers import * from datetime import datetime +from vat_validator import sanitize_vat import uncloud_pay.stripe as uncloud_stripe ### @@ -206,7 +209,28 @@ class BillingAddressViewSet(mixins.CreateModelMixin, def create(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) + + # Validate VAT numbers. + country = serializer.validated_data["country"] + vat_number = serializer.validated_data["vat_number"] + + # We ignore empty VAT numbers. + if vat_number != "": + if not validate_vat(country, vat_number): + return Response( + {'error': 'Malformed VAT number.'}, + status=status.HTTP_400_BAD_REQUEST) + elif country in EU_COUNTRY_CODES: + # FIXME: make a synchroneous call to a third patry API here is + # not a good idea... + vies_state = vies.check_vat(country, vat_number) + if not vies_state.valid: + return Response( + {'error': 'European VAT number does not exist in VIES.'}, + status=status.HTTP_400_BAD_REQUEST) + serializer.save(owner=request.user) + return Response(serializer.data) ### # Old admin stuff.