问题
I am trying to get POST request from Stripe webhooks into Google sheet, in order process sales data in Google apps environment. I set up a web app by Google Apps Script, and try to test the webhook using testing function of Stripe. I chose "invoice.payment_succeeded" as the event type to test. My code is as follow:
function doPost(e) {
var jsonString = e.postData.getDataAsString();
var event = JSON.parse(jsonString)
var ss = SpreadsheetApp.openById("XXX");
var sheet = ss.getSheetByName("XXX");
var timeStamp = new Date();
var time = Utilities.formatDate(timeStamp, "GMT+08:00", "MM/dd/yy, h:mm a");
var lastRow = sheet.getLastRow();
//Insert the data into the sheet
sheet.getRange(lastRow + 1, 1).setValue(time);
sheet.getRange(lastRow + 1, 2).setValue(event["invoice.payment_succeeded"]["data"]["object"]["amount_paid"]);
return HtmlService.createHtmlOutput(200);
}
It seems the webhook can receive POST request since my google sheet can display the "time" in first row once a test POST request is sent, but i also received the following response under the testing environment of Strip:
<!DOCTYPE html><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style type="text/css">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style></head><body style="margin:20px"><div><img alt="Google Apps Script" src="//ssl.gstatic.com/docs/script/images/logo.png"></div><div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">TypeError: Cannot read property 'data' of undefined (line 19, file "Code")</div></body></html>
来源:https://stackoverflow.com/questions/62299906/how-to-use-google-apps-script-to-get-post-request-from-stripe-webhooks