Minimal VAT validation on billing address registration
This commit is contained in:
parent
c6ca94800e
commit
3fa1d5753e
3 changed files with 29 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue