为什么付款成功后不重定向?使用django和braintree dropin-ui

g2ieeal7  于 2022-12-05  发布在  Go
关注(0)|答案(1)|浏览(121)

我使用这个沙盒为商店网站的付款部分。我想重定向用户到页面完成后,成功付款,但当前页面再次加载!pealse帮助
付款处理视图:

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':
        nonce = request.POST.get('paymentMethodNonce', None)
        result = gateway.transaction.sale({
            'amount': f'{total_cost:.2f}',
            'payment_method_nonce': nonce,
            'options': {
                'submit_for_settlement': True
            }
        })
        if result.is_success:
            order.paid = True
            order.braintree_id = result.transaction.id
            order.save()
            return redirect('payment:done')
        else:
            return redirect('payment:canceled')
    else:
        client_token = gateway.client_token.generate()
        return render(request, 'payment/process.html', {'order': order, 'client_token': client_token})

页面:

{% extends "shop/base.html" %}

{% block title %} Pay by credit card {% endblock %}

{% block sidenavigation %}

{% endblock %}

{% block content %}
    <h1>Pay by credit card</h1>

    <!-- includes the Braintree JS client SDK -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="https://js.braintreegateway.com/web/dropin/1.14.1/js/dropin.min.js"></script>

    <form method="post" autocomplete="off">
        {% if braintree_error %}
            <div class="alert alert-danger fade in">
                <button class="close" data-dismiss="alert">&times;</button>
                {{ braintree_error|safe }}
            </div>
        {% endif %}
        <div class="braintree-notifications"></div>
        <div id="braintree-dropin"></div>
        <input style="background-color: #0783ca" id="submit-button" class="btn btn-success btn-lg btn-block"
               type="button" value="Pay now!"/>
    </form>

    <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) {
                    console.log('ok')
                    $.ajax({
                        type: 'POST',
                        url: '{% url "payment:process" %}',
                        data: {
                            'paymentMethodNonce': payload.nonce,
                            'csrfmiddlewaretoken': '{{ csrf_token }}'
                        },
                    }).done(function (result) {
                        //do accordingly
                    });
                });
            });
        });
    </script>

{% endblock %}

这是点击“现在支付!”按钮时终端图片:

这是点击“立即支付!”按钮前process.html图片:

这是点击“现在支付!”按钮后process.html图片:

mrphzbgm

mrphzbgm1#

我认为您在else语句之后呈现了process.html
更改此项:

else:
    client_token = gateway.client_token.generate()
    return render(request, 'payment/process.html', {'order': order, 'client_token': client_token})

对此:

else:
        client_token = gateway.client_token.generate()
    return render(request, 'payment/process.html', {'order': order, 'client_token': client_token})

正因为这个问题,才发生了这样的事情。

相关问题