Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Userscript Harvest invoice update hook to send POST to Zapier webhook URL
// ==UserScript==
// @name Harvest Invoice Hook
// @namespace https://siebird.com
// @version 0.1
// @description Harvest invoice status hook to send POST to Zapier to update Google spreadsheet row
// @include /^https:\/\/\w+\.harvestapp\.com\/invoices\/[0-9]+/
// @exclude /^https:\/\/\w+\.harvestapp\.com\/invoices\/[0-9]+\/duplicate
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
// ==/UserScript==
$(function() {
// Set invoice variables to be sent to Zapier
var status = 'draft';
var paymentDate = 'YYYY-MM-DD';
// Are we viewing or editing the invoice?
if ($('#invoice_issued_at_human_format').length) {
var invoiceSubject = $('#invoice_subject').val();
var invoiceId = $('#invoice_number').val();
var issueDateArr = $('#invoice_issued_at_human_format').val().split('/');
var totalAmount = parseFloat($('#total_amount').text().replace('$','').replace(',',''), 2);
} else {
var invoiceSubject = $('.subject .definition').first().text();
var invoiceId = $('.client-doc-details .definition strong').first().text();
var issueDateArr = $('.definition:eq(1)').text().split('/');
var totalAmount = parseFloat($('#total-amount').text().replace('$','').replace(',',''), 2);
}
var issueDate = issueDateArr[2] + '-' + issueDateArr[0] + '-' + issueDateArr[1];
var postData = {
"invoice_id": invoiceId,
"invoice_subject": invoiceSubject,
"invoice_status": status,
"invoice_total": totalAmount,
"invoice_issueDate": issueDate,
"invoice_paymentDate": "",
"invoice_paymentAmount": 0
};
console.log(invoiceId, invoiceSubject, status, totalAmount, issueDate, paymentDate);
// Submit new invoice or mark invoice as sent
$('#send_invoice_form form :submit, #mark_as_sent_form form :submit').on('click', function(e){
// e.preventDefault();
var formTarget = $(this).closest('form');
postData.invoice_status = "sent";
// Post data to Zapier hook
postWebhook(formTarget, postData);
});
// A payment has been made
$('#payment_form :submit').on('click', function(e){
// e.preventDefault();
// update values if changed
var totalAmount = parseFloat($('#total-amount').text().replace('$','').replace(',',''), 2);
var payment = $('#payment_amount').val();
var paymentDate = $('#payment_paid_at').val().split("/");
var status = (payment < totalAmount) ? "sent" : "paid";
paymentDate = paymentDate[2] + '-' + paymentDate[0] + '-' + paymentDate[1];
//$('#payment_paid_at').val(paymentDate);
var formTarget = $('#payment_form form:first');
postData.invoice_status = status;
postData.invoice_paymentDate = paymentDate;
postData.invoice_paymentAmount = payment;
// Post data to Zapier hook
postWebhook(formTarget, postData);
});
// Edit/update invoice
$('#edit_invoice_form :submit').on('click', function(e){
//e.preventDefault();
// update values if changed
invoiceId = $('#invoice_number').val();
invoiceSubject = $('#invoice_subject').val();
totalAmount = parseFloat($('#total_amount').text().replace('$','').replace(',',''), 2);
issueDateArr = $('#invoice_issued_at_human_format').val().split('/');
issueDate = issueDateArr[2] + '-' + issueDateArr[0] + '-' + issueDateArr[1];
var formTarget = $('#edit_invoice_form');
postData.invoice_id = invoiceId;
postData.invoice_subject = invoiceSubject;
postData.invoice_total = totalAmount;
postData.invoice_issueDate = issueDate;
// Post data to Zapier hook
postWebhook(formTarget, postData);
});
}); // end doc ready
function postWebhook(target, dataObject) {
$.ajax({
type: 'POST',
url: 'https://hooks.zapier.com/hooks/catch/.../.../', // Update with Zapier's webhook URL
dataType: 'json',
success: function (msg) {
if (msg) {
target.submit();
} else {
alert('The HOOK data was not posted.');
}
},
data: dataObject
});
}
@siebird

This comment has been minimized.

Copy link
Owner Author

@siebird siebird commented Feb 13, 2017

Update line 100 with your Zapier webhook url.

@siebird

This comment has been minimized.

Copy link
Owner Author

@siebird siebird commented Apr 6, 2017

The script recently broke with changes Harvest made.

Changelog:

  • added editing/updating invoice - updates row in spreadsheet
  • added subject line + issue date to postData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment