Handle errors related to payment by redirecting to payment page
This commit is contained in:
		
					parent
					
						
							
								23e7edf7c2
							
						
					
				
			
			
				commit
				
					
						bea3477d84
					
				
			
		
					 2 changed files with 84 additions and 9 deletions
				
			
		| 
						 | 
					@ -603,21 +603,46 @@ class OrderConfirmationView(DetailView):
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                return HttpResponse(json.dumps(response),
 | 
					                return HttpResponse(json.dumps(response),
 | 
				
			||||||
                                    content_type="application/json")
 | 
					                                    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(
 | 
					            ucd = UserCardDetail.contains(
 | 
				
			||||||
                request.user.stripecustomer, card_details_dict
 | 
					                request.user.stripecustomer, card_details_dict
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            if not ucd:
 | 
					            if not ucd:
 | 
				
			||||||
                stripe_utils.associate_customer_card(
 | 
					                acc_result = stripe_utils.associate_customer_card(
 | 
				
			||||||
                    stripe_api_cus_id, request.session['token'],
 | 
					                    stripe_api_cus_id, request.session['token'],
 | 
				
			||||||
                    set_as_default=True
 | 
					                    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:
 | 
					        else:
 | 
				
			||||||
            card_id = request.session.get('card_id')
 | 
					            card_id = request.session.get('card_id')
 | 
				
			||||||
            user_card_detail = UserCardDetail.objects.get(id=card_id)
 | 
					            user_card_detail = UserCardDetail.objects.get(id=card_id)
 | 
				
			||||||
            card_details_dict = {
 | 
					            card_details_dict = {
 | 
				
			||||||
                'last4': user_card_detail.last4,
 | 
					                '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:
 | 
					            if not user_card_detail.preferred:
 | 
				
			||||||
                UserCardDetail.set_default_card(
 | 
					                UserCardDetail.set_default_card(
 | 
				
			||||||
| 
						 | 
					@ -646,6 +671,14 @@ class OrderConfirmationView(DetailView):
 | 
				
			||||||
        # 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'):
 | 
				
			||||||
 | 
					            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')
 | 
					            msg = subscription_result.get('error')
 | 
				
			||||||
            messages.add_message(self.request, messages.ERROR, msg,
 | 
					            messages.add_message(self.request, messages.ERROR, msg,
 | 
				
			||||||
                                 extra_tags='failed_payment')
 | 
					                                 extra_tags='failed_payment')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -632,6 +632,18 @@ class SettingsView(LoginRequiredMixin, FormView):
 | 
				
			||||||
                    msg = _('You seem to have already added this card')
 | 
					                    msg = _('You seem to have already added this card')
 | 
				
			||||||
                    messages.add_message(request, messages.ERROR, msg)
 | 
					                    messages.add_message(request, messages.ERROR, msg)
 | 
				
			||||||
                else:
 | 
					                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
 | 
					                    preferred = False
 | 
				
			||||||
                    if stripe_customer.usercarddetail_set.count() == 0:
 | 
					                    if stripe_customer.usercarddetail_set.count() == 0:
 | 
				
			||||||
                        preferred = True
 | 
					                        preferred = True
 | 
				
			||||||
| 
						 | 
					@ -645,9 +657,6 @@ class SettingsView(LoginRequiredMixin, FormView):
 | 
				
			||||||
                        card_id=card['card_id'],
 | 
					                        card_id=card['card_id'],
 | 
				
			||||||
                        preferred=preferred
 | 
					                        preferred=preferred
 | 
				
			||||||
                    )
 | 
					                    )
 | 
				
			||||||
                    stripe_utils.associate_customer_card(
 | 
					 | 
				
			||||||
                        request.user.stripecustomer.stripe_id, token
 | 
					 | 
				
			||||||
                    )
 | 
					 | 
				
			||||||
                    msg = _(
 | 
					                    msg = _(
 | 
				
			||||||
                        "Successfully associated the card with your account"
 | 
					                        "Successfully associated the card with your account"
 | 
				
			||||||
                    )
 | 
					                    )
 | 
				
			||||||
| 
						 | 
					@ -866,22 +875,48 @@ class OrdersHostingDetailView(LoginRequiredMixin,
 | 
				
			||||||
            card_details_response = card_details['response_object']
 | 
					            card_details_response = card_details['response_object']
 | 
				
			||||||
            card_details_dict = {
 | 
					            card_details_dict = {
 | 
				
			||||||
                'last4': card_details_response['last4'],
 | 
					                '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(
 | 
					            ucd = UserCardDetail.contains(
 | 
				
			||||||
                request.user.stripecustomer, card_details_response
 | 
					                request.user.stripecustomer, card_details_response
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            if not ucd:
 | 
					            if not ucd:
 | 
				
			||||||
                stripe_utils.associate_customer_card(
 | 
					                acc_result = stripe_utils.associate_customer_card(
 | 
				
			||||||
                    stripe_api_cus_id, request.session['token'],
 | 
					                    stripe_api_cus_id, request.session['token'],
 | 
				
			||||||
                    set_as_default=True
 | 
					                    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:
 | 
					        else:
 | 
				
			||||||
            card_id = request.session.get('card_id')
 | 
					            card_id = request.session.get('card_id')
 | 
				
			||||||
            user_card_detail = UserCardDetail.objects.get(id=card_id)
 | 
					            user_card_detail = UserCardDetail.objects.get(id=card_id)
 | 
				
			||||||
            card_details_dict = {
 | 
					            card_details_dict = {
 | 
				
			||||||
                'last4': user_card_detail.last4,
 | 
					                '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:
 | 
					            if not user_card_detail.preferred:
 | 
				
			||||||
                UserCardDetail.set_default_card(
 | 
					                UserCardDetail.set_default_card(
 | 
				
			||||||
| 
						 | 
					@ -911,6 +946,13 @@ class OrdersHostingDetailView(LoginRequiredMixin,
 | 
				
			||||||
        # Check if the subscription was approved and is active
 | 
					        # Check if the subscription was approved and is active
 | 
				
			||||||
        if (stripe_subscription_obj is None or
 | 
					        if (stripe_subscription_obj is None or
 | 
				
			||||||
                stripe_subscription_obj.status != 'active'):
 | 
					                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')
 | 
					            msg = subscription_result.get('error')
 | 
				
			||||||
            messages.add_message(self.request, messages.ERROR, msg,
 | 
					            messages.add_message(self.request, messages.ERROR, msg,
 | 
				
			||||||
                                 extra_tags='failed_payment')
 | 
					                                 extra_tags='failed_payment')
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue