Почему я не могу подключить страницу ошибки заказа к моему сайту Django?
Я пытаюсь создать сайт электронной коммерции, но не понимаю, как можно подключить страницу ошибки заказа к сайту.
Я попробовал добавить window.location.replace в файл JavaScript после функции .then в разделе result.error.
}).then(function(result) {
if (result.error) {
console.log('payment error')
console.log(result.error.message);
window.location.replace("http://127.0.0.1:8000/payment/error/");
} else {
if (result.paymentIntent.status === 'succeeded') {
console.log('payment processed')
window.location.replace("http://127.0.0.1:8000/payment/orderplaced/");
}
}
});
},
error: function (xhr, errmsg, err) {},
Я также попробовал добавить функцию if else под преуспевшей.
}).then(function(result) {
if (result.error) {
console.log('payment error')
console.log(result.error.message);
} else {
if (result.paymentIntent.status === 'succeeded') {
console.log('payment processed')
window.location.replace("http://127.0.0.1:8000/payment/orderplaced/");
} else {
if (result.paymentIntent.status === 'failed') {
console.log('payment error')
window.location.replace("http://127.0.0.1:8000/payment/error");
}
}
}
});
},
error: function (xhr, errmsg, err) {},
Я пробовал редактировать класс Error в define, но это не сработало в файле payment views.py. Я попробовал создать новый define под названием error и заставил orders urls.py подключиться к новому error defined, но ничего не произошло, когда я попытался ввести информацию об отклоненной кредитной карте.
Мой платежный views.py:
@csrf_exempt
def stripe_webhook(request):
payload = request.body
event = None
try:
event = stripe.Event.construct_from(
json.loads(payload), stripe.api_key
)
except ValueError as e:
print(e)
return HttpResponse(status=400)
if event.type == 'payment_intent.succeeded':
payment_confirmation(event.data.object.client_secret)
else:
print('Unhandled event type {}'.format(event.type))
return HttpResponse(status=200)
class Error(TemplateView):
template_name = 'payment/error.html'