Payment lifecycle, automated reminders, commission tracking, and escalation
After a procedure is completed (Journey 4), the patient's balance enters the billing system. Oshun's backend tracks every payment through a 6-hop state machine: awaiting → grace → staff_prompted → patient_prompted → at_risk → reconciliation → complete. The current state determines which actions Karen takes and when.
src/webhooks/billing.js → registerBillingRoutes() (line 12)
awaiting state. registerBillingRoutes() mounts the PayPal and manual payment webhook handlers. The state machine advances automatically as time passes or as events (payment received, staff action, webhook callback) come in. Each state transition is logged for audit.payments, billing_notificationsDuring the grace period, Karen sends a gentle, friendly reminder — never a harsh demand. Example: "Just a reminder about your Botox payment of $15,000 JMD. You can pay via the PayPal link below, or settle it at your next visit — whichever is easier for you." Patients can pay online via PayPal or in person at the clinic.
src/actions/billing.js
awaiting to grace once the first reminder is sent. If no action is taken, the state progresses toward patient_prompted and eventually at_risk, triggering escalation.PAYPAL_CLIENT_ID, PAYPAL_SECRET, PAYPAL_MODEWhen the patient pays — either through the PayPal link or recorded manually at the clinic — the payment status moves to complete. Karen sends a confirmation to the patient ("Got it! Your payment is confirmed — thank you!") and notifies staff via Telegram so they have a clean record.
src/webhooks/billing.js → processWebhookEvent() (line 198)
processWebhookEvent() handles the PAYMENT.SALE.COMPLETED event from PayPal. It verifies the webhook signature, matches the payment to the correct patient record, and updates the payments row to complete. For manual clinic payments, staff records via the backend trigger the same state transition. Both paths log to accounting_sync_log for reconciliation.payments, accounting_sync_logOnce payment is confirmed, the system automatically calculates the commission due. Under the Oshun deal, IrieVybz earns a 10% commission on procedures on top of the $500/mo base retainer. This is calculated per payment and logged immediately — no manual spreadsheets, no end-of-month scramble.
src/webhooks/billing.js — commission calculation (inline with payment processing)
accounting_chart_of_accounts for each transaction. Matthew can query commission totals at any time via Telegram ("Karen, what's my commission this month?").commissions, accounting_chart_of_accountsIf a balance remains unpaid, the system escalates through four stages — notifying both the patient and Oshun staff at each point. Day 3: gentle warning. Day 7: service suspension warning. Day 14: final notice. Day 30: account cancellation. Karen handles patient-facing messages; staff get Telegram alerts so they can intervene before it reaches the final stage.
src/actions/billing.js — escalation stage definitionssrc/scheduler.js)
complete state, the next job fires. Karen delivers the patient message (tone escalates from warm to firm across stages). Staff receive a Telegram alert at each step so they can intervene. If payment is received at any point, all pending escalation jobs are cancelled immediately.billing_notifications, payments