Improve stripe_customer_id

This commit is contained in:
PCoder 2019-12-26 18:06:22 +05:30
parent 5ab0bf6993
commit 6d0a7f7049
2 changed files with 15 additions and 10 deletions

View file

@ -226,7 +226,7 @@ def create_tax_id(stripe_customer_id, billing_address_id, type,
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.create_tax_id_for_user(
tax_id_response = stripe_utils.get_or_create_tax_id_for_user(
stripe_customer_id,
vat_number=billing_address.vat_number,
type=type

View file

@ -436,14 +436,19 @@ class StripeUtils(object):
subscription.save()
@handleStripeError
def create_tax_id_for_user(self, stripe_customer_id, vat_number, type="eu_vat"):
tax_id_obj = stripe.Customer.retrieve(
stripe_customer_id,type=type, value=vat_number
def get_or_create_tax_id_for_user(self, stripe_customer_id, vat_number, type="eu_vat"):
tax_ids_list = stripe.Customer.list_tax_ids(
stripe_customer_id,
limit=100,
)
for tax_id_obj in tax_ids_list.data:
if tax_id_obj.value == vat_number:
logger.debug("tax id obj exists already")
return tax_id_obj
logger.debug("tax id obj does not exist. Creating a new one")
tax_id_obj = stripe.Customer.create_tax_id(
stripe_customer_id,
type=type,
value=vat_number,
)
if tax_id_obj["response_object"]["error"]:
tax_id_obj = stripe.Customer.create_tax_id(
stripe_customer_id,
type=type,
value=vat_number,
)
return tax_id_obj