当产品更新或下订单时,Wommcommerce 会触发 Webhook。我正在尝试使用作为 WebApp 发布的 Google Apps 脚本 (GAS) 捕获此 Webhook。我最终想要实现的目标是将收到的订单写入 Google 电子表格。
我无法从这些 webhook 中获取正确的(或至少部分)数据。我尝试了不同的方法将数据写入工作表,但我在工作表中发现的唯一内容是“未定义”。老实说我不知道如何处理 JSON 数据。我的测试功能现在是:
function doPost(e){
var doc = DocumentApp.openById('myDocId');
var jstring = Utilities.jsonStringify(e);
doc.appendParagraph(jstring);
}
当触发 webhook(订单已更新)时,我在文档中得到以下输出:
{"parameter":{},"contextPath":"","contentLength":1523,"queryString":null,"parameters":{},"postData":"FileUpload"}
显然根本没有解析,但我尝试的所有方法(例如 getContentText())都不起作用。 Woocommerce 中的 Webhook-Log 告诉我此信息已发送:
{"product":{"title":"testprodukt","id":136,"created_at":"2015-10-06T13:15:38Z","updated_at":"2015-10-09T15:19:04Z","type":"simple","status":"publish","downloadable":true,"virtual":true,"permalink":"http://MYDOMAIN/produkt/testprodukt/","sku":"","price":"5.00","regular_price":"5.00","sale_price":null,"price_html":"€5,00","taxable":false,"tax_status":"taxable","tax_class":"","managing_stock":false,"stock_quantity":"","in_stock":true,"backorders_allowed":false,"backordered":false,"sold_individually":false,"purchaseable":true,"featured":false,"visible":true,"catalog_visibility":"visible","on_sale":false,"product_url":"","button_text":"","weight":null,"dimensions":{"length":"","width":"","height":"","unit":"cm"},"shipping_required":false,"shipping_taxable":true,"shipping_class":"","shipping_class_id":null,"description":"
somedescription
n","short_description":"","reviews_allowed":false,"average_rating":"0.00","rating_count":0,"related_ids":[],"upsell_ids":[],"cross_sell_ids":[],"parent_id":0,"categories":[],"tags":[],"images":[{"id":0,"created_at":"2015-10-09T15:19:05Z","updated_at":"2015-10-09T15:19:05Z","src":"http://MYDOMAIN/wp-content/plugins/woocommerce/assets/images/placeholder.png","title":"Platzhalter","alt":"Platzhalter","position":0}],"featured_src":false,"attributes":[],"downloads":[],"download_limit":0,"download_expiry":0,"download_type":"","purchase_note":"","total_sales":7,"variations":[],"parent":[]}}
我正在寻找提示、链接、示例,以及如何插入如何从 Webhook 中获取数据的正确方向。
最佳答案
感谢 Riel 和 plus.google 的帮助我想到了。这是我的代码作为示例,也许有帮助:
function doPost(request) {
var json = request.postData.getDataAsString();
var obj = JSON.parse(json);
// getting some of the Woocommerce data just as an example
// Hook was fired after order.created
var id = obj.order.id;
var orderNumber = obj.order.order_number;
var payMethod = obj.order.payment_details.method_title;
// write data in a document, not useful, but straightforward for testing
var doc = DocumentApp.openById('myDocumentId');
doc.appendParagraph("Id: " + id);
doc.appendParagraph("orderNumber: " + orderNumber);
doc.appendParagraph("payMethod: " + payMethod);
}
结果应如下所示:
Id: 171
orderNumber: 171
payMethod: Per Nachnahme
关于json - 解析 Woocommerce Webhook (JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33044189/