Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

102
Vistas
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

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos