{
  "name": "Student Fee Reminders with Stripe Payment Links",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 10
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        336,
        16
      ],
      "id": "ee282eb8-ba2d-4103-824b-1476c564fb68",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "14X8LkYmoiR7iKKevW-4ooWN4-LM1CLb39kZMq5d1cdw",
          "mode": "list",
          "cachedResultName": "Daily FEE Reminder"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Fees Sheet"
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "Status",
              "lookupValue": "Pending"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        512,
        16
      ],
      "id": "f15c1ae5-6a6f-4d54-aa9e-c66f4bd93123",
      "name": "Fetch Pending Fees",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "GojUvCMDXbhY8HrB",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// const results = [];\n\n// const today = new Date();\n// today.setHours(0, 0, 0, 0);\n\n// for (const item of items) {\n\n//   const dueDate = new Date(item.json.DueDate);\n//   dueDate.setHours(0, 0, 0, 0);\n\n//   const diffDays = Math.floor(\n//     (dueDate - today) / (1000 * 60 * 60 * 24)\n//   );\n\n//   const last = item.json.LastReminderSent || \"\";\n//   let reminderType = null;\n\n//   if (diffDays === 5 && last !== \"R1\") reminderType = \"R1\";\n//   else if (diffDays === 2 && last !== \"R2\") reminderType = \"R2\";\n//   else if (diffDays <= 0 && last !== \"FINAL\") reminderType = \"FINAL\";\n\n//   if (reminderType) {\n//     item.json.reminderType = reminderType;\n//     item.json.diffDays = diffDays;\n//     results.push(item);\n//   }\n// }\n\n// return results;\n\n\nconst results = [];\n\nconst today = new Date();\ntoday.setHours(0, 0, 0, 0);\n\nfor (const item of items) {\n\n  // Skip already paid records\n  if (item.json.Status === 'Paid') continue;\n\n  const dueDate = new Date(item.json.DueDate);\n  dueDate.setHours(0, 0, 0, 0);\n\n  const diffDays = Math.floor(\n    (dueDate - today) / (1000 * 60 * 60 * 24)\n  );\n\n  const last = item.json.LastReminderSent || \"\";\n  let reminderType = null;\n\n  // STEP 1: Blank → R1 (Due - 5)\n  if (last === \"\" && diffDays === 5) {\n    reminderType = \"R1\";\n  }\n\n  // STEP 2: R1 → R2 (Due - 3)\n  else if (last === \"R1\" && diffDays === 3) {\n    reminderType = \"R2\";\n  }\n\n  // STEP 3: R2 → R3 (Due ≤ 2)\n  else if (last === \"R2\" && diffDays <= 2 && diffDays > 0) {\n    reminderType = \"R3\";\n  }\n\n  // STEP 4: R3 → FINAL (Due ≤ 0)\n  else if (last === \"R3\" && diffDays <= 0) {\n    reminderType = \"FINAL\";\n  }\n\n  if (reminderType) {\n    item.json.reminderType = reminderType;\n    item.json.diffDays = diffDays;\n    results.push(item);\n  }\n}\n\nreturn results;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        704,
        16
      ],
      "id": "1739f1b0-81c7-4adc-9ee3-d17177e04d83",
      "name": "Determine Reminder Stage (R1 / R2 / R3 / FINAL)"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        896,
        16
      ],
      "id": "22445f16-abb8-4229-aba2-d7f011e7753d",
      "name": "Process Each Student Fee Record"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "14X8LkYmoiR7iKKevW-4ooWN4-LM1CLb39kZMq5d1cdw",
          "mode": "list",
          "cachedResultName": "Daily FEE Reminder"
        },
        "sheetName": {
          "__rl": true,
          "value": 1274009721,
          "mode": "list",
          "cachedResultName": "Students"
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "StudentID",
              "lookupValue": "={{ $json.StudentID }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1120,
        32
      ],
      "id": "40bec5f1-4ee0-48e3-acdd-d4582f2dfbf2",
      "name": "Get Student & Parent Details",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "GojUvCMDXbhY8HrB",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "cf8bec4a-dbf0-41b4-a07a-843c72159487",
              "leftValue": "={{ $('Process Each Student Fee Record').item.json.StripePaymentLink }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1312,
        32
      ],
      "id": "834030fa-b326-40a6-80c7-b633d25f6e59",
      "name": "Check If Stripe Payment Link Exists"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.stripe.com/v1/payment_links",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "stripeApi",
        "sendBody": true,
        "contentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            {
              "name": "line_items[0][price_data][currency]",
              "value": "usd"
            },
            {
              "name": "line_items[0][price_data][product_data][name]",
              "value": "=School Fee - {{ $('Process Each Student Fee Record').item.json.Month }}"
            },
            {
              "name": "line_items[0][price_data][unit_amount]",
              "value": "={{ $('Process Each Student Fee Record').item.json.DueAmount * 100 }}"
            },
            {
              "name": "line_items[0][quantity]",
              "value": "1"
            },
            {
              "name": "metadata[student_id]",
              "value": "={{ $('Process Each Student Fee Record').item.json.StudentID }}"
            },
            {
              "name": "metadata[month]",
              "value": "={{ $('Process Each Student Fee Record').item.json.Month }}"
            },
            {
              "name": "metadata[class]",
              "value": "={{ $('Get Student & Parent Details').item.json.Class }}"
            },
            {
              "name": "after_completion[type]",
              "value": "hosted_confirmation"
            },
            {
              "name": "after_completion[hosted_confirmation][custom_message]",
              "value": "Payment successful. Thank you! Your receipt will be sent on WhatsApp."
            },
            {
              "name": "metadata[student_name]",
              "value": "={{ $('Get Student & Parent Details').item.json.StudentName }}"
            },
            {
              "name": "metadata[parent_number]",
              "value": "={{ $('Get Student & Parent Details').item.json.ParentWhatsApp }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        1520,
        -64
      ],
      "id": "25b5c494-88bf-4bf1-81f6-4fc11c753e66",
      "name": "Create Stripe Payment Link",
      "credentials": {
        "stripeApi": {
          "id": "f3sH6wXZY0EHxmtG",
          "name": "Stripe account"
        }
      }
    },
    {
      "parameters": {
        "from": "+14155238886",
        "to": "=+{{ $('Get Student & Parent Details').item.json.ParentWhatsApp }}",
        "toWhatsapp": true,
        "message": "=🏫 *Brookside Academy*\n\n🔔 Fee Reminder\n\n👩‍🎓 Student: {{ $('Get Student & Parent Details').item.json.StudentName }}\n🏫 Class: {{ $('Get Student & Parent Details').item.json.Class }}\n💰 Due Amount: ${{ $('Process Each Student Fee Record').item.json.DueAmount }}\n📅 Due Date: {{ $('Process Each Student Fee Record').item.json.DueDate }}\n\n🔐 Pay Securely:\n{{ $json.url }}",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1744,
        -64
      ],
      "id": "7b1b51e0-b3a1-4aa6-9cac-864f6f7c36fc",
      "name": "Send WhatsApp Fee Reminder (New Link)",
      "credentials": {
        "twilioApi": {
          "id": "xmQ7NblKKLEiOZwC",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "14X8LkYmoiR7iKKevW-4ooWN4-LM1CLb39kZMq5d1cdw",
          "mode": "list",
          "cachedResultName": "Daily FEE Reminder"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Fees Sheet"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "LastReminderSent": "={{ $('Process Each Student Fee Record').item.json.reminderType }}",
            "StudentID": "={{ $('Process Each Student Fee Record').item.json.StudentID }}",
            "StripePaymentLink": "={{ $('Create Stripe Payment Link').item.json.url }}",
            "ReminderSentAt": "={{ $now.format('yyyy-MM-dd hh:mma') }}"
          },
          "matchingColumns": [
            "StudentID"
          ],
          "schema": [
            {
              "id": "StudentID",
              "displayName": "StudentID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Month",
              "displayName": "Month",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "TotalFee",
              "displayName": "TotalFee",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "DueAmount",
              "displayName": "DueAmount",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "DueDate",
              "displayName": "DueDate",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "LastReminderSent",
              "displayName": "LastReminderSent",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "StripePaymentLink",
              "displayName": "StripePaymentLink",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "ReminderSentAt",
              "displayName": "ReminderSentAt",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "ReceiptLink",
              "displayName": "ReceiptLink",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "PaymentReceiveAt",
              "displayName": "PaymentReceiveAt",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "readOnly": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1984,
        48
      ],
      "id": "9b73bf74-72f7-4707-8599-67ea96972e22",
      "name": "Update Fee Record After Reminder(New Link)",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "GojUvCMDXbhY8HrB",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "from": "+14155238886",
        "to": "=+{{ $('Get Student & Parent Details').item.json.ParentWhatsApp }}",
        "toWhatsapp": true,
        "message": "=🏫 *Brookside Academy*\n\n🔔 Fee Reminder\n\n👩‍🎓 Student: {{ $('Get Student & Parent Details').item.json.StudentName }}\n🏫 Class: {{ $('Get Student & Parent Details').item.json.Class }}\n💰 Due Amount: ${{ $('Process Each Student Fee Record').item.json.DueAmount }}\n📅 Due Date: {{ $('Process Each Student Fee Record').item.json.DueDate }}\n\n🔗 Pay Securely:\n{{ $('Process Each Student Fee Record').item.json.StripePaymentLink }}",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1536,
        112
      ],
      "id": "8b1a6778-73be-4662-80ef-0ee4c711c9a0",
      "name": "Send WhatsApp Fee Reminder (Existing Link)",
      "credentials": {
        "twilioApi": {
          "id": "xmQ7NblKKLEiOZwC",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "14X8LkYmoiR7iKKevW-4ooWN4-LM1CLb39kZMq5d1cdw",
          "mode": "list",
          "cachedResultName": "Daily FEE Reminder"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Fees Sheet"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "LastReminderSent": "={{ $('Process Each Student Fee Record').item.json.reminderType }}",
            "StudentID": "={{ $('Process Each Student Fee Record').item.json.StudentID }}",
            "StripePaymentLink": "={{ $('Create Stripe Payment Link').item.json.url }}",
            "ReminderSentAt": "={{ $now.format('yyyy-MM-dd hh:mma') }}"
          },
          "matchingColumns": [
            "StudentID"
          ],
          "schema": [
            {
              "id": "StudentID",
              "displayName": "StudentID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Month",
              "displayName": "Month",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "TotalFee",
              "displayName": "TotalFee",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "DueAmount",
              "displayName": "DueAmount",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "DueDate",
              "displayName": "DueDate",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "LastReminderSent",
              "displayName": "LastReminderSent",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "StripePaymentLink",
              "displayName": "StripePaymentLink",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "ReminderSentAt",
              "displayName": "ReminderSentAt",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "ReceiptLink",
              "displayName": "ReceiptLink",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "PaymentReceiveAt",
              "displayName": "PaymentReceiveAt",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "readOnly": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1760,
        112
      ],
      "id": "b4350ec6-f763-42cd-bcdb-a5c3318b7980",
      "name": "Update Fee Record After Reminder (Existing Link)",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "GojUvCMDXbhY8HrB",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "events": [
          "checkout.session.completed"
        ]
      },
      "type": "n8n-nodes-base.stripeTrigger",
      "typeVersion": 1,
      "position": [
        336,
        384
      ],
      "id": "98446229-89e2-41c0-88a0-c8e67a8fb93f",
      "name": "Payment Success Trigger",
      "webhookId": "29ac9210-4b26-48e5-ae9b-a1e48b959bbe",
      "credentials": {
        "stripeApi": {
          "id": "f3sH6wXZY0EHxmtG",
          "name": "Stripe account"
        }
      }
    },
    {
      "parameters": {
        "mode": "raw",
        "jsonOutput": "={\n  \"studentName\": \"{{ $json.data.object.metadata.student_name }}\",\n  \"studentId\": \"{{ $json.data.object.metadata.student_id }}\",\n  \"class\": \"{{ $json.data.object.metadata.class }}\",\n  \"month\": \"{{ $json.data.object.metadata.month }}\",\n  \"amount\": \"{{ $json.data.object.amount_total / 100 }}\",\n  \"paymentId\": \"{{ $json.data.object.payment_intent }}\",\n  \"whatsappNo\": \"{{ $json.data.object.metadata.parent_number }}\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        512,
        384
      ],
      "id": "07286a7e-2563-4ab3-9acf-42ec61a5b082",
      "name": "Extract Payment & Student Details"
    },
    {
      "parameters": {
        "jsCode": "// const html = `\n// <!DOCTYPE html>\n// <html>\n// <head>\n//   <meta charset=\"UTF-8\" />\n//   <title>Fee Receipt</title>\n//   <style>\n//     body {\n//       font-family: Arial, Helvetica, sans-serif;\n//       background: #f5f7fb;\n//       margin: 0;\n//       padding: 40px;\n//     }\n\n//     .receipt {\n//       max-width: 600px;\n//       margin: auto;\n//       background: #ffffff;\n//       padding: 32px;\n//       border-radius: 8px;\n//       border: 1px solid #e0e0e0;\n//     }\n\n//     .header {\n//       text-align: center;\n//       border-bottom: 2px solid #2b6cb0;\n//       padding-bottom: 12px;\n//       margin-bottom: 24px;\n//     }\n\n//     .header h1 {\n//       margin: 0;\n//       color: #2b6cb0;\n//       font-size: 26px;\n//       letter-spacing: 1px;\n//     }\n\n//     .sub {\n//       color: #555;\n//       font-size: 13px;\n//       margin-top: 4px;\n//     }\n\n//     .row {\n//       display: flex;\n//       justify-content: space-between;\n//       margin: 12px 0;\n//       font-size: 15px;\n//     }\n\n//     .label {\n//       color: #555;\n//       font-weight: 600;\n//     }\n\n//     .value {\n//       color: #000;\n//       font-weight: 500;\n//       text-align: right;\n//       max-width: 65%;\n//       word-break: break-all;\n//     }\n\n//     .amount-box {\n//       background: #edf2f7;\n//       padding: 16px;\n//       margin: 20px 0;\n//       border-radius: 6px;\n//       display: flex;\n//       justify-content: space-between;\n//       align-items: center;\n//       font-size: 18px;\n//       font-weight: bold;\n//       color: #2b6cb0;\n//     }\n\n//     .success {\n//       margin-top: 20px;\n//       padding: 10px;\n//       background: #e6fffa;\n//       border: 1px solid #38b2ac;\n//       color: #065f46;\n//       border-radius: 4px;\n//       text-align: center;\n//       font-size: 14px;\n//     }\n\n//     .footer {\n//       margin-top: 30px;\n//       padding-top: 12px;\n//       border-top: 1px dashed #ccc;\n//       text-align: center;\n//       font-size: 12px;\n//       color: #777;\n//     }\n//   </style>\n// </head>\n\n// <body>\n//   <div class=\"receipt\">\n//     <div class=\"header\">\n//       <h1>FEE RECEIPT</h1>\n//       <div class=\"sub\">Payment Confirmation</div>\n//     </div>\n\n//     <div class=\"row\">\n//       <div class=\"label\">Student Name</div>\n//       <div class=\"value\">${$json.studentName}</div>\n//     </div>\n\n//     <div class=\"row\">\n//       <div class=\"label\">Roll Number</div>\n//       <div class=\"value\">${$json.studentId}</div>\n//     </div>\n    \n//     <div class=\"row\">\n//       <div class=\"label\">Class</div>\n//       <div class=\"value\">${$json.class}</div>\n//     </div>\n\n//     <div class=\"row\">\n//       <div class=\"label\">Month</div>\n//       <div class=\"value\">${$json.month}</div>\n//     </div>\n\n//     <div class=\"row\">\n//       <div class=\"label\">Payment ID</div>\n//       <div class=\"value\">${$json.paymentId}</div>\n//     </div>\n\n//     <div class=\"amount-box\">\n//       <span>Amount Paid</span>\n//       <span>&#8377; ${$json.amount}</span>\n//     </div>\n\n//     <div class=\"success\">\n//       Payment received successfully ✔\n//     </div>\n\n//     <div class=\"footer\">\n//       This is a system-generated receipt.<br/>\n//       Thank you for your payment.\n//     </div>\n//   </div>\n// </body>\n// </html>\n// `;\n\n// return [{ json: { html } }];\n\n\nconst html = `\n<!DOCTYPE html>\n<html>\n<head>\n  <meta charset=\"UTF-8\" />\n  <title>Brookside Academy – Fee Receipt</title>\n  <style>\n    body {\n      font-family: Arial, Helvetica, sans-serif;\n      background: #f5f7fb;\n      margin: 0;\n      padding: 40px;\n    }\n\n    .receipt {\n      max-width: 650px;\n      margin: auto;\n      background: #ffffff;\n      padding: 32px;\n      border-radius: 10px;\n      border: 1px solid #e0e0e0;\n    }\n\n    .school-header {\n      text-align: center;\n      margin-bottom: 20px;\n    }\n\n    .school-header h1 {\n      margin: 0;\n      font-size: 28px;\n      color: #1e3a8a;\n      letter-spacing: 0.5px;\n    }\n\n    .school-header .tagline {\n      font-size: 13px;\n      color: #555;\n      margin-top: 4px;\n    }\n\n    .receipt-title {\n      text-align: center;\n      margin: 20px 0;\n      border-bottom: 2px solid #1e40af;\n      padding-bottom: 10px;\n    }\n\n    .receipt-title h2 {\n      margin: 0;\n      font-size: 22px;\n      color: #1e40af;\n    }\n\n    .row {\n      display: flex;\n      justify-content: space-between;\n      margin: 12px 0;\n      font-size: 15px;\n    }\n\n    .label {\n      color: #555;\n      font-weight: 600;\n    }\n\n    .value {\n      color: #000;\n      font-weight: 500;\n      text-align: right;\n      max-width: 65%;\n      word-break: break-word;\n    }\n\n    .amount-box {\n      background: #eef2ff;\n      padding: 18px;\n      margin: 22px 0;\n      border-radius: 8px;\n      display: flex;\n      justify-content: space-between;\n      align-items: center;\n      font-size: 18px;\n      font-weight: bold;\n      color: #1e40af;\n    }\n\n    .success {\n      margin-top: 20px;\n      padding: 12px;\n      background: #ecfdf5;\n      border: 1px solid #10b981;\n      color: #065f46;\n      border-radius: 6px;\n      text-align: center;\n      font-size: 14px;\n      font-weight: 600;\n    }\n\n    .footer {\n      margin-top: 30px;\n      padding-top: 12px;\n      border-top: 1px dashed #ccc;\n      text-align: center;\n      font-size: 12px;\n      color: #777;\n    }\n  </style>\n</head>\n\n<body>\n  <div class=\"receipt\">\n\n    <!-- School Header -->\n    <div class=\"school-header\">\n      <h1>🏫 Brookside Academy</h1>\n      <div class=\"tagline\">School Fee Payment Receipt</div>\n    </div>\n\n    <!-- Receipt Title -->\n    <div class=\"receipt-title\">\n      <h2>Fee Receipt</h2>\n    </div>\n\n    <!-- Student Details -->\n    <div class=\"row\">\n      <div class=\"label\">👩‍🎓 Student Name</div>\n      <div class=\"value\">${$json.studentName}</div>\n    </div>\n\n    <div class=\"row\">\n      <div class=\"label\">🆔 Roll Number</div>\n      <div class=\"value\">${$json.studentId}</div>\n    </div>\n\n    <div class=\"row\">\n      <div class=\"label\">🏫 Class</div>\n      <div class=\"value\">${$json.class}</div>\n    </div>\n\n    <div class=\"row\">\n      <div class=\"label\">📅 Month</div>\n      <div class=\"value\">${$json.month}</div>\n    </div>\n\n    <div class=\"row\">\n      <div class=\"label\">💳 Payment ID</div>\n      <div class=\"value\">${$json.paymentId}</div>\n    </div>\n\n    <!-- Amount -->\n    <div class=\"amount-box\">\n      <span>💰 Amount Paid</span>\n      <span>&#8377; ${$json.amount}</span>\n    </div>\n\n    <!-- Success Message -->\n    <div class=\"success\">\n      Payment received successfully ✅\n    </div>\n\n    <!-- Footer -->\n    <div class=\"footer\">\n      This is a system-generated receipt.<br/>\n      Thank you for your payment.<br/>\n      <strong>Brookside Academy</strong>\n    </div>\n\n  </div>\n</body>\n</html>\n`;\n\nreturn [{ json: { html } }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        704,
        384
      ],
      "id": "f469679d-c1d3-4cc4-8a15-c5655a06859f",
      "name": "Generate Receipt HTML (Styled)"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.docraptor.com/docs",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "authorization",
              "value": "Basic WU9VUl9BUElfS0VZX0hFUkU6"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "test",
              "value": "true"
            },
            {
              "name": "document_content",
              "value": "={{ $json.html }}"
            },
            {
              "name": "type",
              "value": "pdf"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        912,
        384
      ],
      "id": "ed85d295-a1df-4b45-b73f-bd892bfc981b",
      "name": "Convert Receipt HTML to PDF"
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "queryString": "Fee Receipts",
        "filter": {
          "driveId": {
            "__rl": true,
            "value": "My Drive",
            "mode": "list",
            "cachedResultName": "My Drive"
          },
          "whatToSearch": "folders"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1120,
        384
      ],
      "id": "1bc5f057-c594-4d29-bbdb-8e547d9e4da3",
      "name": "Find “Fee Receipts” Folder in Drive",
      "alwaysOutputData": true,
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "Wt0oCw6BmieLWjfw",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "589ca1e8-04b9-41eb-9cd1-c21d62e0d81a",
              "leftValue": "={{ $('Find “Fee Receipts” Folder in Drive').item.json.id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1312,
        384
      ],
      "id": "0c63bbc1-4930-4d1d-a5fb-485af74b8aaf",
      "name": "Check If Receipt Folder Exists"
    },
    {
      "parameters": {
        "resource": "folder",
        "name": "Fee Receipts",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "/ (Root folder)"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1536,
        480
      ],
      "id": "039bbafb-282b-492a-bdcc-068795d719a8",
      "name": "Create “Fee Receipts” Folder",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "Wt0oCw6BmieLWjfw",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "inputDataFieldName": "={{ $('Convert Receipt HTML to PDF').item.binary.data }}",
        "name": "=fee_receipts_{{ $('Extract Payment & Student Details').item.json.studentName }}_{{ $('Extract Payment & Student Details').item.json.studentId }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "={{ $json.id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1632,
        320
      ],
      "id": "6fa6d911-a119-4e71-841f-46addb3c9f69",
      "name": "Upload Receipt PDF (Existing Folder)",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "Wt0oCw6BmieLWjfw",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "inputDataFieldName": "={{ $('Convert Receipt HTML to PDF').item.binary.data }}",
        "name": "=fee_receipts_{{ $('Extract Payment & Student Details').item.json.studentName }}_{{ $('Extract Payment & Student Details').item.json.studentId }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "={{ $json.id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1760,
        480
      ],
      "id": "ba5758c5-10d6-42c5-8d08-a6eb3d024a18",
      "name": "Upload Receipt PDF (New Folder)",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "Wt0oCw6BmieLWjfw",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "mode": "raw",
        "jsonOutput": "=={\n  \"ReceiptLink\": \"https://drive.google.com/file/d/{{$json.id}}/view\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1968,
        384
      ],
      "id": "7fb4d231-1a82-493c-a319-3b3605d17834",
      "name": "Generate Public Receipt Link"
    },
    {
      "parameters": {
        "from": "+14155238886",
        "to": "=+{{ $('Extract Payment & Student Details').item.json.whatsappNo }}",
        "toWhatsapp": true,
        "message": "=🏫 *Brookside Academy*\n\nThank you! 😊 Your fee payment was successful Received.\n\n👩‍🎓 Student: {{ $('Extract Payment & Student Details').item.json.studentName }}\n🏫 Class: {{ $('Extract Payment & Student Details').item.json.class }}\n🆔 Roll No: {{ $('Extract Payment & Student Details').item.json.studentId }}\n📅 Month: {{ $('Extract Payment & Student Details').item.json.month }}\n💰 Amount Paid: ${{ $('Extract Payment & Student Details').item.json.amount }}\n\n📄 Download receipt here:\n{{ $('Generate Public Receipt Link').item.json.ReceiptLink }}\n\nHave a great day! 🌟",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        2288,
        384
      ],
      "id": "15ec906d-1524-4cba-9e02-6dca6dabc58e",
      "name": "Send Payment Confirmation on WhatsApp",
      "credentials": {
        "twilioApi": {
          "id": "xmQ7NblKKLEiOZwC",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "14X8LkYmoiR7iKKevW-4ooWN4-LM1CLb39kZMq5d1cdw",
          "mode": "list",
          "cachedResultName": "Daily FEE Reminder"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Fees Sheet"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "StudentID": "={{ $('Extract Payment & Student Details').item.json.studentId }}",
            "Status": "Paid",
            "DueAmount": "0",
            "Month": "={{ $('Extract Payment & Student Details').item.json.month }}",
            "ReceiptLink": "={{ $('Generate Public Receipt Link').item.json.ReceiptLink }}",
            "PaymentReceiveAt": "={{ $now.format('yyyy-MM-dd hh:mma') }}",
            "LastReminderSent": "="
          },
          "matchingColumns": [
            "StudentID"
          ],
          "schema": [
            {
              "id": "StudentID",
              "displayName": "StudentID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Month",
              "displayName": "Month",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "TotalFee",
              "displayName": "TotalFee",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "DueAmount",
              "displayName": "DueAmount",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "DueDate",
              "displayName": "DueDate",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "LastReminderSent",
              "displayName": "LastReminderSent",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "StripePaymentLink",
              "displayName": "StripePaymentLink",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "ReminderSentAt",
              "displayName": "ReminderSentAt",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "ReceiptLink",
              "displayName": "ReceiptLink",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "PaymentReceiveAt",
              "displayName": "PaymentReceiveAt",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "readOnly": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        2464,
        384
      ],
      "id": "7458a0a9-fc73-459e-8eb7-bf3da7edfc52",
      "name": "Update Fee Status & Receipt Link",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "GojUvCMDXbhY8HrB",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "content": "## WF-1 | Automated Fee Reminder & Stripe Payment Link Generate",
        "height": 176,
        "width": 464
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        336,
        -64
      ],
      "id": "6856e656-d11f-4343-a269-498a384ce9c9",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "content": "## WF-1 | Stripe Payment Confirmation, Receipt Generation & WhatsApp Delivery",
        "height": 208,
        "width": 464
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        336,
        272
      ],
      "id": "74a1c93e-bb82-4d93-bb8f-07b693b88bc8",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "content": "## Google sheet\nCreate your own spreadsheet with these tabs/columns, then select it in the Google Sheets nodes:\n- **Daily FEE Reminder → Fees Sheet**: `Status`, `StudentID`, `LastReminderSent`, `StripePaymentLink`, `ReminderSentAt`, `Month`, `DueAmount`, `ReceiptLink`, `PaymentReceiveAt`\n- **Daily FEE Reminder → Students**: `StudentID`",
        "height": 128,
        "width": 288
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        144
      ],
      "id": "0fd254dc-c612-43bc-b6cc-84b85fcfe0a9",
      "name": "Sticky Note2"
    },
    {
      "parameters": {
        "operation": "share",
        "fileId": {
          "__rl": true,
          "value": "={{ $json.ReceiptLink }}",
          "mode": "url"
        },
        "permissionsUi": {
          "permissionsValues": {
            "role": "reader",
            "type": "anyone",
            "allowFileDiscovery": true
          }
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        2144,
        384
      ],
      "id": "8ade870f-b205-4f16-bd7e-7569bfca4e31",
      "name": "Share Receipt",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "Wt0oCw6BmieLWjfw",
          "name": "Google Drive account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Fetch Pending Fees",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Pending Fees": {
      "main": [
        [
          {
            "node": "Determine Reminder Stage (R1 / R2 / R3 / FINAL)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Determine Reminder Stage (R1 / R2 / R3 / FINAL)": {
      "main": [
        [
          {
            "node": "Process Each Student Fee Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Each Student Fee Record": {
      "main": [
        [],
        [
          {
            "node": "Get Student & Parent Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Student & Parent Details": {
      "main": [
        [
          {
            "node": "Check If Stripe Payment Link Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If Stripe Payment Link Exists": {
      "main": [
        [
          {
            "node": "Create Stripe Payment Link",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send WhatsApp Fee Reminder (Existing Link)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Stripe Payment Link": {
      "main": [
        [
          {
            "node": "Send WhatsApp Fee Reminder (New Link)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp Fee Reminder (New Link)": {
      "main": [
        [
          {
            "node": "Update Fee Record After Reminder(New Link)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Fee Record After Reminder(New Link)": {
      "main": [
        [
          {
            "node": "Process Each Student Fee Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp Fee Reminder (Existing Link)": {
      "main": [
        [
          {
            "node": "Update Fee Record After Reminder (Existing Link)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Fee Record After Reminder (Existing Link)": {
      "main": [
        [
          {
            "node": "Process Each Student Fee Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Payment Success Trigger": {
      "main": [
        [
          {
            "node": "Extract Payment & Student Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Payment & Student Details": {
      "main": [
        [
          {
            "node": "Generate Receipt HTML (Styled)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Receipt HTML (Styled)": {
      "main": [
        [
          {
            "node": "Convert Receipt HTML to PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert Receipt HTML to PDF": {
      "main": [
        [
          {
            "node": "Find “Fee Receipts” Folder in Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find “Fee Receipts” Folder in Drive": {
      "main": [
        [
          {
            "node": "Check If Receipt Folder Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If Receipt Folder Exists": {
      "main": [
        [
          {
            "node": "Upload Receipt PDF (Existing Folder)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create “Fee Receipts” Folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create “Fee Receipts” Folder": {
      "main": [
        [
          {
            "node": "Upload Receipt PDF (New Folder)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload Receipt PDF (Existing Folder)": {
      "main": [
        [
          {
            "node": "Generate Public Receipt Link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload Receipt PDF (New Folder)": {
      "main": [
        [
          {
            "node": "Generate Public Receipt Link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Public Receipt Link": {
      "main": [
        [
          {
            "node": "Share Receipt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Payment Confirmation on WhatsApp": {
      "main": [
        [
          {
            "node": "Update Fee Status & Receipt Link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Share Receipt": {
      "main": [
        [
          {
            "node": "Send Payment Confirmation on WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": false
  },
  "versionId": "e96966df-909a-422f-8d6d-cc68300660b0",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f1c26778ec36097a9722a22c47c5e9c01f00b8862dadd0121ec118026a370fa6"
  },
  "id": "iWRuJ9LkrrpzlJoO",
  "tags": []
}