Removed unncessary check for error for make_charge

This commit is contained in:
PCoder 2017-08-24 02:29:24 +05:30
parent 4cc2e09b0a
commit 6484684ac5
3 changed files with 67 additions and 35 deletions

View file

@ -93,7 +93,7 @@ class CeleryTaskTestCase(TestCase):
# Check if the payment was approved # Check if the payment was approved
if not charge_response.get( if not charge_response.get(
'response_object') and not charge_response.get('paid'): 'response_object'):
msg = charge_response.get('error') msg = charge_response.get('error')
raise Exception("make_charge failed: {}".format(msg)) raise Exception("make_charge failed: {}".format(msg))

View file

@ -18,7 +18,8 @@ from hosting.models import HostingOrder
from utils.stripe_utils import StripeUtils from utils.stripe_utils import StripeUtils
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, VMTemplateSerializer from opennebula_api.serializers import VirtualMachineTemplateSerializer, \
VMTemplateSerializer
from datacenterlight.tasks import create_vm_task from datacenterlight.tasks import create_vm_task
@ -35,9 +36,11 @@ class SuccessView(TemplateView):
elif 'token' not in request.session: elif '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(reverse('datacenterlight:order_confirmation')) return HttpResponseRedirect(
reverse('datacenterlight:order_confirmation'))
else: else:
for session_var in ['specs', 'user', 'template', 'billing_address', 'billing_address_data', for session_var in ['specs', 'user', 'template', 'billing_address',
'billing_address_data',
'token', 'customer']: 'token', 'customer']:
if session_var in request.session: if session_var in request.session:
del request.session[session_var] del request.session[session_var]
@ -53,7 +56,8 @@ class PricingView(TemplateView):
templates = manager.get_templates() templates = manager.get_templates()
context = { context = {
'templates': VirtualMachineTemplateSerializer(templates, many=True).data, 'templates': VirtualMachineTemplateSerializer(templates,
many=True).data,
} }
except: except:
messages.error(request, messages.error(request,
@ -77,7 +81,8 @@ class PricingView(TemplateView):
manager = OpenNebulaManager() manager = OpenNebulaManager()
template = manager.get_template(template_id) template = manager.get_template(template_id)
request.session['template'] = VirtualMachineTemplateSerializer(template).data request.session['template'] = VirtualMachineTemplateSerializer(
template).data
if not request.user.is_authenticated(): if not request.user.is_authenticated():
request.session['next'] = reverse('hosting:payment') request.session['next'] = reverse('hosting:payment')
@ -99,7 +104,8 @@ class BetaAccessView(FormView):
def form_valid(self, form): def form_valid(self, form):
context = { context = {
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()) 'base_url': "{0}://{1}".format(self.request.scheme,
self.request.get_host())
} }
email_data = { email_data = {
@ -129,7 +135,8 @@ class BetaAccessView(FormView):
email = BaseEmail(**email_data) email = BaseEmail(**email_data)
email.send() email.send()
messages.add_message(self.request, messages.SUCCESS, self.success_message) messages.add_message(self.request, messages.SUCCESS,
self.success_message)
return render(self.request, 'datacenterlight/beta_success.html', {}) return render(self.request, 'datacenterlight/beta_success.html', {})
@ -154,7 +161,8 @@ class BetaProgramView(CreateView):
# data = VirtualMachineTemplateSerializer(templates, many=True).data # data = VirtualMachineTemplateSerializer(templates, many=True).data
context.update({ context.update({
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()), 'base_url': "{0}://{1}".format(self.request.scheme,
self.request.get_host()),
'vms': vms 'vms': vms
}) })
return context return context
@ -164,7 +172,8 @@ class BetaProgramView(CreateView):
vms = BetaAccessVM.create(data) vms = BetaAccessVM.create(data)
context = { context = {
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()), 'base_url': "{0}://{1}".format(self.request.scheme,
self.request.get_host()),
'email': data.get('email'), 'email': data.get('email'),
'name': data.get('name'), 'name': data.get('name'),
'vms': vms 'vms': vms
@ -181,7 +190,8 @@ class BetaProgramView(CreateView):
email = BaseEmail(**email_data) email = BaseEmail(**email_data)
email.send() email.send()
messages.add_message(self.request, messages.SUCCESS, self.success_message) messages.add_message(self.request, messages.SUCCESS,
self.success_message)
return HttpResponseRedirect(self.get_success_url()) return HttpResponseRedirect(self.get_success_url())
@ -225,7 +235,8 @@ class IndexView(CreateView):
storage_field = forms.IntegerField(validators=[self.validate_storage]) storage_field = forms.IntegerField(validators=[self.validate_storage])
price = request.POST.get('total') price = request.POST.get('total')
template_id = int(request.POST.get('config')) template_id = int(request.POST.get('config'))
template = VMTemplate.objects.filter(opennebula_vm_template_id=template_id).first() template = VMTemplate.objects.filter(
opennebula_vm_template_id=template_id).first()
template_data = VMTemplateSerializer(template).data template_data = VMTemplateSerializer(template).data
name = request.POST.get('name') name = request.POST.get('name')
@ -237,36 +248,46 @@ class IndexView(CreateView):
cores = cores_field.clean(cores) cores = cores_field.clean(cores)
except ValidationError as err: except ValidationError as err:
msg = '{} : {}.'.format(cores, str(err)) msg = '{} : {}.'.format(cores, str(err))
messages.add_message(self.request, messages.ERROR, msg, extra_tags='cores') messages.add_message(self.request, messages.ERROR, msg,
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") extra_tags='cores')
return HttpResponseRedirect(
reverse('datacenterlight:index') + "#order_form")
try: try:
memory = memory_field.clean(memory) memory = memory_field.clean(memory)
except ValidationError as err: except ValidationError as err:
msg = '{} : {}.'.format(memory, str(err)) msg = '{} : {}.'.format(memory, str(err))
messages.add_message(self.request, messages.ERROR, msg, extra_tags='memory') messages.add_message(self.request, messages.ERROR, msg,
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") extra_tags='memory')
return HttpResponseRedirect(
reverse('datacenterlight:index') + "#order_form")
try: try:
storage = storage_field.clean(storage) storage = storage_field.clean(storage)
except ValidationError as err: except ValidationError as err:
msg = '{} : {}.'.format(storage, str(err)) msg = '{} : {}.'.format(storage, str(err))
messages.add_message(self.request, messages.ERROR, msg, extra_tags='storage') messages.add_message(self.request, messages.ERROR, msg,
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") extra_tags='storage')
return HttpResponseRedirect(
reverse('datacenterlight:index') + "#order_form")
try: try:
name = name_field.clean(name) name = name_field.clean(name)
except ValidationError as err: except ValidationError as err:
msg = '{} {}.'.format(name, _('is not a proper name')) msg = '{} {}.'.format(name, _('is not a proper name'))
messages.add_message(self.request, messages.ERROR, msg, extra_tags='name') messages.add_message(self.request, messages.ERROR, msg,
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") extra_tags='name')
return HttpResponseRedirect(
reverse('datacenterlight:index') + "#order_form")
try: try:
email = email_field.clean(email) email = email_field.clean(email)
except ValidationError as err: except ValidationError as err:
msg = '{} {}.'.format(email, _('is not a proper email')) msg = '{} {}.'.format(email, _('is not a proper email'))
messages.add_message(self.request, messages.ERROR, msg, extra_tags='email') messages.add_message(self.request, messages.ERROR, msg,
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") extra_tags='email')
return HttpResponseRedirect(
reverse('datacenterlight:index') + "#order_form")
specs = { specs = {
'cpu': cores, 'cpu': cores,
@ -293,14 +314,16 @@ class IndexView(CreateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs) context = super(IndexView, self).get_context_data(**kwargs)
context.update({ context.update({
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()) 'base_url': "{0}://{1}".format(self.request.scheme,
self.request.get_host())
}) })
return context return context
def form_valid(self, form): def form_valid(self, form):
context = { context = {
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()) 'base_url': "{0}://{1}".format(self.request.scheme,
self.request.get_host())
} }
email_data = { email_data = {
@ -330,7 +353,8 @@ class IndexView(CreateView):
email = BaseEmail(**email_data) email = BaseEmail(**email_data)
email.send() email.send()
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)
@ -403,7 +427,8 @@ class PaymentOrderView(FormView):
request.session['billing_address'] = billing_address.id 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(reverse('datacenterlight:order_confirmation')) return HttpResponseRedirect(
reverse('datacenterlight:order_confirmation'))
else: else:
return self.form_invalid(form) return self.form_invalid(form)
@ -423,11 +448,15 @@ class OrderConfirmationView(DetailView):
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()
stripe_utils = StripeUtils() stripe_utils = StripeUtils()
card_details = stripe_utils.get_card_details(customer.stripe_id, request.session.get('token')) card_details = stripe_utils.get_card_details(customer.stripe_id,
if not card_details.get('response_object') and not card_details.get('paid'): request.session.get(
'token'))
if not card_details.get('response_object'):
msg = card_details.get('error') msg = card_details.get('error')
messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') messages.add_message(self.request, messages.ERROR, msg,
return HttpResponseRedirect(reverse('datacenterlight:payment') + '#payment_error') extra_tags='failed_payment')
return HttpResponseRedirect(
reverse('datacenterlight:payment') + '#payment_error')
context = { context = {
'site_url': reverse('datacenterlight:index'), 'site_url': reverse('datacenterlight:index'),
'cc_last4': card_details.get('response_object').get('last4'), 'cc_last4': card_details.get('response_object').get('last4'),
@ -452,13 +481,16 @@ class OrderConfirmationView(DetailView):
customer=customer.stripe_id) customer=customer.stripe_id)
# Check if the payment was approved # Check if the payment was approved
if not charge_response.get('response_object') and not charge_response.get('paid'): if not charge_response.get('response_object'):
msg = charge_response.get('error') msg = charge_response.get('error')
messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') messages.add_message(self.request, messages.ERROR, msg,
return HttpResponseRedirect(reverse('datacenterlight:payment') + '#payment_error') extra_tags='make_charge_error')
return HttpResponseRedirect(
reverse('datacenterlight:payment') + '#payment_error')
charge = charge_response.get('response_object') charge = charge_response.get('response_object')
create_vm_task.delay(vm_template_id, user, specs, template, stripe_customer_id, billing_address_data, create_vm_task.delay(vm_template_id, user, specs, template,
stripe_customer_id, billing_address_data,
billing_address_id, billing_address_id,
charge) charge)
request.session['order_confirmation'] = True request.session['order_confirmation'] = True

View file

@ -569,7 +569,7 @@ class PaymentVMView(LoginRequiredMixin, FormView):
customer=customer.stripe_id) customer=customer.stripe_id)
# Check if the payment was approved # Check if the payment was approved
if not charge_response.get('response_object') and not charge_response.get('paid'): if not charge_response.get('response_object'):
msg = charge_response.get('error') msg = charge_response.get('error')
messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error')
return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error') return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error')