【发布时间】:2020-04-08 16:10:19
【问题描述】:
您好,我在 django 中遇到了网络挂钩问题到我的网络钩子我如何向用户显示它以通知它是否成功
我的代码是:这是用户将结帐详细信息发送到我的服务器的地方 跳跃添加一个等待的微调器,根据用户是否已付款,解析为滴答声或 X
<script>
document.getElementById("checkout-btn").addEventListener('click', analyzeText());
function analyzeText(){
var wallet = document.getElementById('wallet').value;
var phone = document.getElementById('phone').value;
var order_id = document.getElementById('order_id').value;
$.ajax({
type: "POST",
url: "{% url 'orders:Checkout' %}", /* Call python function in this script */
data: {csrfmiddlewaretoken: '{{ csrf_token }}',
wallet: wallet,
phone:phone,
order_id:order_id,
}, /* Passing the text data */
success: callback
});
}
function callback(response){
console.log(response);
}
</script>
发送到的视图执行此操作,它基本上只是调用结帐 API,如果成功或失败,该 API 现在处理结帐,应将 webhook 发送到我的回调 url
def processOrder(request):
def spektraCheckoutLink(amount, reference, transaction_id, order_id):
url = "https://api.spektra.co/api/v1/payments/pay-in"
# request payload
# spektraAccountName is Optional, used for subaccount-specific payments
payload = {
"amount": amount,
"account": phone,
"currency": "KES",
"description": reference,
}
# pass authorization token obtained during authentication in headers
authcode = str(auth())
headers = {
'Content-Type': "application/json",
'Authorization': "Bearer " + authcode
}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
# Print response data
print(response.json())
data = response.json()
return True
if request.method == "POST":
global order_id
try:
order_id = request.POST['order_id']
except Exception as e:
print(e)
print(traceback.format_exc())
raise HttpResponseServerError()
order = Order.objects.get(id=order_id)
try:
wallet = request.POST['wallet']
order.wallet = wallet
except Exception as e:
print(e)
print(traceback.format_exc())
raise HttpResponseServerError()
try:
phone = request.POST['phone']
order.phone = phone
except Exception as e:
print(e)
print(traceback.format_exc())
raise HttpResponseServerError()
order.save()
try:
current_user = get_user_model().objects.get(id=request.user.id)
Transaction.objects.get_or_create(user=current_user, order=order, amount=order.price, phone=order.phone)
trans = Transaction.objects.get(order=order)
link = spektraCheckoutLink(order.price, order.order_reference, trans.id, order.id)
return redirect(link)
except Exception as e:
print(e)
print(traceback.format_exc())
# redirect to 404
raise HttpResponseServerError()
我不知道当回调到达时我将如何更改用户前端的微调器状态以通知他们交易是否成功
这是我的 webhook 视图,它更新了支付订单我的问题是我如何在他的浏览器中通知用户付款已完成我不知道该怎么做
def processOrderStatus(request):
# updates transaction number and status
data = json.loads(request.body)
order = Order.objects.get(phone=data['account'])
order.order_status = order.PAID
order.save
id = request.user.id
return redirect('orders:Orders', user_id=id)
【问题讨论】:
-
WC to SO,你能解释一下你想如何通知用户吗?使用电子邮件?更改订单状态?向用户发出警报?包含一些代码(你的 webhook)会很棒
-
嗨,我更改了它添加了更多详细信息
-
如果我理解得很好,用户将被发送到“spektra”结帐,当用户完成该过程时,它会被发送回您的站点吗?
-
基本上是的,但我现在想要的是不使用结帐链接,而是根据回调 url 中给出的数据状态更改状态的微调器,我不知道该怎么做
标签: django notifications webhooks