Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

json - API Call from Zapier Javascript - Unexpected Token Error

I have a webhook set up to catch all new orders from a Shopify store. Zapier captures all of the information from the order as variables. Anything in the following code with the prefix "inputData" is data extracted directly from the Shopify new order webhook.

I'm trying to reformat this data a bit, convert any PDF files attached to the order to JPEG using ConvertAPI, then submit the order to Printify via their API. API keys have obviously been removed.

As it stands, I am receiving an "Unexpected Token" error from Zapier when I try to test this code. Any advice would be greatly appreciated, thanks in advance.

//input data from shopify (not included in actual code)
var inputData = {
order_number : "1234",
shopify_id : "123456789",
line_item_name : 'Test Product 1, Test Product 2, Test Product 3',
line_item_sku : "P-2-50-33728-ME01, 21212391028-12345, P-2-50-33726-ME01",
line_item_fulfillment : "manual, printify, manual",
line_item_properties : '12345,123456789-abcdefg123456789,https://www.google.com/,1,https://www.google.com/google.jpg,https://www.google.com/google.svg,2,12346,123456789-abcdefg123456790,https://www.google.com/,1,https://www.google.com/google.jpg,https://www.google.com/google.svg,2',
shipping_fname : "John",
shipping_lname : "Thompson",
shipping_email : "[email protected]",
shipping_phone : "800.555.5555",
shipping_country : "United States",
shipping_region : "VA",
shipping_address1 : "1234 Main St",
shipping_address2 : "Apt 2",
shipping_city : "Richmond",
shipping_zip : "23223",
}

//prefixes
var personalized_prefix = "Prs-"
var non_personalized_prefix = "NonPrs-"
var custom_image_url_prefix = 'https://fpd-shopify-v2.herokuapp.com/api/fe/line_item_cache_print_pdf/'

//create arrays
var sku_array = inputData.line_item_sku.split(", ");
var properties_array = inputData.line_item_properties.split(",");

//count personalized items
var personalized_skus = sku_array.filter(function (sku) {
  return sku[0].toLowerCase() === 'p';
});

//If there are no personalized items, end script
if (personalized_skus.length = 0) {
    throw new Error("No Personalized Items");
}

//Begin Image Conversion
var image_url = new Array();

var requestOptions = {
  method: 'POST',
  redirect: 'follow'
};

personalized_skus.forEach(async(personalized_sku, index) => {
let pdf_url = custom_image_url_prefix.concat(properties_array[(7 * index) + 1]);
const response = await (`httpsdo.convertapi.com//Pdf2Image?ApiKey=[my_key]&File=${pdf_url}&ImageResolutionH=300&ImageResolutionV=300&JpgQuality=100&OutputFormat=jpg&StoreFile=true&Timeout=900`, requestOptions)
  .then(response => console.log(response))
  .then(result => image_url[index] = result)
  .catch(error => console.log('error', error))
  });

//build json for printify order
var printify_order = {};

//Shopify order ID and order number
printify_order.external_id = personalized_prefix.concat(inputData.shopify_id);
printify_order.label = personalized_prefix.concat(inputData.order_number);

//add personalized products
printify_order.line_items = new Array();
personalized_skus.forEach((personalized_sku, index) => {
  let p_sku_split = personalized_sku.split("-");
  printify_order.line_items[index] = {
    print_provider_id : p_sku_split[1],
  blueprint_id : p_sku_split[2],
  variant_id : p_sku_split[3],
  print_areas: {
    front: image_url[index]
  }
}
});

//Add shipping information
printify_order.shipping_method = 1;
printify_order.send_shipping_notification = false;
printify_order.address_to = {};
printify_order.address_to.first_name = inputData.shipping_fname;
printify_order.address_to.last_name = inputData.shipping_lname;
printify_order.address_to.email = inputData.shipping_email;
printify_order.address_to.phone = inputData.shipping_phone;
printify_order.address_to.country = inputData.shipping_country;
printify_order.address_to.region = inputData.shipping_region;
printify_order.address_to.address1 = inputData.shipping_address1;
printify_order.address_to.address2 = inputData.shipping_address2;
printify_order.address_to.city = inputData.shipping_city;
printify_order.address_to.zip = inputData.shipping_zip;

var json_order = JSON.stringify(printify_order);

var requestOptions = {
  method: 'POST',
  headers: {
    Content-type : "application/json",
    Authorization : "Bearer [api_key]"
  },
  body: json_order,
  redirect: 'follow'
};

fetch("https://api.printify.com/v1/shops/[shop_id]/orders.json", requestOptions)
  .then(response => response.text())
  .then(result => callback(null, result))
  .catch(error => callback(null, error));
question from:https://stackoverflow.com/questions/65894920/api-call-from-zapier-javascript-unexpected-token-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...