Add selected card handling in landing flow "Order confirmation" page
This commit is contained in:
parent
2ffaee2d5b
commit
82ad9ac337
1 changed files with 122 additions and 50 deletions
|
@ -21,7 +21,7 @@ from opennebula_api.serializers import VMTemplateSerializer
|
||||||
from utils.forms import (
|
from utils.forms import (
|
||||||
BillingAddressForm, BillingAddressFormSignup
|
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.mailer import BaseEmail
|
||||||
from utils.stripe_utils import StripeUtils
|
from utils.stripe_utils import StripeUtils
|
||||||
from utils.tasks import send_plain_email_task
|
from utils.tasks import send_plain_email_task
|
||||||
|
@ -401,6 +401,13 @@ class PaymentOrderView(FormView):
|
||||||
# user is no longer added to session on the index page
|
# user is no longer added to session on the index page
|
||||||
if 'specs' not in request.session:
|
if 'specs' not in request.session:
|
||||||
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
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())
|
return self.render_to_response(self.get_context_data())
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
|
@ -436,8 +443,40 @@ class PaymentOrderView(FormView):
|
||||||
'name': request.user.name
|
'name': request.user.name
|
||||||
}
|
}
|
||||||
customer = StripeCustomer.get_or_create(
|
customer = StripeCustomer.get_or_create(
|
||||||
email=this_user.get('email'),
|
email=this_user.get('email'), token=token
|
||||||
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:
|
else:
|
||||||
user_email = address_form.cleaned_data.get('email')
|
user_email = address_form.cleaned_data.get('email')
|
||||||
user_name = address_form.cleaned_data.get('name')
|
user_name = address_form.cleaned_data.get('name')
|
||||||
|
@ -472,7 +511,7 @@ class PaymentOrderView(FormView):
|
||||||
email=user_email,
|
email=user_email,
|
||||||
token=token,
|
token=token,
|
||||||
customer_name=user_name)
|
customer_name=user_name)
|
||||||
|
request.session['token'] = token
|
||||||
request.session['billing_address_data'] = address_form.cleaned_data
|
request.session['billing_address_data'] = address_form.cleaned_data
|
||||||
request.session['user'] = this_user
|
request.session['user'] = this_user
|
||||||
# Get or create stripe customer
|
# Get or create stripe customer
|
||||||
|
@ -485,7 +524,6 @@ class PaymentOrderView(FormView):
|
||||||
billing_address_form=address_form
|
billing_address_form=address_form
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
request.session['token'] = token
|
|
||||||
if type(customer) is StripeCustomer:
|
if type(customer) is StripeCustomer:
|
||||||
request.session['customer'] = customer.stripe_id
|
request.session['customer'] = customer.stripe_id
|
||||||
else:
|
else:
|
||||||
|
@ -508,27 +546,33 @@ class OrderConfirmationView(DetailView):
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
if 'specs' not in request.session or 'user' not in request.session:
|
if 'specs' not in request.session or 'user' not in request.session:
|
||||||
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
||||||
if 'token' not in request.session:
|
if 'token' not in request.session and 'card_id' not in request.session:
|
||||||
return HttpResponseRedirect(reverse('datacenterlight:payment'))
|
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 = {
|
context = {
|
||||||
'site_url': reverse('datacenterlight:index'),
|
'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'),
|
'vm': request.session.get('specs'),
|
||||||
'page_header_text': _('Confirm Order'),
|
'page_header_text': _('Confirm Order'),
|
||||||
'billing_address_data': request.session.get('billing_address_data')
|
'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)
|
return render(request, self.template_name, context)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
|
@ -538,43 +582,62 @@ class OrderConfirmationView(DetailView):
|
||||||
stripe_api_cus_id = request.session.get('customer')
|
stripe_api_cus_id = request.session.get('customer')
|
||||||
vm_template_id = template.get('id', 1)
|
vm_template_id = template.get('id', 1)
|
||||||
stripe_utils = StripeUtils()
|
stripe_utils = StripeUtils()
|
||||||
card_details = stripe_utils.get_card_details(stripe_api_cus_id,
|
if 'token' in self.request.session:
|
||||||
request.session.get(
|
card_details = stripe_utils.get_cards_details_from_token(
|
||||||
'token'))
|
request.session['token']
|
||||||
if not card_details.get('response_object'):
|
)
|
||||||
msg = card_details.get('error')
|
if not card_details.get('response_object'):
|
||||||
messages.add_message(self.request, messages.ERROR, msg,
|
msg = card_details.get('error')
|
||||||
extra_tags='failed_payment')
|
messages.add_message(self.request, messages.ERROR, msg,
|
||||||
response = {
|
extra_tags='failed_payment')
|
||||||
'status': False,
|
response = {
|
||||||
'redirect': "{url}#{section}".format(
|
'status': False,
|
||||||
url=reverse('datacenterlight:payment'),
|
'redirect': "{url}#{section}".format(
|
||||||
section='payment_error'),
|
url=reverse('datacenterlight:payment'),
|
||||||
'msg_title': str(_('Error.')),
|
section='payment_error'),
|
||||||
'msg_body': str(
|
'msg_title': str(_('Error.')),
|
||||||
_('There was a payment related error.'
|
'msg_body': str(
|
||||||
' On close of this popup, you will be redirected back to'
|
_('There was a payment related error.'
|
||||||
' 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")
|
||||||
|
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),
|
if not user_card_detail.preferred:
|
||||||
content_type="application/json")
|
UserCardDetail.set_default_card(
|
||||||
card_details_dict = card_details.get('response_object')
|
stripe_api_cus_id=stripe_api_cus_id,
|
||||||
|
stripe_source_id=user_card_detail.card_id
|
||||||
|
)
|
||||||
cpu = specs.get('cpu')
|
cpu = specs.get('cpu')
|
||||||
memory = specs.get('memory')
|
memory = specs.get('memory')
|
||||||
disk_size = specs.get('disk_size')
|
disk_size = specs.get('disk_size')
|
||||||
amount_to_be_charged = specs.get('price')
|
amount_to_be_charged = specs.get('price')
|
||||||
plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu,
|
plan_name = StripeUtils.get_stripe_plan_name(
|
||||||
memory=memory,
|
cpu=cpu, memory=memory, disk_size=disk_size
|
||||||
disk_size=disk_size)
|
)
|
||||||
stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu,
|
stripe_plan_id = StripeUtils.get_stripe_plan_id(
|
||||||
ram=memory,
|
cpu=cpu, ram=memory, ssd=disk_size, version=1, app='dcl'
|
||||||
ssd=disk_size,
|
)
|
||||||
version=1,
|
|
||||||
app='dcl')
|
|
||||||
stripe_plan = stripe_utils.get_or_create_stripe_plan(
|
stripe_plan = stripe_utils.get_or_create_stripe_plan(
|
||||||
amount=amount_to_be_charged,
|
amount=amount_to_be_charged, name=plan_name,
|
||||||
name=plan_name,
|
stripe_plan_id=stripe_plan_id
|
||||||
stripe_plan_id=stripe_plan_id)
|
)
|
||||||
subscription_result = stripe_utils.subscribe_customer_to_plan(
|
subscription_result = stripe_utils.subscribe_customer_to_plan(
|
||||||
stripe_api_cus_id,
|
stripe_api_cus_id,
|
||||||
[{"plan": stripe_plan.get(
|
[{"plan": stripe_plan.get(
|
||||||
|
@ -644,6 +707,15 @@ class OrderConfirmationView(DetailView):
|
||||||
billing_address_data.update({
|
billing_address_data.update({
|
||||||
'user': custom_user.id
|
'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 = {
|
user = {
|
||||||
'name': custom_user.name,
|
'name': custom_user.name,
|
||||||
'email': custom_user.email,
|
'email': custom_user.email,
|
||||||
|
|
Loading…
Reference in a new issue