12/06/2020

How to upload Shopify PDF invoices to Google Drive with Zapier

Yesterday I discovered that my fiancée was manually downloading every invoice (generated with Order Printer Pro app) from Shopify so then she could upload them to her Google Drive "invoices" folder. It's more than a hundred manual operations per month and that number is growing. FAST.

Let's automate that with Zapier 😇.

Step 1: Trigger Zap on new Shopify paid order

First thing first, create a new zap with a Shopify trigger "New Paid Order".

Step 2: Add some glue code

Sadly the Shopify connector does not expose metadata from apps like Order Printer Pro. We won't get the pdf invoice link for free. Hopefully we can download the receipt page html source code and extract the "Download Invoice" link from there.

Add a "Code by Zapier" step along in order to run JavaScript code. Setup the 3 inputs variables below:

Copy/paste the -good-enough- code below:

const order_name_as_file_name = inputData.order_name.replace('#', '-');

fetch(inputData.order_url)
.then((res) => res.text())
.then((body) => {
  const url_regexp = /href="(.*?)"/ig;
  let match;
  while(match = url_regexp.exec(body)){
    if(match[1].includes('.pdf')){
      return callback(null, {
        order_name: order_name_as_file_name, 
        invoice_url: match[1].replace('/.pdf', `/${inputData.order_number}.pdf`)
      });
    }
  }
  
  // we could not find the PDF invoice, fail loudly
 callback(`Could not find pdf URL in: ${body}`);
})
.catch((err) => callback(err));

Step 3: Upload PDF Invoice to Google Drive

One last thing, add a "Upload File in Google Drive" action, select your drive and folder. Select the "Invoice URL" we got from our previous step as the file parameter, customize the filename as needed and you are good to go!

Job done, you now get full tracability and alerting capabilities from Zapier, enjoy!

« »
 
 
Made with on a hot august night from an airplane the 19th of March 2017.