为何无法将订单错误页面接入我的Django电商网站?
电商网站订单错误页面接入问题
我正在搭建一个电商网站,不清楚如何将订单错误页面接入网站,使用拒付信用卡测试时毫无反应。我已经尝试了以下几种方法:
尝试1:在JavaScript的.then函数result.error区域添加跳转
在处理支付结果的JS代码中,针对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) {},
尝试2:在支付成功判断后增加失败分支跳转
给paymentIntent.status增加failed判断分支,触发错误页面跳转:
}).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) {},
尝试3:修改后端视图与路由
- 将
payment views.py中的Error类视图改为函数定义 - 新增独立的error视图函数,并在
orders urls.py中配置对应路由
当前我的payment views.py代码如下:
Stripe Webhook处理函数
@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)
Error视图
class Error(TemplateView): template_name = 'payment/error.html'
内容的提问来源于stack exchange,提问作者cantony2
相关产品推荐
相关产品推荐

