2018-02-20 14:06:24 +00:00
|
|
|
import logging
|
2017-09-21 12:29:49 +00:00
|
|
|
|
2017-08-30 08:04:19 +00:00
|
|
|
from django import forms
|
|
|
|
from django.conf import settings
|
2016-12-20 23:05:20 +00:00
|
|
|
from django.contrib import messages
|
2017-09-08 20:40:25 +00:00
|
|
|
from django.contrib.auth import login, authenticate
|
2017-08-30 08:04:19 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-06-29 14:34:40 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2018-04-20 14:57:26 +00:00
|
|
|
from django.http import HttpResponseRedirect, JsonResponse
|
2017-08-30 08:04:19 +00:00
|
|
|
from django.shortcuts import render
|
2017-09-22 10:38:05 +00:00
|
|
|
from django.utils.translation import get_language, ugettext_lazy as _
|
2017-08-30 08:04:19 +00:00
|
|
|
from django.views.decorators.cache import cache_control
|
2018-02-23 13:35:45 +00:00
|
|
|
from django.views.generic import FormView, CreateView, DetailView
|
2017-08-30 08:04:19 +00:00
|
|
|
|
2017-09-05 18:42:13 +00:00
|
|
|
from hosting.forms import HostingUserLoginForm
|
2018-04-20 14:57:26 +00:00
|
|
|
from hosting.models import HostingOrder
|
2017-06-30 06:57:18 +00:00
|
|
|
from membership.models import CustomUser, StripeCustomer
|
2017-09-23 15:54:42 +00:00
|
|
|
from opennebula_api.serializers import VMTemplateSerializer
|
2018-04-20 14:57:26 +00:00
|
|
|
from utils.forms import BillingAddressForm, BillingAddressFormSignup
|
2018-04-15 23:30:02 +00:00
|
|
|
from utils.hosting_utils import get_vm_price_with_vat
|
2017-08-30 08:04:19 +00:00
|
|
|
from utils.stripe_utils import StripeUtils
|
2017-08-27 09:53:19 +00:00
|
|
|
from utils.tasks import send_plain_email_task
|
2018-02-20 14:06:24 +00:00
|
|
|
from .forms import ContactForm
|
2018-04-15 19:03:31 +00:00
|
|
|
from .models import VMTemplate, VMPricing
|
2018-04-20 14:55:24 +00:00
|
|
|
from .utils import get_cms_integration, create_vm
|
2017-04-23 21:31:05 +00:00
|
|
|
|
2017-09-21 12:29:49 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2017-07-03 21:53:37 +00:00
|
|
|
|
2017-09-22 09:45:21 +00:00
|
|
|
|
2017-08-27 07:49:05 +00:00
|
|
|
class ContactUsView(FormView):
|
|
|
|
template_name = "datacenterlight/contact_form.html"
|
|
|
|
form_class = ContactForm
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
|
|
|
|
|
|
|
def form_invalid(self, form):
|
|
|
|
if self.request.is_ajax():
|
|
|
|
return self.render_to_response(
|
|
|
|
self.get_context_data(contact_form=form))
|
|
|
|
else:
|
2018-03-27 13:49:26 +00:00
|
|
|
return render(
|
|
|
|
self.request, 'datacenterlight/index.html',
|
|
|
|
self.get_context_data(contact_form=form)
|
|
|
|
)
|
2017-08-27 07:49:05 +00:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.save()
|
2017-10-10 19:06:17 +00:00
|
|
|
from_emails = {
|
|
|
|
'glasfaser': 'glasfaser@ungleich.ch'
|
|
|
|
}
|
|
|
|
from_page = self.request.POST.get('from_page')
|
2017-08-27 09:53:19 +00:00
|
|
|
email_data = {
|
2017-08-30 08:04:19 +00:00
|
|
|
'subject': "{dcl_text} Message from {sender}".format(
|
|
|
|
dcl_text=settings.DCL_TEXT,
|
|
|
|
sender=form.cleaned_data.get('email')
|
|
|
|
),
|
2017-08-27 09:53:19 +00:00
|
|
|
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
|
2017-10-10 19:58:35 +00:00
|
|
|
'to': [from_emails.get(from_page, 'info@ungleich.ch')],
|
2017-08-27 09:53:19 +00:00
|
|
|
'body': "\n".join(
|
|
|
|
["%s=%s" % (k, v) for (k, v) in form.cleaned_data.items()]),
|
|
|
|
'reply_to': [form.cleaned_data.get('email')],
|
|
|
|
}
|
|
|
|
send_plain_email_task.delay(email_data)
|
2017-08-27 07:49:05 +00:00
|
|
|
if self.request.is_ajax():
|
|
|
|
return self.render_to_response(
|
|
|
|
self.get_context_data(success=True, contact_form=form))
|
|
|
|
else:
|
2018-03-27 13:49:26 +00:00
|
|
|
return render(
|
|
|
|
self.request, 'datacenterlight/index.html',
|
|
|
|
self.get_context_data(success=True, contact_form=form)
|
|
|
|
)
|
2017-04-23 21:31:05 +00:00
|
|
|
|
2017-07-03 21:53:37 +00:00
|
|
|
|
2016-12-20 23:05:20 +00:00
|
|
|
class IndexView(CreateView):
|
|
|
|
template_name = "datacenterlight/index.html"
|
|
|
|
success_url = "/datacenterlight#requestform"
|
|
|
|
success_message = "Thank you, we will contact you as soon as possible"
|
2017-06-29 14:34:40 +00:00
|
|
|
|
2017-07-14 08:34:40 +00:00
|
|
|
def validate_cores(self, value):
|
|
|
|
if (value > 48) or (value < 1):
|
2017-07-31 18:31:19 +00:00
|
|
|
raise ValidationError(_('Invalid number of cores'))
|
2017-07-14 08:34:40 +00:00
|
|
|
|
|
|
|
def validate_memory(self, value):
|
2017-12-21 00:25:52 +00:00
|
|
|
if (value > 200) or (value < 1):
|
2017-07-31 18:31:19 +00:00
|
|
|
raise ValidationError(_('Invalid RAM size'))
|
2017-07-14 08:34:40 +00:00
|
|
|
|
|
|
|
def validate_storage(self, value):
|
|
|
|
if (value > 2000) or (value < 10):
|
2017-07-31 18:31:19 +00:00
|
|
|
raise ValidationError(_('Invalid storage size'))
|
2017-07-14 08:34:40 +00:00
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
2017-06-11 11:02:38 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2018-04-15 19:03:31 +00:00
|
|
|
for session_var in ['specs', 'user', 'billing_address_data',
|
|
|
|
'pricing_name']:
|
2017-07-01 12:32:10 +00:00
|
|
|
if session_var in request.session:
|
|
|
|
del request.session[session_var]
|
2018-03-21 22:00:43 +00:00
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:cms_index'))
|
2017-06-11 11:02:38 +00:00
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
cores = request.POST.get('cpu')
|
2017-07-14 08:34:40 +00:00
|
|
|
cores_field = forms.IntegerField(validators=[self.validate_cores])
|
2017-06-11 11:02:38 +00:00
|
|
|
memory = request.POST.get('ram')
|
2017-07-14 08:34:40 +00:00
|
|
|
memory_field = forms.IntegerField(validators=[self.validate_memory])
|
2017-06-11 11:02:38 +00:00
|
|
|
storage = request.POST.get('storage')
|
2017-07-14 08:34:40 +00:00
|
|
|
storage_field = forms.IntegerField(validators=[self.validate_storage])
|
2017-06-11 11:02:38 +00:00
|
|
|
template_id = int(request.POST.get('config'))
|
2018-04-15 19:03:31 +00:00
|
|
|
pricing_name = request.POST.get('pricing_name')
|
|
|
|
vm_pricing = VMPricing.get_vm_pricing_by_name(pricing_name)
|
|
|
|
|
2017-08-03 16:30:41 +00:00
|
|
|
template = VMTemplate.objects.filter(
|
2018-03-05 13:11:13 +00:00
|
|
|
opennebula_vm_template_id=template_id
|
|
|
|
).first()
|
2017-07-27 06:35:01 +00:00
|
|
|
template_data = VMTemplateSerializer(template).data
|
2018-03-05 13:11:13 +00:00
|
|
|
referer_url = request.META['HTTP_REFERER']
|
2017-06-29 14:34:40 +00:00
|
|
|
|
2018-04-15 19:03:31 +00:00
|
|
|
if vm_pricing is None:
|
|
|
|
vm_pricing_name_msg = _(
|
|
|
|
"Incorrect pricing name. Please contact support"
|
|
|
|
"{support_email}".format(
|
|
|
|
support_email=settings.DCL_SUPPORT_FROM_ADDRESS
|
|
|
|
)
|
|
|
|
)
|
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.ERROR, vm_pricing_name_msg,
|
|
|
|
extra_tags='pricing'
|
|
|
|
)
|
|
|
|
return HttpResponseRedirect(referer_url + "#order_form")
|
|
|
|
else:
|
|
|
|
vm_pricing_name = vm_pricing.name
|
|
|
|
|
2017-07-14 08:34:40 +00:00
|
|
|
try:
|
|
|
|
cores = cores_field.clean(cores)
|
|
|
|
except ValidationError as err:
|
2017-07-25 20:25:31 +00:00
|
|
|
msg = '{} : {}.'.format(cores, str(err))
|
2018-03-05 13:11:13 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.ERROR, msg, extra_tags='cores'
|
|
|
|
)
|
|
|
|
return HttpResponseRedirect(referer_url + "#order_form")
|
2017-07-14 08:34:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
memory = memory_field.clean(memory)
|
|
|
|
except ValidationError as err:
|
2017-07-25 20:25:31 +00:00
|
|
|
msg = '{} : {}.'.format(memory, str(err))
|
2018-03-05 13:11:13 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.ERROR, msg, extra_tags='memory'
|
|
|
|
)
|
|
|
|
return HttpResponseRedirect(referer_url + "#order_form")
|
2017-07-14 08:34:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
storage = storage_field.clean(storage)
|
|
|
|
except ValidationError as err:
|
2017-07-25 20:25:31 +00:00
|
|
|
msg = '{} : {}.'.format(storage, str(err))
|
2018-03-05 13:11:13 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.ERROR, msg, extra_tags='storage'
|
|
|
|
)
|
|
|
|
return HttpResponseRedirect(referer_url + "#order_form")
|
|
|
|
|
2018-05-07 00:41:44 +00:00
|
|
|
price, vat, vat_percent, discount = get_vm_price_with_vat(
|
2018-04-15 11:42:39 +00:00
|
|
|
cpu=cores,
|
|
|
|
memory=memory,
|
2018-04-16 01:26:51 +00:00
|
|
|
ssd_size=storage,
|
2018-04-15 11:42:39 +00:00
|
|
|
pricing_name=vm_pricing_name
|
2018-03-05 13:11:13 +00:00
|
|
|
)
|
2017-06-21 07:44:58 +00:00
|
|
|
specs = {
|
|
|
|
'cpu': cores,
|
2017-06-11 11:02:38 +00:00
|
|
|
'memory': memory,
|
2017-06-21 07:44:58 +00:00
|
|
|
'disk_size': storage,
|
2018-04-15 23:31:16 +00:00
|
|
|
'price': price,
|
2018-04-15 19:03:31 +00:00
|
|
|
'vat': vat,
|
2018-04-17 19:38:28 +00:00
|
|
|
'vat_percent': vat_percent,
|
2018-05-07 00:41:44 +00:00
|
|
|
'discount': discount,
|
2018-05-07 00:59:53 +00:00
|
|
|
'total_price': price + vat - discount['amount'],
|
2018-04-15 19:03:31 +00:00
|
|
|
'pricing_name': vm_pricing_name
|
2017-06-11 11:02:38 +00:00
|
|
|
}
|
2017-06-21 07:44:58 +00:00
|
|
|
request.session['specs'] = specs
|
|
|
|
request.session['template'] = template_data
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:payment'))
|
2016-12-20 23:05:20 +00:00
|
|
|
|
2016-12-21 06:24:04 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
success_url = reverse('datacenterlight:index')
|
|
|
|
success_url += "#requestform"
|
|
|
|
return success_url
|
|
|
|
|
2017-04-23 21:31:05 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(IndexView, self).get_context_data(**kwargs)
|
|
|
|
context.update({
|
2018-03-05 13:11:13 +00:00
|
|
|
'base_url': "{0}://{1}".format(
|
|
|
|
self.request.scheme, self.request.get_host()
|
|
|
|
),
|
2017-08-27 07:49:05 +00:00
|
|
|
'contact_form': ContactForm
|
2017-04-23 21:31:05 +00:00
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
2017-07-06 19:50:16 +00:00
|
|
|
|
2017-06-29 07:44:13 +00:00
|
|
|
class WhyDataCenterLightView(IndexView):
|
|
|
|
template_name = "datacenterlight/whydatacenterlight.html"
|
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
|
|
|
|
class PaymentOrderView(FormView):
|
2017-09-01 17:36:57 +00:00
|
|
|
template_name = 'datacenterlight/landing_payment.html'
|
2017-09-08 18:16:29 +00:00
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
if self.request.user.is_authenticated():
|
|
|
|
return BillingAddressForm
|
|
|
|
else:
|
|
|
|
return BillingAddressFormSignup
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PaymentOrderView, self).get_context_data(**kwargs)
|
2017-10-08 11:50:43 +00:00
|
|
|
if 'billing_address_data' in self.request.session:
|
|
|
|
billing_address_data = self.request.session['billing_address_data']
|
|
|
|
else:
|
|
|
|
billing_address_data = {}
|
2017-10-08 15:21:38 +00:00
|
|
|
|
|
|
|
if self.request.user.is_authenticated():
|
|
|
|
if billing_address_data:
|
|
|
|
billing_address_form = BillingAddressForm(
|
|
|
|
initial=billing_address_data
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
billing_address_form = BillingAddressForm(
|
2017-10-08 17:24:58 +00:00
|
|
|
instance=self.request.user.billing_addresses.first()
|
|
|
|
)
|
2017-10-08 18:21:17 +00:00
|
|
|
# Get user last order
|
|
|
|
last_hosting_order = HostingOrder.objects.filter(
|
|
|
|
customer__user=self.request.user
|
|
|
|
).last()
|
|
|
|
|
|
|
|
# If user has already an hosting order, get the credit card
|
|
|
|
# data from it
|
|
|
|
if last_hosting_order:
|
|
|
|
credit_card_data = last_hosting_order.get_cc_data()
|
|
|
|
if credit_card_data:
|
|
|
|
context['credit_card_data'] = credit_card_data
|
|
|
|
else:
|
|
|
|
context['credit_card_data'] = None
|
2017-10-08 15:29:10 +00:00
|
|
|
else:
|
|
|
|
billing_address_form = BillingAddressFormSignup(
|
|
|
|
initial=billing_address_data
|
|
|
|
)
|
2017-10-08 15:21:38 +00:00
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
context.update({
|
2017-06-30 22:59:11 +00:00
|
|
|
'stripe_key': settings.STRIPE_API_PUBLIC_KEY,
|
2017-09-05 18:42:13 +00:00
|
|
|
'site_url': reverse('datacenterlight:index'),
|
2017-10-08 11:50:43 +00:00
|
|
|
'login_form': HostingUserLoginForm(prefix='login_form'),
|
2018-03-27 13:49:26 +00:00
|
|
|
'billing_address_form': billing_address_form,
|
2018-04-15 19:03:31 +00:00
|
|
|
'cms_integration': get_cms_integration('default'),
|
|
|
|
'vm_pricing': VMPricing.get_vm_pricing_by_name(
|
|
|
|
self.request.session['specs']['pricing_name']
|
|
|
|
)
|
2017-06-21 07:44:58 +00:00
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
|
|
|
def get(self, request, *args, **kwargs):
|
2017-09-08 18:16:29 +00:00
|
|
|
if 'specs' not in request.session:
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
2017-06-21 07:44:58 +00:00
|
|
|
return self.render_to_response(self.get_context_data())
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2017-10-08 11:50:43 +00:00
|
|
|
if 'login_form' in request.POST:
|
2018-03-05 13:11:13 +00:00
|
|
|
login_form = HostingUserLoginForm(
|
|
|
|
data=request.POST, prefix='login_form'
|
|
|
|
)
|
2017-10-08 11:50:43 +00:00
|
|
|
if login_form.is_valid():
|
|
|
|
email = login_form.cleaned_data.get('email')
|
|
|
|
password = login_form.cleaned_data.get('password')
|
|
|
|
auth_user = authenticate(email=email, password=password)
|
|
|
|
if auth_user:
|
|
|
|
login(self.request, auth_user)
|
2017-10-12 20:50:54 +00:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse('datacenterlight:payment')
|
|
|
|
)
|
2017-10-08 11:50:43 +00:00
|
|
|
else:
|
|
|
|
context = self.get_context_data()
|
|
|
|
context['login_form'] = login_form
|
|
|
|
return self.render_to_response(context)
|
|
|
|
if request.user.is_authenticated():
|
|
|
|
address_form = BillingAddressForm(
|
|
|
|
data=request.POST,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
address_form = BillingAddressFormSignup(
|
|
|
|
data=request.POST,
|
|
|
|
)
|
|
|
|
if address_form.is_valid():
|
|
|
|
token = address_form.cleaned_data.get('token')
|
2017-09-08 18:35:13 +00:00
|
|
|
if request.user.is_authenticated():
|
2017-09-08 20:40:25 +00:00
|
|
|
this_user = {
|
2017-09-08 18:35:13 +00:00
|
|
|
'email': request.user.email,
|
|
|
|
'name': request.user.name
|
|
|
|
}
|
2017-09-21 01:11:09 +00:00
|
|
|
customer = StripeCustomer.get_or_create(
|
|
|
|
email=this_user.get('email'),
|
|
|
|
token=token)
|
2017-09-08 18:35:13 +00:00
|
|
|
else:
|
2017-10-08 11:50:43 +00:00
|
|
|
user_email = address_form.cleaned_data.get('email')
|
|
|
|
user_name = address_form.cleaned_data.get('name')
|
2017-09-08 20:40:25 +00:00
|
|
|
this_user = {
|
2017-09-28 12:46:56 +00:00
|
|
|
'email': user_email,
|
|
|
|
'name': user_name
|
2017-09-08 18:35:13 +00:00
|
|
|
}
|
2017-09-28 12:46:56 +00:00
|
|
|
try:
|
|
|
|
custom_user = CustomUser.objects.get(email=user_email)
|
|
|
|
customer = StripeCustomer.objects.filter(
|
|
|
|
user_id=custom_user.id).first()
|
|
|
|
if customer is None:
|
|
|
|
logger.debug(
|
|
|
|
("User {email} is already registered with us."
|
|
|
|
"But, StripeCustomer does not exist for {email}."
|
|
|
|
"Hence, creating a new StripeCustomer.").format(
|
|
|
|
email=user_email
|
|
|
|
)
|
|
|
|
)
|
|
|
|
customer = StripeCustomer.create_stripe_api_customer(
|
|
|
|
email=user_email,
|
|
|
|
token=token,
|
|
|
|
customer_name=user_name)
|
|
|
|
except CustomUser.DoesNotExist:
|
|
|
|
logger.debug(
|
|
|
|
("StripeCustomer does not exist for {email}."
|
|
|
|
"Hence, creating a new StripeCustomer.").format(
|
|
|
|
email=user_email
|
|
|
|
)
|
|
|
|
)
|
|
|
|
customer = StripeCustomer.create_stripe_api_customer(
|
|
|
|
email=user_email,
|
|
|
|
token=token,
|
|
|
|
customer_name=user_name)
|
|
|
|
|
2017-10-08 11:50:43 +00:00
|
|
|
request.session['billing_address_data'] = address_form.cleaned_data
|
2017-09-08 20:40:25 +00:00
|
|
|
request.session['user'] = this_user
|
2017-06-22 07:51:07 +00:00
|
|
|
# Get or create stripe customer
|
|
|
|
if not customer:
|
2017-10-08 11:50:43 +00:00
|
|
|
address_form.add_error(
|
|
|
|
"__all__", "Invalid credit card"
|
|
|
|
)
|
2017-08-30 08:04:19 +00:00
|
|
|
return self.render_to_response(
|
2017-10-08 11:50:43 +00:00
|
|
|
self.get_context_data(
|
|
|
|
billing_address_form=address_form
|
|
|
|
)
|
|
|
|
)
|
2017-06-29 07:38:24 +00:00
|
|
|
request.session['token'] = token
|
2017-09-28 13:56:09 +00:00
|
|
|
if type(customer) is StripeCustomer:
|
|
|
|
request.session['customer'] = customer.stripe_id
|
|
|
|
else:
|
|
|
|
request.session['customer'] = customer
|
2017-08-23 20:59:24 +00:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse('datacenterlight:order_confirmation'))
|
2017-06-21 07:44:58 +00:00
|
|
|
else:
|
2017-10-08 11:50:43 +00:00
|
|
|
context = self.get_context_data()
|
|
|
|
context['billing_address_form'] = address_form
|
|
|
|
return self.render_to_response(context)
|
2017-06-22 07:51:07 +00:00
|
|
|
|
2017-06-29 16:23:25 +00:00
|
|
|
|
2017-06-22 07:51:07 +00:00
|
|
|
class OrderConfirmationView(DetailView):
|
|
|
|
template_name = "datacenterlight/order_detail.html"
|
2017-09-01 20:32:05 +00:00
|
|
|
payment_template_name = 'datacenterlight/landing_payment.html'
|
2017-06-22 07:51:07 +00:00
|
|
|
context_object_name = "order"
|
|
|
|
model = HostingOrder
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-30 06:38:07 +00:00
|
|
|
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
2017-06-29 07:38:24 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
if 'specs' not in request.session or 'user' not in request.session:
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
2017-06-30 06:38:07 +00:00
|
|
|
if 'token' not in request.session:
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:payment'))
|
2017-09-28 13:56:09 +00:00
|
|
|
stripe_api_cus_id = request.session.get('customer')
|
2017-06-30 05:36:58 +00:00
|
|
|
stripe_utils = StripeUtils()
|
2017-09-21 01:11:09 +00:00
|
|
|
card_details = stripe_utils.get_card_details(stripe_api_cus_id,
|
2017-08-23 20:59:24 +00:00
|
|
|
request.session.get(
|
|
|
|
'token'))
|
|
|
|
if not card_details.get('response_object'):
|
2017-08-04 08:01:32 +00:00
|
|
|
msg = card_details.get('error')
|
2017-08-23 20:59:24 +00:00
|
|
|
messages.add_message(self.request, messages.ERROR, msg,
|
|
|
|
extra_tags='failed_payment')
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse('datacenterlight:payment') + '#payment_error')
|
2017-06-30 05:36:58 +00:00
|
|
|
context = {
|
2017-06-30 22:59:11 +00:00
|
|
|
'site_url': reverse('datacenterlight:index'),
|
2017-07-01 13:53:25 +00:00
|
|
|
'cc_last4': card_details.get('response_object').get('last4'),
|
2017-09-21 01:11:09 +00:00
|
|
|
'cc_brand': card_details.get('response_object').get('brand'),
|
2017-09-25 22:12:10 +00:00
|
|
|
'vm': request.session.get('specs'),
|
2017-09-28 12:57:26 +00:00
|
|
|
'page_header_text': _('Confirm Order'),
|
2018-03-27 13:49:26 +00:00
|
|
|
'billing_address_data': (
|
|
|
|
request.session.get('billing_address_data')
|
|
|
|
),
|
2018-05-06 23:37:58 +00:00
|
|
|
'cms_integration': get_cms_integration('default'),
|
2017-06-30 05:36:58 +00:00
|
|
|
}
|
|
|
|
return render(request, self.template_name, context)
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-29 07:38:24 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
template = request.session.get('template')
|
|
|
|
specs = request.session.get('specs')
|
|
|
|
user = request.session.get('user')
|
2017-09-28 13:56:09 +00:00
|
|
|
stripe_api_cus_id = request.session.get('customer')
|
2017-06-29 07:38:24 +00:00
|
|
|
vm_template_id = template.get('id', 1)
|
|
|
|
stripe_utils = StripeUtils()
|
2017-09-21 01:11:09 +00:00
|
|
|
card_details = stripe_utils.get_card_details(stripe_api_cus_id,
|
2017-08-24 06:24:01 +00:00
|
|
|
request.session.get(
|
|
|
|
'token'))
|
|
|
|
if not card_details.get('response_object'):
|
|
|
|
msg = card_details.get('error')
|
|
|
|
messages.add_message(self.request, messages.ERROR, msg,
|
|
|
|
extra_tags='failed_payment')
|
2017-09-22 19:30:42 +00:00
|
|
|
response = {
|
|
|
|
'status': False,
|
|
|
|
'redirect': "{url}#{section}".format(
|
|
|
|
url=reverse('datacenterlight:payment'),
|
|
|
|
section='payment_error'),
|
|
|
|
'msg_title': str(_('Error.')),
|
|
|
|
'msg_body': str(
|
|
|
|
_('There was a payment related error.'
|
|
|
|
' On close of this popup, you will be redirected back to'
|
|
|
|
' the payment page.'))
|
|
|
|
}
|
2018-04-20 14:55:24 +00:00
|
|
|
return JsonResponse(response)
|
|
|
|
|
2017-08-24 06:24:01 +00:00
|
|
|
card_details_dict = card_details.get('response_object')
|
2017-08-20 20:24:55 +00:00
|
|
|
cpu = specs.get('cpu')
|
|
|
|
memory = specs.get('memory')
|
|
|
|
disk_size = specs.get('disk_size')
|
2018-04-15 23:32:38 +00:00
|
|
|
amount_to_be_charged = specs.get('total_price')
|
2017-09-22 21:48:05 +00:00
|
|
|
plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu,
|
|
|
|
memory=memory,
|
|
|
|
disk_size=disk_size)
|
2017-08-20 20:24:55 +00:00
|
|
|
stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu,
|
|
|
|
ram=memory,
|
|
|
|
ssd=disk_size,
|
|
|
|
version=1,
|
|
|
|
app='dcl')
|
2017-08-24 06:24:01 +00:00
|
|
|
stripe_plan = stripe_utils.get_or_create_stripe_plan(
|
|
|
|
amount=amount_to_be_charged,
|
|
|
|
name=plan_name,
|
|
|
|
stripe_plan_id=stripe_plan_id)
|
|
|
|
subscription_result = stripe_utils.subscribe_customer_to_plan(
|
2017-09-21 12:29:49 +00:00
|
|
|
stripe_api_cus_id,
|
2017-08-24 06:24:01 +00:00
|
|
|
[{"plan": stripe_plan.get(
|
|
|
|
'response_object').stripe_plan_id}])
|
|
|
|
stripe_subscription_obj = subscription_result.get('response_object')
|
2017-08-20 20:24:55 +00:00
|
|
|
# Check if the subscription was approved and is active
|
2017-09-29 07:16:00 +00:00
|
|
|
if (stripe_subscription_obj is None
|
|
|
|
or stripe_subscription_obj.status != 'active'):
|
2017-08-21 18:37:58 +00:00
|
|
|
msg = subscription_result.get('error')
|
2017-08-24 06:24:01 +00:00
|
|
|
messages.add_message(self.request, messages.ERROR, msg,
|
|
|
|
extra_tags='failed_payment')
|
2017-09-22 19:30:42 +00:00
|
|
|
response = {
|
|
|
|
'status': False,
|
|
|
|
'redirect': "{url}#{section}".format(
|
|
|
|
url=reverse('datacenterlight:payment'),
|
|
|
|
section='payment_error'),
|
|
|
|
'msg_title': str(_('Error.')),
|
|
|
|
'msg_body': str(
|
2017-09-28 21:20:43 +00:00
|
|
|
_('There was a payment related error.'
|
|
|
|
' On close of this popup, you will be redirected back to'
|
2017-09-29 19:54:31 +00:00
|
|
|
' the payment page.'))
|
2017-09-22 19:30:42 +00:00
|
|
|
}
|
2018-04-20 14:55:24 +00:00
|
|
|
return JsonResponse(response)
|
2017-09-21 01:11:09 +00:00
|
|
|
|
2017-09-22 09:45:21 +00:00
|
|
|
# Create user if the user is not logged in and if he is not already
|
|
|
|
# registered
|
2017-09-22 10:38:05 +00:00
|
|
|
if not request.user.is_authenticated():
|
2017-09-21 01:11:09 +00:00
|
|
|
try:
|
|
|
|
custom_user = CustomUser.objects.get(
|
|
|
|
email=user.get('email'))
|
2017-10-08 19:01:25 +00:00
|
|
|
stripe_customer = StripeCustomer.objects.filter(
|
2017-09-22 19:20:00 +00:00
|
|
|
user_id=custom_user.id).first()
|
2017-10-08 19:01:25 +00:00
|
|
|
if stripe_customer is None:
|
|
|
|
stripe_customer = StripeCustomer.objects.create(
|
|
|
|
user=custom_user, stripe_id=stripe_api_cus_id
|
|
|
|
)
|
|
|
|
stripe_customer_id = stripe_customer.id
|
2017-09-21 01:11:09 +00:00
|
|
|
except CustomUser.DoesNotExist:
|
2017-09-22 09:45:21 +00:00
|
|
|
logger.debug(
|
|
|
|
"Customer {} does not exist.".format(user.get('email')))
|
2017-09-21 01:11:09 +00:00
|
|
|
password = CustomUser.get_random_password()
|
2017-09-22 11:46:22 +00:00
|
|
|
base_url = "{0}://{1}".format(self.request.scheme,
|
|
|
|
self.request.get_host())
|
2017-09-21 01:11:09 +00:00
|
|
|
custom_user = CustomUser.register(
|
2017-09-21 12:29:49 +00:00
|
|
|
user.get('name'), password,
|
|
|
|
user.get('email'),
|
2017-09-22 11:46:22 +00:00
|
|
|
app='dcl', base_url=base_url, send_email=True,
|
2017-09-22 10:01:09 +00:00
|
|
|
account_details=password
|
2017-09-21 01:11:09 +00:00
|
|
|
)
|
|
|
|
logger.debug("Created user {}.".format(user.get('email')))
|
2017-09-21 12:29:49 +00:00
|
|
|
stripe_customer = StripeCustomer.objects. \
|
|
|
|
create(user=custom_user, stripe_id=stripe_api_cus_id)
|
|
|
|
stripe_customer_id = stripe_customer.id
|
2017-09-22 10:46:33 +00:00
|
|
|
new_user = authenticate(username=custom_user.email,
|
|
|
|
password=password)
|
|
|
|
login(request, new_user)
|
2017-09-21 12:29:49 +00:00
|
|
|
else:
|
2017-09-28 13:56:09 +00:00
|
|
|
# We assume that if the user is here, his/her StripeCustomer
|
|
|
|
# object already exists
|
|
|
|
stripe_customer_id = request.user.stripecustomer.id
|
|
|
|
custom_user = request.user
|
2017-09-21 01:11:09 +00:00
|
|
|
|
|
|
|
# Save billing address
|
|
|
|
billing_address_data = request.session.get('billing_address_data')
|
|
|
|
logger.debug('billing_address_data is {}'.format(billing_address_data))
|
|
|
|
billing_address_data.update({
|
|
|
|
'user': custom_user.id
|
|
|
|
})
|
2017-09-22 10:38:05 +00:00
|
|
|
user = {
|
|
|
|
'name': custom_user.name,
|
|
|
|
'email': custom_user.email,
|
|
|
|
'pass': custom_user.password,
|
|
|
|
'request_scheme': request.scheme,
|
|
|
|
'request_host': request.get_host(),
|
|
|
|
'language': get_language(),
|
|
|
|
}
|
2017-09-22 09:45:21 +00:00
|
|
|
|
2018-04-20 14:55:24 +00:00
|
|
|
create_vm(
|
|
|
|
billing_address_data, stripe_customer_id, specs,
|
|
|
|
stripe_subscription_obj, card_details_dict, request,
|
|
|
|
vm_template_id, template, user
|
2018-04-18 21:50:52 +00:00
|
|
|
)
|
|
|
|
|
2017-09-22 19:20:00 +00:00
|
|
|
response = {
|
|
|
|
'status': True,
|
2018-02-27 22:39:19 +00:00
|
|
|
'redirect': (
|
|
|
|
reverse('hosting:virtual_machines')
|
|
|
|
if request.user.is_authenticated()
|
|
|
|
else reverse('datacenterlight:index')
|
|
|
|
),
|
2017-09-22 19:20:00 +00:00
|
|
|
'msg_title': str(_('Thank you for the order.')),
|
|
|
|
'msg_body': str(
|
|
|
|
_('Your VM will be up and running in a few moments.'
|
|
|
|
' We will send you a confirmation email as soon as'
|
|
|
|
' it is ready.'))
|
|
|
|
}
|
|
|
|
|
2018-04-20 14:55:24 +00:00
|
|
|
return JsonResponse(response)
|