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
1 changed files with 15 additions and 6 deletions

View File

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