From 785f99311d7d54ad50d5670d6ce676fb414b5753 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 25 Dec 2019 21:02:43 +0530 Subject: [PATCH] Handle invalid VAT number case --- datacenterlight/utils.py | 14 ++++++++++++-- datacenterlight/views.py | 10 ++++++++++ utils/stripe_utils.py | 9 +++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/datacenterlight/utils.py b/datacenterlight/utils.py index cdbe1913..58d9de61 100644 --- a/datacenterlight/utils.py +++ b/datacenterlight/utils.py @@ -12,6 +12,7 @@ from hosting.models import HostingOrder, HostingBill, OrderDetail from membership.models import StripeCustomer from utils.forms import UserBillingAddressForm from utils.models import BillingAddress, UserBillingAddress +from utils.stripe_utils import StripeUtils from .cms_models import CMSIntegration from .models import VMPricing, VMTemplate @@ -165,6 +166,9 @@ def validate_vat_number(stripe_customer_id, vat_number): else: tax_id_obj = create_tax_id(stripe_customer_id, vat_number) + if 'response_object' in tax_id_obj: + return tax_id_obj + return { "status": tax_id_obj.verification.status, "validated_on": datetime.datetime.now() if tax_id_obj.verification.status == "verified" else "" @@ -172,11 +176,17 @@ def validate_vat_number(stripe_customer_id, vat_number): def create_tax_id(stripe_customer_id, vat_number): - tax_id_obj = stripe.Customer.create_tax_id( + stripe_utils = StripeUtils() + tax_id_response = stripe_utils.create_tax_id_for_user( stripe_customer_id, - type="eu_vat", value=vat_number, ) + + tax_id_obj = tax_id_response.get('response_object') + + if not tax_id_obj: + return tax_id_response + b_addresses = BillingAddress.objects.filter( stripe_customer_id=stripe_customer_id, vat_number=vat_number diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 512989ac..36b8e505 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -547,6 +547,16 @@ class PaymentOrderView(FormView): vat_number=address_form.cleaned_data.get('vat_number') ) + if 'response_object' in validate_result: + address_form.add_error( + "__all__", validate_result["error"] + ) + return self.render_to_response( + self.get_context_data( + billing_address_form=address_form + ) + ) + request.session["vat_validation_status"] = validate_result["status"] request.session["vat_validated_on"] = validate_result["validated_on"] diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index e2bdb983..90437056 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -434,3 +434,12 @@ class StripeUtils(object): subscription = stripe.Subscription.retrieve(subscription_id) subscription.metadata = meta_data subscription.save() + + @handleStripeError + def create_tax_id_for_user(self, stripe_customer_id, vat_number): + tax_id_obj = stripe.Customer.create_tax_id( + stripe_customer_id, + type="eu_vat", + value=vat_number, + ) + return tax_id_obj