django - 在 Django 中实现 webhook
问题描述
我以前从未实现过 webhook。我正在处理付款确认,我需要知道付款是否成功以及是否应该将客户重定向到付款完成站点。下面是结帐页面的视图。
请注意,该视图是 GET 而不是 POST,因为在付款过程中,客户端将使用第三方支付应用程序来处理付款,并且一旦处理了付款,第三方应用程序将以某种方式再次调用付款视图。使用帖子视图我遇到了错误
此网页需要您之前输入的数据才能正确显示。您可以再次发送此数据,但这样做会重复此页面之前执行的任何操作。按下reload按钮重新提交加载页面ERR_CACHE_MISS所需的数据
class Payment(View):
def __init__(self):
#Get model webshopRestaurant data for hd2900 restaurant for location id for this restaurant
self.hd2900RestaurantObject = RestaurantUtils(restaurantName = restaurantName)
def get(self, request, *args, **kwargs):
#Check if session is still valid
sessionValid = webshopUtils.checkSessionIdValidity(request = request, session_id_key = session_id_key, validPeriodInDays = self.hd2900RestaurantObject.restaurantModelData.session_valid_time)
#In this case the session has expired and the user will be redirected
if sessionValid is False:
return redirect('hd2900_takeaway_webshop')
if 'pickupForm' in request.GET:
deliveryType ='pickup'
elif 'deliveryForm' in request.GET:
deliveryType = 'delivery'
elif ('deliveryForm' not in request.GET) and ('pickupForm' not in request.GET):
#Here is where I need to implement the webhook
pass
#Write the customer info into the data base
webshopUtils.create_or_update_Order(session_id_key=session_id_key, request=request, deliveryType = deliveryType)
#Calculate the total price followed by creation of paymentID from NETS
if 'pickupForm' in request.GET:
totalPrice = webshopUtils.get_BasketTotalPrice(request.session[session_id_key]) + self.hd2900RestaurantObject.restaurantModelData.bagFee
elif 'deliveryForm' in request.GET:
totalPrice = webshopUtils.get_BasketTotalPrice(request.session[session_id_key]) + self.hd2900RestaurantObject.restaurantModelData.delivery_fee + self.hd2900RestaurantObject.restaurantModelData.bagFee
#Create an order reference and link that to NETS reference
reference = webshopUtils.get_order_reference(request = request, session_id_key=session_id_key)
#Get the payment id from NETS
payment = NETS()
paymentId = payment.get_paymentId(platform = platform,
reference = reference,
name = 'Hidden Dimsum 2900 Takeaway',
paymentReference = session_id_key,
unitPrice = int(totalPrice) *100)
if paymentId.status_code == 201:
paymentId = paymentId.json()['paymentId']
#Put the checkout key and the payment id into the page that javascript is going to read from during payment
checkoutKey = payment.getCheckoutKey(platform = platform)
context = dict()
context['paymentId'] = paymentId
context['checkoutKey'] = checkoutKey
return render(request, template_name="takeawayWebshop/webshopPayment.html", context = context)
在上面代码中的elif语句中是我需要监听webhook看支付是否成功的地方。从创建付款部分下的此文档中,可以在请求付款 ID 时添加一个 webhook(在通知键中),但我不知道如何在 Django 中实现这一点。文档片段说
通知 - 用于获取交易状态(可选) webhook - 商家想要注册支付的 webhook 列表。webhook 的最大数量为 32。 eventName - 输入您要侦听的事件 url - 回调发送到商家网站上的此 url。必须是 https。最大长度为 256 个字符。授权 - 发送到商家站点的回调的标头将设置为此值。长度必须介于 8 到 32 个字符之间,并且是字母数字。
这篇文章与这篇文章中的问题非常相似,但是在研究了这个问题之后,我仍然不确定如何实现它。特别是我真的不知道 webhook 是如何工作的。这就像我经常需要调用的获取请求吗?我应该如何在 webhook 中形成 url,以便我知道这个 url 是针对这个特定的支付会话的?
最后,对我来说重要的是添加我首先需要收听 webhook 的原因。在获得付款 ID 后的付款集成中,我使用下面的 javascript 来呈现嵌入在我的页面中的付款表单。付款完成后,它将重定向到“/paymentComplete”网址。这里的问题是在支付过程中,如果用户重新加载页面,那么 get 方法将被再次调用,在这种情况下,下面的 javascript 将不再起作用。
$(document).ready(function() {
//Get payment id from server
var paymentId = document.getElementById("paymentId").innerHTML.trim();
var checkoutKey = document.getElementById("checkoutKey").innerHTML.trim();
const checkoutOptions = {
checkoutKey: checkoutKey,
paymentId: paymentId,
containerId: "checkout-container-div",
};
const checkout = new Dibs.Checkout(checkoutOptions)
checkout.on('payment-completed', function (response) {
window.location = '/paymentComplete';
});
});
解决方案
推荐阅读
- excel - 删除标题另存为 PDF 的示例 Excel 文件
- xml - SSRS 开发:Visual Studio 2017 (VS2017) 安装的最低配置
- php - php中两个单词的比较总是返回true
- javascript - 使用 webpack react 时图像总是出现损坏
- loops - 如何在 python 中使用空格或不使用 \n 进行打印
- java - Wait.until 在 Java 和 Gradle 中使用 Selenium 返回 NoSuchMethod 异常
- php - 使用 DomDocument 简单的 Web Scraping 获取“隐藏元素”
- java - Android 和 CherryPy
- powershell - Powershell Compare-Object 比较不在组列表中的广告用户
- android - 如何导航到半片段?(导航架构组件)