// Code by Zapier → Run JavaScript, right after Webhooks by Zapier → Catch Raw Hook. // Input Data (name on the left, value on the right): // raw_body Raw Body, from the Catch Raw Hook step // webhook_id the webhook-id header // webhook_timestamp the webhook-timestamp header // webhook_signature the webhook-signature header // secret the endpoint's signing secret (whsec_…), typed in // Next step: a Filter that only continues if "verified" is true. const crypto = require('crypto'); const TOLERANCE_SECONDS = 300; const raw = String(inputData.raw_body || ''); const id = String(inputData.webhook_id || '').trim(); const timestamp = String(inputData.webhook_timestamp || '').trim(); const signatures = String(inputData.webhook_signature || '').trim().split(' '); const secret = String(inputData.secret || '').trim(); if (!raw || !id || !timestamp || !secret) return { verified: false, reason: 'missing body, header or secret' }; if (!/^\d+$/.test(timestamp) || Math.abs(Date.now() / 1000 - Number(timestamp)) > TOLERANCE_SECONDS) { return { verified: false, reason: 'timestamp outside tolerance' }; } const key = Buffer.from(secret.replace(/^whsec_/, ''), 'base64'); const expected = Buffer.from('v1,' + crypto.createHmac('sha256', key).update(id + '.' + timestamp + '.' + raw).digest('base64')); const valid = signatures.some((entry) => { const candidate = Buffer.from(entry); return candidate.length === expected.length && crypto.timingSafeEqual(candidate, expected); }); if (!valid) return { verified: false, reason: 'signature mismatch' }; // Verified: flatten the event into fields later steps can map. const event = JSON.parse(raw); const o = (event.data && event.data.object) || {}; const text = (value) => (value === null || value === undefined ? '' : String(value)); const refs = (prefix, list) => Object.fromEntries((list || []).map((ref) => [prefix + '_ref_' + ref.system, text(ref.id)])); const out = { verified: true, reason: '', webhook_id: id, event_id: text(event.id), event_type: text(event.type), livemode: event.livemode === true, origin_type: text(event.origin && event.origin.type), origin_source_system: text(event.origin && event.origin.source_system), organization_id: text(event.organization && event.organization.id), grant_reference: text(event.grant && event.grant.external_reference), }; const person = (p, address) => Object.assign(out, { first_name: text(p.first_name), last_name: text(p.last_name), full_name: [p.first_name, p.last_name].filter(Boolean).join(' '), email: text(p.email), phone: text(p.phone), address: text(address.formatted), address_line1: text(address.line1 || address.formatted), city: text(address.city), region: text(address.region), postal_code: text(address.postal_code), country: text(address.country), }); if (o.object === 'lead') { person(o.contact || {}, o.address || {}); Object.assign(out, { lead_id: text(o.id), lead_source: text(o.source), lead_status: text(o.status), needs_address: o.needs_address === true, service_interest: text(o.service_interest), message: text(o.message), submitted_at: text(o.submitted_at), form_id: text(o.form && o.form.id), form_name: text(o.form && o.form.name), mockup_status: text(o.mockup_status), preview_looks: (o.preview_looks || []).join(', '), answers_text: (o.form_answers || []).map((a) => a.question + ': ' + ((a.labels || []).join(', ') || text(a.value))).join('\n'), project_id: text(o.project && o.project.id), project_app_url: text(o.project && o.project.app_url), gclid: text(o.attribution && o.attribution.gclid), }, refs('lead', o.external_refs)); } if (o.object === 'quote') { const client = o.client || {}; const totals = o.totals || {}; const money = (m) => (m && m.decimal) || ''; person(client, (o.property && o.property.address) || {}); Object.assign(out, { quote_id: text(o.id), quote_status: text(o.status), quote_title: text(o.title), currency: text(o.currency), total: money(totals.total), total_cents: totals.total ? totals.total.cents : 0, subtotal: money(totals.subtotal), deposit: money(totals.deposit && totals.deposit.amount), truncated: o.truncated === true, line_items_text: (o.line_items || []).filter((item) => item.selected !== false) .map((item) => [item.name, item.quantity !== null && item.quantity !== undefined ? item.quantity + ' ' + text(item.unit) : '', money(item.amount)].filter(Boolean).join(' · ')) .join('\n'), sent_at: text(o.sent_at), approved_at: text(o.approved_at), declined_at: text(o.declined_at), expires_at: text(o.expires_at), view_count: (o.engagement && o.engagement.view_count) || 0, first_viewed_at: text(o.engagement && o.engagement.first_viewed_at), app_url: text(o.links && o.links.app_url), customer_url: text(o.links && o.links.customer_url), rep_name: text(o.assigned_rep && o.assigned_rep.name), rep_email: text(o.assigned_rep && o.assigned_rep.email), project_id: text(o.project && o.project.id), client_id: text(client.id), lead_id: text(o.lead && o.lead.id), }, refs('project', o.project && o.project.external_refs), refs('client', client.external_refs), refs('quote', o.external_refs)); } return out;