diff --git a/datacenterlight/utils.py b/datacenterlight/utils.py index 087db3a3..de37f95c 100644 --- a/datacenterlight/utils.py +++ b/datacenterlight/utils.py @@ -255,18 +255,26 @@ def create_tax_id(stripe_customer_id, billing_address_id, type, except BillingAddress.MultipleObjectsReturned as mor: logger.debug("Multiple BillingAddress exist for %s" % billing_address_id) billing_address = BillingAddress.objects.filter(billing_address_id).order_by('-id').first() - stripe_utils = StripeUtils() - tax_id_response = stripe_utils.get_or_create_tax_id_for_user( - stripe_customer_id, - vat_number=billing_address.vat_number, - type=type - ) - tax_id_obj = tax_id_response.get('response_object') + tax_id_obj = None + if billing_address: + stripe_utils = StripeUtils() + tax_id_response = stripe_utils.get_or_create_tax_id_for_user( + stripe_customer_id, + vat_number=billing_address.vat_number, + type=type, + country=billing_address.country + ) + + tax_id_obj = tax_id_response.get('response_object') if not tax_id_obj: logger.debug("Received none in tax_id_obj") - return tax_id_response + return { + 'paid': False, + 'response_object': None, + 'error': "No such address found" + } try: stripe_customer = StripeCustomer.objects.get(stripe_id=stripe_customer_id) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index 50de6979..f8d4c46d 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -436,22 +436,28 @@ class StripeUtils(object): subscription.save() @handleStripeError - def get_or_create_tax_id_for_user(self, stripe_customer_id, vat_number, type="eu_vat"): + def get_or_create_tax_id_for_user(self, stripe_customer_id, vat_number, + type="eu_vat", country=""): tax_ids_list = stripe.Customer.list_tax_ids( stripe_customer_id, limit=100, ) for tax_id_obj in tax_ids_list.data: - if self.compare_vat_numbers(tax_id_obj.value, vat_number): + if (self.compare_vat_numbers(tax_id_obj.value, vat_number) and + tax_id_obj.country.lower().strip() == + country.lower().strip()): logger.debug("tax id obj exists already") return tax_id_obj else: - logger.debug("{val1} is not equal to {val2}".format( - val1=tax_id_obj.value, val2=vat_number - )) - logger.debug("tax id obj does not exist for {val}. Creating a new one".format( - val=vat_number - )) + logger.debug( + "{val1} is not equal to {val2} or {con1} not same as " + "{con2}".format(val1=tax_id_obj.value, val2=vat_number, + con1=tax_id_obj.country.lower(), + con2=country.lower().strip())) + logger.debug( + "tax id obj does not exist for {val}. Creating a new one".format( + val=vat_number + )) tax_id_obj = stripe.Customer.create_tax_id( stripe_customer_id, type=type,