dynamicweb/datacenterlight/utils.py

210 lines
7.1 KiB
Python
Raw Normal View History

2019-12-25 14:41:15 +00:00
import datetime
2018-08-21 21:24:40 +00:00
import logging
2019-05-13 01:34:54 +00:00
import pyotp
import requests
2019-12-25 14:41:15 +00:00
import stripe
from django.conf import settings
from django.contrib.sites.models import Site
2018-04-20 14:55:24 +00:00
from datacenterlight.tasks import create_vm_task
from hosting.models import HostingOrder, HostingBill, OrderDetail
2018-04-20 14:55:24 +00:00
from membership.models import StripeCustomer
from utils.forms import UserBillingAddressForm
2019-12-25 14:41:15 +00:00
from utils.models import BillingAddress, UserBillingAddress
2019-12-25 15:32:43 +00:00
from utils.stripe_utils import StripeUtils
from .cms_models import CMSIntegration
from .models import VMPricing, VMTemplate
2018-08-21 21:24:40 +00:00
logger = logging.getLogger(__name__)
2019-12-25 14:41:15 +00:00
def get_cms_integration(name):
current_site = Site.objects.get_current()
try:
cms_integration = CMSIntegration.objects.get(
name=name, domain=current_site
)
except CMSIntegration.DoesNotExist:
cms_integration = CMSIntegration.objects.get(name=name, domain=None)
return cms_integration
2018-04-20 14:55:24 +00:00
def create_vm(billing_address_data, stripe_customer_id, specs,
stripe_subscription_obj, card_details_dict, request,
vm_template_id, template, user):
billing_address = BillingAddress(
cardholder_name=billing_address_data['cardholder_name'],
street_address=billing_address_data['street_address'],
city=billing_address_data['city'],
postal_code=billing_address_data['postal_code'],
country=billing_address_data['country'],
vat_number=billing_address_data['vat_number'],
2018-04-20 14:55:24 +00:00
)
billing_address.save()
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first()
2018-04-21 15:31:42 +00:00
vm_pricing = (
VMPricing.get_vm_pricing_by_name(name=specs['pricing_name'])
if 'pricing_name' in specs else
VMPricing.get_default_pricing()
)
final_price = (
specs.get('total_price')
if 'total_price' in specs
else specs.get('price')
)
2018-04-20 14:55:24 +00:00
# Create a Hosting Order with vm_id = 0, we shall set it later in
# celery task once the VM instance is up and running
order = HostingOrder.create(
2018-04-21 15:31:42 +00:00
price=final_price,
2018-04-20 14:55:24 +00:00
customer=customer,
2018-04-21 15:31:42 +00:00
billing_address=billing_address,
vm_pricing=vm_pricing
2018-04-20 14:55:24 +00:00
)
order_detail_obj, obj_created = OrderDetail.objects.get_or_create(
vm_template=VMTemplate.objects.get(
opennebula_vm_template_id=vm_template_id
),
cores=specs['cpu'], memory=specs['memory'], ssd_size=specs['disk_size']
)
order.order_detail = order_detail_obj
order.save()
2018-04-20 14:55:24 +00:00
# Create a Hosting Bill
HostingBill.create(customer=customer, billing_address=billing_address)
# Create Billing Address for User if he does not have one
if not customer.user.billing_addresses.count():
billing_address_data.update({
'user': customer.user.id
})
billing_address_user_form = UserBillingAddressForm(
billing_address_data
)
billing_address_user_form.is_valid()
billing_address_user_form.save()
2018-06-27 10:24:53 +00:00
# Associate the given stripe subscription with the order
2018-04-20 14:55:24 +00:00
order.set_subscription_id(
stripe_subscription_obj.id, card_details_dict
)
2018-06-27 10:24:53 +00:00
# Set order status approved
2018-04-20 14:55:24 +00:00
order.set_approved()
create_vm_task.delay(vm_template_id, user, specs, template, order.id)
clear_all_session_vars(request)
def clear_all_session_vars(request):
if request.session is not None:
for session_var in ['specs', 'template', 'billing_address',
'billing_address_data', 'card_id',
2018-10-06 05:47:40 +00:00
'token', 'customer', 'generic_payment_type',
'generic_payment_details', 'product_id',
'order_confirm_url', 'new_user_hosting_key_id',
'vat_validation_status']:
if session_var in request.session:
2018-10-06 05:47:40 +00:00
del request.session[session_var]
2019-05-13 01:34:54 +00:00
def check_otp(name, realm, token):
data = {
"auth_name": settings.AUTH_NAME,
"auth_token": pyotp.TOTP(settings.AUTH_SEED).now(),
"auth_realm": settings.AUTH_REALM,
2019-05-13 01:34:54 +00:00
"name": name,
"realm": realm,
"token": token
}
response = requests.post(
"https://{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format(
OTP_SERVER=settings.OTP_SERVER,
OTP_VERIFY_ENDPOINT=settings.OTP_VERIFY_ENDPOINT
2019-05-13 01:34:54 +00:00
),
data=data
)
return response.status_code
2019-12-25 14:41:15 +00:00
2019-12-25 17:40:18 +00:00
def validate_vat_number(stripe_customer_id, vat_number, country):
2019-12-25 14:41:15 +00:00
try:
billing_address = BillingAddress.objects.get(vat_number=vat_number)
except BillingAddress.DoesNotExist as dne:
billing_address = None
logger.debug("BillingAddress does not exist for %s" % vat_number)
except BillingAddress.MultipleObjectsReturned as mor:
logger.debug("Multiple BillingAddress exist for %s" % vat_number)
2019-12-25 17:05:41 +00:00
billing_address = BillingAddress.objects.filter(vat_number=vat_number).order_by('-id').first()
2019-12-25 14:41:15 +00:00
if billing_address is not None:
if billing_address.vat_number_validated_on:
return {
"validated_on": billing_address.vat_number_validated_on,
"status": "verified"
}
else:
if billing_address.stripe_tax_id:
tax_id_obj = stripe.Customer.retrieve_tax_id(
stripe_customer_id,
billing_address.stripe_tax_id,
)
if tax_id_obj.verification.status == "verified":
return {
"status": "verified",
"validated_on": billing_address.vat_number_validated_on
}
else:
return {
"status": tax_id_obj.verification.status,
"validated_on": ""
}
else:
2019-12-25 17:40:18 +00:00
tax_id_obj = create_tax_id(
stripe_customer_id, vat_number,
"ch_vat" if country.lower() == "ch" else "eu_vat")
2019-12-25 14:41:15 +00:00
else:
2019-12-25 17:40:18 +00:00
tax_id_obj = create_tax_id(
stripe_customer_id, vat_number,
"ch_vat" if country.lower() == "ch" else "eu_vat")
2019-12-25 14:41:15 +00:00
2019-12-25 15:32:43 +00:00
if 'response_object' in tax_id_obj:
return tax_id_obj
2019-12-25 14:41:15 +00:00
return {
"status": tax_id_obj.verification.status,
"validated_on": datetime.datetime.now() if tax_id_obj.verification.status == "verified" else ""
}
2019-12-25 17:40:18 +00:00
def create_tax_id(stripe_customer_id, vat_number, type):
2019-12-25 15:32:43 +00:00
stripe_utils = StripeUtils()
tax_id_response = stripe_utils.create_tax_id_for_user(
2019-12-25 14:41:15 +00:00
stripe_customer_id,
2019-12-25 16:52:08 +00:00
vat_number=vat_number,
2019-12-25 17:40:18 +00:00
type=type
2019-12-25 14:41:15 +00:00
)
2019-12-25 15:32:43 +00:00
tax_id_obj = tax_id_response.get('response_object')
if not tax_id_obj:
return tax_id_response
2019-12-25 14:41:15 +00:00
b_addresses = BillingAddress.objects.filter(
stripe_customer_id=stripe_customer_id,
vat_number=vat_number
)
for b_address in b_addresses:
b_address.stripe_tax_id = tax_id_obj.id
ub_addresses = UserBillingAddress.objects.filter(
stripe_customer_id=stripe_customer_id,
vat_number=vat_number
)
for ub_address in ub_addresses:
ub_address.stripe_tax_id = tax_id_obj.id
return tax_id_obj