Added PaymentOrderView
This commit is contained in:
parent
385df4e069
commit
52524a97a6
1 changed files with 160 additions and 19 deletions
|
@ -11,9 +11,15 @@ from django.shortcuts import redirect
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.views.decorators.cache import cache_control
|
from django.views.decorators.cache import cache_control
|
||||||
|
from django.conf import settings
|
||||||
|
from utils.forms import BillingAddressForm, UserBillingAddressForm
|
||||||
|
from membership.models import StripeCustomer
|
||||||
|
from hosting.models import HostingOrder, HostingBill
|
||||||
|
from utils.stripe_utils import StripeUtils
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from opennebula_api.models import OpenNebulaManager
|
from opennebula_api.models import OpenNebulaManager
|
||||||
from opennebula_api.serializers import VirtualMachineTemplateSerializer
|
from opennebula_api.serializers import VirtualMachineTemplateSerializer, VirtualMachineSerializer
|
||||||
|
|
||||||
class LandingProgramView(TemplateView):
|
class LandingProgramView(TemplateView):
|
||||||
template_name = "datacenterlight/landing.html"
|
template_name = "datacenterlight/landing.html"
|
||||||
|
@ -21,6 +27,14 @@ class LandingProgramView(TemplateView):
|
||||||
class SuccessView(TemplateView):
|
class SuccessView(TemplateView):
|
||||||
template_name = "datacenterlight/success.html"
|
template_name = "datacenterlight/success.html"
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
if 'specs' not in request.session or 'user' not in request.session:
|
||||||
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
||||||
|
else :
|
||||||
|
del request.session['specs']
|
||||||
|
del request.session['user']
|
||||||
|
return render(request, self.template_name)
|
||||||
|
|
||||||
class PricingView(TemplateView):
|
class PricingView(TemplateView):
|
||||||
template_name = "datacenterlight/pricing.html"
|
template_name = "datacenterlight/pricing.html"
|
||||||
|
|
||||||
|
@ -173,6 +187,10 @@ class IndexView(CreateView):
|
||||||
|
|
||||||
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
|
if 'specs' in request.session :
|
||||||
|
del request.session['specs']
|
||||||
|
if 'user' in request.session :
|
||||||
|
del request.session['user']
|
||||||
try:
|
try:
|
||||||
manager = OpenNebulaManager()
|
manager = OpenNebulaManager()
|
||||||
templates = manager.get_templates()
|
templates = manager.get_templates()
|
||||||
|
@ -215,26 +233,22 @@ class IndexView(CreateView):
|
||||||
messages.add_message(self.request, messages.ERROR, '%(value) is not a proper email.'.format(email))
|
messages.add_message(self.request, messages.ERROR, '%(value) is not a proper email.'.format(email))
|
||||||
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
||||||
|
|
||||||
context = {
|
specs = {
|
||||||
'name': name,
|
'cpu': cores,
|
||||||
'email': email,
|
|
||||||
'cores': cores,
|
|
||||||
'memory': memory,
|
'memory': memory,
|
||||||
'storage': storage,
|
'disk_size': storage,
|
||||||
'price': price,
|
'price': price
|
||||||
'template': template_data['name'],
|
|
||||||
}
|
}
|
||||||
email_data = {
|
|
||||||
'subject': "Data Center Light Order from %s" % context['email'],
|
|
||||||
'from_email': '(datacenterlight) datacenterlight Support <support@datacenterlight.ch>',
|
|
||||||
'to': ['info@ungleich.ch'],
|
|
||||||
'body': "\n".join(["%s=%s" % (k, v) for (k, v) in context.items()]),
|
|
||||||
'reply_to': [context['email']],
|
|
||||||
}
|
|
||||||
email = EmailMessage(**email_data)
|
|
||||||
email.send()
|
|
||||||
|
|
||||||
return HttpResponseRedirect(reverse('datacenterlight:order_success'))
|
this_user = {
|
||||||
|
'name': name,
|
||||||
|
'email': email
|
||||||
|
}
|
||||||
|
|
||||||
|
request.session['specs'] = specs
|
||||||
|
request.session['template'] = template_data
|
||||||
|
request.session['user'] = this_user
|
||||||
|
return HttpResponseRedirect(reverse('datacenterlight:payment'))
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
success_url = reverse('datacenterlight:index')
|
success_url = reverse('datacenterlight:index')
|
||||||
|
@ -283,3 +297,130 @@ class IndexView(CreateView):
|
||||||
|
|
||||||
messages.add_message(self.request, messages.SUCCESS, self.success_message)
|
messages.add_message(self.request, messages.SUCCESS, self.success_message)
|
||||||
return super(IndexView, self).form_valid(form)
|
return super(IndexView, self).form_valid(form)
|
||||||
|
|
||||||
|
|
||||||
|
class PaymentOrderView(FormView):
|
||||||
|
template_name = 'hosting/payment.html'
|
||||||
|
form_class = BillingAddressForm
|
||||||
|
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
context = super(PaymentOrderView, self).get_context_data(**kwargs)
|
||||||
|
context.update({
|
||||||
|
'stripe_key': settings.STRIPE_API_PUBLIC_KEY
|
||||||
|
})
|
||||||
|
return context
|
||||||
|
|
||||||
|
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
if 'specs' not in request.session or 'user' not in request.session:
|
||||||
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
||||||
|
return self.render_to_response(self.get_context_data())
|
||||||
|
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
form = self.get_form()
|
||||||
|
if form.is_valid():
|
||||||
|
# Get billing address data
|
||||||
|
billing_address_data = form.cleaned_data
|
||||||
|
context = self.get_context_data()
|
||||||
|
template = request.session.get('template')
|
||||||
|
specs = request.session.get('specs')
|
||||||
|
user = request.session.get('user')
|
||||||
|
vm_template_id = template.get('id', 1)
|
||||||
|
final_price = specs.get('price')
|
||||||
|
token = form.cleaned_data.get('token')
|
||||||
|
owner = self.request.user
|
||||||
|
|
||||||
|
# Get or create stripe customer
|
||||||
|
customer = StripeCustomer.get_or_create(email=user.get('email'),
|
||||||
|
token=token)
|
||||||
|
if not customer:
|
||||||
|
form.add_error("__all__", "Invalid credit card")
|
||||||
|
return self.render_to_response(self.get_context_data(form=form))
|
||||||
|
|
||||||
|
# Create Billing Address
|
||||||
|
billing_address = form.save()
|
||||||
|
|
||||||
|
# Make stripe charge to a customer
|
||||||
|
stripe_utils = StripeUtils()
|
||||||
|
charge_response = stripe_utils.make_charge(amount=final_price,
|
||||||
|
customer=customer.stripe_id)
|
||||||
|
charge = charge_response.get('response_object')
|
||||||
|
|
||||||
|
# Check if the payment was approved
|
||||||
|
if not charge:
|
||||||
|
context.update({
|
||||||
|
'paymentError': charge_response.get('error'),
|
||||||
|
'form': form
|
||||||
|
})
|
||||||
|
return render(request, self.template_name, context)
|
||||||
|
|
||||||
|
charge = charge_response.get('response_object')
|
||||||
|
|
||||||
|
# Create OpenNebulaManager
|
||||||
|
manager = OpenNebulaManager(email=settings.OPENNEBULA_USERNAME,
|
||||||
|
password=settings.OPENNEBULA_PASSWORD)
|
||||||
|
|
||||||
|
# Create a vm using logged user
|
||||||
|
vm_id = manager.create_vm(
|
||||||
|
template_id=vm_template_id,
|
||||||
|
specs=specs,
|
||||||
|
name="{email}-{template_name}-{date}".format(
|
||||||
|
email=user.get('email'),
|
||||||
|
template_name=template.get('name'),
|
||||||
|
date=int(datetime.now().strftime("%s")))
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create a Hosting Order
|
||||||
|
#order = HostingOrder.create(
|
||||||
|
# price=final_price,
|
||||||
|
# vm_id=vm_id,
|
||||||
|
# customer=customer,
|
||||||
|
# billing_address=billing_address
|
||||||
|
#)
|
||||||
|
|
||||||
|
## Create a Hosting Bill
|
||||||
|
#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()
|
||||||
|
#
|
||||||
|
## Associate an order with a stripe payment
|
||||||
|
#order.set_stripe_charge(charge)
|
||||||
|
|
||||||
|
# If the Stripe payment was successed, set order status approved
|
||||||
|
#order.set_approved()
|
||||||
|
|
||||||
|
vm = VirtualMachineSerializer(manager.get_vm(vm_id)).data
|
||||||
|
|
||||||
|
context = {
|
||||||
|
'name': user.get('name'),
|
||||||
|
'email': user.get('email'),
|
||||||
|
'cores': specs.get('cpu'),
|
||||||
|
'memory': specs.get('memory'),
|
||||||
|
'storage': specs.get('disk_size'),
|
||||||
|
'price': specs.get('price'),
|
||||||
|
'template': template.get('name'),
|
||||||
|
'vm.name': vm['name'],
|
||||||
|
'vm.id': vm['vm_id'],
|
||||||
|
'order id': order.id
|
||||||
|
}
|
||||||
|
email_data = {
|
||||||
|
'subject': "Data Center Light Order from %s" % context['email'],
|
||||||
|
'from_email': '(Data Center Light) Data Center Light Support <support@datacenterlight.ch>',
|
||||||
|
'to': ['info@ungleich.ch'],
|
||||||
|
'body': "\n".join(["%s=%s" % (k, v) for (k, v) in context.items()]),
|
||||||
|
'reply_to': [context['email']],
|
||||||
|
}
|
||||||
|
email = EmailMessage(**email_data)
|
||||||
|
email.send()
|
||||||
|
return HttpResponseRedirect(reverse('datacenterlight:order_success'))
|
||||||
|
else:
|
||||||
|
return self.form_invalid(form)
|
||||||
|
|
Loading…
Reference in a new issue