How to use Google Apps Script to get POST request from Stripe webhooks

有些话、适合烂在心里 提交于 2020-06-29 05:23:08

问题


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 &#39;data&#39; of undefined (line 19, file &quot;Code&quot;)</div></body></html>

来源:https://stackoverflow.com/questions/62299906/how-to-use-google-apps-script-to-get-post-request-from-stripe-webhooks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!