Handle payment_intent requires SCA case

This commit is contained in:
PCoder 2020-12-18 16:47:45 +05:30
parent 0b0c932e5a
commit d0d5fb0196

View file

@ -783,7 +783,6 @@ class OrderConfirmationView(DetailView, FormView):
customer=stripe_api_cus_id customer=stripe_api_cus_id
) )
stripe_onetime_charge = charge_response.get('response_object') stripe_onetime_charge = charge_response.get('response_object')
# Check if the payment was approved # Check if the payment was approved
if not stripe_onetime_charge: if not stripe_onetime_charge:
msg = charge_response.get('error') msg = charge_response.get('error')
@ -893,52 +892,37 @@ class OrderConfirmationView(DetailView, FormView):
logger.debug(stripe_subscription_obj) logger.debug(stripe_subscription_obj)
latest_invoice = stripe.Invoice.retrieve( latest_invoice = stripe.Invoice.retrieve(
stripe_subscription_obj.latest_invoice) stripe_subscription_obj.latest_invoice)
# ret = stripe.PaymentIntent.confirm(
# latest_invoice.payment_intent
# )
if ret.status == 'requires_source_action' or 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 # Check if the subscription was approved and is active
if (stripe_subscription_obj is None if (stripe_subscription_obj is None
or stripe_subscription_obj.status != 'active'): or (stripe_subscription_obj.status != 'active'
and stripe_subscription_obj.status != 'incomplete')):
# At this point, we have created a Stripe API card and # At this point, we have created a Stripe API card and
# associated it with the customer; but the transaction failed # associated it with the customer; but the transaction failed
# due to some reason. So, we would want to dissociate this card # due to some reason. So, we would want to dissociate this card
# here. # here.
# ... # ...
msg = subscription_result.get('error') msg = subscription_result.get('error')
messages.add_message(self.request, messages.ERROR, msg, return show_error(msg, self.request)
extra_tags='failed_payment') elif stripe_subscription_obj.status == 'incomplete':
response = { pi = stripe.PaymentIntent.retrieve(
'status': False, latest_invoice.payment_intent
'redirect': "{url}#{section}".format( )
url=(reverse( # TODO: requires_attention is probably wrong value to compare
'show_product', if (pi.status == 'requires_attention' or
kwargs={'product_slug': pi.status == 'requires_source_action'):
request.session['generic_payment_details'] logger.debug("Display SCA authentication")
['product_slug']} context = {
) if 'generic_payment_details' in request.session else 'sid': stripe_subscription_obj.id,
reverse('datacenterlight:payment') 'payment_intent_secret': pi.client_secret,
), 'STRIPE_PUBLISHABLE_KEY': settings.STRIPE_API_PUBLIC_KEY,
section='payment_error' 'showSCA': True
), }
'msg_title': str(_('Error.')), return JsonResponse(context)
'msg_body': str( else:
_('There was a payment related error.' logger.debug("Handle this case")
' On close of this popup, you will be redirected back to' msg = subscription_result.get('error')
' the payment page.')) return show_error(msg, self.request)
}
return JsonResponse(response)
# Create user if the user is not logged in and if he is not already # Create user if the user is not logged in and if he is not already
# registered # registered
@ -1184,4 +1168,3 @@ def show_error(msg, request):
' the payment page.')) ' the payment page.'))
} }
return JsonResponse(response) return JsonResponse(response)