Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

376
Views
How to manage a redirect after a Ajax call + django

I'm managing my payments using braintree drop in and i'm using django. when the payment is success it should redirect to the payment success page and when it's unsuccess it should redirect to the payment failed page. I define the redirects in my views.py but i don't know how should i handle the redirect in ajax in this code after payment nothing happens and redirect is not working. what should i add to my ajax to handle the redirect?

Braintree drop in Javascript code:

<script>
var braintree_client_token = "{{ client_token}}";
var button = document.querySelector('#submit-button');

braintree.dropin.create({
    authorization: "{{client_token}}",
    container: '#braintree-dropin',
    card: {
        cardholderName: {
            required: false
        }
    }
}, function (createErr, instance) {
    button.addEventListener('click', function () {
        instance.requestPaymentMethod(function (err, payload) {
            $.ajax({
                type: 'POST',
                url: '{% url "payment:process" %}',
                data: {
                    'paymentMethodNonce': payload.nonce,
                    'csrfmiddlewaretoken': '{{ csrf_token }}'
                }
            }).done(function (result) {
            });
        });
    });
});
</script>

Views.py

@login_required
def payment_process(request):
order_id = request.session.get('order_id')
order = get_object_or_404(Order, id=order_id)
total_cost = order.get_total_cost()

if request.method == 'POST':
    # retrive nonce
    nonce = request.POST.get('paymentMethodNonce', None)
    # create and submit transaction
    result = gateway.transaction.sale({
        'amount': f'{total_cost:.2f}',
        'payment_method_nonce': nonce,
        'options': {
            'submit_for_settlement': True
        }
    })
    if result.is_success:
        # mark the order as paid
        order.paid = True
        # store the unique transaction id
        order.braintree_id = result.transaction.id
        order.save()
        request.session['coupon_id'] = None
        return redirect('payment:done')
    else:
        return redirect('payment:canceled')
else:
    # generate token
    client_token = gateway.client_token.generate()
    context = {
        'order': order,
        'client_token': client_token,

    }
    return render(request, 'process.html', context)


def payment_done(request):
    return render(request, 'done.html')


def payment_canceled(request):
    return render(request, 'canceled.html')

everything works fine except redirects

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!