I created below code for capturing some data from webhooks and recording into google sheet + sending emails. Everything working fine. But code is executing multiple time. And executing after a random interval of time again and again.
Help me to solve the issue. Why its executing again and again.
All Required details are as below:
Click here to see Execution Screenshot.
Click here to see Screenshot from Google Sheet.
My Code is Below:
//this is a function that fires when the webapp receives a GET request
function doGet(e) {
return HtmlService.createHtmlOutput("request received");
}
function doPost(e) {
var jsonString = e.postData.getDataAsString();
var myData = JSON.parse(jsonString);
var email = myData.payload.payment.entity.email;
var contact = myData.payload.payment.entity.contact;
var amount = myData.payload.payment.entity.amount;
var paymentid = myData.payload.payment.entity.id;
var orderid = myData.payload.payment.entity.order_id;
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = Math.max(sheet.getLastRow(),1);
sheet.insertRowAfter(lastRow);
var currentTime = new Date();
var trimname = email.substring(0, 5);
var subject = "Download Starter E-Book";
var description = "Please find attachment of Book and Invoice";
var docFile = DriveApp.getFileById("1PD628HNgP4XO1Cy37DEGWi50OPK0iwq_K-HzlLfPhc0");
var docpdfFile = DriveApp.getFileById("1vPhguJJfaXflxKVzwuUzZsNk3ZC9fBqV");
var tempFolder = DriveApp.getFolderById("1UmqqjC35t0Zxu4ULcoDelsoZBW-NpHtM");
var pdfFolder = DriveApp.getFolderById("1Mvwx54H3lccPDLDxSpYZ5ir83Ylq3gUK");
var tempFile = docFile.makeCopy(tempFolder);
var tempDocFile = DocumentApp.openById(tempFile.getId());
var body = tempDocFile.getBody();
body.replaceText("{name}", trimname);
body.replaceText("{time}", currentTime);
body.replaceText("{email}", email);
body.replaceText("{contact}", contact);
body.replaceText("{orderid}", orderid);
body.replaceText("{payid}", paymentid);
body.replaceText("{invoice}", paymentid);
tempDocFile.saveAndClose();
var pdfContentBlob = tempFile.getAs(MimeType.PDF);
var finalpdfFile = pdfFolder.createFile(pdfContentBlob).setName(paymentid);
GmailApp.sendEmail(email, subject, description, { attachments: [finalpdfFile, docpdfFile], name: 'Book Centre'});
sheet.getRange(lastRow + 1, 1).setValue(currentTime);
sheet.getRange(lastRow + 1, 2).setValue(email);
sheet.getRange(lastRow + 1, 3).setValue(contact);
sheet.getRange(lastRow + 1, 4).setValue(amount/100);
sheet.getRange(lastRow + 1, 5).setValue(paymentid);
sheet.getRange(lastRow + 1, 6).setValue(orderid);
SpreadsheetApp.flush();
return HtmlService.createHtmlOutput("post request received");
}
//--------------// Updates...
I have verified that webhook triggering once. I parallelly connected the webhook validator and found my Razorpay account is triggering once time.
I trigger 5 times from Razorpay. Webhook Validator capture it 5 times. but Google AppScript captures it multiple times.
One more thing I observed is that Every time Google Script is capturing different data but validator is capturing the same data in a good way. All reference screenshot is attached below.
Razorpay Webhook Configuration: Screenshot
Webhook Validator: Screenshot
Google App Script Execution: Screenshot