From 70bfef473885cdc7ca72df5b47120ab21e410169 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 11 Oct 2020 16:01:55 +0530 Subject: [PATCH] Show SCA modal when required --- .../hosting/js/virtual_machine_detail.js | 40 ++++++++++++++----- hosting/views.py | 15 +++++++ utils/stripe_utils.py | 1 + 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/hosting/static/hosting/js/virtual_machine_detail.js b/hosting/static/hosting/js/virtual_machine_detail.js index 4a60d32b..f01cc435 100644 --- a/hosting/static/hosting/js/virtual_machine_detail.js +++ b/hosting/static/hosting/js/virtual_machine_detail.js @@ -107,18 +107,36 @@ $(document).ready(function() { success: function (data) { fa_icon = $('.modal-icon > .fa'); modal_btn = $('#createvm-modal-done-btn'); - $('#createvm-modal-title').text(data.msg_title); - $('#createvm-modal-body').html(data.msg_body); - if (data.redirect) { - modal_btn.attr('href', data.redirect).removeClass('hide'); + if (data.showSCA){ + console.log("Show SCA"); + var stripe = Stripe(data.STRIPE_PUBLISHABLE_KEY); + + stripe.confirmCardPayment(pi_secret).then(function(result) { + if (result.error) { + // Display error.message in your UI. + $("#3ds_result").text("Error!"); + $("#3ds_result").addClass("text-danger"); + } else { + // The payment has succeeded. Display a success message. + $("#3ds_result").text("Thank you for payment"); + $("#3ds_result").addClass("text-success"); + } + }); + $('#3Dsecure-modal').show(); } else { - modal_btn.attr('href', ""); - } - if (data.status === true) { - fa_icon.attr('class', 'checkmark'); - } else { - fa_icon.attr('class', 'fa fa-close'); - modal_btn.attr('class', '').addClass('btn btn-danger btn-ok btn-wide'); + $('#createvm-modal-title').text(data.msg_title); + $('#createvm-modal-body').html(data.msg_body); + if (data.redirect) { + modal_btn.attr('href', data.redirect).removeClass('hide'); + } else { + modal_btn.attr('href', ""); + } + if (data.status === true) { + fa_icon.attr('class', 'checkmark'); + } else { + fa_icon.attr('class', 'fa fa-close'); + modal_btn.attr('class', '').addClass('btn btn-danger btn-ok btn-wide'); + } } }, error: function (xmlhttprequest, textstatus, message) { diff --git a/hosting/views.py b/hosting/views.py index 438a0d55..5660b7d8 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1195,6 +1195,21 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView, FormView): tax_rates=[stripe_tax_rate.tax_rate_id] if stripe_tax_rate else [], ) stripe_subscription_obj = subscription_result.get('response_object') + latest_invoice = stripe.Invoice.retrieve(stripe_subscription_obj.latest_invoice) + ret = stripe.PaymentIntent.confirm( + latest_invoice.payment_intent + ) + if ret.status == 'requires_action': + pi = stripe.PaymentIntent.retrieve( + latest_invoice.payment_intent + ) + context = { + 'sid': stripe_subscription_obj.id, + 'payment_intent_secret': pi.client_secret, + 'STRIPE_PUBLISHABLE_KEY': settings.STRIPE_API_PUBLIC_KEY, + 'showSCA': True + } + return JsonResponse(context) # Check if the subscription was approved and is active if (stripe_subscription_obj is None or stripe_subscription_obj.status != 'active'): diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index ade06dd3..b8667a40 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -322,6 +322,7 @@ class StripeUtils(object): customer=customer, items=plans, trial_end=trial_end, coupon=coupon, default_tax_rates=tax_rates, + payment_behavior='allow_incomplete' ) return subscription_result