Adjust hosting VM buy flow
This commit is contained in:
parent
a82c4d556c
commit
399f9ed6c9
2 changed files with 56 additions and 14 deletions
|
@ -42,7 +42,7 @@ from datacenterlight.models import VMTemplate, VMPricing
|
||||||
from datacenterlight.utils import (
|
from datacenterlight.utils import (
|
||||||
create_vm, get_cms_integration, check_otp, validate_vat_number
|
create_vm, get_cms_integration, check_otp, validate_vat_number
|
||||||
)
|
)
|
||||||
from hosting.models import UserCardDetail
|
from hosting.models import UserCardDetail, StripeTaxRate
|
||||||
from membership.models import CustomUser, StripeCustomer
|
from membership.models import CustomUser, StripeCustomer
|
||||||
from opennebula_api.models import OpenNebulaManager
|
from opennebula_api.models import OpenNebulaManager
|
||||||
from opennebula_api.serializers import (
|
from opennebula_api.serializers import (
|
||||||
|
@ -1135,7 +1135,8 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView, FormView):
|
||||||
cpu = specs.get('cpu')
|
cpu = specs.get('cpu')
|
||||||
memory = specs.get('memory')
|
memory = specs.get('memory')
|
||||||
disk_size = specs.get('disk_size')
|
disk_size = specs.get('disk_size')
|
||||||
amount_to_be_charged = specs.get('total_price')
|
amount_to_be_charged = specs.get('price')
|
||||||
|
discount = specs.get('discount')
|
||||||
plan_name = StripeUtils.get_stripe_plan_name(
|
plan_name = StripeUtils.get_stripe_plan_name(
|
||||||
cpu=cpu,
|
cpu=cpu,
|
||||||
memory=memory,
|
memory=memory,
|
||||||
|
@ -1154,10 +1155,39 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView, FormView):
|
||||||
amount=amount_to_be_charged,
|
amount=amount_to_be_charged,
|
||||||
name=plan_name,
|
name=plan_name,
|
||||||
stripe_plan_id=stripe_plan_id)
|
stripe_plan_id=stripe_plan_id)
|
||||||
|
# Create StripeTaxRate if applicable to the user
|
||||||
|
stripe_tax_rate = None
|
||||||
|
if specs["vat_percent"] > 0:
|
||||||
|
try:
|
||||||
|
stripe_tax_rate = StripeTaxRate.objects.get(
|
||||||
|
description="VAT for %s" % specs["vat_country"]
|
||||||
|
)
|
||||||
|
print("Stripe Tax Rate exists")
|
||||||
|
except StripeTaxRate.DoesNotExist as dne:
|
||||||
|
print("StripeTaxRate does not exist")
|
||||||
|
tax_rate_obj = stripe.TaxRate.create(
|
||||||
|
display_name="VAT",
|
||||||
|
description="VAT for %s" % specs["vat_country"],
|
||||||
|
jurisdiction=specs["vat_country"],
|
||||||
|
percentage=specs["vat_percent"] * 100,
|
||||||
|
inclusive=False,
|
||||||
|
)
|
||||||
|
stripe_tax_rate = StripeTaxRate.objects.create(
|
||||||
|
display_name=tax_rate_obj.display_name,
|
||||||
|
description=tax_rate_obj.description,
|
||||||
|
jurisdiction=tax_rate_obj.jurisdiction,
|
||||||
|
percentage=tax_rate_obj.percentage,
|
||||||
|
inclusive=False,
|
||||||
|
tax_rate_id=tax_rate_obj.id
|
||||||
|
)
|
||||||
|
logger.debug("Created StripeTaxRate %s" %
|
||||||
|
stripe_tax_rate.tax_rate_id)
|
||||||
subscription_result = stripe_utils.subscribe_customer_to_plan(
|
subscription_result = stripe_utils.subscribe_customer_to_plan(
|
||||||
stripe_api_cus_id,
|
stripe_api_cus_id,
|
||||||
[{"plan": stripe_plan.get(
|
[{"plan": stripe_plan.get('response_object').stripe_plan_id}],
|
||||||
'response_object').stripe_plan_id}])
|
coupon='ipv6-discount-8chf' if 'name' in discount and 'ipv6' in discount['name'].lower() else "",
|
||||||
|
tax_rate=[stripe_tax_rate.tax_rate_id] if stripe_tax_rate else [],
|
||||||
|
)
|
||||||
stripe_subscription_obj = subscription_result.get('response_object')
|
stripe_subscription_obj = subscription_result.get('response_object')
|
||||||
# Check if the subscription was approved and is active
|
# Check if the subscription was approved and is active
|
||||||
if (stripe_subscription_obj is None or
|
if (stripe_subscription_obj is None or
|
||||||
|
|
|
@ -297,7 +297,8 @@ class StripeUtils(object):
|
||||||
return return_value
|
return return_value
|
||||||
|
|
||||||
@handleStripeError
|
@handleStripeError
|
||||||
def subscribe_customer_to_plan(self, customer, plans, trial_end=None):
|
def subscribe_customer_to_plan(self, customer, plans, trial_end=None,
|
||||||
|
coupon="", tax_rates=list()):
|
||||||
"""
|
"""
|
||||||
Subscribes the given customer to the list of given plans
|
Subscribes the given customer to the list of given plans
|
||||||
|
|
||||||
|
@ -316,7 +317,9 @@ class StripeUtils(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
subscription_result = self.stripe.Subscription.create(
|
subscription_result = self.stripe.Subscription.create(
|
||||||
customer=customer, items=plans, trial_end=trial_end
|
customer=customer, items=plans, trial_end=trial_end,
|
||||||
|
coupon=coupon,
|
||||||
|
default_tax_rates=tax_rates,
|
||||||
)
|
)
|
||||||
return subscription_result
|
return subscription_result
|
||||||
|
|
||||||
|
@ -410,11 +413,20 @@ class StripeUtils(object):
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_stripe_plan_name(cpu, memory, disk_size, price):
|
def get_stripe_plan_name(cpu, memory, disk_size, price, excl_vat=True):
|
||||||
"""
|
"""
|
||||||
Returns the Stripe plan name
|
Returns the Stripe plan name
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
if excl_vat:
|
||||||
|
return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD, " \
|
||||||
|
"{price} CHF Excl. VAT".format(
|
||||||
|
cpu=cpu,
|
||||||
|
memory=memory,
|
||||||
|
disk_size=disk_size,
|
||||||
|
price=round(price, 2)
|
||||||
|
)
|
||||||
|
else:
|
||||||
return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD, " \
|
return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD, " \
|
||||||
"{price} CHF".format(
|
"{price} CHF".format(
|
||||||
cpu=cpu,
|
cpu=cpu,
|
||||||
|
|
Loading…
Reference in a new issue