Я хотел бы интегрировать платеж Braintree на свой веб-сайт.Я знаю, что в моем коде много ошибок, так как я учусь.В настоящее время я перенаправлен на страницу Failed http, так как платеж не проходит.Как я могу осуществить платеж на странице корзины?Спасибо.
views.py (приложение корзины)
def cart_detail(request, total=0, cart_items = None):
try:
cart = Cart.objects.get(cart_id=_cart_id(request))
cart_items = CartItem.objects.filter(cart=cart, active=True)
for cart_item in cart_items:
total += (cart_item.service.price)
except ObjectDoesNotExist:
pass
gateway = braintree.BraintreeGateway(
braintree.Configuration(
braintree.Environment.Sandbox,
merchant_id="",
public_key="",
private_key=""
)
)
braintree_total = int(total)
#print(braintree_total)
if request.method == 'GET':
client_token = gateway.client_token.generate()
else: # for when the method is POST
print(request.POST)
result = gateway.transaction.sale({
'amount': braintree_total,
'payment_method_nonce': request.POST['payment_method_nonce'],
'options': {
"submit_for_settlement": True
}
})
if result.is_success or result.transaction:
return HttpResponse('Done')
return HttpResponse('Failed')
return render(request, 'cart.html', dict(cart_items = cart_items, total = total, client_token = client_token))
cart.html (приложение корзины)
<div class="col-12 col-sm-12 col-md-12 col-lg-6 text-center">
<table class="table my_custom_table">
<thead class="my_custom_thead">
<tr>
<th>
Checkout
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Please review your shopping cart items before proceeding with the order payment.
</td>
</tr>
<tr>
<td class="text-left">
Your total is: <strong>£{{ total }}</strong>
</td>
</tr>
</tbody>
</table>
<div class="mx-auto">
<form id="payment-form" method="post" action="{% url 'cart:cart_detail' %}">
{% csrf_token %}
<div id="bt-dropin"></div>
<input type="hidden" id="nonce" name="payment_method_nonce" />
<button class="btn btn-primary btn-block" type="submit" id="submit-button"><span>Test Transaction</span></button>
</form>
<a href="{% url 'home:allProd' %}" class="btn btn-secondary btn-block my_custom_button">Continue Shopping</a>
</div>
</div>
</div>
<br>
{% endif %}
<script src="https://js.braintreegateway.com/web/dropin/1.13.0/js/dropin.min.js"></script>
<script>
var button = document.querySelector('#submit-button');
var client_token = '{{ client_token }}';
braintree.dropin.create({
authorization: client_token,
container: '#bt-dropin',
paypal: {
flow: 'vault'
}
}, function (createErr, instance) {
form.addEventListener('submit', function (event) {
event.preventDefault();
instance.requestPaymentMethod(function (err, payload) {
if (err) {
console.log('Error', err);
return;
}
// Add the nonce to the form and submit
document.querySelector('#nonce').value = payload.nonce;
form.submit();
});
});
});
</script>
{% endblock %}
Спасибо за помощь.