Compare country for creating new tax id

This commit is contained in:
PCoder 2019-12-31 18:22:57 +05:30
parent 9078e46196
commit 2378410f2d
2 changed files with 30 additions and 16 deletions

View file

@ -255,18 +255,26 @@ def create_tax_id(stripe_customer_id, billing_address_id, type,
except BillingAddress.MultipleObjectsReturned as mor: except BillingAddress.MultipleObjectsReturned as mor:
logger.debug("Multiple BillingAddress exist for %s" % billing_address_id) logger.debug("Multiple BillingAddress exist for %s" % billing_address_id)
billing_address = BillingAddress.objects.filter(billing_address_id).order_by('-id').first() 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: if not tax_id_obj:
logger.debug("Received none in 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: try:
stripe_customer = StripeCustomer.objects.get(stripe_id=stripe_customer_id) stripe_customer = StripeCustomer.objects.get(stripe_id=stripe_customer_id)

View file

@ -436,22 +436,28 @@ class StripeUtils(object):
subscription.save() subscription.save()
@handleStripeError @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( tax_ids_list = stripe.Customer.list_tax_ids(
stripe_customer_id, stripe_customer_id,
limit=100, limit=100,
) )
for tax_id_obj in tax_ids_list.data: 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") logger.debug("tax id obj exists already")
return tax_id_obj return tax_id_obj
else: else:
logger.debug("{val1} is not equal to {val2}".format( logger.debug(
val1=tax_id_obj.value, val2=vat_number "{val1} is not equal to {val2} or {con1} not same as "
)) "{con2}".format(val1=tax_id_obj.value, val2=vat_number,
logger.debug("tax id obj does not exist for {val}. Creating a new one".format( con1=tax_id_obj.country.lower(),
val=vat_number 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( tax_id_obj = stripe.Customer.create_tax_id(
stripe_customer_id, stripe_customer_id,
type=type, type=type,