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

View file

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