VAT number validation in settings

This commit is contained in:
PCoder 2019-12-26 13:31:15 +05:30
parent 7d9ab322c9
commit 99e70d95c4
3 changed files with 54 additions and 13 deletions

View file

@ -131,15 +131,30 @@ def check_otp(name, realm, token):
return response.status_code return response.status_code
def validate_vat_number(stripe_customer_id, billing_address_id): def validate_vat_number(stripe_customer_id, billing_address_id,
try: is_user_ba=False):
billing_address = BillingAddress.objects.get(id=billing_address_id) if is_user_ba:
except BillingAddress.DoesNotExist as dne: try:
billing_address = None billing_address = UserBillingAddress.objects.get(
logger.debug("BillingAddress does not exist for %s" % billing_address_id) id=billing_address_id)
except BillingAddress.MultipleObjectsReturned as mor: except UserBillingAddress.DoesNotExist as dne:
logger.debug("Multiple BillingAddress exist for %s" % billing_address_id) billing_address = None
billing_address = BillingAddress.objects.filter(billing_address_id).order_by('-id').first() logger.debug(
"UserBillingAddress does not exist for %s" % billing_address_id)
except UserBillingAddress.MultipleObjectsReturned as mor:
logger.debug(
"Multiple UserBillingAddress exist for %s" % billing_address_id)
billing_address = UserBillingAddress.objects.filter(
id=billing_address_id).order_by('-id').first()
else:
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(id=billing_address_id).order_by('-id').first()
if billing_address is not None: if billing_address is not None:
if billing_address.vat_number_validated_on: if billing_address.vat_number_validated_on:
return { return {

View file

@ -26,6 +26,13 @@
{% for field in form %} {% for field in form %}
{% bootstrap_field field show_label=False type='fields' bound_css_class='' %} {% bootstrap_field field show_label=False type='fields' bound_css_class='' %}
{% endfor %} {% endfor %}
{% if form.instance.vat_number %}
{% if form.instance.vat_validation_status == "verified" %}
<span class="fa fa-fw fa-check-circle" aria-hidden="true" title='{% trans "Your VAT number has been verified" %}'></span>
{% else %}
<span class="fa fa-fw fa-info-circle" aria-hidden="true" title='{% trans "Your VAT number is under validation. VAT will be adjusted, once the validation is complete." %}'></span>
{% endif %}
{% endif %}
<div class="form-group text-right"> <div class="form-group text-right">
<button type="submit" class="btn btn-vm-contact btn-wide" name="billing-form">{% trans "UPDATE" %}</button> <button type="submit" class="btn btn-vm-contact btn-wide" name="billing-form">{% trans "UPDATE" %}</button>
</div> </div>

View file

@ -37,7 +37,9 @@ from stored_messages.settings import stored_messages_settings
from datacenterlight.cms_models import DCLCalculatorPluginModel from datacenterlight.cms_models import DCLCalculatorPluginModel
from datacenterlight.models import VMTemplate, VMPricing from datacenterlight.models import VMTemplate, VMPricing
from datacenterlight.utils import create_vm, get_cms_integration, check_otp from datacenterlight.utils import (
create_vm, get_cms_integration, check_otp, validate_vat_number
)
from hosting.models import UserCardDetail from hosting.models import UserCardDetail
from membership.models import CustomUser, StripeCustomer from membership.models import CustomUser, StripeCustomer
from opennebula_api.models import OpenNebulaManager from opennebula_api.models import OpenNebulaManager
@ -626,9 +628,26 @@ class SettingsView(LoginRequiredMixin, FormView):
billing_address_user_form = UserBillingAddressForm( billing_address_user_form = UserBillingAddressForm(
instance=self.request.user.billing_addresses.first(), instance=self.request.user.billing_addresses.first(),
data=billing_address_data) data=billing_address_data)
billing_address_user_form.save() billing_address = billing_address_user_form.save()
msg = _("Billing address updated successfully") vat_number = billing_address_user_form.cleaned_data.get(
messages.add_message(request, messages.SUCCESS, msg) 'vat_number').strip()
if vat_number:
validate_result = validate_vat_number(
stripe_customer_id=request.user.stripecustomer.stripe_id,
billing_address_id=billing_address.id,
is_user_ba=True
)
if 'error' in validate_result and validate_result['error']:
messages.add_message(
request, messages.ERROR, validate_result["error"],
extra_tags='vat_error'
)
else:
msg = _("Billing address updated successfully")
messages.add_message(request, messages.SUCCESS, msg)
else:
msg = _("Billing address updated successfully")
messages.add_message(request, messages.SUCCESS, msg)
else: else:
token = form.cleaned_data.get('token') token = form.cleaned_data.get('token')
stripe_utils = StripeUtils() stripe_utils = StripeUtils()