billing address save and get

This commit is contained in:
Arvind Tiwari 2017-09-12 00:54:02 +05:30
parent 5f6f2e4ca9
commit 4d2903ca86
2 changed files with 22 additions and 83 deletions

View file

@ -39,9 +39,9 @@
<div class="col-xs-12 col-sm-6">
<address>
<h3><b>{% trans "Billed To:"%}</b></h3>
{% with request.session.billing_address_data as billing_address %}
{{billing_address|get_value_from_dict:'cardholder_name'}}<br> {{billing_address|get_value_from_dict:'street_address'}}, {{billing_address|get_value_from_dict:'postal_code'}}<br>
{{billing_address|get_value_from_dict:'city'}}, {{billing_address|get_value_from_dict:'country'}}.
{% with request.user.billing_addresses.first as billing_address %}
{{billing_address.cardholder_name}}<br> {{billing_address.street_address}}, {{billing_address.postal_code}}<br>
{{billing_address.city}}, {{billing_address.country}}.
{% endwith %}
</address>
</div>

View file

@ -16,9 +16,12 @@ from hosting.models import HostingOrder
from hosting.forms import HostingUserLoginForm
from membership.models import CustomUser, StripeCustomer
from opennebula_api.models import OpenNebulaManager
from opennebula_api.serializers import VirtualMachineTemplateSerializer, \
VMTemplateSerializer
from utils.forms import BillingAddressForm, BillingAddressFormSignup
from opennebula_api.serializers import (
VirtualMachineTemplateSerializer, VMTemplateSerializer
)
from utils.forms import (
BillingAddressForm, BillingAddressFormSignup, UserBillingAddressForm
)
from utils.mailer import BaseEmail
from utils.stripe_utils import StripeUtils
from utils.tasks import send_plain_email_task
@ -76,7 +79,7 @@ class SuccessView(TemplateView):
def get(self, request, *args, **kwargs):
if 'specs' not in request.session or 'user' not in request.session:
return HttpResponseRedirect(reverse('datacenterlight:index'))
elif 'token' not in request.session:
if 'token' not in request.session:
return HttpResponseRedirect(reverse('datacenterlight:payment'))
elif 'order_confirmation' not in request.session:
return HttpResponseRedirect(
@ -102,7 +105,7 @@ class PricingView(TemplateView):
'templates': VirtualMachineTemplateSerializer(templates,
many=True).data,
}
except:
except Exception:
messages.error(request,
'We have a temporary problem to connect to our backend. \
Please try again in a few minutes'
@ -282,11 +285,6 @@ class IndexView(CreateView):
opennebula_vm_template_id=template_id).first()
template_data = VMTemplateSerializer(template).data
# name = request.POST.get('name')
# email = request.POST.get('email')
# name_field = forms.CharField()
# email_field = forms.EmailField()
try:
cores = cores_field.clean(cores)
except ValidationError as err:
@ -314,39 +312,14 @@ class IndexView(CreateView):
return HttpResponseRedirect(
reverse('datacenterlight:index') + "#order_form")
# try:
# name = name_field.clean(name)
# except ValidationError as err:
# msg = '{} {}.'.format(name, _('is not a proper name'))
# messages.add_message(self.request, messages.ERROR, msg,
# extra_tags='name')
# return HttpResponseRedirect(
# reverse('datacenterlight:index') + "#order_form")
# try:
# email = email_field.clean(email)
# except ValidationError as err:
# msg = '{} {}.'.format(email, _('is not a proper email'))
# messages.add_message(self.request, messages.ERROR, msg,
# extra_tags='email')
# return HttpResponseRedirect(
# reverse('datacenterlight:index') + "#order_form")
specs = {
'cpu': cores,
'memory': memory,
'disk_size': storage,
'price': price
}
# 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):
@ -423,20 +396,6 @@ class PaymentOrderView(FormView):
form_kwargs.update({
'instance': self.request.user.billing_addresses.first()
})
else:
# if billing address in session, get data
billing_address_data = self.request.session.get(
'billing_address_data')
if billing_address_data:
form_kwargs.update({
'initial': {
'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'],
}
})
return form_kwargs
def get_context_data(self, **kwargs):
@ -459,36 +418,13 @@ class PaymentOrderView(FormView):
def post(self, request, *args, **kwargs):
form = self.get_form()
if form.is_valid():
# name = request.POST.get('name')
# email = request.POST.get('email')
# name_field = forms.CharField()
# email_field = forms.EmailField()
# try:
# name = name_field.clean(name)
# except ValidationError as err:
# msg = '{} {}.'.format(name, _('is not a proper name'))
# messages.add_message(self.request, messages.ERROR, msg,
# extra_tags='name')
# return HttpResponseRedirect(
# reverse('datacenterlight:payment'))
# try:
# email = email_field.clean(email)
# except ValidationError as err:
# msg = '{} {}.'.format(email, _('is not a proper email'))
# messages.add_message(self.request, messages.ERROR, msg,
# extra_tags='email')
# return HttpResponseRedirect(
# reverse('datacenterlight:payment'))
# Get billing address data
# user = request.session.get('user')
billing_address_data = form.cleaned_data
token = form.cleaned_data.get('token')
if request.user.is_authenticated():
this_user = {
'email': request.user.email,
'name': request.user.name
}
custom_user = request.user
else:
this_user = {
'email': form.cleaned_data.get('email'),
@ -518,7 +454,14 @@ class PaymentOrderView(FormView):
)
return HttpResponseRedirect(
reverse('datacenterlight:payment'))
billing_address_data = form.cleaned_data
billing_address_data.update({
'user': custom_user.id
})
billing_address_user_form = UserBillingAddressForm(
instance=custom_user.billing_addresses.first(),
data=billing_address_data)
billing_address_user_form.save()
request.session['user'] = this_user
# Get or create stripe customer
customer = StripeCustomer.get_or_create(
@ -529,10 +472,6 @@ class PaymentOrderView(FormView):
return self.render_to_response(
self.get_context_data(form=form))
# Create Billing Address
billing_address = form.save()
request.session['billing_address_data'] = billing_address_data
request.session['billing_address'] = billing_address.id
request.session['token'] = token
request.session['customer'] = customer.id
return HttpResponseRedirect(
@ -578,8 +517,8 @@ class OrderConfirmationView(DetailView):
user = request.session.get('user')
stripe_customer_id = request.session.get('customer')
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first()
billing_address_data = request.session.get('billing_address_data')
billing_address_id = request.session.get('billing_address')
billing_address_data = {}
billing_address_id = request.user.billing_addresses.first().id
vm_template_id = template.get('id', 1)
# Make stripe charge to a customer