From 16f000c7232839e929655d7212b98961fd9697fd Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 24 Aug 2017 11:54:01 +0530 Subject: [PATCH] dcl views.py: passing cc details to create_vm_task + reformatted code --- datacenterlight/views.py | 48 +++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index fd5c7959..fd1435a1 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -476,32 +476,50 @@ class OrderConfirmationView(DetailView): # Make stripe charge to a customer stripe_utils = StripeUtils() + card_details = stripe_utils.get_card_details(customer.stripe_id, + 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') + return HttpResponseRedirect( + reverse('datacenterlight:payment') + '#payment_error') + card_details_dict = card_details.get('response_object') cpu = specs.get('cpu') memory = specs.get('memory') disk_size = specs.get('disk_size') amount_to_be_charged = (cpu * 5) + (memory * 2) + (disk_size * 0.6) - plan_name = "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format(cpu=cpu, - memory=memory, - disk_size=disk_size) + plan_name = "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format( + cpu=cpu, + memory=memory, + disk_size=disk_size) + stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu, ram=memory, ssd=disk_size, version=1, app='dcl') - 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(customer.stripe_id, - [{"plan": stripe_plan.get( - 'response_object').stripe_plan_id}]) - response_object = subscription_result.get('response_object') + 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( + customer.stripe_id, + [{"plan": stripe_plan.get( + 'response_object').stripe_plan_id}]) + stripe_subscription_obj = subscription_result.get('response_object') # Check if the subscription was approved and is active - if response_object is None or response_object.status != 'active': + if stripe_subscription_obj is None or \ + stripe_subscription_obj.status != 'active': msg = subscription_result.get('error') - messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') - return HttpResponseRedirect(reverse('datacenterlight:payment') + '#payment_error') - create_vm_task.delay(vm_template_id, user, specs, template, stripe_customer_id, billing_address_data, + messages.add_message(self.request, messages.ERROR, msg, + extra_tags='failed_payment') + return HttpResponseRedirect( + reverse('datacenterlight:payment') + '#payment_error') + create_vm_task.delay(vm_template_id, user, specs, template, + stripe_customer_id, billing_address_data, billing_address_id, - response_object) + stripe_subscription_obj, card_details_dict) request.session['order_confirmation'] = True return HttpResponseRedirect(reverse('datacenterlight:order_success'))