My apologies for asking what may be a basic question for experienced coders. I am trying to capture webhook data from Shopify into a google sheet. I am using Google Apps Script but I feel like I have hit a roadblock.
This is the link of the code that I have changed to suit my needs: https://github.com/samaybar/googlewebhookreceiver/blob/master/Code.gs
I published the script as a web app and entered the url link for the Shopify webhook to send me the data, but I get no data. I know that the webhook is firing as I have made another webhook(for same type - Orders Paid), and it is showing JSON data in webhook.site These are the resources that I have checked :
- POSTed JSON to Google Apps Script, need to extract value and place in Google Sheets
- How do I create a doPost(e) function in Apps Script project to capture HTTP POST data from web service?
I also checked the https://github.com/gsuitedevs/apps-script-samples for any clue to see what I might be doing wrong but not getting anywhere.
When I try to debug the function doPost (which contains almost all of my code), I get this error:
TypeError: Cannot read property "postData" from undefined. (line 9, file "
This is the part of my code:
function doPost(e){
var ss=SpreadsheetApp.openById("SHEETID");
var sheet = ss.getSheetByName("Sheet1");
var lastRow = ss.getDataRange().getLastRow();
ss.insertRowAfter(lastRow);
var params=JSON.stringify(e.postData.contents);
params=JSON.parse(params);
var myObj=JSON.parse(e.postData.contents);
var orderId=myObj.name;
var email=myObj.customer.email;
var phone=myObj.shipping_address.phone;
var ss = SpreadsheetApp.getActivesheet();
ss.getRange(lastRow +1,1).setValue(orderId);
ss.getRange(lastRow +1,2).setValue(email);
ss.getRange(lastRow +1,3).setValue(phone);
SpreadsheetApp.flush();
return HtmlService.createHtmlOutput("Post Received");
}
I Googled the error and came across this reddit post which describes the same problem https://www.reddit.com/r/javascript/comments/55rwaj/google_apps_scripts_json_postdata_typeerror/
I am however, unable to follow what should I do to fix the code.
References from Shopify: https://help.shopify.com/en/manual/sell-online/notifications/webhooks#create-webhooks https://help.shopify.com/en/api/reference/events/webhook
Any help will be really appreciated as I have nearly exhausted myself trying to figure out what to do.
Note: I know this can be done via Zapier integration but I would like to do it using google Apps script. I am not a programmer/coder
Update: I modified the code above by removing the following lines:
var params=JSON.stringify(e.postData.contents);
params=JSON.parse(params);
var myObj=JSON.parse(e.postData.contents);
var orderId=myObj.name;
var email=myObj.customer.email;
var phone=myObj.shipping_address.phone;
var ss = SpreadsheetApp.getActivesheet();
ss.getRange(lastRow +1,1).setValue(orderId);
ss.getRange(lastRow +1,2).setValue(email);
ss.getRange(lastRow +1,3).setValue(pone);
And adding these lines of code:
Logger.log("Post function called");
if (typeof e !== 'undefined')
Logger.log(e.parameter);
sheet.getRange(lastRow+1, 1).setValue(JSON.stringify(e));
My aim was to verify if data was being sent at all. I am now getting the following which I believe is the raw data :
{"parameter":{},"contextPath":"","contentLength":6102,"queryString":"","parameters":{},"postData":{"type":"application/json","length":6102,"contents":"{\"id\":820982911946154508,\"email\":\"jon@doe.ca\",\"closed_at\":null,\"created_at\":\"2018-12-30T20:58:25-05:00\",\"updated_at\":\"2018-12-30T20:58:25-05:00\",\"number\":234,\"note\":null,\"token\":\"123456abcd\",\"gateway\":null,\"test\":true,\"total_price\":\"254.98\",\"subtotal_price\":\"244.98\",\"total_weight\":0,\"total_tax\":\"0.00\",\"taxes_included\":false,\"currency\":\"USD\",\"financial_status\":\"voided\",\"confirmed\":false,\"total_discounts\":\"5.00\",\"total_line_items_price\":\"249.98\",\"cart_token\":null,\"buyer_accepts_marketing\":true,\"name\":\"#9999\",\"referring_site\":null,\"landing_site\":null,\"cancelled_at\":\"2018-12-30T20:58:25-05:00\",\"cancel_reason\":\"customer\",\"total_price_usd\":null,\"checkout_token\":null,\"reference\":null,\"user_id\":null,\"location_id\":null,\"source_identifier\":null,\"source_url\":null,\"processed_at\":null,\"device_id\":null,\"phone\":null,\"customer_locale\":\"en\",\"app_id\":null,\"browser_ip\":null,\"landing_site_ref\":null,\"order_number\":1234,\"discount_applications\":[{\"type\":\"manual\",\"value\":\"5.0\",\"value_type\":\"fixed_amount\",\"allocation_method\":\"one\",\"target_selection\":\"explicit\",\"target_type\":\"line_item\",\"description\":\"Discount\",\"title\":\"Discount\"}],\"discount_codes\":[],\"note_attributes\":[],\"payment_gateway_names\":[\"visa\",\"bogus\"],\"processing_method\":\"\",\"checkout_id\":null,\"source_name\":\"web\",\"fulfillment_status\":\"pending\",\"tax_lines\":[],\"tags\":\"\",\"contact_email\":\"jon@doe.ca\",\"order_status_url\":\"https:\/\/checkout.shopify.com\/13609500736\/orders\/123456abcd\/authenticate?key=abcdefg\",\"presentment_currency\":\"USD\",\"total_line_items_price_set\":{\"shop_money\":{\"amount\":\"249.98\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"249.98\",\"currency_code\":\"USD\"}},\"total_discounts_set\":{\"shop_money\":{\"amount\":\"5.00\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"5.00\",\"currency_code\":\"USD\"}},\"total_shipping_price_set\":{\"shop_money\":{\"amount\":\"10.00\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"10.00\",\"currency_code\":\"USD\"}},\"subtotal_price_set\":{\"shop_money\":{\"amount\":\"244.98\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"244.98\",\"currency_code\":\"USD\"}},\"total_price_set\":{\"shop_money\":{\"amount\":\"254.98\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"254.98\",\"currency_code\":\"USD\"}},\"total_tax_set\":{\"shop_money\":{\"amount\":\"0.00\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"0.00\",\"currency_code\":\"USD\"}},\"total_tip_received\":\"0.0\",\"line_items\":[{\"id\":487817672276298554,\"variant_id\":null,\"title\":\"Aviator sunglasses\",\"quantity\":1,\"price\":\"89.99\",\"sku\":\"SKU2006-001\",\"variant_title\":null,\"vendor\":null,\"fulfillment_service\":\"manual\",\"product_id\":788032119674292922,\"requires_shipping\":true,\"taxable\":true,\"gift_card\":false,\"name\":\"Aviator sunglasses\",\"variant_inventory_management\":null,\"properties\":[],\"product_exists\":true,\"fulfillable_quantity\":1,\"grams\":100,\"total_discount\":\"0.00\",\"fulfillment_status\":null,\"price_set\":{\"shop_money\":{\"amount\":\"89.99\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"89.99\",\"currency_code\":\"USD\"}},\"total_discount_set\":{\"shop_money\":{\"amount\":\"0.00\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"0.00\",\"currency_code\":\"USD\"}},\"discount_allocations\":[],\"tax_lines\":[]},{\"id\":976318377106520349,\"variant_id\":null,\"title\":\"Mid-century lounger\",\"quantity\":1,\"price\":\"159.99\",\"sku\":\"SKU2006-020\",\"variant_title\":null,\"vendor\":null,\"fulfillment_service\":\"manual\",\"product_id\":788032119674292922,\"requires_shipping\":true,\"taxable\":true,\"gift_card\":false,\"name\":\"Mid-century lounger\",\"variant_inventory_management\":null,\"properties\":[],\"product_exists\":true,\"fulfillable_quantity\":1,\"grams\":1000,\"total_discount\":\"5.00\",\"fulfillment_status\":null,\"price_set\":{\"shop_money\":{\"amount\":\"159.99\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"159.99\",\"currency_code\":\"USD\"}},\"total_discount_set\":{\"shop_money\":{\"amount\":\"5.00\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"5.00\",\"currency_code\":\"USD\"}},\"discount_allocations\":[{\"amount\":\"5.00\",\"discount_application_index\":0,\"amount_set\":{\"shop_money\":{\"amount\":\"5.00\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"5.00\",\"currency_code\":\"USD\"}}}],\"tax_lines\":[]}],\"shipping_lines\":[{\"id\":271878346596884015,\"title\":\"Generic Shipping\",\"price\":\"10.00\",\"code\":null,\"source\":\"shopify\",\"phone\":null,\"requested_fulfillment_service_id\":null,\"delivery_category\":null,\"carrier_identifier\":null,\"discounted_price\":\"10.00\",\"price_set\":{\"shop_money\":{\"amount\":\"10.00\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"10.00\",\"currency_code\":\"USD\"}},\"discounted_price_set\":{\"shop_money\":{\"amount\":\"10.00\",\"currency_code\":\"USD\"},\"presentment_money\":{\"amount\":\"10.00\",\"currency_code\":\"USD\"}},\"discount_allocations\":[],\"tax_lines\":[]}],\"billing_address\":{\"first_name\":\"Bob\",\"address1\":\"123 Billing Street\",\"phone\":\"555-555-BILL\",\"city\":\"Billtown\",\"zip\":\"K2P0B0\",\"province\":\"Kentucky\",\"country\":\"United States\",\"last_name\":\"Biller\",\"address2\":null,\"company\":\"My Company\",\"latitude\":null,\"longitude\":null,\"name\":\"Bob Biller\",\"country_code\":\"US\",\"province_code\":\"KY\"},\"shipping_address\":{\"first_name\":\"Steve\",\"address1\":\"123 Shipping Street\",\"phone\":\"555-555-SHIP\",\"city\":\"Shippington\",\"zip\":\"40003\",\"province\":\"Kentucky\",\"country\":\"United States\",\"last_name\":\"Shipper\",\"address2\":null,\"company\":\"Shipping Company\",\"latitude\":null,\"longitude\":null,\"name\":\"Steve Shipper\",\"country_code\":\"US\",\"province_code\":\"KY\"},\"fulfillments\":[],\"refunds\":[],\"customer\":{\"id\":115310627314723954,\"email\":\"john@test.com\",\"accepts_marketing\":false,\"created_at\":null,\"updated_at\":null,\"first_name\":\"John\",\"last_name\":\"Smith\",\"orders_count\":0,\"state\":\"disabled\",\"total_spent\":\"0.00\",\"last_order_id\":null,\"note\":null,\"verified_email\":true,\"multipass_identifier\":null,\"tax_exempt\":false,\"phone\":null,\"tags\":\"\",\"last_order_name\":null,\"currency\":\"USD\",\"default_address\":{\"id\":715243470612851245,\"customer_id\":115310627314723954,\"first_name\":null,\"last_name\":null,\"company\":null,\"address1\":\"123 Elm St.\",\"address2\":null,\"city\":\"Ottawa\",\"province\":\"Ontario\",\"country\":\"Canada\",\"zip\":\"K2H7A8\",\"phone\":\"123-123-1234\",\"name\":\"\",\"province_code\":\"ON\",\"country_code\":\"CA\",\"country_name\":\"Canada\",\"default\":true}}}","name":"postData"}}
This entire value is coming inside a single cell. Now I have two challenges/issues here:
- How to only parse data that I need? (order ID, email, Phone)
- How to send back the 200 OK code so I do not end up getting multiple rows of same value?(currently I am getting 4-5 multiple rows when Webhook is fired in a short interval)