2

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

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Your server is hitting the doPost five times. Check your web server/api server logs – TheMaster Feb 17 '22 at 19:12
  • No, the Server is hitting once time. I verified it by the validator that is Pipedream. All reference image and information is added to the question below side. Please check. – Krishan Kumar Feb 18 '22 at 06:13
  • Make a post with postman and see if it is reproducible – TheMaster Feb 18 '22 at 06:47
  • If anything, I suspect that the push server is trying a exponential backoff algorithm. – TheMaster Feb 18 '22 at 06:54
  • Can you suggest to me, Now what can I do? can you change my code and provide me? Because I have lack of knowledge about that. Please help me. Thanks – Krishan Kumar Feb 18 '22 at 10:03
  • I added a answer below with code sample links. See [tag info page](https://stackoverflow.com/tags/google-apps-script/info) for official documentation, free resources and more details. – TheMaster Feb 18 '22 at 10:50
  • 1
    improve your runtime execution, @TheMaster have provided a good answer below. Consider accepting/upvoting the answer if it helped you in any way. – NightEye Feb 21 '22 at 18:38
  • Hello @TheMaster , I seen both post for Optimizing my code, but I not understand and I failed to optimize. My code is still executing more than 5 seconds. Can you please help me and modify my code? and send me. Please help me... – Krishan Kumar Mar 02 '22 at 14:51

1 Answers1

1

As written in razorpay docs,

All webhook responses must return a status code in the range 2XX within a window of 5 seconds. If we receive response codes other than this or the request times out, it is considered a failure.

On failure, a webhook is re-tried at progressive intervals of time, defined in the exponential back-off policy,

The script is failing to provide a 200 status within 5 seconds. Consider optimizing your script performance using

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Hello, I seen both post for optimizimg my code, but I not understand. – Krishan Kumar Mar 02 '22 at 14:48
  • @KrishanKumar What exactly do you not understand? – TheMaster Mar 02 '22 at 18:08
  • I have very less knowledge about coding. So I don't know how can I optimize? So that's why I want Can you please optimize my code? – Krishan Kumar Mar 04 '22 at 04:33
  • @KrishanKumar Hi.. if you read the solution, attempt to fix it yourself. If you run into a specific error, when attempting, ask a new question about that error. Stackoverflow is a programmer's question and answer site. I'm afraid I cannot accept free code requests. See https://meta.stackoverflow.com/a/284237/ and https://meta.stackoverflow.com/questions/261592/ for the type of questions, you're expected to ask here. If you still want someone else to do it for you, hire a developer. – TheMaster Mar 04 '22 at 07:55