8

I'm trying to create a script to capture data via HTTP POST from Ejunkie. When someone makes a purchase on ejunkie, they can transmit all the order data via HTTP POST to a common notification url (documentation). I want to capture that data so I can get it into a Google Sheet.

So I've setup a sheet with a doPost(e) function like so:

// attempt 1
function doPost(e) {
  if(typeof e !== 'undefined')
  Logger.log(e.parameters);
}

// attempt 2
function doPost(e) {
  var data = JSON.stringify(e);
  Logger.log(data);
}

which I've published as a Web App with access to anyone, and then entered this script URL as the common notification URL in ejunkie.

I've tried a couple of test transactions, but I'm getting nothing in the Logs.

Any ideas? Thanks in advance for any help.

Here's the ejunkie documentation on this subject.

Ben Collins
  • 321
  • 1
  • 2
  • 9
  • 1
    You have the letter "s" on the end: "parameters." You can't have that. It's `e.parameter` Show the URL that you are using, minus the file ID, does it match the published URL of the Web App? – Alan Wells Mar 30 '17 at 20:52
  • Hi @SandyGood thanks for replying and pointing that out. I've changed it to just "parameter" now but not seeing anything. The URL of the published web app (set to access by anyone) is: **https://script.google.com/macros/s/{SCRIPT_ID}/exec** and that's what I've put into ejunkie as the common notification url. – Ben Collins Mar 31 '17 at 00:48
  • Markdown got messed up there, that url should read: **`https://script.google.com/macros/s/{SCRIPT_ID}/exec`** – Ben Collins Mar 31 '17 at 01:03
  • I don't know what the answer is, and I'm not sure how to proceed from here. Hopefully someone else can help. – Alan Wells Mar 31 '17 at 02:00
  • Thanks for chipping in anyway. Cheers! – Ben Collins Mar 31 '17 at 02:09
  • I did a quick test, I cannot see anything in the logs as well. However, still able paste stringyfied post data to a spreadsheet, when I made a dummy post using UrlFetchApp. Did you try pasting the data onto a spreadsheet rather than just trying to log it? Did you try making a dummy call? Also is the access set to "anyone even anonymous"? – Jack Brown Mar 31 '17 at 04:48
  • Hi, thanks for commenting @JagannathanAlagurajan - can you share your code? I've not been able to paste the data into a Sheet either. I'm trying dummy calls using the Hurl.it service. Yes, access is set to anyone even anonymous. – Ben Collins Mar 31 '17 at 20:31
  • I have posted by code below, try it and let me know what happens. Note hurl would also get an echo of the parameter sent, that way you are sure your call went through! All the best! – Jack Brown Mar 31 '17 at 22:41

3 Answers3

14

This is the code I used to post the data to my sheet:

function doPost(e) {
  Logger.log("I was called")
  if(typeof e !== 'undefined')
  Logger.log(e.parameter);
  var ss= SpreadsheetApp.openById("ID here")
  var sheet = ss.getSheetByName("Sheet2")
  sheet.getRange(1, 1).setValue(JSON.stringify(e))
  return ContentService.createTextOutput(JSON.stringify(e))
}

Used curl to make a post request and got echo of the data sent!

albert
  • 8,112
  • 3
  • 47
  • 63
Jack Brown
  • 5,802
  • 2
  • 12
  • 27
  • Thanks @jagannathan! I've got it working now. I think I was getting confused by the echo (showing as a 302 redirect?) but now the data is showing in my sheet. I've got the ejunkie data showing up in my sheet now when I test some transactions. Awesome! As @noogui mentioned below, I don't think the Logger.log will do anything, certainly nothing showed up for me int he logs. Again, thanks a bunch! – Ben Collins Apr 01 '17 at 01:59
  • The reason you are getting a 302 redirect error: https://developers.google.com/apps-script/guides/content#redirects. Set your hurl to allow redirect, you will notice your data you sent via post is returned to hurl (i.e echoed back from the server, if you use the above code as is). Hope that make sense! Cheers! – Jack Brown Apr 01 '17 at 02:32
  • Ah, ok! Thanks for clarifying @jackbrown, v helpful. Cheers! – Ben Collins Apr 03 '17 at 14:45
  • Update 13 April: returning the ContentService will cause a redirect, so simply leaving the return empty avoids this issue and results in a 200 response. Thanks to GDE Riël Notermans in helping me figure it out. – Ben Collins Apr 13 '17 at 13:32
6

Instead of using Logger.log() as a way to notify yourself if your calls made it through, try sending an email to yourself instead. This is the snippet:

function doPost(e) {
  if(typeof e !== 'undefined')

  MailApp.sendEmail({
     to: "youremail@gmail.com",
     subject: "Call Sucessful",
     htmlBody: "I am your <br>" +
               "DATA"
    });
}

Just allow the necessary permission if asked. If I'm not mistaken Logger.log is for script editor only and not for your production web apps.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
2

Could try something like this? Also you might look @ this thread: doPost(e) does not return parameters but doGet(e) does?

function doPost(e) {
  
  if(typeof e !== 'undefined')
    return ContentService.createTextOutput(JSON.stringify(e.parameter));
  
}
Community
  • 1
  • 1
OblongMedulla
  • 1,471
  • 9
  • 21