Merge pull request #432 from siarheipuhach/feature/task3637
task/3637/Added stripe failed payment error handler
This commit is contained in:
		
				commit
				
					
						a80b0668a2
					
				
			
		
					 5 changed files with 67 additions and 40 deletions
				
			
		| 
						 | 
				
			
			@ -438,6 +438,11 @@ class OrderConfirmationView(DetailView):
 | 
			
		|||
        stripe_utils = StripeUtils()
 | 
			
		||||
        card_details = stripe_utils.get_card_details(
 | 
			
		||||
            customer.stripe_id, request.session.get('token'))
 | 
			
		||||
        if not card_details.get('response_object') and not card_details.get('paid'):
 | 
			
		||||
            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'),
 | 
			
		||||
| 
						 | 
				
			
			@ -462,15 +467,12 @@ class OrderConfirmationView(DetailView):
 | 
			
		|||
        stripe_utils = StripeUtils()
 | 
			
		||||
        charge_response = stripe_utils.make_charge(amount=final_price,
 | 
			
		||||
                                                   customer=customer.stripe_id)
 | 
			
		||||
        charge = charge_response.get('response_object')
 | 
			
		||||
 | 
			
		||||
        # Check if the payment was approved
 | 
			
		||||
        if not charge:
 | 
			
		||||
            context = {}
 | 
			
		||||
            context.update({
 | 
			
		||||
                'paymentError': charge_response.get('error')
 | 
			
		||||
            })
 | 
			
		||||
            return render(request, self.payment_template_name, context)
 | 
			
		||||
        if not charge_response.get('response_object') and not charge_response.get('paid'):
 | 
			
		||||
            msg = charge_response.get('error')
 | 
			
		||||
            messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error')
 | 
			
		||||
            return HttpResponseRedirect(reverse('datacenterlight:payment') + '#payment_error')
 | 
			
		||||
 | 
			
		||||
        charge = charge_response.get('response_object')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -557,6 +557,10 @@ a.unlink:hover {
 | 
			
		|||
    border-radius: 3px;
 | 
			
		||||
    padding: 5px;
 | 
			
		||||
}
 | 
			
		||||
.card-warning-error {
 | 
			
		||||
    border: 1px solid #EB4D5C;
 | 
			
		||||
    color: #EB4D5C;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.card-warning-addtional-margin {
 | 
			
		||||
    margin-top: 15px;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -59,7 +59,6 @@
 | 
			
		|||
                        {% csrf_token %}
 | 
			
		||||
                        {% bootstrap_field field show_label=False type='fields'%}
 | 
			
		||||
                        {% endfor %}
 | 
			
		||||
                        {% bootstrap_form_errors form type='non_fields'%}
 | 
			
		||||
                    </form>
 | 
			
		||||
                </div>
 | 
			
		||||
                <div class="col-xs-12 col-sm-7 col-md-6 creditcard-box dcl-creditcard">
 | 
			
		||||
| 
						 | 
				
			
			@ -86,12 +85,28 @@
 | 
			
		|||
                            </form>
 | 
			
		||||
                            <div class="row">
 | 
			
		||||
                                <div class="col-xs-12">
 | 
			
		||||
                                        {% if not messages and not form.non_field_errors %}
 | 
			
		||||
                                            <p class="card-warning-content card-warning-addtional-margin">
 | 
			
		||||
                                                {% blocktrans %}
 | 
			
		||||
                                                You are not making any payment yet. After submitting your card
 | 
			
		||||
                                                information, you will be taken to the Confirm Order Page.
 | 
			
		||||
                                                {% endblocktrans %}
 | 
			
		||||
                                            </p>
 | 
			
		||||
                                        {% endif %}
 | 
			
		||||
                                        <div id='payment_error'>
 | 
			
		||||
                                            {% for message in messages %}
 | 
			
		||||
                                                {% if 'failed_payment' or 'make_charge_error' in message.tags %}
 | 
			
		||||
                                                 <ul class="list-unstyled"><li>
 | 
			
		||||
                                                     <p class="card-warning-content card-warning-error">{{ message|safe }}</p>
 | 
			
		||||
                                                </li></ul>
 | 
			
		||||
                                                {% endif %}
 | 
			
		||||
                                            {% endfor %}
 | 
			
		||||
                                            {% for error in form.non_field_errors %}
 | 
			
		||||
                                                <p class="card-warning-content card-warning-error">
 | 
			
		||||
                                                    {{ error|escape }}
 | 
			
		||||
                                                </p>
 | 
			
		||||
                                            {% endfor %}
 | 
			
		||||
                                        </div>
 | 
			
		||||
                                    </div>
 | 
			
		||||
                                <div class="col-xs-12">
 | 
			
		||||
                                    <div class="col-xs-6 pull-right">
 | 
			
		||||
| 
						 | 
				
			
			@ -130,12 +145,29 @@
 | 
			
		|||
                                <div id="card-errors" role="alert"></div>
 | 
			
		||||
                                <div class="row">
 | 
			
		||||
                                    <div class="col-xs-12">
 | 
			
		||||
                                        {% if not messages and not form.non_field_errors %}
 | 
			
		||||
                                            <p class="card-warning-content">
 | 
			
		||||
                                                {% blocktrans %}
 | 
			
		||||
                                                You are not making any payment yet. After submitting your card
 | 
			
		||||
                                                information, you will be taken to the Confirm Order Page.
 | 
			
		||||
                                                {% endblocktrans %}
 | 
			
		||||
                                            </p>
 | 
			
		||||
                                        {% endif %}
 | 
			
		||||
                                        <div id='payment_error'>
 | 
			
		||||
                                            {% for message in messages %}
 | 
			
		||||
                                                {% if 'failed_payment' or 'make_charge_error' in message.tags %}
 | 
			
		||||
                                                 <ul class="list-unstyled"><li>
 | 
			
		||||
                                                     <p class="card-warning-content card-warning-error">{{ message|safe }}</p>
 | 
			
		||||
                                                </li></ul>
 | 
			
		||||
                                                {% endif %}
 | 
			
		||||
                                            {% endfor %}
 | 
			
		||||
 | 
			
		||||
                                            {% for error in form.non_field_errors %}
 | 
			
		||||
                                                <p class="card-warning-content card-warning-error">
 | 
			
		||||
                                                    {{ error|escape }}
 | 
			
		||||
                                                </p>
 | 
			
		||||
                                            {% endfor %}
 | 
			
		||||
                                        </div>
 | 
			
		||||
                                    </div>
 | 
			
		||||
                                    <div class="col-xs-12">
 | 
			
		||||
                                        <div class="col-xs-6 pull-right">
 | 
			
		||||
| 
						 | 
				
			
			@ -150,15 +182,6 @@
 | 
			
		|||
                                        <p class="payment-errors"></p>
 | 
			
		||||
                                    </div>
 | 
			
		||||
                                </div>
 | 
			
		||||
                                {% if paymentError %}
 | 
			
		||||
                                <div class="row">
 | 
			
		||||
                                    <div class="col-xs-12">
 | 
			
		||||
                                        <p>
 | 
			
		||||
                                            {% bootstrap_alert paymentError alert_type='danger' %}
 | 
			
		||||
                                        </p>
 | 
			
		||||
                                    </div>
 | 
			
		||||
                                </div>
 | 
			
		||||
                                {% endif %}
 | 
			
		||||
                            </form>
 | 
			
		||||
 | 
			
		||||
                            {% endif %}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -556,8 +556,9 @@ class PaymentVMView(LoginRequiredMixin, FormView):
 | 
			
		|||
            customer = StripeCustomer.get_or_create(email=owner.email,
 | 
			
		||||
                                                    token=token)
 | 
			
		||||
            if not customer:
 | 
			
		||||
                form.add_error("__all__", "Invalid credit card")
 | 
			
		||||
                return self.render_to_response(self.get_context_data(form=form))
 | 
			
		||||
                msg = _("Invalid credit card")
 | 
			
		||||
                messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error')
 | 
			
		||||
                return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error')
 | 
			
		||||
 | 
			
		||||
            # Create Billing Address
 | 
			
		||||
            billing_address = form.save()
 | 
			
		||||
| 
						 | 
				
			
			@ -566,15 +567,12 @@ class PaymentVMView(LoginRequiredMixin, FormView):
 | 
			
		|||
            stripe_utils = StripeUtils()
 | 
			
		||||
            charge_response = stripe_utils.make_charge(amount=final_price,
 | 
			
		||||
                                                       customer=customer.stripe_id)
 | 
			
		||||
            charge = charge_response.get('response_object')
 | 
			
		||||
 | 
			
		||||
            # Check if the payment was approved
 | 
			
		||||
            if not charge:
 | 
			
		||||
                context.update({
 | 
			
		||||
                    'paymentError': charge_response.get('error'),
 | 
			
		||||
                    'form': form
 | 
			
		||||
                })
 | 
			
		||||
                return render(request, self.template_name, context)
 | 
			
		||||
            if not charge_response.get('response_object') and not charge_response.get('paid'):
 | 
			
		||||
                msg = charge_response.get('error')
 | 
			
		||||
                messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error')
 | 
			
		||||
                return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error')
 | 
			
		||||
 | 
			
		||||
            charge = charge_response.get('response_object')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,7 +11,7 @@ def handleStripeError(f):
 | 
			
		|||
            'error': None
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        common_message = "Currently its not possible to make payments."
 | 
			
		||||
        common_message = "Currently it's not possible to make payments."
 | 
			
		||||
        try:
 | 
			
		||||
            response_object = f(*args, **kwargs)
 | 
			
		||||
            response = {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue