解析 Woocommerce Webhook (JSON)
Parse Woocommerce Webhook (JSON)
Wommcommerce 在更新产品或下订单时触发 Webhook。我正在尝试使用作为 WebApp 发布的 Google Apps Script (GAS) 捕获此 Webhook。我最终想要实现的是将收到的订单写入 Google Spreadsheet。
我无法从这些 webhook 中获取正确的(或至少一些)数据。我尝试了不同的方法将数据写入 sheet,但我在 sheet 中捕获的唯一东西是 'undefined'。老实说,我不知道如何处理 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 日志告诉我这是已发送的:
{"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 获取数据的正确方向。
在e.postData.content
也使用 JSON.parse 而不是实用程序。
感谢 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
Wommcommerce 在更新产品或下订单时触发 Webhook。我正在尝试使用作为 WebApp 发布的 Google Apps Script (GAS) 捕获此 Webhook。我最终想要实现的是将收到的订单写入 Google Spreadsheet。
我无法从这些 webhook 中获取正确的(或至少一些)数据。我尝试了不同的方法将数据写入 sheet,但我在 sheet 中捕获的唯一东西是 'undefined'。老实说,我不知道如何处理 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 日志告诉我这是已发送的:
{"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 获取数据的正确方向。
在e.postData.content
也使用 JSON.parse 而不是实用程序。
感谢 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