{
  "active": false,
  "name": "01 - Daily Stripe Data Sync",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 6 * * *"
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Every Day at 6 AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "url": "https://api.stripe.com/v1/subscriptions",
        "options": {
          "queryParameters": {
            "parameters": [
              {
                "name": "status",
                "value": "active"
              },
              {
                "name": "limit",
                "value": "100"
              }
            ]
          }
        }
      },
      "id": "get-stripe-subs",
      "name": "Get Stripe Subscriptions",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Transform Stripe subscription data\nconst stripeData = $input.first().json;\nconst subscriptions = stripeData.data || [];\n\nconst transformed = [];\n\nfor (const sub of subscriptions) {\n  // Skip if no plan\n  if (!sub.plan) continue;\n  \n  // Get discount amount\n  let discountAmount = 0;\n  if (sub.discount && sub.discount.coupon) {\n    if (sub.discount.coupon.amount_off) {\n      discountAmount = sub.discount.coupon.amount_off / 100;\n    } else if (sub.discount.coupon.percent_off) {\n      discountAmount = (sub.plan.amount / 100) * (sub.discount.coupon.percent_off / 100);\n    }\n  }\n  \n  transformed.push({\n    stripe_customer_id: sub.customer,\n    stripe_subscription_id: sub.id,\n    stripe_invoice_id: sub.latest_invoice,\n    billing_period_start: new Date(sub.current_period_start * 1000).toISOString().split('T')[0],\n    billing_period_end: new Date(sub.current_period_end * 1000).toISOString().split('T')[0],\n    plan_name: sub.plan.nickname || sub.plan.id,\n    plan_amount: sub.plan.amount / 100,\n    total_charged: sub.plan.amount / 100,\n    base_charge: sub.plan.amount / 100,\n    overage_charges: 0,\n    discounts: discountAmount,\n    status: sub.status,\n    created: new Date(sub.created * 1000).toISOString()\n  });\n}\n\nreturn transformed.map(item => ({ json: item }));"
      },
      "id": "transform-data",
      "name": "Transform Stripe Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "operation": "upsert",
        "tableId": "customers",
        "options": {
          "onConflict": "stripe_customer_id"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "stripe_customer_id": "={{ $json.stripe_customer_id }}",
            "current_plan": "={{ $json.plan_name }}",
            "current_price": "={{ $json.plan_amount }}",
            "updated_at": "={{ $now.toISO() }}"
          }
        }
      },
      "id": "upsert-customer",
      "name": "Upsert Customer",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        850,
        300
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "insert",
        "tableId": "actual_charges",
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "stripe_invoice_id": "={{ $json.stripe_invoice_id }}",
            "billing_period_start": "={{ $json.billing_period_start }}",
            "billing_period_end": "={{ $json.billing_period_end }}",
            "base_charge": "={{ $json.base_charge }}",
            "overage_charges": "={{ $json.overage_charges }}",
            "discounts": "={{ $json.discounts }}",
            "total_charged": "={{ $json.total_charged }}"
          }
        },
        "options": {
          "queryName": "customer_id",
          "queryValue": "=(SELECT id FROM customers WHERE stripe_customer_id = '{{ $json.stripe_customer_id }}')"
        }
      },
      "id": "insert-charges",
      "name": "Insert Actual Charges",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "\u2705 Stripe Data Sync Complete\n\nProcessed: {{ $('Transform Stripe Data').all().length }} subscriptions\nTime: {{ $now.toLocaleString() }}",
        "channel": "revenue-alerts",
        "otherOptions": {}
      },
      "id": "slack-notification",
      "name": "Send Slack Notification",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1250,
        300
      ],
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Every Day at 6 AM": {
      "main": [
        [
          {
            "node": "Get Stripe Subscriptions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Stripe Subscriptions": {
      "main": [
        [
          {
            "node": "Transform Stripe Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Transform Stripe Data": {
      "main": [
        [
          {
            "node": "Upsert Customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upsert Customer": {
      "main": [
        [
          {
            "node": "Insert Actual Charges",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert Actual Charges": {
      "main": [
        [
          {
            "node": "Send Slack Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1,
  "updatedAt": "2024-01-01T00:00:00.000Z",
  "versionId": "1"
}