Save tax id in billing address even if no StripeCustomer

This commit is contained in:
PCoder 2019-12-26 11:30:01 +05:30
parent 74d1bbb6d3
commit 9310f72cf9
2 changed files with 27 additions and 15 deletions

View file

@ -167,7 +167,7 @@ def validate_vat_number(stripe_customer_id, billing_address_id):
} }
else: else:
tax_id_obj = create_tax_id( tax_id_obj = create_tax_id(
stripe_customer_id, billing_address.vat_number, stripe_customer_id, billing_address_id,
"ch_vat" if billing_address.country.lower() == "ch" else "eu_vat") "ch_vat" if billing_address.country.lower() == "ch" else "eu_vat")
else: else:
return { return {
@ -184,11 +184,19 @@ def validate_vat_number(stripe_customer_id, billing_address_id):
} }
def create_tax_id(stripe_customer_id, vat_number, type): def create_tax_id(stripe_customer_id, billing_address_id, type):
try:
billing_address = BillingAddress.objects.get(id=billing_address_id)
except BillingAddress.DoesNotExist as dne:
billing_address = None
logger.debug("BillingAddress does not exist for %s" % billing_address_id)
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() stripe_utils = StripeUtils()
tax_id_response = stripe_utils.create_tax_id_for_user( tax_id_response = stripe_utils.create_tax_id_for_user(
stripe_customer_id, stripe_customer_id,
vat_number=vat_number, vat_number=billing_address.vat_number,
type=type type=type
) )
@ -197,16 +205,21 @@ def create_tax_id(stripe_customer_id, vat_number, type):
if not tax_id_obj: if not tax_id_obj:
return tax_id_response return tax_id_response
stripe_customer = StripeCustomer.objects.get(stripe_id=stripe_customer_id) try:
billing_address_set = set() stripe_customer = StripeCustomer.objects.get(stripe_id=stripe_customer_id)
for ho in stripe_customer.hostingorder_set.all(): billing_address_set = set()
if ho.billing_address.vat_number==vat_number: for ho in stripe_customer.hostingorder_set.all():
billing_address_set.add(ho.billing_address) if ho.billing_address.vat_number == billing_address.vat_number:
for b_address in billing_address_set: billing_address_set.add(ho.billing_address)
b_address.stripe_tax_id = tax_id_obj.id for b_address in billing_address_set:
b_address.stripe_tax_id = tax_id_obj.id
ub_addresses = stripe_customer.user.billing_addresses.filter( ub_addresses = stripe_customer.user.billing_addresses.filter(
vat_number=vat_number) vat_number=billing_address.vat_number)
for ub_address in ub_addresses: for ub_address in ub_addresses:
ub_address.stripe_tax_id = tax_id_obj.id ub_address.stripe_tax_id = tax_id_obj.id
except StripeCustomer.DoesNotExist as dne:
logger.debug("StripeCustomer %s does not exist" % stripe_customer_id)
billing_address.stripe_tax_id = tax_id_obj.id
billing_address.save()
return tax_id_obj return tax_id_obj

View file

@ -524,7 +524,6 @@ class PaymentOrderView(FormView):
email=user_email, email=user_email,
token=token, token=token,
customer_name=user_name) customer_name=user_name)
customer.save()
billing_address = address_form.save() billing_address = address_form.save()
request.session["billing_address_id"] = billing_address.id request.session["billing_address_id"] = billing_address.id