From 2a9b208719e7d2eb3cff3c64c38cf4ea6dbafc0a Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 6 Sep 2017 13:25:18 +0530 Subject: [PATCH] hosting: Moving the stripe charge to order confirmation page (still incomplete) --- hosting/templates/hosting/order_detail.html | 91 +++++++++++++++---- hosting/views.py | 99 ++------------------- 2 files changed, 80 insertions(+), 110 deletions(-) diff --git a/hosting/templates/hosting/order_detail.html b/hosting/templates/hosting/order_detail.html index 64c0b5d3..989d7419 100644 --- a/hosting/templates/hosting/order_detail.html +++ b/hosting/templates/hosting/order_detail.html @@ -20,30 +20,50 @@
-

{{page_header_text}}

{% trans "Order #"%} {{order.id}}

+

{{page_header_text}}

+

+ {% if order %} + {% trans "Order #"%} {{order.id}} + {% endif %} +


{% trans "Date"%}:
- {{order.created_at|date:'Y-m-d H:i'}}

+ + {% if order %} + {{order.created_at|date:'Y-m-d H:i'}} + {% else %} + {% now "Y-m-d H:i" %} + {% endif %} +

+ {% if order %} {% trans "Status:"%}
- {% if order.status == 'Approved' %} - {% trans "Approved" %} - {% else %} - {% trans "Declined" %} - {% endif %} + {% if order.status == 'Approved' %} + {% trans "Approved" %} + {% else %} + {% trans "Declined" %} + {% endif %}

+ {% endif %}

{% trans "Billed To:"%}

+ {% if order %} {{user.name}}
{{order.billing_address.street_address}},{{order.billing_address.postal_code}}
{{order.billing_address.city}}, {{order.billing_address.country}}. + {% else %} + {% with request.session.billing_address_data as billing_address %} + {{billing_address|get_value_from_dict:'cardholder_name'}}
{{billing_address|get_value_from_dict:'street_address'}}, {{billing_address|get_value_from_dict:'postal_code'}}
+ {{billing_address|get_value_from_dict:'city'}}, {{billing_address|get_value_from_dict:'country'}}. + {% endwith %} + {% endif %}
@@ -52,8 +72,13 @@
{% trans "Payment Method:"%}
+ {% if order %} {{order.cc_brand}} {% trans "ending in" %} **** {{order.last4}}
{{user.email}} + {% else %} + {{cc_brand}} {% trans "ending" %} **** {{cc_last4}}
+ {{request.session.user.email}} + {% endif %}
@@ -65,20 +90,48 @@

{% trans "Order summary"%}


-

{% trans "Cores"%} {{vm.cores}}

-
-

{% trans "Memory"%} {{vm.memory}} GB

-
-

{% trans "Disk space"%} {{vm.disk_size}} GB

-
-

{% trans "Total"%}

{{vm.price}} CHF

+ {% if request.session.specs %} + {% with request.session.specs as vm %} +

{% trans "Cores"%} {{vm.cpu}}

+
+

{% trans "Memory"%} {{vm.memory}} GB

+
+

{% trans "Disk space"%} {{vm.disk_size}} GB

+
+

{% trans "Configuration"%} {{request.session.template.name}}

+
+

{% trans "Total"%}

{{vm.price}} CHF /{% trans "Month" %}

+ {% endwith %} + {% else %} +

{% trans "Cores"%} {{vm.cores}}

+
+

{% trans "Memory"%} {{vm.memory}} GB

+
+

{% trans "Disk space"%} {{vm.disk_size}} GB

+
+

{% trans "Total"%}

{{vm.price}} CHF

+ {% endif %}

- {% url 'hosting:payment' as payment_url %} - {% if payment_url in request.META.HTTP_REFERER %} - + {% if not order %} +
+ {% csrf_token %} +
+
+

{% blocktrans with vm_price=request.session.specs.price %}By clicking "Place order" this plan will charge your credit card account with the fee of {{ vm_price }}CHF/month{% endblocktrans %}.

+
+ +
+
+ {% else %} + {% url 'hosting:payment' as payment_url %} + {% if payment_url in request.META.HTTP_REFERER %} + + {% endif %} {% endif %}
diff --git a/hosting/views.py b/hosting/views.py index 08f0862e..67cf8c28 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -569,100 +569,17 @@ class PaymentVMView(LoginRequiredMixin, FormView): customer = StripeCustomer.get_or_create(email=owner.email, token=token) if not customer: - msg = _("Invalid credit card") - messages.add_message( - self.request, messages.ERROR, msg, extra_tags='make_charge_error') - return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error') + 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) - - # Check if the payment was approved - if not charge_response.get('response_object'): - msg = charge_response.get('error') - messages.add_message( - self.request, messages.ERROR, msg, extra_tags='make_charge_error') - return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error') - - charge = charge_response.get('response_object') - - # Create OpenNebulaManager - manager = OpenNebulaManager(email=owner.email, - password=owner.password) - # Get user ssh key - if not UserHostingKey.objects.filter(user=self.request.user).exists(): - context.update({ - 'sshError': 'error', - 'form': form - }) - return render(request, self.template_name, context) - # For now just get first one - user_key = UserHostingKey.objects.filter( - user=self.request.user).first() - - # Create a vm using logged user - vm_id = manager.create_vm( - template_id=vm_template_id, - # XXX: Confi - specs=specs, - ssh_key=user_key.public_key, - ) - - # Create a Hosting Order - order = HostingOrder.create( - price=final_price, - vm_id=vm_id, - customer=customer, - billing_address=billing_address - ) - - # Create a Hosting 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 - - # Send notification to ungleich as soon as VM has been booked - context = { - 'vm': vm, - 'order': order, - 'base_url': "{0}://{1}".format(request.scheme, request.get_host()) - - } - email_data = { - 'subject': 'New VM request', - 'to': request.user.email, - 'context': context, - 'template_name': 'new_booked_vm', - 'template_path': 'hosting/emails/' - } - email = BaseEmail(**email_data) - email.send() - - return HttpResponseRedirect( - "{url}?{query_params}".format(url=reverse('hosting:orders', kwargs={'pk': order.id}), - query_params='page=payment')) + 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(reverse('hosting:orders')) else: return self.form_invalid(form)