From 4a40438edc6209795113271509267836e0a96faa Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 25 Dec 2019 23:10:18 +0530 Subject: [PATCH] Also handle ch_vat --- datacenterlight/utils.py | 13 +++++++++---- datacenterlight/views.py | 3 ++- utils/stripe_utils.py | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/datacenterlight/utils.py b/datacenterlight/utils.py index 7d6494f6..096f835c 100644 --- a/datacenterlight/utils.py +++ b/datacenterlight/utils.py @@ -131,7 +131,7 @@ def check_otp(name, realm, token): return response.status_code -def validate_vat_number(stripe_customer_id, vat_number): +def validate_vat_number(stripe_customer_id, vat_number, country): try: billing_address = BillingAddress.objects.get(vat_number=vat_number) except BillingAddress.DoesNotExist as dne: @@ -163,9 +163,13 @@ def validate_vat_number(stripe_customer_id, vat_number): "validated_on": "" } else: - tax_id_obj = create_tax_id(stripe_customer_id, vat_number) + tax_id_obj = create_tax_id( + stripe_customer_id, vat_number, + "ch_vat" if country.lower() == "ch" else "eu_vat") else: - tax_id_obj = create_tax_id(stripe_customer_id, vat_number) + tax_id_obj = create_tax_id( + stripe_customer_id, vat_number, + "ch_vat" if country.lower() == "ch" else "eu_vat") if 'response_object' in tax_id_obj: return tax_id_obj @@ -176,11 +180,12 @@ def validate_vat_number(stripe_customer_id, vat_number): } -def create_tax_id(stripe_customer_id, vat_number): +def create_tax_id(stripe_customer_id, vat_number, type): stripe_utils = StripeUtils() tax_id_response = stripe_utils.create_tax_id_for_user( stripe_customer_id, vat_number=vat_number, + type=type ) tax_id_obj = tax_id_response.get('response_object') diff --git a/datacenterlight/views.py b/datacenterlight/views.py index ccec2142..f3f07e43 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -546,7 +546,8 @@ class PaymentOrderView(FormView): if vat_number: validate_result = validate_vat_number( stripe_customer_id=request.session['customer'], - vat_number=address_form.cleaned_data.get('vat_number') + vat_number=address_form.cleaned_data.get('vat_number'), + country=address_form.cleaned_data.get("country").strip() ) if 'response_object' in validate_result: diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index 90437056..2cadefd2 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -436,10 +436,10 @@ class StripeUtils(object): subscription.save() @handleStripeError - def create_tax_id_for_user(self, stripe_customer_id, vat_number): + def create_tax_id_for_user(self, stripe_customer_id, vat_number, type="eu_vat"): tax_id_obj = stripe.Customer.create_tax_id( stripe_customer_id, - type="eu_vat", + type=type, value=vat_number, ) return tax_id_obj