From 6536991209fc25af113a91dfa93988ede3a00952 Mon Sep 17 00:00:00 2001 From: Arvind Tiwari Date: Fri, 6 Oct 2017 01:17:35 +0530 Subject: [PATCH 001/126] list all cards, card_input template --- .../datacenterlight/landing_payment.html | 58 +-------------- hosting/templates/hosting/payment.html | 57 +------------- hosting/templates/hosting/settings.html | 74 +++---------------- hosting/views.py | 19 +++-- 4 files changed, 20 insertions(+), 188 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/landing_payment.html b/datacenterlight/templates/datacenterlight/landing_payment.html index fa638d77..1a476797 100644 --- a/datacenterlight/templates/datacenterlight/landing_payment.html +++ b/datacenterlight/templates/datacenterlight/landing_payment.html @@ -121,63 +121,7 @@ {% else %} -
- -
-
-
- -
-
-
-
- -
-
-
- -
-
-
-
- - -
-
-
-
- {% if not messages and not form.non_field_errors %} -

- {% trans "You are not making any payment yet. After placing your order, you will be taken to the Submit Payment Page." %} -

- {% endif %} -
- {% for message in messages %} - {% if 'failed_payment' in message.tags or 'make_charge_error' in message.tags %} -
    -
  • {{ message|safe }}

  • -
- {% elif not form.non_field_errors %} -

- {% trans "You are not making any payment yet. After placing your order, you will be taken to the Submit Payment Page." %} -

- {% endif %} - {% endfor %} - - {% for error in form.non_field_errors %} -

- {{ error|escape }} -

- {% endfor %} -
-
- -
- -
-

-
-
+ {% include "hosting/includes/_card_input.html" %} {% endif %} diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index e9d6476f..a10cfbca 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -105,62 +105,7 @@ {% else %} -
- -
-
-
- -
-
-
-
- -
-
-
- -
-
-
-
- - -
-
-
-
- {% if not messages and not form.non_field_errors %} -

- {% trans "You are not making any payment yet. After submitting your card information, you will be taken to the Confirm Order Page." %} -

- {% endif %} -
- {% for message in messages %} - {% if 'failed_payment' or 'make_charge_error' in message.tags %} -
    -
  • -

    {{ message|safe }}

    -
  • -
- {% endif %} - {% endfor %} - - {% for error in form.non_field_errors %} -

- {{ error|escape }} -

- {% endfor %} -
-
- -
- - -
-

-
-
+ {% include "hosting/includes/_card_input.html" %} {% endif %} diff --git a/hosting/templates/hosting/settings.html b/hosting/templates/hosting/settings.html index 0bafe8e5..5e898b04 100644 --- a/hosting/templates/hosting/settings.html +++ b/hosting/templates/hosting/settings.html @@ -30,12 +30,13 @@

{%trans "Credit Card"%}


- {% if credit_card_data.last4 %} + {% for card in cards_list %}
{% trans "Credit Card" %}
-
{% trans "Last" %} 4: *****{{credit_card_data.last4}}
-
{% trans "Type" %}: {{credit_card_data.cc_brand}}
+
{% trans "Last" %} 4: *****{{card.last4}}
+
{% trans "Type" %}: {{card.cc_brand}}
{% comment %} + {% endcomment %}
@@ -46,75 +47,18 @@
- {% endcomment %}
+ {% endfor %} + {% if credit_card_data.last4 %} {% else %}

{% trans "No Credit Cards Added" %}

{% blocktrans %}We are using Stripe for payment and do not store your information in our database.{% endblocktrans %}

- {% comment %}

{% trans "Add a new Card." %}

-

- {% blocktrans %}Please fill in your credit card information below. We are using Stripe for payment and do not store your information in our database.{% endblocktrans %} -

-
- -
-
- -
-
-
-
- -
-
-
- -
-
-
-
- - -
-
- -
- {% if not messages and not form.non_field_errors %} -

- {% blocktrans %}You are not making any payment here.{% endblocktrans %} -

- {% endif %} -
- {% for message in messages %} - {% if 'failed_payment' or 'make_charge_error' in message.tags %} -
  • -

    {{ message|safe }}

    -
- {% endif %} - {% endfor %} - - {% for error in form.non_field_errors %} -

- {{ error|escape }} -

- {% endfor %} -
-
-
- -
-
-
- -
-

-
-
+ {% include "hosting/includes/_card_input.html" %} + {% comment %} {% endcomment %} {% endif %}
@@ -123,7 +67,6 @@ - {% comment %} {% if stripe_key %} {% get_current_language as LANGUAGE_CODE %} @@ -138,6 +81,7 @@ {%endif%} + {% comment %} {% if credit_card_data.last4 and credit_card_data.cc_brand %} {%endif%} - -{% if credit_card_data.last4 and credit_card_data.cc_brand %} - -{%endif%} - {%endblock%} From 248283b369c55b3989c421a93921453d33248f42 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 28 Oct 2017 23:44:06 +0200 Subject: [PATCH 070/126] Show user's credit cards dynamically in landing payment page --- .../datacenterlight/landing_payment.html | 35 ++++++++++++++----- datacenterlight/views.py | 11 +++++- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/landing_payment.html b/datacenterlight/templates/datacenterlight/landing_payment.html index 58246b6a..69c80d35 100644 --- a/datacenterlight/templates/datacenterlight/landing_payment.html +++ b/datacenterlight/templates/datacenterlight/landing_payment.html @@ -84,11 +84,35 @@
+ {% with card_list_len=cards_list|length %}

{%trans "Credit Card"%}


+ {% if card_list_len > 0 %} + {% blocktrans %}Please select one of the previous cards that you used before or fill in your credit card information below. We are using Stripe for payment and do not store your information in our database.{% endblocktrans %} + {% else %} {% blocktrans %}Please fill in your credit card information below. We are using Stripe for payment and do not store your information in our database.{% endblocktrans %} + {% endif %}

+
+ {% for card in cards_list %} +
+
+
{% trans "Credit Card" %}
+
{% trans "Last" %} 4: ***** {{card.last4}}
+
{% trans "Type" %}: {{card.brand}}
+
+ +
+ {% endfor %} + {% if card_list_len > 0 %} +
Use another card
+ {% endif %} + {% include "hosting/includes/_card_input.html" %} +
+ {% comment %}
{% if credit_card_data.last4 %}
@@ -125,6 +149,8 @@ {% include "hosting/includes/_card_input.html" %} {% endif %}
+ {% endcomment %} + {% endwith %}
@@ -144,13 +170,4 @@ })(); {%endif%} - -{% if credit_card_data.last4 and credit_card_data.cc_brand %} - -{%endif%} - {%endblock%} diff --git a/datacenterlight/views.py b/datacenterlight/views.py index bd1a7f51..861817e1 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -14,7 +14,7 @@ from django.views.decorators.cache import cache_control from django.views.generic import FormView, CreateView, TemplateView, DetailView from datacenterlight.tasks import create_vm_task -from hosting.models import HostingOrder +from hosting.models import HostingOrder, UserCardDetail from hosting.forms import HostingUserLoginForm from membership.models import CustomUser, StripeCustomer from opennebula_api.serializers import VMTemplateSerializer @@ -347,6 +347,14 @@ class PaymentOrderView(FormView): def get_context_data(self, **kwargs): context = super(PaymentOrderView, self).get_context_data(**kwargs) + user = self.request.user + if hasattr(user, 'stripecustomer'): + stripe_customer = user.stripecustomer + else: + stripe_customer = None + cards_list = UserCardDetail.get_all_cards_list( + stripe_customer=stripe_customer + ) if 'billing_address_data' in self.request.session: billing_address_data = self.request.session['billing_address_data'] else: @@ -380,6 +388,7 @@ class PaymentOrderView(FormView): ) context.update({ + 'cards_list': cards_list, 'stripe_key': settings.STRIPE_API_PUBLIC_KEY, 'site_url': reverse('datacenterlight:index'), 'login_form': HostingUserLoginForm(prefix='login_form'), From 2ffaee2d5b9722a50ed33e5d6a338df77af5232c Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 13:14:29 +0100 Subject: [PATCH 071/126] Add clear_items_from_list utility function --- utils/hosting_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index 3c193ad7..9541588d 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -60,3 +60,23 @@ def get_vm_price(cpu, memory, disk_size): :return: The price of the VM """ return (cpu * 5) + (memory * 2) + (disk_size * 0.6) + + +class HostingUtils: + @staticmethod + def clear_items_from_list(from_list, items_list): + """ + A utility function to clear items from a given list. + Useful when deleting items in bulk from session. + e.g.: + HostingUtils.clear_items_from_list( + request.session, + ['token', 'billing_address_data', 'card_id',] + ) + :param from_list: + :param items_list: + :return: + """ + for var in items_list: + if var in from_list: + del from_list[var] From 82ad9ac337a9f9ab83256debecc05160b7379274 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 13:17:26 +0100 Subject: [PATCH 072/126] Add selected card handling in landing flow "Order confirmation" page --- datacenterlight/views.py | 172 +++++++++++++++++++++++++++------------ 1 file changed, 122 insertions(+), 50 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 861817e1..baec4ee6 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -21,7 +21,7 @@ from opennebula_api.serializers import VMTemplateSerializer from utils.forms import ( BillingAddressForm, BillingAddressFormSignup ) -from utils.hosting_utils import get_vm_price +from utils.hosting_utils import get_vm_price, HostingUtils from utils.mailer import BaseEmail from utils.stripe_utils import StripeUtils from utils.tasks import send_plain_email_task @@ -401,6 +401,13 @@ class PaymentOrderView(FormView): # user is no longer added to session on the index page if 'specs' not in request.session: return HttpResponseRedirect(reverse('datacenterlight:index')) + HostingUtils.clear_items_from_list( + request.session, + ['token', 'billing_address_data', 'card_id', 'customer', + 'user'] + ) + if 'token' in request.session: + del request.session['token'] return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): @@ -436,8 +443,40 @@ class PaymentOrderView(FormView): 'name': request.user.name } customer = StripeCustomer.get_or_create( - email=this_user.get('email'), - token=token) + email=this_user.get('email'), token=token + ) + if token is '': + # card selected case + card_id = address_form.cleaned_data.get('card') + try: + user_card_detail = UserCardDetail.objects.get( + id=card_id) + if not request.user.has_perm( + 'view_usercarddetail', user_card_detail + ): + raise UserCardDetail.DoesNotExist( + _( + "{user} does not have permission to access the " + "card").format(user=request.user.email) + ) + except UserCardDetail.DoesNotExist as e: + ex = str(e) + logger.error( + "Card Id: {card_id}, Exception: {ex}".format( + card_id=card_id, ex=ex + ) + ) + msg = _("An error occurred. Details: {}".format(ex)) + messages.add_message( + self.request, messages.ERROR, msg, + extra_tags='make_charge_error' + ) + return HttpResponseRedirect( + reverse('hosting:payment') + '#payment_error' + ) + request.session['card_id'] = user_card_detail.id + else: + request.session['token'] = token else: user_email = address_form.cleaned_data.get('email') user_name = address_form.cleaned_data.get('name') @@ -472,7 +511,7 @@ class PaymentOrderView(FormView): email=user_email, token=token, customer_name=user_name) - + request.session['token'] = token request.session['billing_address_data'] = address_form.cleaned_data request.session['user'] = this_user # Get or create stripe customer @@ -485,7 +524,6 @@ class PaymentOrderView(FormView): billing_address_form=address_form ) ) - request.session['token'] = token if type(customer) is StripeCustomer: request.session['customer'] = customer.stripe_id else: @@ -508,27 +546,33 @@ class OrderConfirmationView(DetailView): def get(self, request, *args, **kwargs): if 'specs' not in request.session or 'user' not in request.session: return HttpResponseRedirect(reverse('datacenterlight:index')) - if 'token' not in request.session: + if 'token' not in request.session and 'card_id' not in request.session: return HttpResponseRedirect(reverse('datacenterlight:payment')) - stripe_api_cus_id = request.session.get('customer') - stripe_utils = StripeUtils() - card_details = stripe_utils.get_card_details(stripe_api_cus_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') context = { 'site_url': reverse('datacenterlight:index'), - 'cc_last4': card_details.get('response_object').get('last4'), - 'cc_brand': card_details.get('response_object').get('brand'), 'vm': request.session.get('specs'), 'page_header_text': _('Confirm Order'), 'billing_address_data': request.session.get('billing_address_data') } + if 'token' in request.session: + stripe_utils = StripeUtils() + card_details = stripe_utils.get_cards_details_from_token( + request.session.get('token') + ) + card_detail_resp = card_details.get('response_object') + if not card_detail_resp: + msg = card_details.get('error') + messages.add_message(self.request, messages.ERROR, msg, + extra_tags='failed_payment') + return HttpResponseRedirect( + reverse('datacenterlight:payment') + '#payment_error') + context['cc_last4'] = card_detail_resp.get('last4') + context['cc_brand'] = card_detail_resp.get('brand') + else: + card_id = self.request.session.get('card_id') + card_detail = UserCardDetail.objects.get(id=card_id) + context['cc_last4'] = card_detail.last4 + context['cc_brand'] = card_detail.brand return render(request, self.template_name, context) def post(self, request, *args, **kwargs): @@ -538,43 +582,62 @@ class OrderConfirmationView(DetailView): stripe_api_cus_id = request.session.get('customer') vm_template_id = template.get('id', 1) stripe_utils = StripeUtils() - card_details = stripe_utils.get_card_details(stripe_api_cus_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') - response = { - 'status': False, - 'redirect': "{url}#{section}".format( - url=reverse('datacenterlight:payment'), - section='payment_error'), - 'msg_title': str(_('Error.')), - 'msg_body': str( - _('There was a payment related error.' - ' On close of this popup, you will be redirected back to' - ' the payment page.')) + if 'token' in self.request.session: + card_details = stripe_utils.get_cards_details_from_token( + request.session['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') + response = { + 'status': False, + 'redirect': "{url}#{section}".format( + url=reverse('datacenterlight:payment'), + section='payment_error'), + 'msg_title': str(_('Error.')), + 'msg_body': str( + _('There was a payment related error.' + ' On close of this popup, you will be redirected back to' + ' the payment page.')) + } + return HttpResponse(json.dumps(response), + content_type="application/json") + card_details_dict = card_details.get('response_object') + ucd = UserCardDetail.contains( + request.user.stripecustomer, card_details_dict + ) + if not ucd: + stripe_utils.associate_customer_card( + stripe_api_cus_id, request.session['token'], + set_as_default=True + ) + else: + card_id = request.session.get('card_id') + user_card_detail = UserCardDetail.objects.get(id=card_id) + card_details_dict = { + 'last4': user_card_detail.last4, + 'brand': user_card_detail.brand } - return HttpResponse(json.dumps(response), - content_type="application/json") - card_details_dict = card_details.get('response_object') + if not user_card_detail.preferred: + UserCardDetail.set_default_card( + stripe_api_cus_id=stripe_api_cus_id, + stripe_source_id=user_card_detail.card_id + ) cpu = specs.get('cpu') memory = specs.get('memory') disk_size = specs.get('disk_size') amount_to_be_charged = specs.get('price') - plan_name = StripeUtils.get_stripe_plan_name(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') + plan_name = StripeUtils.get_stripe_plan_name( + 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) + amount=amount_to_be_charged, name=plan_name, + stripe_plan_id=stripe_plan_id + ) subscription_result = stripe_utils.subscribe_customer_to_plan( stripe_api_cus_id, [{"plan": stripe_plan.get( @@ -644,6 +707,15 @@ class OrderConfirmationView(DetailView): billing_address_data.update({ 'user': custom_user.id }) + if 'token' in request.session: + ucd = UserCardDetail.get_or_create_user_card_detail( + stripe_customer=self.request.user.stripecustomer, + card_details=card_details_dict + ) + UserCardDetail.save_default_card_local( + self.request.user.stripecustomer.stripe_id, + ucd.card_id + ) user = { 'name': custom_user.name, 'email': custom_user.email, From 23e7edf7c28af39b0d8d93517a47538b5cd1911c Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 14:40:15 +0100 Subject: [PATCH 073/126] Return True if associate_customer_card runs ok --- utils/stripe_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index 2742045a..cb9ae6fb 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -86,6 +86,7 @@ class StripeUtils(object): if set_as_default: customer.default_source = card.id customer.save() + return True @handleStripeError def dissociate_customer_card(self, stripe_customer_id, card_id): From bea3477d848a8f87526e44ece06df5c0352ee801 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 14:45:14 +0100 Subject: [PATCH 074/126] Handle errors related to payment by redirecting to payment page --- datacenterlight/views.py | 39 ++++++++++++++++++++++++++--- hosting/views.py | 54 +++++++++++++++++++++++++++++++++++----- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index baec4ee6..d9c0fbc1 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -603,21 +603,46 @@ class OrderConfirmationView(DetailView): } return HttpResponse(json.dumps(response), content_type="application/json") - card_details_dict = card_details.get('response_object') + card_details_response = card_details['response_object'] + card_details_dict = { + 'last4': card_details_response['last4'], + 'brand': card_details_response['brand'], + 'card_id': card_details_response['card_id'] + } ucd = UserCardDetail.contains( request.user.stripecustomer, card_details_dict ) if not ucd: - stripe_utils.associate_customer_card( + acc_result = stripe_utils.associate_customer_card( stripe_api_cus_id, request.session['token'], set_as_default=True ) + if acc_result['response_object'] is None: + response = { + 'status': False, + 'redirect': "{url}#{section}".format( + url=reverse('datacenterlight:payment'), + section='payment_error'), + 'msg_title': str(_('Error.')), + 'msg_body': str( + _('There was a payment related error.' + ' On close of this popup, you will be redirected back to' + ' the payment page.')) + } + logger.error( + "Card association failed. Error {error}".format( + error=acc_result['error'] + ) + ) + return HttpResponse(json.dumps(response), + content_type="application/json") else: card_id = request.session.get('card_id') user_card_detail = UserCardDetail.objects.get(id=card_id) card_details_dict = { 'last4': user_card_detail.last4, - 'brand': user_card_detail.brand + 'brand': user_card_detail.brand, + 'card_id': user_card_detail.card_id } if not user_card_detail.preferred: UserCardDetail.set_default_card( @@ -646,6 +671,14 @@ class OrderConfirmationView(DetailView): # Check if the subscription was approved and is active if (stripe_subscription_obj is None or stripe_subscription_obj.status != 'active'): + if request.user.is_authenticated(): + sac_id = request.user.stripecustomer.stripe_id + else: + sac_id = stripe_api_cus_id + stripe_utils.dissociate_customer_card( + sac_id, + card_details_dict['card_id'] + ) msg = subscription_result.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') diff --git a/hosting/views.py b/hosting/views.py index f61a4b19..6d5c9a78 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -632,6 +632,18 @@ class SettingsView(LoginRequiredMixin, FormView): msg = _('You seem to have already added this card') messages.add_message(request, messages.ERROR, msg) else: + acc_result = stripe_utils.associate_customer_card( + request.user.stripecustomer.stripe_id, token + ) + if acc_result['response_object'] is None: + msg = _( + 'An error occurred while associating the card.' + ' Details: {details}'.format( + details=acc_result['error'] + ) + ) + messages.add_message(request, messages.ERROR, msg) + return self.render_to_response(self.get_context_data()) preferred = False if stripe_customer.usercarddetail_set.count() == 0: preferred = True @@ -645,9 +657,6 @@ class SettingsView(LoginRequiredMixin, FormView): card_id=card['card_id'], preferred=preferred ) - stripe_utils.associate_customer_card( - request.user.stripecustomer.stripe_id, token - ) msg = _( "Successfully associated the card with your account" ) @@ -866,22 +875,48 @@ class OrdersHostingDetailView(LoginRequiredMixin, card_details_response = card_details['response_object'] card_details_dict = { 'last4': card_details_response['last4'], - 'brand': card_details_response['brand'] + 'brand': card_details_response['brand'], + 'card_id': card_details_response['card_id'] } ucd = UserCardDetail.contains( request.user.stripecustomer, card_details_response ) if not ucd: - stripe_utils.associate_customer_card( + acc_result = stripe_utils.associate_customer_card( stripe_api_cus_id, request.session['token'], set_as_default=True ) + if acc_result['response_object'] is None: + msg = _( + 'An error occurred while associating the card.' + ' Details: {details}'.format( + details=acc_result['error'] + ) + ) + messages.add_message(self.request, messages.ERROR, msg, + extra_tags='failed_payment') + response = { + 'status': False, + 'redirect': "{url}#{section}".format( + url=reverse('hosting:payment'), + section='payment_error'), + 'msg_title': str(_('Error.')), + 'msg_body': str( + _('There was a payment related error.' + ' On close of this popup, you will be redirected' + ' back to the payment page.') + ) + } + return HttpResponse( + json.dumps(response), content_type="application/json" + ) else: card_id = request.session.get('card_id') user_card_detail = UserCardDetail.objects.get(id=card_id) card_details_dict = { 'last4': user_card_detail.last4, - 'brand': user_card_detail.brand + 'brand': user_card_detail.brand, + 'card_id': user_card_detail.card_id } if not user_card_detail.preferred: UserCardDetail.set_default_card( @@ -911,6 +946,13 @@ class OrdersHostingDetailView(LoginRequiredMixin, # Check if the subscription was approved and is active if (stripe_subscription_obj is None or stripe_subscription_obj.status != 'active'): + # At this point, we have created a Stripe API card, but and + # associated it with the customer; but the transaction failed + # due to some reason. So, we dissociate this card. + stripe_utils.dissociate_customer_card( + request.user.stripecustomer.stripe_id, + card_details_dict['card_id'] + ) msg = subscription_result.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') From abe8c9efa52b13f4d87eb07392b63c0d3494a4ad Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 15:05:32 +0100 Subject: [PATCH 075/126] Add error details to messages datacenterlight landing flow --- datacenterlight/views.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index d9c0fbc1..f58b8276 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -610,7 +610,7 @@ class OrderConfirmationView(DetailView): 'card_id': card_details_response['card_id'] } ucd = UserCardDetail.contains( - request.user.stripecustomer, card_details_dict + request.user.stripecustomer, card_details_response ) if not ucd: acc_result = stripe_utils.associate_customer_card( @@ -618,6 +618,11 @@ class OrderConfirmationView(DetailView): set_as_default=True ) if acc_result['response_object'] is None: + msg = acc_result.get('error') + messages.add_message( + self.request, messages.ERROR, msg, + extra_tags='failed_payment' + ) response = { 'status': False, 'redirect': "{url}#{section}".format( From 63eb7fc0e2278721932adebe114e3cfe7236313e Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 21:31:11 +0100 Subject: [PATCH 076/126] Rename contains to get_user_card_details --- hosting/models.py | 9 +++++---- hosting/views.py | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/hosting/models.py b/hosting/models.py index 9486eae7..7a181fe1 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -312,21 +312,22 @@ class UserCardDetail(AssignPermissionsMixin, models.Model): user_card_detail.save() @staticmethod - def contains(stripe_customer, card_details): + def get_user_card_details(stripe_customer, card_details): """ A utility function to check whether a StripeCustomer is already associated with the card having given details + :param stripe_customer: :param card_details: - :return: + :return: The UserCardDetails object if it exists, False otherwise """ try: - UserCardDetail.objects.get( + ucd = UserCardDetail.objects.get( stripe_customer=stripe_customer, fingerprint=card_details['fingerprint'], exp_month=card_details['exp_month'], exp_year=card_details['exp_year'] ) - return True + return ucd except UserCardDetail.DoesNotExist: return False diff --git a/hosting/views.py b/hosting/views.py index 6d5c9a78..5a9c2e2e 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -628,7 +628,7 @@ class SettingsView(LoginRequiredMixin, FormView): email=request.user.email, token=token ) card = card_details['response_object'] - if UserCardDetail.contains(stripe_customer, card): + if UserCardDetail.get_user_card_details(stripe_customer, card): msg = _('You seem to have already added this card') messages.add_message(request, messages.ERROR, msg) else: @@ -878,7 +878,7 @@ class OrdersHostingDetailView(LoginRequiredMixin, 'brand': card_details_response['brand'], 'card_id': card_details_response['card_id'] } - ucd = UserCardDetail.contains( + ucd = UserCardDetail.get_user_card_details( request.user.stripecustomer, card_details_response ) if not ucd: From af1690b84621322c86fa27a07031d72388da614f Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 21:36:12 +0100 Subject: [PATCH 077/126] Fix: obtaining stripe_customer in landing flow --- datacenterlight/views.py | 82 ++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index f58b8276..1a817310 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -403,8 +403,7 @@ class PaymentOrderView(FormView): return HttpResponseRedirect(reverse('datacenterlight:index')) HostingUtils.clear_items_from_list( request.session, - ['token', 'billing_address_data', 'card_id', 'customer', - 'user'] + ['token', 'card_id', 'customer', 'user'] ) if 'token' in request.session: del request.session['token'] @@ -609,38 +608,49 @@ class OrderConfirmationView(DetailView): 'brand': card_details_response['brand'], 'card_id': card_details_response['card_id'] } - ucd = UserCardDetail.contains( - request.user.stripecustomer, card_details_response - ) - if not ucd: - acc_result = stripe_utils.associate_customer_card( - stripe_api_cus_id, request.session['token'], - set_as_default=True - ) - if acc_result['response_object'] is None: - msg = acc_result.get('error') - messages.add_message( - self.request, messages.ERROR, msg, - extra_tags='failed_payment' + s_cus = None + try: + s_cus = StripeCustomer.objects.get(stripe_id=stripe_api_cus_id) + except StripeCustomer.DoesNotExist: + pass + if s_cus: + ucd = UserCardDetail.get_user_card_details(s_cus, card_details_response) + if not ucd: + acc_result = stripe_utils.associate_customer_card( + stripe_api_cus_id, request.session['token'], + set_as_default=True ) - response = { - 'status': False, - 'redirect': "{url}#{section}".format( - url=reverse('datacenterlight:payment'), - section='payment_error'), - 'msg_title': str(_('Error.')), - 'msg_body': str( - _('There was a payment related error.' - ' On close of this popup, you will be redirected back to' - ' the payment page.')) - } - logger.error( - "Card association failed. Error {error}".format( - error=acc_result['error'] + if acc_result['response_object'] is None: + msg = acc_result.get('error') + messages.add_message( + self.request, messages.ERROR, msg, + extra_tags='failed_payment' + ) + response = { + 'status': False, + 'redirect': "{url}#{section}".format( + url=reverse('datacenterlight:payment'), + section='payment_error'), + 'msg_title': str(_('Error.')), + 'msg_body': str( + _('There was a payment related error.' + ' On close of this popup, you will be ' + 'redirected back to the payment page.') + ) + } + logger.error( + "Card association failed. Error {error}".format( + error=acc_result['error'] + ) + ) + return HttpResponse(json.dumps(response), + content_type="application/json") + else: + if not ucd.preferred: + UserCardDetail.set_default_card( + stripe_api_cus_id=stripe_api_cus_id, + stripe_source_id=ucd.card_id ) - ) - return HttpResponse(json.dumps(response), - content_type="application/json") else: card_id = request.session.get('card_id') user_card_detail = UserCardDetail.objects.get(id=card_id) @@ -745,13 +755,13 @@ class OrderConfirmationView(DetailView): billing_address_data.update({ 'user': custom_user.id }) - if 'token' in request.session: + if 'token' in request.session and s_cus is not None: ucd = UserCardDetail.get_or_create_user_card_detail( - stripe_customer=self.request.user.stripecustomer, - card_details=card_details_dict + stripe_customer=s_cus, + card_details=card_details_response ) UserCardDetail.save_default_card_local( - self.request.user.stripecustomer.stripe_id, + s_cus.stripe_id, ucd.card_id ) user = { From 618d0004f2456b236d4d427e98631234ea355922 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 21:41:54 +0100 Subject: [PATCH 078/126] Rearrange code --- datacenterlight/views.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 1a817310..36ca81d6 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -451,12 +451,11 @@ class PaymentOrderView(FormView): user_card_detail = UserCardDetail.objects.get( id=card_id) if not request.user.has_perm( - 'view_usercarddetail', user_card_detail + 'view_usercarddetail', user_card_detail ): raise UserCardDetail.DoesNotExist( - _( - "{user} does not have permission to access the " - "card").format(user=request.user.email) + _("{user} does not have permission to access" + " the card").format(user=request.user.email) ) except UserCardDetail.DoesNotExist as e: ex = str(e) @@ -496,9 +495,9 @@ class PaymentOrderView(FormView): ) ) customer = StripeCustomer.create_stripe_api_customer( - email=user_email, - token=token, - customer_name=user_name) + email=user_email, token=token, + customer_name=user_name + ) except CustomUser.DoesNotExist: logger.debug( ("StripeCustomer does not exist for {email}." @@ -597,8 +596,8 @@ class OrderConfirmationView(DetailView): 'msg_title': str(_('Error.')), 'msg_body': str( _('There was a payment related error.' - ' On close of this popup, you will be redirected back to' - ' the payment page.')) + ' On close of this popup, you will be redirected ' + 'back to the payment page.')) } return HttpResponse(json.dumps(response), content_type="application/json") From 3d8f81339b41218e58a2d8f2659d37781188838d Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 21:49:15 +0100 Subject: [PATCH 079/126] Refactor, reorganize some code --- datacenterlight/views.py | 42 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 36ca81d6..51cd18fd 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -560,10 +560,13 @@ class OrderConfirmationView(DetailView): card_detail_resp = card_details.get('response_object') if not card_detail_resp: 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, + extra_tags='failed_payment' + ) return HttpResponseRedirect( - reverse('datacenterlight:payment') + '#payment_error') + reverse('datacenterlight:payment') + '#payment_error' + ) context['cc_last4'] = card_detail_resp.get('last4') context['cc_brand'] = card_detail_resp.get('brand') else: @@ -586,8 +589,10 @@ class OrderConfirmationView(DetailView): ) if not card_details.get('response_object'): 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, + extra_tags='failed_payment' + ) response = { 'status': False, 'redirect': "{url}#{section}".format( @@ -599,8 +604,9 @@ class OrderConfirmationView(DetailView): ' On close of this popup, you will be redirected ' 'back to the payment page.')) } - return HttpResponse(json.dumps(response), - content_type="application/json") + return HttpResponse( + json.dumps(response), content_type="application/json" + ) card_details_response = card_details['response_object'] card_details_dict = { 'last4': card_details_response['last4'], @@ -709,7 +715,6 @@ class OrderConfirmationView(DetailView): } return HttpResponse(json.dumps(response), content_type="application/json") - # Create user if the user is not logged in and if he is not already # registered if not request.user.is_authenticated(): @@ -747,7 +752,6 @@ class OrderConfirmationView(DetailView): # object already exists stripe_customer_id = request.user.stripecustomer.id custom_user = request.user - # Save billing address billing_address_data = request.session.get('billing_address_data') logger.debug('billing_address_data is {}'.format(billing_address_data)) @@ -771,16 +775,16 @@ class OrderConfirmationView(DetailView): 'request_host': request.get_host(), 'language': get_language(), } - - create_vm_task.delay(vm_template_id, user, specs, template, - stripe_customer_id, billing_address_data, - stripe_subscription_obj.id, card_details_dict) - for session_var in ['specs', 'template', 'billing_address', - 'billing_address_data', - 'token', 'customer']: - if session_var in request.session: - del request.session[session_var] - + create_vm_task.delay( + vm_template_id, user, specs, template, stripe_customer_id, + billing_address_data, stripe_subscription_obj.id, + card_details_dict + ) + HostingUtils.clear_items_from_list( + request.session, + ['specs', 'template', 'billing_address', 'billing_address_data', + 'token', 'customer'] + ) response = { 'status': True, 'redirect': reverse( From edac806c118f642b737c7999c02d99643a67b639 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 21:56:29 +0100 Subject: [PATCH 080/126] Some more refactoring --- datacenterlight/views.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 51cd18fd..50148418 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -785,17 +785,18 @@ class OrderConfirmationView(DetailView): ['specs', 'template', 'billing_address', 'billing_address_data', 'token', 'customer'] ) + if request.user.is_authenticated(): + redirect_url = reverse('hosting:virtual_machines') + else: + redirect_url = reverse('datacenterlight:index') response = { 'status': True, - 'redirect': reverse( - 'hosting:virtual_machines') if request.user.is_authenticated() else reverse( - 'datacenterlight:index'), + 'redirect': redirect_url, 'msg_title': str(_('Thank you for the order.')), 'msg_body': str( _('Your VM will be up and running in a few moments.' ' We will send you a confirmation email as soon as' ' it is ready.')) } - return HttpResponse(json.dumps(response), content_type="application/json") From 62f30bf03c37301061b357ab14daa90ea363b648 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 22:03:19 +0100 Subject: [PATCH 081/126] Remove some commented and unnecessary code --- .../datacenterlight/landing_payment.html | 38 ------------------- datacenterlight/views.py | 15 -------- hosting/templates/hosting/settings.html | 10 ----- 3 files changed, 63 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/landing_payment.html b/datacenterlight/templates/datacenterlight/landing_payment.html index 69c80d35..5ac601ce 100644 --- a/datacenterlight/templates/datacenterlight/landing_payment.html +++ b/datacenterlight/templates/datacenterlight/landing_payment.html @@ -112,44 +112,6 @@ {% endif %} {% include "hosting/includes/_card_input.html" %} - {% comment %} -
- {% if credit_card_data.last4 %} - -
Credit Card
-
Last 4: *****{{credit_card_data.last4}}
-
Type: {{credit_card_data.cc_brand}}
- - - {% if not messages and not form.non_field_errors %} -

- {% trans "You are not making any payment yet. After submitting your card information, you will be taken to the Confirm Order Page." %} -

- {% endif %} -
- {% for message in messages %} - {% if 'failed_payment' or 'make_charge_error' in message.tags %} -
    -
  • -

    {{ message|safe }}

    -
  • -
- {% endif %} - {% endfor %} - {% for error in form.non_field_errors %} -

- {{ error|escape }} -

- {% endfor %} -
-
- -
- {% else %} - {% include "hosting/includes/_card_input.html" %} - {% endif %} -
- {% endcomment %} {% endwith %} diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 50148418..3a8bdac6 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -359,7 +359,6 @@ class PaymentOrderView(FormView): billing_address_data = self.request.session['billing_address_data'] else: billing_address_data = {} - if self.request.user.is_authenticated(): if billing_address_data: billing_address_form = BillingAddressForm( @@ -369,24 +368,10 @@ class PaymentOrderView(FormView): billing_address_form = BillingAddressForm( instance=self.request.user.billing_addresses.first() ) - # Get user last order - last_hosting_order = HostingOrder.objects.filter( - customer__user=self.request.user - ).last() - - # If user has already an hosting order, get the credit card - # data from it - if last_hosting_order: - credit_card_data = last_hosting_order.get_cc_data() - if credit_card_data: - context['credit_card_data'] = credit_card_data - else: - context['credit_card_data'] = None else: billing_address_form = BillingAddressFormSignup( initial=billing_address_data ) - context.update({ 'cards_list': cards_list, 'stripe_key': settings.STRIPE_API_PUBLIC_KEY, diff --git a/hosting/templates/hosting/settings.html b/hosting/templates/hosting/settings.html index 6b5acbee..64057747 100644 --- a/hosting/templates/hosting/settings.html +++ b/hosting/templates/hosting/settings.html @@ -123,14 +123,4 @@ })(); {%endif%} - - {% comment %} - {% if credit_card_data.last4 and credit_card_data.cc_brand %} - - {%endif%} - {% endcomment %} {%endblock%} From 4be27962703cdb2774e1b0d13842ba3df3b22846 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 22:40:21 +0100 Subject: [PATCH 082/126] Clean up some unnecessary code --- datacenterlight/views.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 3a8bdac6..fb34f5b4 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -348,10 +348,9 @@ class PaymentOrderView(FormView): def get_context_data(self, **kwargs): context = super(PaymentOrderView, self).get_context_data(**kwargs) user = self.request.user + stripe_customer = None if hasattr(user, 'stripecustomer'): stripe_customer = user.stripecustomer - else: - stripe_customer = None cards_list = UserCardDetail.get_all_cards_list( stripe_customer=stripe_customer ) @@ -390,8 +389,6 @@ class PaymentOrderView(FormView): request.session, ['token', 'card_id', 'customer', 'user'] ) - if 'token' in request.session: - del request.session['token'] return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): From 83dbae74e6a661b649f79d2b691ea8dc73a2e8d2 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 22:54:33 +0100 Subject: [PATCH 083/126] Add missing card_id to session variables to be cleared --- datacenterlight/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index fb34f5b4..f9d1dede 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -765,7 +765,7 @@ class OrderConfirmationView(DetailView): HostingUtils.clear_items_from_list( request.session, ['specs', 'template', 'billing_address', 'billing_address_data', - 'token', 'customer'] + 'token', 'customer', 'card_id'] ) if request.user.is_authenticated(): redirect_url = reverse('hosting:virtual_machines') From 16b6ecb38cdc87737c54e9f51e3ffa7eff51014a Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 29 Oct 2017 23:48:33 +0100 Subject: [PATCH 084/126] Clear session variables on payment error --- hosting/views.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hosting/views.py b/hosting/views.py index 5a9c2e2e..37d6eabb 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -39,7 +39,7 @@ from utils.forms import ( BillingAddressForm, PasswordResetRequestForm, UserBillingAddressForm, ResendActivationEmailForm ) -from utils.hosting_utils import get_vm_price +from utils.hosting_utils import get_vm_price, HostingUtils from utils.mailer import BaseEmail from utils.stripe_utils import StripeUtils from utils.views import ( @@ -707,6 +707,10 @@ class PaymentVMView(LoginRequiredMixin, FormView): def get(self, request, *args, **kwargs): if 'next' in request.session: del request.session['next'] + HostingUtils.clear_items_from_list( + request.session, + ['token', 'card_id', 'customer', 'user'] + ) return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): From 24d904288fd0724178af021fcbeec01fadf54947 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 30 Oct 2017 00:26:18 +0100 Subject: [PATCH 085/126] Clear token and card form variables explicitly --- datacenterlight/views.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index f9d1dede..f2af35cd 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -356,6 +356,10 @@ class PaymentOrderView(FormView): ) if 'billing_address_data' in self.request.session: billing_address_data = self.request.session['billing_address_data'] + if 'token' in billing_address_data: + billing_address_data.pop('token') + if 'card' in billing_address_data: + billing_address_data.pop('card') else: billing_address_data = {} if self.request.user.is_authenticated(): From a2a35a9475eb6bafd7d6d70d991416a21caab0f4 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 30 Oct 2017 08:26:35 +0100 Subject: [PATCH 086/126] Fix error accessing stripecustomer for user when it doesn't exist --- hosting/views.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hosting/views.py b/hosting/views.py index 37d6eabb..3b259242 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -552,8 +552,11 @@ class SettingsView(LoginRequiredMixin, FormView): def get_context_data(self, **kwargs): context = super(SettingsView, self).get_context_data(**kwargs) user = self.request.user + stripe_customer = None + if hasattr(user, 'stripecustomer'): + stripe_customer = user.stripecustomer cards_list = UserCardDetail.get_all_cards_list( - stripe_customer=user.stripecustomer + stripe_customer=stripe_customer ) context.update({ 'cards_list': cards_list, From 3e08760e044bfed66cb1363a4bfcb8de95b93d52 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 31 Oct 2017 08:58:43 +0100 Subject: [PATCH 087/126] Change letter-spacing to 2px for settings-container buttons --- hosting/static/hosting/css/commons.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hosting/static/hosting/css/commons.css b/hosting/static/hosting/css/commons.css index 33381370..32a1338e 100644 --- a/hosting/static/hosting/css/commons.css +++ b/hosting/static/hosting/css/commons.css @@ -363,7 +363,7 @@ } .settings-container .choice-btn { - letter-spacing: 1px; + letter-spacing: 2px; min-width: 127px; } From d8ce0f95c5d92d2c9648051e5cef5f54f03ce04b Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 31 Oct 2017 09:02:07 +0100 Subject: [PATCH 088/126] Change letter-spacing to 2px for btn-vm-contact --- hosting/static/hosting/css/virtual-machine.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hosting/static/hosting/css/virtual-machine.css b/hosting/static/hosting/css/virtual-machine.css index 3329d6fe..5f91fe2f 100644 --- a/hosting/static/hosting/css/virtual-machine.css +++ b/hosting/static/hosting/css/virtual-machine.css @@ -413,7 +413,7 @@ border: 2px solid #A3C0E2; padding: 5px 25px; font-size: 12px; - letter-spacing: 1.3px; + letter-spacing: 2px; } .btn-vm-contact:hover, .btn-vm-contact:focus { background: #fff; From f9bd8493330cd809f4c47ea98e7c21ba8b1fb1cd Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 31 Oct 2017 09:05:49 +0100 Subject: [PATCH 089/126] Remove redundant "previous" text --- .../templates/datacenterlight/landing_payment.html | 4 ++-- hosting/templates/hosting/payment.html | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/landing_payment.html b/datacenterlight/templates/datacenterlight/landing_payment.html index 5ac601ce..e0bd56a9 100644 --- a/datacenterlight/templates/datacenterlight/landing_payment.html +++ b/datacenterlight/templates/datacenterlight/landing_payment.html @@ -89,7 +89,7 @@

{% if card_list_len > 0 %} - {% blocktrans %}Please select one of the previous cards that you used before or fill in your credit card information below. We are using Stripe for payment and do not store your information in our database.{% endblocktrans %} + {% blocktrans %}Please select one of the cards that you used before or fill in your credit card information below. We are using Stripe for payment and do not store your information in our database.{% endblocktrans %} {% else %} {% blocktrans %}Please fill in your credit card information below. We are using Stripe for payment and do not store your information in our database.{% endblocktrans %} {% endif %} @@ -108,7 +108,7 @@ {% endfor %} {% if card_list_len > 0 %} -

Use another card
+
{% trans "Use another card" %}
{% endif %} {% include "hosting/includes/_card_input.html" %} diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index 10fd4674..a321b44c 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -72,7 +72,7 @@

{% if card_list_len > 0 %} - {% blocktrans %}Please select one of the previous cards that you used before or fill in your credit card information below. We are using Stripe for payment and do not store your information in our database.{% endblocktrans %} + {% blocktrans %}Please select one of the cards that you used before or fill in your credit card information below. We are using Stripe for payment and do not store your information in our database.{% endblocktrans %} {% else %} {% blocktrans %}Please fill in your credit card information below. We are using Stripe for payment and do not store your information in our database.{% endblocktrans %} {% endif %} @@ -91,7 +91,7 @@

{% endfor %} {% if card_list_len > 0 %} -
Use another card
+
{% trans "Use another card" %}
{% endif %} {% include "hosting/includes/_card_input.html" %} From c438c0d8cbab7d8518d929cc675fa5d8fc5d1d13 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 31 Oct 2017 09:38:36 +0100 Subject: [PATCH 090/126] Add some de translations --- .../locale/de/LC_MESSAGES/django.po | 48 +++++++--- hosting/locale/de/LC_MESSAGES/django.po | 87 +++++++++++++++---- 2 files changed, 108 insertions(+), 27 deletions(-) diff --git a/datacenterlight/locale/de/LC_MESSAGES/django.po b/datacenterlight/locale/de/LC_MESSAGES/django.po index 3b8c9ca6..2fe8dba8 100644 --- a/datacenterlight/locale/de/LC_MESSAGES/django.po +++ b/datacenterlight/locale/de/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-10-17 00:32+0530\n" +"POT-Creation-Date: 2017-10-31 08:23+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -345,6 +345,17 @@ msgstr "Monat" msgid "Credit Card" msgstr "Kreditkarte" +msgid "" +"Please select one of the cards that you used before or fill in your credit " +"card information below. We are using Stripe for payment and do not store your information in our " +"database." +msgstr "" +"Bitte wähle Seine der Karten aus, die du zuvor verwendet hast, oder gib " +"deine Kreditkartendaten unten ein. Wir verwenden Stripe als " +"Zahlungdiensleister und speichern deine Daten nicht in unserer Datenbank." + msgid "" "Please fill in your credit card information below. We are using Stripe for payment and do not " @@ -354,12 +365,17 @@ msgstr "" "\"https://stripe.com\" target=\"_blank\">Stripe für die Bezahlung und " "speichern keine Informationen in unserer Datenbank." -msgid "" -"You are not making any payment yet. After submitting your card information, " -"you will be taken to the Confirm Order Page." -msgstr "" -"Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst ausgelöst, " -"nachdem Du die Bestellung auf der nächsten Seite bestätigt hast." +msgid "Last" +msgstr "Vor" + +msgid "Type" +msgstr "Kartentyp" + +msgid "SELECT" +msgstr "Auswählen" + +msgid "Use another card" +msgstr "Benutze eine andere Kreditkarte" msgid "Processing" msgstr "Weiter" @@ -471,6 +487,13 @@ msgstr "Ungültige RAM-Grösse" msgid "Invalid storage size" msgstr "Ungültige Speicher-Grösse" +#, python-brace-format +msgid "{user} does not have permission to access the card" +msgstr "" + +msgid "An error occurred. Details: {}" +msgstr "" + msgid "Confirm Order" msgstr "Bestellung Bestätigen" @@ -494,15 +517,20 @@ msgstr "" "Deine VM ist gleich bereit. Wir senden Dir eine Bestätigungsemail, sobald Du " "auf sie zugreifen kannst." +#~ msgid "" +#~ "You are not making any payment yet. After submitting your card " +#~ "information, you will be taken to the Confirm Order Page." +#~ msgstr "" +#~ "Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst " +#~ "ausgelöst, nachdem Du die Bestellung auf der nächsten Seite bestätigt " +#~ "hast." + #~ msgid "Card Number" #~ msgstr "Kreditkartennummer" #~ msgid "Expiry Date" #~ msgstr "Ablaufdatum" -#~ msgid "Card Type" -#~ msgstr "Kartentyp" - #~ msgid "Processing..." #~ msgstr "Abarbeitung..." diff --git a/hosting/locale/de/LC_MESSAGES/django.po b/hosting/locale/de/LC_MESSAGES/django.po index 64e4a73e..3614dc49 100644 --- a/hosting/locale/de/LC_MESSAGES/django.po +++ b/hosting/locale/de/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-10-17 00:32+0530\n" +"POT-Creation-Date: 2017-10-31 08:23+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -437,6 +437,17 @@ msgstr "inkl. Mehrwertsteuer" msgid "Billing Address" msgstr "Rechnungsadresse" +msgid "" +"Please select one of the cards that you used before or fill in your credit " +"card information below. We are using Stripe for payment and do not store your information in our " +"database." +msgstr "" +"Bitte wähle Seine der Karten aus, die du zuvor verwendet hast, oder gib " +"deine Kreditkartendaten unten ein. Wir verwenden Stripe als " +"Zahlungdiensleister und speichern deine Daten nicht in unserer Datenbank." + msgid "" "Please fill in your credit card information below. We are using Stripe for payment and do not " @@ -446,12 +457,17 @@ msgstr "" "\"https://stripe.com\" target=\"_blank\">Stripe für die Bezahlung und " "speichern keine Informationen in unserer Datenbank." -msgid "" -"You are not making any payment yet. After submitting your card information, " -"you will be taken to the Confirm Order Page." -msgstr "" -"Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst ausgelöst, " -"nachdem Du die Bestellung auf der nächsten Seite bestätigt hast." +msgid "Last" +msgstr "Vor" + +msgid "Type" +msgstr "Kartentyp" + +msgid "SELECT" +msgstr "Auswählen" + +msgid "Use another card" +msgstr "Benutze eine andere Kreditkarte" msgid "Processing" msgstr "Weiter" @@ -468,16 +484,19 @@ msgstr "Passwort zurücksetzen" msgid "UPDATE" msgstr "" -msgid "Last" -msgstr "" - -msgid "Type" -msgstr "Kartentyp" - msgid "REMOVE CARD" msgstr "KARTE ENTFERNEN" -msgid "SELECT" +msgid "Remove Card" +msgstr "Karte Entfernen" + +msgid "Do you want to remove this associated card?" +msgstr "" + +msgid "Delete" +msgstr "Löschen" + +msgid "DEFAULT" msgstr "" msgid "No Credit Cards Added" @@ -534,9 +553,6 @@ msgstr "" msgid "Private Key" msgstr "" -msgid "Delete" -msgstr "Löschen" - msgid "Delete SSH Key" msgstr "SSH Key löschen" @@ -668,6 +684,35 @@ msgstr "Dein Passwort konnte nicht zurückgesetzt werden." msgid "The reset password link is no longer valid." msgstr "Der Link zum Zurücksetzen Deines Passwortes ist nicht mehr gültig." +msgid "Card deassociation successful" +msgstr "" + +msgid "You are not permitted to do this operation" +msgstr "" + +msgid "The selected card does not exist" +msgstr "" + +msgid "Billing address updated successfully" +msgstr "" + +msgid "You seem to have already added this card" +msgstr "" + +#, python-brace-format +msgid "An error occurred while associating the card. Details: {details}" +msgstr "Beim Zuordnen der Karte ist ein Fehler aufgetreten. Details: {details}" + +msgid "Successfully associated the card with your account" +msgstr "" + +#, python-brace-format +msgid "{user} does not have permission to access the card" +msgstr "" + +msgid "An error occurred. Details: {}" +msgstr "" + msgid "Invalid credit card" msgstr "Ungültige Kreditkarte" @@ -735,6 +780,14 @@ msgstr "" "Es gab einen Fehler bei der Bearbeitung Deine Anfrage. Bitte versuche es " "noch einmal." +#~ msgid "" +#~ "You are not making any payment yet. After submitting your card " +#~ "information, you will be taken to the Confirm Order Page." +#~ msgstr "" +#~ "Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst " +#~ "ausgelöst, nachdem Du die Bestellung auf der nächsten Seite bestätigt " +#~ "hast." + #~ msgid "Reset your password" #~ msgstr "Passwort zurücksetzen" From 272e14f71253dbf2dcac22a1a8f0e7cf889be301 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 27 Nov 2017 08:43:34 +0100 Subject: [PATCH 091/126] Update hosting django.po --- hosting/locale/de/LC_MESSAGES/django.po | 42 +++---------------------- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/hosting/locale/de/LC_MESSAGES/django.po b/hosting/locale/de/LC_MESSAGES/django.po index 6721a75c..6774fa8d 100644 --- a/hosting/locale/de/LC_MESSAGES/django.po +++ b/hosting/locale/de/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-10-31 08:23+0000\n" +"POT-Creation-Date: 2017-11-27 07:43+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -453,9 +453,9 @@ msgid "" "database." msgstr "" "Bitte wähle Seine der Karten aus, die du zuvor verwendet hast, oder gib " -"deine Kreditkartendaten unten ein. Wir verwenden Stripe als " -"Zahlungdiensleister und speichern deine Daten nicht in unserer Datenbank." +"deine Kreditkartendaten unten ein. Wir verwenden Stripe als Zahlungdiensleister und speichern deine " +"Daten nicht in unserer Datenbank." msgid "" "Please fill in your credit card information below. We are using Stripe für die Bezahlung und " "speichern keine Informationen in unserer Datenbank." -msgid "" -"You are not making any payment yet. After submitting your card information, " -"you will be taken to the Confirm Order Page." -msgstr "" -"Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst ausgelöst, " -"nachdem Du die Bestellung auf der nächsten Seite bestätigt hast." - -msgid "SUBMIT" -msgstr "ABSENDEN" - -msgid "Card Number" -msgstr "Kreditkartennummer" - -msgid "Expiry Date" -msgstr "Ablaufdatum" - -msgid "CVC" -msgstr "" - -msgid "Card Type" -msgstr "Kartentyp" - msgid "Last" msgstr "Vor" @@ -524,12 +502,6 @@ msgstr "Karte Entfernen" msgid "Do you want to remove this associated card?" msgstr "" -msgid "Last" -msgstr "" - -msgid "Type" -msgstr "Kartentyp" - msgid "Delete" msgstr "Löschen" @@ -590,9 +562,6 @@ msgstr "" msgid "Private Key" msgstr "" -msgid "Delete" -msgstr "Löschen" - msgid "Delete SSH Key" msgstr "SSH Key löschen" @@ -894,9 +863,6 @@ msgstr "" #~ msgid "Notifications " #~ msgstr "Benachrichtigungen" -#~ msgid "REMOVE CARD" -#~ msgstr "KARTE ENTFERNEN" - #~ msgid "EDIT CARD" #~ msgstr "BEARBEITEN" From 57eda625864732695e47f95b5dde37e3f0c4cd7a Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 12 Jun 2018 08:28:46 +0200 Subject: [PATCH 092/126] Update UserCardDetail migration --- .../{0044_usercarddetail.py => 0045_usercarddetail.py} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename hosting/migrations/{0044_usercarddetail.py => 0045_usercarddetail.py} (89%) diff --git a/hosting/migrations/0044_usercarddetail.py b/hosting/migrations/0045_usercarddetail.py similarity index 89% rename from hosting/migrations/0044_usercarddetail.py rename to hosting/migrations/0045_usercarddetail.py index 2900a806..14785689 100644 --- a/hosting/migrations/0044_usercarddetail.py +++ b/hosting/migrations/0045_usercarddetail.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.9.4 on 2017-10-21 10:26 +# Generated by Django 1.9.4 on 2018-06-12 06:28 from __future__ import unicode_literals from django.db import migrations, models @@ -10,8 +10,8 @@ import utils.mixins class Migration(migrations.Migration): dependencies = [ - ('membership', '0006_auto_20160526_0445'), - ('hosting', '0043_vmdetail'), + ('membership', '0007_auto_20180213_0128'), + ('hosting', '0044_hostingorder_vm_pricing'), ] operations = [ From 7494116468d63c96d1f178b80a6587bb9f96d448 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 12 Jun 2018 09:36:00 +0200 Subject: [PATCH 093/126] Add missing import --- hosting/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hosting/views.py b/hosting/views.py index 49937102..8ad15afa 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -34,6 +34,7 @@ from stored_messages.settings import stored_messages_settings from datacenterlight.models import VMTemplate, VMPricing from datacenterlight.tasks import create_vm_task from datacenterlight.utils import get_cms_integration +from hosting.models import UserCardDetail from membership.models import CustomUser, StripeCustomer from opennebula_api.models import OpenNebulaManager from opennebula_api.serializers import ( From 23630d44735bec8a50c09cc935907977f50fec0a Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 12 Jun 2018 11:11:37 +0200 Subject: [PATCH 094/126] Move csrf_token call outside the for loop --- hosting/templates/hosting/payment.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index befac44d..b473f31a 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -105,8 +105,8 @@

{%trans "Billing Address"%}


- {% for field in form %} {% csrf_token %} + {% for field in form %} {% bootstrap_field field show_label=False type='fields'%} {% endfor %}
From 86f0526773ff57a93cf140759875600922ba9bf7 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 12 Jun 2018 11:13:10 +0200 Subject: [PATCH 095/126] Reformat code --- hosting/views.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index 8ad15afa..b63bae9f 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -14,12 +14,12 @@ from django.core.files.base import ContentFile from django.core.urlresolvers import reverse_lazy, reverse from django.http import Http404, HttpResponseRedirect, HttpResponse from django.shortcuts import redirect, render +from django.utils.decorators import method_decorator from django.utils.html import escape from django.utils.http import urlsafe_base64_decode from django.utils.safestring import mark_safe from django.utils.translation import get_language, ugettext_lazy as _ from django.utils.translation import ugettext -from django.utils.decorators import method_decorator from django.views.decorators.cache import never_cache from django.views.generic import ( View, CreateView, FormView, ListView, DetailView, DeleteView, @@ -895,8 +895,9 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): context['vm']['price'] = price context['vm']['discount'] = discount context['vm']['vat_percent'] = vat_percent - context['vm']['total_price'] = price + \ - vat - discount['amount'] + context['vm']['total_price'] = ( + price + vat - discount['amount'] + ) except WrongIdError: messages.error( self.request, From 6db38d7e292959bfef78355225d3ef39144f8bcb Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 12 Jun 2018 11:14:36 +0200 Subject: [PATCH 096/126] Check card_id also for order confirmation --- hosting/views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hosting/views.py b/hosting/views.py index b63bae9f..47ed44a7 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -945,7 +945,9 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): return HttpResponseRedirect( reverse('hosting:create_virtual_machine') ) - if 'token' not in self.request.session: + + if ('token' not in self.request.session and + 'card_id' not in self.request.session): return HttpResponseRedirect(reverse('hosting:payment')) self.object = self.get_object() context = self.get_context_data(object=self.object) From fae9fce5c6be35851d1ce10617c415c7e6f1474d Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 12 Jun 2018 11:15:17 +0200 Subject: [PATCH 097/126] Remove unnecessary code --- hosting/views.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index 47ed44a7..dea6447d 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -834,12 +834,6 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): ).get_context_data(**kwargs) obj = self.get_object() owner = self.request.user - stripe_api_cus_id = self.request.session.get('customer') - stripe_utils = StripeUtils() - card_details = stripe_utils.get_card_details( - stripe_api_cus_id, - self.request.session.get('token') - ) if self.request.GET.get('page') == 'payment': context['page_header_text'] = _('Confirm Order') @@ -912,10 +906,6 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): _('In order to create a VM, you need to create/upload ' 'your SSH KEY first.') ) - elif not card_details.get('response_object'): - # new order, failed to get card details - context['failed_payment'] = True - context['card_details'] = card_details else: # new order, confirm payment if 'token' in self.request.session: From c9d01ba95f0d18b722623c5bc16575abf689040d Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 3 Jul 2018 21:52:44 +0200 Subject: [PATCH 098/126] Fix some errors --- hosting/views.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index 778b3835..1a9a3c30 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -15,6 +15,7 @@ from django.http import ( Http404, HttpResponseRedirect, HttpResponse, JsonResponse ) from django.shortcuts import redirect, render +from django.utils.html import escape from django.utils.http import urlsafe_base64_decode from django.utils.safestring import mark_safe from django.utils.translation import get_language, ugettext_lazy as _ @@ -1012,9 +1013,7 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): ' back to the payment page.') ) } - return HttpResponse( - json.dumps(response), content_type="application/json" - ) + return JsonResponse(response) else: card_id = request.session.get('card_id') user_card_detail = UserCardDetail.objects.get(id=card_id) From 9904a71d384f8a31d9a3bc0de42455c8c7e9d533 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 3 Jul 2018 22:34:59 +0200 Subject: [PATCH 099/126] Rename usercarddetail migration --- .../{0045_usercarddetail.py => 0046_usercarddetail.py} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename hosting/migrations/{0045_usercarddetail.py => 0046_usercarddetail.py} (92%) diff --git a/hosting/migrations/0045_usercarddetail.py b/hosting/migrations/0046_usercarddetail.py similarity index 92% rename from hosting/migrations/0045_usercarddetail.py rename to hosting/migrations/0046_usercarddetail.py index 14785689..97c1e94a 100644 --- a/hosting/migrations/0045_usercarddetail.py +++ b/hosting/migrations/0046_usercarddetail.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.9.4 on 2018-06-12 06:28 +# Generated by Django 1.9.4 on 2018-07-03 20:32 from __future__ import unicode_literals from django.db import migrations, models @@ -11,7 +11,7 @@ class Migration(migrations.Migration): dependencies = [ ('membership', '0007_auto_20180213_0128'), - ('hosting', '0044_hostingorder_vm_pricing'), + ('hosting', '0045_auto_20180701_2028'), ] operations = [ From 1c5ff1f9dd4da604e3b57fd4eaa3d4e0e74fdda7 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 3 Jul 2018 23:22:10 +0200 Subject: [PATCH 100/126] Add UserCardDetail in the landing non-logged in flow --- datacenterlight/views.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index db36d23a..3ea3cdc8 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -13,7 +13,7 @@ from django.views.decorators.cache import cache_control from django.views.generic import FormView, CreateView, DetailView from hosting.forms import HostingUserLoginForm -from hosting.models import HostingOrder +from hosting.models import HostingOrder, UserCardDetail from membership.models import CustomUser, StripeCustomer from opennebula_api.serializers import VMTemplateSerializer from utils.forms import BillingAddressForm, BillingAddressFormSignup @@ -397,9 +397,9 @@ class OrderConfirmationView(DetailView): stripe_api_cus_id = request.session.get('customer') vm_template_id = template.get('id', 1) stripe_utils = StripeUtils() - card_details = stripe_utils.get_card_details(stripe_api_cus_id, - request.session.get( - 'token')) + card_details = stripe_utils.get_cards_details_from_token( + request.session.get('token') + ) if not card_details.get('response_object'): msg = card_details.get('error') messages.add_message(self.request, messages.ERROR, msg, @@ -493,6 +493,7 @@ class OrderConfirmationView(DetailView): else: # We assume that if the user is here, his/her StripeCustomer # object already exists + stripe_customer = request.user.stripecustomer stripe_customer_id = request.user.stripecustomer.id custom_user = request.user @@ -502,6 +503,16 @@ class OrderConfirmationView(DetailView): billing_address_data.update({ 'user': custom_user.id }) + + if 'token' in request.session: + ucd = UserCardDetail.get_or_create_user_card_detail( + stripe_customer=stripe_customer, + card_details=card_details_dict + ) + UserCardDetail.save_default_card_local( + stripe_customer.stripe_id, + ucd.card_id + ) user = { 'name': custom_user.name, 'email': custom_user.email, From d98a683b2a389f05df2e83150e84e3b668f739ae Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 3 Jul 2018 23:23:02 +0200 Subject: [PATCH 101/126] Do a get on UserCardDetail only when fingerprint, exp_month and exp_year are known --- hosting/models.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/hosting/models.py b/hosting/models.py index e1577bd8..b5fc9023 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -272,12 +272,16 @@ class UserCardDetail(AssignPermissionsMixin, models.Model): :return: UserCardDetail object """ try: - card_detail = UserCardDetail.objects.get( - stripe_customer=stripe_customer, - fingerprint=card_details['fingerprint'], - exp_month=card_details['exp_month'], - exp_year=card_details['exp_year'] - ) + if ('fingerprint' in card_details and 'exp_month' in card_details + and 'exp_year' in card_details): + card_detail = UserCardDetail.objects.get( + stripe_customer=stripe_customer, + fingerprint=card_details['fingerprint'], + exp_month=card_details['exp_month'], + exp_year=card_details['exp_year'] + ) + else: + raise UserCardDetail.DoesNotExist() except UserCardDetail.DoesNotExist: preferred = False if 'preferred' in card_details: From c118e86230e1905f9c64a4aa2a796b4c0607d586 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 3 Jul 2018 23:56:42 +0200 Subject: [PATCH 102/126] Remove unused context parameters -- introduced during merge --- hosting/views.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index 1a9a3c30..a28815f2 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -930,10 +930,6 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): context['cc_last4'] = card_detail.last4 context['cc_brand'] = card_detail.brand context['site_url'] = reverse('hosting:create_virtual_machine') - context['cc_last4'] = card_details.get('response_object').get( - 'last4') - context['cc_brand'] = card_details.get('response_object').get( - 'cc_brand') context['vm'] = self.request.session.get('specs') return context From 3d8237a34a5a91346456a6ee99d89f169fd89686 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 4 Jul 2018 00:50:44 +0200 Subject: [PATCH 103/126] Get and show all card details in the landing flow, if the user is logged in --- datacenterlight/views.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 3ea3cdc8..616dde30 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -222,19 +222,15 @@ class PaymentOrderView(FormView): billing_address_form = BillingAddressForm( instance=self.request.user.billing_addresses.first() ) - # Get user last order - last_hosting_order = HostingOrder.objects.filter( - customer__user=self.request.user - ).last() - - # If user has already an hosting order, get the credit card - # data from it - if last_hosting_order: - credit_card_data = last_hosting_order.get_cc_data() - if credit_card_data: - context['credit_card_data'] = credit_card_data - else: - context['credit_card_data'] = None + user = self.request.user + if hasattr(user, 'stripecustomer'): + stripe_customer = user.stripecustomer + else: + stripe_customer = None + cards_list = UserCardDetail.get_all_cards_list( + stripe_customer=stripe_customer + ) + context.update({'cards_list': cards_list}) else: billing_address_form = BillingAddressFormSignup( initial=billing_address_data From 8e742852a58302cb0291430abe94afcc37de2d21 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 4 Jul 2018 02:03:08 +0200 Subject: [PATCH 104/126] Show all cards in landing and hosting payment page --- .../datacenterlight/landing_payment.html | 24 +++++++++++++++++-- hosting/templates/hosting/payment.html | 24 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/landing_payment.html b/datacenterlight/templates/datacenterlight/landing_payment.html index 256b0931..b774f4c2 100644 --- a/datacenterlight/templates/datacenterlight/landing_payment.html +++ b/datacenterlight/templates/datacenterlight/landing_payment.html @@ -125,9 +125,29 @@ {% endfor %} {% if card_list_len > 0 %} -
{% trans "Use another card" %}
+
+
+
+

{% trans "Add a new credit card" %}

+
+
+ +
+
+
+
+
+
+

{%trans "New Credit Card" %}

+
+ {% include "hosting/includes/_card_input.html" %} +
+
+ {% else%} + {% include "hosting/includes/_card_input.html" %} {% endif %} - {% include "hosting/includes/_card_input.html" %} {% endwith %} diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index b473f31a..e09775cf 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -138,9 +138,29 @@ {% endfor %} {% if card_list_len > 0 %} -
{% trans "Use another card" %}
+
+
+
+

{% trans "Add a new credit card" %}

+
+
+ +
+
+
+
+
+
+

{%trans "New Credit Card" %}

+
+ {% include "hosting/includes/_card_input.html" %} +
+
+ {% else%} + {% include "hosting/includes/_card_input.html" %} {% endif %} - {% include "hosting/includes/_card_input.html" %} {% endwith %} From dc28186fe9e7cb6d69c9d4d2d87431088f611a2b Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 4 Jul 2018 02:03:50 +0200 Subject: [PATCH 105/126] Add styles for landing payment choices --- .../static/datacenterlight/css/common.css | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/datacenterlight/static/datacenterlight/css/common.css b/datacenterlight/static/datacenterlight/css/common.css index b6eabd75..4b3b7b41 100644 --- a/datacenterlight/static/datacenterlight/css/common.css +++ b/datacenterlight/static/datacenterlight/css/common.css @@ -158,4 +158,17 @@ footer .dcl-link-separator::before { .thin-hr { margin-top: 10px; margin-bottom: 10px; +} + +.payment-container .credit-card-info { + padding-bottom: 15px; + border-bottom: 1px solid #eee; +} +.credit-card-info { + display: flex; +} + +.credit-card-info .align-bottom { + align-self: flex-end; + padding-right: 0 !important; } \ No newline at end of file From 9bf8992ff4f0142fbf178cd95592e8b7d08b36e5 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 4 Jul 2018 02:53:43 +0200 Subject: [PATCH 106/126] Landing flow: set/get token/card_id --- datacenterlight/views.py | 82 ++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 616dde30..26afd574 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -282,14 +282,55 @@ class PaymentOrderView(FormView): ) if address_form.is_valid(): token = address_form.cleaned_data.get('token') + owner = self.request.user + if token is '': + card_id = address_form.cleaned_data.get('card') + customer = owner.stripecustomer + try: + user_card_detail = UserCardDetail.objects.get(id=card_id) + if not request.user.has_perm( + 'view_usercarddetail', user_card_detail + ): + raise UserCardDetail.DoesNotExist( + _("{user} does not have permission to access the " + "card").format(user=request.user.email) + ) + except UserCardDetail.DoesNotExist as e: + ex = str(e) + logger.error("Card Id: {card_id}, Exception: {ex}".format( + card_id=card_id, ex=ex + ) + ) + msg = _("An error occurred. Details: {}".format(ex)) + messages.add_message( + self.request, messages.ERROR, msg, + extra_tags='make_charge_error' + ) + return HttpResponseRedirect( + reverse('datacenterlight:payment') + '#payment_error' + ) + request.session['card_id'] = user_card_detail.id + else: + # Get or create stripe customer + 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('datacenterlight:payment') + '#payment_error') + request.session['token'] = token if request.user.is_authenticated(): this_user = { 'email': request.user.email, 'name': request.user.name } customer = StripeCustomer.get_or_create( - email=this_user.get('email'), - token=token) + email=this_user.get('email'), token=token + ) else: user_email = address_form.cleaned_data.get('email') user_name = address_form.cleaned_data.get('name') @@ -337,7 +378,6 @@ class PaymentOrderView(FormView): billing_address_form=address_form ) ) - request.session['token'] = token if type(customer) is StripeCustomer: request.session['customer'] = customer.stripe_id else: @@ -358,32 +398,34 @@ class OrderConfirmationView(DetailView): @cache_control(no_cache=True, must_revalidate=True, no_store=True) def get(self, request, *args, **kwargs): + context = {} if 'specs' not in request.session or 'user' not in request.session: return HttpResponseRedirect(reverse('datacenterlight:index')) - if 'token' not in request.session: - return HttpResponseRedirect(reverse('datacenterlight:payment')) - stripe_api_cus_id = request.session.get('customer') - stripe_utils = StripeUtils() - card_details = stripe_utils.get_card_details(stripe_api_cus_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') - context = { + if 'token' in self.request.session: + token = self.request.session['token'] + stripe_utils = StripeUtils() + card_details = stripe_utils.get_cards_details_from_token( + token + ) + if not card_details.get('response_object'): + return HttpResponseRedirect(reverse('hosting:payment')) + card_details_response = card_details['response_object'] + context['cc_last4'] = card_details_response['last4'] + context['cc_brand'] = card_details_response['brand'] + else: + card_id = self.request.session.get('card_id') + card_detail = UserCardDetail.objects.get(id=card_id) + context['cc_last4'] = card_detail.last4 + context['cc_brand'] = card_detail.brand + context.update({ 'site_url': reverse('datacenterlight:index'), - 'cc_last4': card_details.get('response_object').get('last4'), - 'cc_brand': card_details.get('response_object').get('brand'), 'vm': request.session.get('specs'), 'page_header_text': _('Confirm Order'), 'billing_address_data': ( request.session.get('billing_address_data') ), 'cms_integration': get_cms_integration('default'), - } + }) return render(request, self.template_name, context) def post(self, request, *args, **kwargs): From ec0216790fb08f483a06fd5e4846f52ec88b3576 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 09:55:45 +0200 Subject: [PATCH 107/126] Remove redundant code --- datacenterlight/views.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 26afd574..02e91470 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -311,17 +311,6 @@ class PaymentOrderView(FormView): ) request.session['card_id'] = user_card_detail.id else: - # Get or create stripe customer - 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('datacenterlight:payment') + '#payment_error') request.session['token'] = token if request.user.is_authenticated(): this_user = { From e9ac699be8b69d75098712b427e1dc7e16c0fc13 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 09:57:18 +0200 Subject: [PATCH 108/126] Add card detail creation code in various landing cases --- datacenterlight/views.py | 149 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 141 insertions(+), 8 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 02e91470..cd69c3ad 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -424,13 +424,75 @@ class OrderConfirmationView(DetailView): stripe_api_cus_id = request.session.get('customer') vm_template_id = template.get('id', 1) stripe_utils = StripeUtils() - card_details = stripe_utils.get_cards_details_from_token( - 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') + + if 'token' in request.session: + card_details = stripe_utils.get_cards_details_from_token( + 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') + response = { + 'status': False, + 'redirect': "{url}#{section}".format( + url=reverse('datacenterlight:payment'), + section='payment_error'), + 'msg_title': str(_('Error.')), + 'msg_body': str( + _('There was a payment related error.' + ' On close of this popup, you will be' + ' redirected back to the payment page.') + ) + } + return JsonResponse(response) + card_details_response = card_details['response_object'] + card_details_dict = { + 'last4': card_details_response['last4'], + 'brand': card_details_response['brand'], + 'card_id': card_details_response['card_id'] + } + stripe_customer_obj = StripeCustomer.objects.filter(stripe_id=stripe_api_cus_id).first() + if stripe_customer_obj: + ucd = UserCardDetail.get_user_card_details( + stripe_customer_obj, card_details_response + ) + if not ucd: + acc_result = stripe_utils.associate_customer_card( + stripe_api_cus_id, request.session['token'], + set_as_default=True + ) + if acc_result['response_object'] is None: + msg = _( + 'An error occurred while associating the card.' + ' Details: {details}'.format( + details=acc_result['error'] + ) + ) + messages.add_message(self.request, messages.ERROR, msg, + extra_tags='failed_payment') + response = { + 'status': False, + 'redirect': "{url}#{section}".format( + url=reverse('hosting:payment'), + section='payment_error'), + 'msg_title': str(_('Error.')), + 'msg_body': str( + _('There was a payment related error.' + ' On close of this popup, you will be redirected' + ' back to the payment page.') + ) + } + return JsonResponse(response) + elif 'card_id' in request.session: + card_id = request.session.get('card_id') + user_card_detail = UserCardDetail.objects.get(id=card_id) + card_details_dict = { + 'last4': user_card_detail.last4, + 'brand': user_card_detail.brand, + 'card_id': user_card_detail.card_id + } + else: response = { 'status': False, 'redirect': "{url}#{section}".format( @@ -444,7 +506,6 @@ class OrderConfirmationView(DetailView): } return JsonResponse(response) - card_details_dict = card_details.get('response_object') cpu = specs.get('cpu') memory = specs.get('memory') disk_size = specs.get('disk_size') @@ -498,6 +559,30 @@ class OrderConfirmationView(DetailView): user=custom_user, stripe_id=stripe_api_cus_id ) stripe_customer_id = stripe_customer.id + + if 'token' in request.session: + ucd = UserCardDetail.get_or_create_user_card_detail( + stripe_customer=self.request.user.stripecustomer, + card_details=card_details_response + ) + UserCardDetail.save_default_card_local( + self.request.user.stripecustomer.stripe_id, + ucd.card_id + ) + + else: + card_id = request.session.get('card_id') + user_card_detail = UserCardDetail.objects.get(id=card_id) + card_details_dict = { + 'last4': user_card_detail.last4, + 'brand': user_card_detail.brand, + 'card_id': user_card_detail.card_id + } + if not user_card_detail.preferred: + UserCardDetail.set_default_card( + stripe_api_cus_id=stripe_api_cus_id, + stripe_source_id=user_card_detail.card_id + ) except CustomUser.DoesNotExist: logger.debug( "Customer {} does not exist.".format(user.get('email'))) @@ -517,6 +602,31 @@ class OrderConfirmationView(DetailView): new_user = authenticate(username=custom_user.email, password=password) login(request, new_user) + + if 'token' in request.session: + ucd = UserCardDetail.get_or_create_user_card_detail( + stripe_customer=self.request.user.stripecustomer, + card_details=card_details_response + ) + UserCardDetail.save_default_card_local( + self.request.user.stripecustomer.stripe_id, + ucd.card_id + ) + + else: + card_id = request.session.get('card_id') + user_card_detail = UserCardDetail.objects.get(id=card_id) + card_details_dict = { + 'last4': user_card_detail.last4, + 'brand': user_card_detail.brand, + 'card_id': user_card_detail.card_id + } + if not user_card_detail.preferred: + UserCardDetail.set_default_card( + stripe_api_cus_id=stripe_api_cus_id, + stripe_source_id=user_card_detail.card_id + ) + else: # We assume that if the user is here, his/her StripeCustomer # object already exists @@ -524,6 +634,29 @@ class OrderConfirmationView(DetailView): stripe_customer_id = request.user.stripecustomer.id custom_user = request.user + if 'token' in request.session: + ucd = UserCardDetail.get_or_create_user_card_detail( + stripe_customer=self.request.user.stripecustomer, + card_details=card_details_response + ) + UserCardDetail.save_default_card_local( + self.request.user.stripecustomer.stripe_id, + ucd.card_id + ) + else: + card_id = request.session.get('card_id') + user_card_detail = UserCardDetail.objects.get(id=card_id) + card_details_dict = { + 'last4': user_card_detail.last4, + 'brand': user_card_detail.brand, + 'card_id': user_card_detail.card_id + } + if not user_card_detail.preferred: + UserCardDetail.set_default_card( + stripe_api_cus_id=stripe_api_cus_id, + stripe_source_id=user_card_detail.card_id + ) + # Save billing address billing_address_data = request.session.get('billing_address_data') logger.debug('billing_address_data is {}'.format(billing_address_data)) From 8d7b01d7e273ac48a9aaa1ea9ba2b85ed084a6f4 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 09:57:47 +0200 Subject: [PATCH 109/126] Disable dissociating card on subscription failure --- datacenterlight/views.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index cd69c3ad..b93df4d8 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -530,6 +530,13 @@ class OrderConfirmationView(DetailView): # Check if the subscription was approved and is active if (stripe_subscription_obj is None or stripe_subscription_obj.status != 'active'): + # At this point, we have created a Stripe API card and + # associated it with the customer; but the transaction failed + # due to some reason. + # stripe_utils.dissociate_customer_card( + # stripe_api_cus_id, + # card_details_dict['card_id'] + # ) msg = subscription_result.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') From faa0604faef9caa4934924aebce0851e9464a7f3 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 09:58:07 +0200 Subject: [PATCH 110/126] Remove redundant code --- datacenterlight/views.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index b93df4d8..c0d509cf 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -671,15 +671,6 @@ class OrderConfirmationView(DetailView): 'user': custom_user.id }) - if 'token' in request.session: - ucd = UserCardDetail.get_or_create_user_card_detail( - stripe_customer=stripe_customer, - card_details=card_details_dict - ) - UserCardDetail.save_default_card_local( - stripe_customer.stripe_id, - ucd.card_id - ) user = { 'name': custom_user.name, 'email': custom_user.email, From 9035f9806040ec6dbe52180daf3b18cdf07b9e41 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 09:58:57 +0200 Subject: [PATCH 111/126] Update UserCardDetail get method --- hosting/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hosting/models.py b/hosting/models.py index b5fc9023..3ae3b0a5 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -347,7 +347,7 @@ class UserCardDetail(AssignPermissionsMixin, models.Model): :param stripe_customer: :param card_details: - :return: The UserCardDetails object if it exists, False otherwise + :return: The UserCardDetails object if it exists, None otherwise """ try: ucd = UserCardDetail.objects.get( @@ -358,4 +358,4 @@ class UserCardDetail(AssignPermissionsMixin, models.Model): ) return ucd except UserCardDetail.DoesNotExist: - return False + return None From baa2817f573a4c437dee9c5319588feca6a50b30 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 10:00:12 +0200 Subject: [PATCH 112/126] Update BillingAddressFormSignup: add email validation --- utils/forms.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/utils/forms.py b/utils/forms.py index bd00b42d..fe5dc282 100644 --- a/utils/forms.py +++ b/utils/forms.py @@ -137,6 +137,31 @@ class BillingAddressFormSignup(BillingAddressForm): email = forms.EmailField(label=_('Email Address')) field_order = ['name', 'email'] + class Meta: + model = BillingAddress + fields = ['name', 'email', 'cardholder_name', 'street_address', + 'city', 'postal_code', 'country'] + labels = { + 'name': 'Name', + 'email': _('Email'), + 'cardholder_name': _('Cardholder Name'), + 'street_address': _('Street Address'), + 'city': _('City'), + 'postal_code': _('Postal Code'), + 'Country': _('Country'), + } + + def clean_email(self): + email = self.cleaned_data.get('email') + try: + CustomUser.objects.get(email=email) + raise forms.ValidationError( + _("The email {} is already registered with us. Please reset " + "your password and access your account.".format(email)) + ) + except CustomUser.DoesNotExist: + return email + class UserBillingAddressForm(forms.ModelForm): user = forms.ModelChoiceField(queryset=CustomUser.objects.all(), From 6b663d82a1f8246d67d1c4f6806c833d08240fef Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 20:34:57 +0200 Subject: [PATCH 113/126] Remove unused variable --- datacenterlight/views.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index c0d509cf..6d129ebd 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -282,10 +282,8 @@ class PaymentOrderView(FormView): ) if address_form.is_valid(): token = address_form.cleaned_data.get('token') - owner = self.request.user if token is '': card_id = address_form.cleaned_data.get('card') - customer = owner.stripecustomer try: user_card_detail = UserCardDetail.objects.get(id=card_id) if not request.user.has_perm( From 1e214f7b210becdecc3d25b4707eccff0de9e50a Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 20:35:34 +0200 Subject: [PATCH 114/126] Improve comment --- datacenterlight/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 6d129ebd..127a272e 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -530,7 +530,7 @@ class OrderConfirmationView(DetailView): or stripe_subscription_obj.status != 'active'): # At this point, we have created a Stripe API card and # associated it with the customer; but the transaction failed - # due to some reason. + # due to some reason. We may want to dissociate this card. # stripe_utils.dissociate_customer_card( # stripe_api_cus_id, # card_details_dict['card_id'] From 5cd62abc70453b215e925a98f4478de73a50b434 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 20:36:43 +0200 Subject: [PATCH 115/126] Refactor code --- datacenterlight/views.py | 92 +++++++++------------------------------- 1 file changed, 21 insertions(+), 71 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 127a272e..649810d0 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -564,30 +564,6 @@ class OrderConfirmationView(DetailView): user=custom_user, stripe_id=stripe_api_cus_id ) stripe_customer_id = stripe_customer.id - - if 'token' in request.session: - ucd = UserCardDetail.get_or_create_user_card_detail( - stripe_customer=self.request.user.stripecustomer, - card_details=card_details_response - ) - UserCardDetail.save_default_card_local( - self.request.user.stripecustomer.stripe_id, - ucd.card_id - ) - - else: - card_id = request.session.get('card_id') - user_card_detail = UserCardDetail.objects.get(id=card_id) - card_details_dict = { - 'last4': user_card_detail.last4, - 'brand': user_card_detail.brand, - 'card_id': user_card_detail.card_id - } - if not user_card_detail.preferred: - UserCardDetail.set_default_card( - stripe_api_cus_id=stripe_api_cus_id, - stripe_source_id=user_card_detail.card_id - ) except CustomUser.DoesNotExist: logger.debug( "Customer {} does not exist.".format(user.get('email'))) @@ -607,60 +583,34 @@ class OrderConfirmationView(DetailView): new_user = authenticate(username=custom_user.email, password=password) login(request, new_user) - - if 'token' in request.session: - ucd = UserCardDetail.get_or_create_user_card_detail( - stripe_customer=self.request.user.stripecustomer, - card_details=card_details_response - ) - UserCardDetail.save_default_card_local( - self.request.user.stripecustomer.stripe_id, - ucd.card_id - ) - - else: - card_id = request.session.get('card_id') - user_card_detail = UserCardDetail.objects.get(id=card_id) - card_details_dict = { - 'last4': user_card_detail.last4, - 'brand': user_card_detail.brand, - 'card_id': user_card_detail.card_id - } - if not user_card_detail.preferred: - UserCardDetail.set_default_card( - stripe_api_cus_id=stripe_api_cus_id, - stripe_source_id=user_card_detail.card_id - ) - else: # We assume that if the user is here, his/her StripeCustomer # object already exists - stripe_customer = request.user.stripecustomer stripe_customer_id = request.user.stripecustomer.id custom_user = request.user - if 'token' in request.session: - ucd = UserCardDetail.get_or_create_user_card_detail( - stripe_customer=self.request.user.stripecustomer, - card_details=card_details_response + if 'token' in request.session: + ucd = UserCardDetail.get_or_create_user_card_detail( + stripe_customer=self.request.user.stripecustomer, + card_details=card_details_response + ) + UserCardDetail.save_default_card_local( + self.request.user.stripecustomer.stripe_id, + ucd.card_id + ) + else: + card_id = request.session.get('card_id') + user_card_detail = UserCardDetail.objects.get(id=card_id) + card_details_dict = { + 'last4': user_card_detail.last4, + 'brand': user_card_detail.brand, + 'card_id': user_card_detail.card_id + } + if not user_card_detail.preferred: + UserCardDetail.set_default_card( + stripe_api_cus_id=stripe_api_cus_id, + stripe_source_id=user_card_detail.card_id ) - UserCardDetail.save_default_card_local( - self.request.user.stripecustomer.stripe_id, - ucd.card_id - ) - else: - card_id = request.session.get('card_id') - user_card_detail = UserCardDetail.objects.get(id=card_id) - card_details_dict = { - 'last4': user_card_detail.last4, - 'brand': user_card_detail.brand, - 'card_id': user_card_detail.card_id - } - if not user_card_detail.preferred: - UserCardDetail.set_default_card( - stripe_api_cus_id=stripe_api_cus_id, - stripe_source_id=user_card_detail.card_id - ) # Save billing address billing_address_data = request.session.get('billing_address_data') From 0db4a113e6d1fa281ec0368297240dc254e81bb2 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 5 Jul 2018 23:06:47 +0200 Subject: [PATCH 116/126] Add some styles and do not let user remove card if he has only one --- datacenterlight/static/datacenterlight/css/common.css | 8 ++++++++ .../templates/datacenterlight/landing_payment.html | 2 +- hosting/static/hosting/css/commons.css | 9 +++++++++ hosting/templates/hosting/settings.html | 4 ++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/datacenterlight/static/datacenterlight/css/common.css b/datacenterlight/static/datacenterlight/css/common.css index 4b3b7b41..28674b30 100644 --- a/datacenterlight/static/datacenterlight/css/common.css +++ b/datacenterlight/static/datacenterlight/css/common.css @@ -171,4 +171,12 @@ footer .dcl-link-separator::before { .credit-card-info .align-bottom { align-self: flex-end; padding-right: 0 !important; +} + +.new-card-head { + margin-top: 10px; +} +.new-card-button-margin button{ + margin-top: 5px; + margin-bottom: 5px; } \ No newline at end of file diff --git a/datacenterlight/templates/datacenterlight/landing_payment.html b/datacenterlight/templates/datacenterlight/landing_payment.html index b774f4c2..4c43f41c 100644 --- a/datacenterlight/templates/datacenterlight/landing_payment.html +++ b/datacenterlight/templates/datacenterlight/landing_payment.html @@ -130,7 +130,7 @@

{% trans "Add a new credit card" %}

-
+
diff --git a/hosting/static/hosting/css/commons.css b/hosting/static/hosting/css/commons.css index 5de13353..292aeda9 100644 --- a/hosting/static/hosting/css/commons.css +++ b/hosting/static/hosting/css/commons.css @@ -431,3 +431,12 @@ margin-top: 10px; margin-bottom: 10px; } + + +.new-card-head { + margin-top: 10px; +} +.new-card-button-margin button{ + margin-top: 5px; + margin-bottom: 5px; +} \ No newline at end of file diff --git a/hosting/templates/hosting/settings.html b/hosting/templates/hosting/settings.html index 64057747..62dcc947 100644 --- a/hosting/templates/hosting/settings.html +++ b/hosting/templates/hosting/settings.html @@ -31,6 +31,7 @@

{%trans "Credit Card" %}


+ {% with card_list_len=cards_list|length %} {% for card in cards_list %}
{% trans "Credit Card" %}
@@ -38,6 +39,7 @@
{% trans "Type" %}: {{card.brand}}
+ {% if card_list_len > 1 %}
{% trans "REMOVE CARD" %}
+ {% endif %}
{% if card.preferred %} {% trans "DEFAULT" %} @@ -83,6 +86,7 @@

{% blocktrans %}We are using Stripe for payment and do not store your information in our database.{% endblocktrans %}

{% endfor %} + {% endwith %}
From 1f2743a65d4843ee7537da2d72593e316397354c Mon Sep 17 00:00:00 2001 From: PCoder Date: Fri, 6 Jul 2018 01:19:44 +0200 Subject: [PATCH 117/126] Makemessages for datacenterlight, hosting and utils --- .../locale/de/LC_MESSAGES/django.po | 79 ++++++++---- hosting/locale/de/LC_MESSAGES/django.po | 119 +++++++++++++----- utils/locale/de/LC_MESSAGES/django.po | 11 +- 3 files changed, 149 insertions(+), 60 deletions(-) diff --git a/datacenterlight/locale/de/LC_MESSAGES/django.po b/datacenterlight/locale/de/LC_MESSAGES/django.po index 4a95c2fc..42eab589 100644 --- a/datacenterlight/locale/de/LC_MESSAGES/django.po +++ b/datacenterlight/locale/de/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-05-12 21:43+0530\n" +"POT-Creation-Date: 2018-07-05 23:11+0000\n" "PO-Revision-Date: 2018-03-30 23:22+0000\n" "Last-Translator: b'Anonymous User '\n" "Language-Team: LANGUAGE \n" @@ -329,6 +329,14 @@ msgstr "wird an der Kasse angewendet" msgid "Credit Card" msgstr "Kreditkarte" +msgid "" +"Please select one of the cards that you used before or fill in your credit " +"card information below. We are using Stripe for payment and do not store your information in our " +"database." +msgstr "" +"" + msgid "" "Please fill in your credit card information below. We are using Stripe for payment and do not " @@ -338,31 +346,23 @@ msgstr "" "\"https://stripe.com\" target=\"_blank\">Stripe für die Bezahlung und " "speichern keine Informationen in unserer Datenbank." -msgid "" -"You are not making any payment yet. After submitting your card information, " -"you will be taken to the Confirm Order Page." -msgstr "" -"Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst ausgelöst, " -"nachdem Du die Bestellung auf der nächsten Seite bestätigt hast." - -msgid "Card Number" -msgstr "Kreditkartennummer" - -msgid "Expiry Date" -msgstr "Ablaufdatum" - -msgid "CVC" +msgid "Last" msgstr "" -msgid "Card Type" -msgstr "Kartentyp" - -msgid "" -"You are not making any payment yet. After placing your order, you will be " -"taken to the Submit Payment Page." +msgid "Type" +msgstr "" + +msgid "SELECT" +msgstr "" + +msgid "Add a new credit card" +msgstr "" + +msgid "NEW CARD" +msgstr "" + +msgid "New Credit Card" msgstr "" -"Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst ausgelöst, " -"nachdem Du die Bestellung auf der nächsten Seite bestätigt hast." msgid "Processing" msgstr "Weiter" @@ -516,6 +516,13 @@ msgstr "Ungültige Speicher-Grösse" msgid "Incorrect pricing name. Please contact support{support_email}" msgstr "" +#, python-brace-format +msgid "{user} does not have permission to access the card" +msgstr "" + +msgid "An error occurred. Details: {}" +msgstr "" + msgid "Confirm Order" msgstr "Bestellung Bestätigen" @@ -529,6 +536,10 @@ msgstr "" "Es ist ein Fehler bei der Zahlung betreten. Du wirst nach dem Schliessen vom " "Popup zur Bezahlseite weitergeleitet." +#, python-brace-format +msgid "An error occurred while associating the card. Details: {details}" +msgstr "" + msgid "Thank you for the order." msgstr "Danke für Deine Bestellung." @@ -539,6 +550,28 @@ msgstr "" "Deine VM ist gleich bereit. Wir senden Dir eine Bestätigungsemail, sobald Du " "auf sie zugreifen kannst." +#~ msgid "" +#~ "You are not making any payment yet. After submitting your card " +#~ "information, you will be taken to the Confirm Order Page." +#~ msgstr "" +#~ "Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst " +#~ "ausgelöst, nachdem Du die Bestellung auf der nächsten Seite bestätigt " +#~ "hast." + +#~ msgid "Card Number" +#~ msgstr "Kreditkartennummer" + +#~ msgid "Expiry Date" +#~ msgstr "Ablaufdatum" + +#~ msgid "" +#~ "You are not making any payment yet. After placing your order, you will be " +#~ "taken to the Submit Payment Page." +#~ msgstr "" +#~ "Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst " +#~ "ausgelöst, nachdem Du die Bestellung auf der nächsten Seite bestätigt " +#~ "hast." + #~ msgid "Pricing" #~ msgstr "Preise" diff --git a/hosting/locale/de/LC_MESSAGES/django.po b/hosting/locale/de/LC_MESSAGES/django.po index d61d09c0..8d55d1b7 100644 --- a/hosting/locale/de/LC_MESSAGES/django.po +++ b/hosting/locale/de/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-05-12 03:53+0530\n" +"POT-Creation-Date: 2018-07-05 23:15+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -274,6 +274,27 @@ msgstr "" msgid "You can always order a new VM by following the link below." msgstr "" +msgid "Card Number" +msgstr "Kreditkartennummer" + +msgid "Expiry Date" +msgstr "Ablaufdatum" + +msgid "CVC" +msgstr "" + +msgid "Card Type" +msgstr "Kartentyp" + +msgid "" +"You are not making any payment yet. After placing your order, you will be " +"taken to the Submit Payment Page." +msgstr "" +"" + +msgid "SUBMIT" +msgstr "ABSENDEN" + msgid "Toggle navigation" msgstr "Umschalten" @@ -439,6 +460,14 @@ msgstr "wird an der Kasse angewendet" msgid "Billing Address" msgstr "Rechnungsadresse" +msgid "" +"Please select one of the cards that you used before or fill in your credit " +"card information below. We are using Stripe for payment and do not store your information in our " +"database." +msgstr "" +"" + msgid "" "Please fill in your credit card information below. We are using Stripe for payment and do not " @@ -448,28 +477,24 @@ msgstr "" "\"https://stripe.com\" target=\"_blank\">Stripe für die Bezahlung und " "speichern keine Informationen in unserer Datenbank." -msgid "" -"You are not making any payment yet. After submitting your card information, " -"you will be taken to the Confirm Order Page." -msgstr "" -"Es wird noch keine Bezahlung vorgenommen. Die Bezahlung wird erst ausgelöst, " -"nachdem Du die Bestellung auf der nächsten Seite bestätigt hast." - -msgid "SUBMIT" -msgstr "ABSENDEN" - -msgid "Card Number" -msgstr "Kreditkartennummer" - -msgid "Expiry Date" -msgstr "Ablaufdatum" - -msgid "CVC" +msgid "Last" msgstr "" -msgid "Card Type" +msgid "Type" msgstr "Kartentyp" +msgid "SELECT" +msgstr "" + +msgid "Add a new credit card" +msgstr "Neue Kreditkarte hinzufügen." + +msgid "NEW CARD" +msgstr "BEARBEITEN" + +msgid "New Credit Card" +msgstr "" + msgid "Processing" msgstr "Weiter" @@ -485,11 +510,20 @@ msgstr "Passwort zurücksetzen" msgid "UPDATE" msgstr "" -msgid "Last" +msgid "REMOVE CARD" +msgstr "KARTE ENTFERNEN" + +msgid "Remove Card" msgstr "" -msgid "Type" -msgstr "Kartentyp" +msgid "Do you want to remove this associated card?" +msgstr "Möchtest Du den Schlüssel löschen?" + +msgid "Delete" +msgstr "Löschen" + +msgid "DEFAULT" +msgstr "" msgid "No Credit Cards Added" msgstr "Es wurde keine Kreditkarte hinzugefügt" @@ -536,9 +570,6 @@ msgstr "" msgid "Private Key" msgstr "" -msgid "Delete" -msgstr "Löschen" - msgid "Delete SSH Key" msgstr "SSH Key löschen" @@ -670,6 +701,35 @@ msgstr "Dein Passwort konnte nicht zurückgesetzt werden." msgid "The reset password link is no longer valid." msgstr "Der Link zum Zurücksetzen Deines Passwortes ist nicht mehr gültig." +msgid "Card deassociation successful" +msgstr "" + +msgid "You are not permitted to do this operation" +msgstr "" + +msgid "The selected card does not exist" +msgstr "" + +msgid "Billing address updated successfully" +msgstr "" + +msgid "You seem to have already added this card" +msgstr "" + +#, python-brace-format +msgid "An error occurred while associating the card. Details: {details}" +msgstr "" + +msgid "Successfully associated the card with your account" +msgstr "" + +#, python-brace-format +msgid "{user} does not have permission to access the card" +msgstr "" + +msgid "An error occurred. Details: {}" +msgstr "" + msgid "Invalid credit card" msgstr "Ungültige Kreditkarte" @@ -807,15 +867,6 @@ msgstr "" #~ msgid "Notifications " #~ msgstr "Benachrichtigungen" -#~ msgid "REMOVE CARD" -#~ msgstr "KARTE ENTFERNEN" - -#~ msgid "EDIT CARD" -#~ msgstr "BEARBEITEN" - -#~ msgid "Add a new Card." -#~ msgstr "Neue Kreditkarte hinzufügen." - #~ msgid "You are not making any payment here." #~ msgstr "Es wird noch keine Bezahlung vorgenommen" diff --git a/utils/locale/de/LC_MESSAGES/django.po b/utils/locale/de/LC_MESSAGES/django.po index f18fc9c2..a9aaf8a0 100644 --- a/utils/locale/de/LC_MESSAGES/django.po +++ b/utils/locale/de/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-10-10 21:35+0530\n" +"POT-Creation-Date: 2018-07-05 23:18+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -777,10 +777,15 @@ msgstr "" msgid "Email Address" msgstr "" -msgid "Street Building" +msgid "Email" msgstr "" -msgid "Email" +msgid "" +"The email {} is already registered with us. Please reset your password and " +"access your account." +msgstr "" + +msgid "Street Building" msgstr "" msgid "Phone number" From 4c06a9e730bfc671a2709740bb45f45676093d65 Mon Sep 17 00:00:00 2001 From: PCoder Date: Fri, 6 Jul 2018 19:33:26 +0200 Subject: [PATCH 118/126] Add de translations by MalcolmA --- .../locale/de/LC_MESSAGES/django.po | 24 +++++++----- hosting/locale/de/LC_MESSAGES/django.po | 37 ++++++++++--------- utils/locale/de/LC_MESSAGES/django.po | 6 ++- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/datacenterlight/locale/de/LC_MESSAGES/django.po b/datacenterlight/locale/de/LC_MESSAGES/django.po index 42eab589..1b66b640 100644 --- a/datacenterlight/locale/de/LC_MESSAGES/django.po +++ b/datacenterlight/locale/de/LC_MESSAGES/django.po @@ -335,7 +335,10 @@ msgid "" "\"_blank\">Stripe for payment and do not store your information in our " "database." msgstr "" -"" +"Bitte wähle eine der zuvor genutzten Kreditkarten oder gib Deine " +"Kreditkartendetails unten an. Die Bezahlung wird über " +"Stripe abgewickelt. " +"Wir speichern Deine Kreditkartendetails nicht in unserer Datenbank." msgid "" "Please fill in your credit card information below. We are using Date: Sat, 7 Jul 2018 01:08:45 +0200 Subject: [PATCH 119/126] Remove token parameter from stripe_utils.get_card_details and add additional paramters to uniquely identify a card --- datacenterlight/tests.py | 3 +-- utils/stripe_utils.py | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/datacenterlight/tests.py b/datacenterlight/tests.py index d6cd6adf..c755cc6f 100644 --- a/datacenterlight/tests.py +++ b/datacenterlight/tests.py @@ -86,8 +86,7 @@ class CeleryTaskTestCase(TestCase): token=self.token ) card_details = self.stripe_utils.get_card_details( - stripe_customer.stripe_id, - self.token + stripe_customer.stripe_id ) card_details_dict = card_details.get('error') self.assertEquals(card_details_dict, None) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index e4b9b02e..2045df8e 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -109,12 +109,16 @@ class StripeUtils(object): return new_card_data @handleStripeError - def get_card_details(self, customer_id, token): + def get_card_details(self, customer_id): customer = stripe.Customer.retrieve(customer_id) credit_card_raw_data = customer.sources.data.pop() card_details = { 'last4': credit_card_raw_data.last4, - 'brand': credit_card_raw_data.brand + 'brand': credit_card_raw_data.brand, + 'exp_month': credit_card_raw_data.exp_month, + 'exp_year': credit_card_raw_data.exp_year, + 'fingerprint': credit_card_raw_data.fingerprint, + 'card_id': credit_card_raw_data.id } return card_details From e60b93d126dfe81a9f0360144136d08c91beb05a Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 7 Jul 2018 01:28:25 +0200 Subject: [PATCH 120/126] Add management command import_usercarddetails --- .../commands/import_usercarddetails.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 hosting/management/commands/import_usercarddetails.py diff --git a/hosting/management/commands/import_usercarddetails.py b/hosting/management/commands/import_usercarddetails.py new file mode 100644 index 00000000..fa5b975e --- /dev/null +++ b/hosting/management/commands/import_usercarddetails.py @@ -0,0 +1,39 @@ +from django.core.management.base import BaseCommand + +from hosting.models import UserCardDetail +from membership.models import CustomUser +from utils.stripe_utils import StripeUtils + + +class Command(BaseCommand): + help = '''Imports the usercard details of all customers. Created just for + multiple card support.''' + + def handle(self, *args, **options): + try: + stripe_utils = StripeUtils() + for user in CustomUser.objects.filter(id__gte=114): + if user.stripecustomer: + card_details_resp = stripe_utils.get_card_details( + user.stripecustomer.stripe_id + ) + card_details = card_details_resp['response_object'] + if card_details: + ucd = UserCardDetail.get_or_create_user_card_detail( + stripe_customer=user.stripecustomer, + card_details=card_details + ) + UserCardDetail.save_default_card_local( + user.stripecustomer.stripe_id, + ucd.card_id + ) + print("Saved user card details for {}".format( + user.email + )) + else: + print(" --- Could not get card details for {}".format( + user.email + )) + print("Error: {}".format(card_details_resp['error'])) + except Exception as e: + print("Error occurred. Details {}".format(str(e))) From e18b8a527a3d19d219613fc90a800a5df786d65a Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 7 Jul 2018 01:35:27 +0200 Subject: [PATCH 121/126] Add missing DE translation --- hosting/locale/de/LC_MESSAGES/django.po | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hosting/locale/de/LC_MESSAGES/django.po b/hosting/locale/de/LC_MESSAGES/django.po index d924fd4e..64dde71e 100644 --- a/hosting/locale/de/LC_MESSAGES/django.po +++ b/hosting/locale/de/LC_MESSAGES/django.po @@ -468,7 +468,10 @@ msgid "" "\"_blank\">Stripe for payment and do not store your information in our " "database." msgstr "" -"" +"Bitte wähle eine der zuvor genutzten Kreditkarten oder gib Deine " +"Kreditkartendetails unten an. Die Bezahlung wird über " +"Stripe abgewickelt. " +"Wir speichern Deine Kreditkartendetails nicht in unserer Datenbank." msgid "" "Please fill in your credit card information below. We are using Date: Sat, 7 Jul 2018 01:50:46 +0200 Subject: [PATCH 123/126] Add check to forbid user from deleting the last card --- hosting/views.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index a28815f2..7e23a682 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -15,12 +15,12 @@ from django.http import ( Http404, HttpResponseRedirect, HttpResponse, JsonResponse ) from django.shortcuts import redirect, render +from django.utils.decorators import method_decorator from django.utils.html import escape from django.utils.http import urlsafe_base64_decode from django.utils.safestring import mark_safe from django.utils.translation import get_language, ugettext_lazy as _ from django.utils.translation import ugettext -from django.utils.decorators import method_decorator from django.views.decorators.cache import never_cache from django.views.generic import ( View, CreateView, FormView, ListView, DetailView, DeleteView, @@ -615,7 +615,13 @@ class SettingsView(LoginRequiredMixin, FormView): if 'delete_card' in request.POST: try: card = UserCardDetail.objects.get(pk=self.kwargs.get('pk')) - if request.user.has_perm(self.permission_required[0], card): + if (request.user.has_perm(self.permission_required[0], card) + and + request.user + .stripecustomer + .usercarddetail_set + .count() > 1 + ): if card.card_id is not None: stripe_utils = StripeUtils() stripe_utils.dissociate_customer_card( From 0f26917f35fc6ba13f181763f0d1e980498b7429 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 7 Jul 2018 01:54:45 +0200 Subject: [PATCH 124/126] Fix PEP8 warning --- hosting/views.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index 7e23a682..f8bcbe14 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -620,8 +620,7 @@ class SettingsView(LoginRequiredMixin, FormView): request.user .stripecustomer .usercarddetail_set - .count() > 1 - ): + .count() > 1): if card.card_id is not None: stripe_utils = StripeUtils() stripe_utils.dissociate_customer_card( From 34ed51a643a95b794f632768cd7a44a2946fabf6 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 7 Jul 2018 02:03:42 +0200 Subject: [PATCH 125/126] Don't dissociate card if transaction fails --- datacenterlight/views.py | 9 ++++----- hosting/views.py | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 649810d0..bf87d9b9 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -530,11 +530,10 @@ class OrderConfirmationView(DetailView): or stripe_subscription_obj.status != 'active'): # At this point, we have created a Stripe API card and # associated it with the customer; but the transaction failed - # due to some reason. We may want to dissociate this card. - # stripe_utils.dissociate_customer_card( - # stripe_api_cus_id, - # card_details_dict['card_id'] - # ) + # due to some reason. So, we would want to dissociate this card + # here. + # ... + msg = subscription_result.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') diff --git a/hosting/views.py b/hosting/views.py index f8bcbe14..6af1885b 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1054,11 +1054,10 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): stripe_subscription_obj.status != 'active'): # At this point, we have created a Stripe API card and # associated it with the customer; but the transaction failed - # due to some reason. So, we dissociate this card. - stripe_utils.dissociate_customer_card( - request.user.stripecustomer.stripe_id, - card_details_dict['card_id'] - ) + # due to some reason. So, we would want to dissociate this card + # here. + # ... + msg = subscription_result.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') From 33bd2e1760fb33112b33c7a3d31efcd2881efd60 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 7 Jul 2018 02:15:38 +0200 Subject: [PATCH 126/126] Improve import_usercarddetails management command --- .../commands/import_usercarddetails.py | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/hosting/management/commands/import_usercarddetails.py b/hosting/management/commands/import_usercarddetails.py index fa5b975e..de5a91f9 100644 --- a/hosting/management/commands/import_usercarddetails.py +++ b/hosting/management/commands/import_usercarddetails.py @@ -12,28 +12,34 @@ class Command(BaseCommand): def handle(self, *args, **options): try: stripe_utils = StripeUtils() - for user in CustomUser.objects.filter(id__gte=114): - if user.stripecustomer: - card_details_resp = stripe_utils.get_card_details( - user.stripecustomer.stripe_id - ) - card_details = card_details_resp['response_object'] - if card_details: - ucd = UserCardDetail.get_or_create_user_card_detail( - stripe_customer=user.stripecustomer, - card_details=card_details + for user in CustomUser.objects.all(): + if hasattr(user, 'stripecustomer'): + if user.stripecustomer: + card_details_resp = stripe_utils.get_card_details( + user.stripecustomer.stripe_id ) - UserCardDetail.save_default_card_local( - user.stripecustomer.stripe_id, - ucd.card_id - ) - print("Saved user card details for {}".format( - user.email - )) - else: - print(" --- Could not get card details for {}".format( - user.email - )) - print("Error: {}".format(card_details_resp['error'])) + card_details = card_details_resp['response_object'] + if card_details: + ucd = UserCardDetail.get_or_create_user_card_detail( + stripe_customer=user.stripecustomer, + card_details=card_details + ) + UserCardDetail.save_default_card_local( + user.stripecustomer.stripe_id, + ucd.card_id + ) + print("Saved user card details for {}".format( + user.email + )) + else: + print(" --- Could not get card details for " + "{}".format(user.email)) + print(" --- Error: {}".format( + card_details_resp['error'] + )) + else: + print(" === {} does not have a StripeCustomer object".format( + user.email + )) except Exception as e: - print("Error occurred. Details {}".format(str(e))) + print(" *** Error occurred. Details {}".format(str(e)))