{
  "schema_version": "1.0.0",
  "id": "stripe-payout-gap",
  "title": "Log a Stripe payout and flag a gap",
  "category": "finance",
  "difficulty": "advanced",
  "featured": true,
  "description": "When Stripe pays out, compare the payout with gross charges minus fees and refunds for the period, log the result, and flag any gap over a tolerance.",
  "tags": [
    "stripe",
    "reconciliation",
    "payouts",
    "webhooks",
    "google-sheets",
    "finance"
  ],
  "apps": [
    "Webhooks by Zapier",
    "Google Sheets",
    "Code by Zapier",
    "Filter by Zapier",
    "Slack"
  ],
  "plan": {
    "minimum": "Professional",
    "reasons": [
      "multi-step Zap: 5 steps after the trigger",
      "Filter by Zapier",
      "premium app: Webhooks by Zapier"
    ]
  },
  "tasks": {
    "typical": 3,
    "max": 4
  },
  "trigger": {
    "app": "Webhooks by Zapier",
    "event": "Catch Hook",
    "kind": "instant",
    "title": "Stripe payout.paid webhook",
    "why": "Zapier's Stripe app has no New Payout trigger, so this catches Stripe's own `payout.paid` webhook. In Stripe, add an endpoint pointing at the Catch Hook URL for that one event; Zapier sees the payout object's fields.",
    "config": {
      "Pick off a Child Key": "(leave blank)"
    },
    "outputs": [
      "id",
      "type",
      "amount",
      "currency",
      "arrival_date",
      "status",
      "balance_transaction"
    ],
    "sample": "sample-trigger.json"
  },
  "steps": [
    {
      "key": "period",
      "kind": "search",
      "app": "Google Sheets",
      "event": "Lookup Spreadsheet Row",
      "title": "Find the totals for this payout's period",
      "why": "Your sales sheet keeps one row per payout date with gross charges, Stripe fees, and refunds summed by formula. The lookup reads that row.",
      "fields": {
        "Drive": "My Google Drive",
        "Spreadsheet": "Stripe reconciliation",
        "Worksheet": "Periods",
        "Lookup Column": "arrival_date",
        "Lookup Value": "{{trigger.arrival_date}}",
        "Should this step be considered a \"success\" when nothing is found?": "Yes"
      },
      "outputs": [
        "row_number",
        "_zap_search_was_found_status",
        "charges_gross",
        "fees",
        "refunds"
      ]
    },
    {
      "key": "compare",
      "kind": "code",
      "app": "Code by Zapier",
      "event": "Run JavaScript",
      "title": "Compare in cents",
      "why": "Stripe sends the payout in the smallest currency unit (cents, or whole yen); the sheet holds decimals. The comparison is done in integers so 0.1 + 0.2 never becomes a false gap.",
      "code": {
        "language": "javascript",
        "module": "payout-cents",
        "file": "code/payout-cents.js"
      },
      "input_data": {
        "payout_amount": "{{trigger.amount}}",
        "payout_currency": "{{trigger.currency}}",
        "charges_gross": "{{period.charges_gross}}",
        "fees": "{{period.fees}}",
        "refunds": "{{period.refunds}}",
        "tolerance": "100"
      },
      "outputs": [
        "payout_minor",
        "expected_minor",
        "gap_minor",
        "gap_display",
        "payout_display",
        "needs_review"
      ]
    },
    {
      "key": "log",
      "kind": "action",
      "app": "Google Sheets",
      "event": "Create Spreadsheet Row",
      "title": "Log the payout",
      "why": "One row per payout with the expected amount and the gap, matched or not.",
      "fields": {
        "Drive": "My Google Drive",
        "Spreadsheet": "Stripe reconciliation",
        "Worksheet": "Payouts",
        "Columns": {
          "payout_id": "{{trigger.id}}",
          "arrival_date": "{{trigger.arrival_date}}",
          "payout": "{{compare.payout_display}}",
          "expected_minor": "{{compare.expected_minor}}",
          "gap": "{{compare.gap_display}}",
          "needs_review": "{{compare.needs_review}}",
          "period_found": "{{period._zap_search_was_found_status}}",
          "logged_at": "{{meta.human_now}}"
        }
      },
      "outputs": [
        "id",
        "row_number"
      ]
    },
    {
      "key": "gap",
      "kind": "filter",
      "app": "Filter by Zapier",
      "event": "Only continue if...",
      "title": "Only payouts with a gap over tolerance",
      "why": "Most payouts match within a dollar and stop here.",
      "conditions": [
        [
          {
            "field": "{{compare.needs_review}}",
            "operator": "(Boolean) Is true"
          }
        ]
      ],
      "usually": false
    },
    {
      "key": "flag",
      "kind": "action",
      "app": "Slack",
      "event": "Send Channel Message",
      "title": "Flag the gap in #finance",
      "why": "States the payout, the expected amount, and the difference, so the reviewer starts in the Stripe balance report for that date.",
      "fields": {
        "Channel": "#finance",
        "Message Text": ":mag: Stripe payout {{trigger.id}} ({{trigger.arrival_date}}) was {{compare.payout_display}}, gap {{compare.gap_display}} against the sheet. Check disputes, adjustments, and refunds for that period. Row {{log.row_number}}.",
        "Send as a bot?": "Yes",
        "Bot Name": "Zap alerts",
        "Include a link to this Zap?": "No"
      },
      "outputs": [
        "ts",
        "permalink"
      ]
    }
  ]
}
