Catch any exception from VIES VAT check

This commit is contained in:
fnux 2020-04-18 11:51:13 +02:00
parent b3afad5d5d
commit f61b91dab2

View file

@ -11,6 +11,7 @@ from vat_validator import validate_vat, vies
from vat_validator.countries import EU_COUNTRY_CODES from vat_validator.countries import EU_COUNTRY_CODES
import json import json
import logging
from .models import * from .models import *
from .serializers import * from .serializers import *
@ -18,6 +19,8 @@ from datetime import datetime
from vat_validator import sanitize_vat from vat_validator import sanitize_vat
import uncloud_pay.stripe as uncloud_stripe import uncloud_pay.stripe as uncloud_stripe
logger = logging.getLogger(__name__)
### ###
# Payments and Payment Methods. # Payments and Payment Methods.
@ -221,13 +224,19 @@ class BillingAddressViewSet(mixins.CreateModelMixin,
{'error': 'Malformed VAT number.'}, {'error': 'Malformed VAT number.'},
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
elif country in EU_COUNTRY_CODES: elif country in EU_COUNTRY_CODES:
# FIXME: make a synchroneous call to a third patry API here is # XXX: make a synchroneous call to a third patry API here might not be a good idea..
# not a good idea... try:
vies_state = vies.check_vat(country, vat_number) vies_state = vies.check_vat(country, vat_number)
if not vies_state.valid: if not vies_state.valid:
return Response( return Response(
{'error': 'European VAT number does not exist in VIES.'}, {'error': 'European VAT number does not exist in VIES.'},
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
logger.warning(e)
return Response(
{'error': 'Could not validate EU VAT number against VIES. Try again later..'},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
serializer.save(owner=request.user) serializer.save(owner=request.user)
return Response(serializer.data) return Response(serializer.data)