{
        "description": "Process payment confirmation and update invoice status",
        "trigger": {
            "event": "payment:recorded",
            "table": "payments",
            "condition": "status == \"completed\""
        },
        "steps": [
            {
                "order": 1,
                "type": "query",
                "sql": "SELECT SUM(amount) as total_paid FROM payments WHERE reference_table = 'invoices' AND reference_id = {invoice_id} AND status = 'completed'",
                "outputVar": "payment_total",
                "description": "Calculate total payments for invoice"
            },
            {
                "order": 2,
                "type": "update",
                "table": "invoices",
                "fields": {
                    "amount_paid": "{payment_total.total_paid}",
                    "status": "{payment_total.total_paid} >= amount ? \"paid\" : \"pending\""
                },
                "where": {
                    "invoice_id": "{invoice_id}"
                },
                "description": "Update invoice payment status"
            },
            {
                "order": 3,
                "type": "conditional",
                "condition": "{payment_total.total_paid} >= {invoice.amount}",
                "ifTrue": [
                    {
                        "type": "action",
                        "action": "invoices:send-receipt",
                        "description": "Send payment receipt to customer"
                    },
                    {
                        "type": "notification",
                        "recipients": ["created_by", "accounting_team"],
                        "template": "invoice_fully_paid",
                        "data": {
                            "invoice_number": "{invoice.invoice_number}",
                            "amount": "{invoice.amount}",
                            "paid_at": "NOW()"
                        }
                    },
                    {
                        "type": "emit",
                        "event": "invoice:fully:paid",
                        "data": {
                            "invoice_id": "{invoice_id}",
                            "amount": "{invoice.amount}"
                        }
                    }
                ],
                "ifFalse": [
                    {
                        "type": "notification",
                        "recipients": ["created_by"],
                        "template": "partial_payment_received",
                        "data": {
                            "invoice_number": "{invoice.invoice_number}",
                            "amount_paid": "{payment_total.total_paid}",
                            "amount_remaining": "{invoice.amount} - {payment_total.total_paid}"
                        }
                    }
                ]
            },
            {
                "order": 4,
                "type": "audit",
                "action": "payment_processed",
                "data": {
                    "invoice_id": "{invoice_id}",
                    "payment_id": "{payment_id}",
                    "amount": "{payment.amount}",
                    "new_total": "{payment_total.total_paid}"
                }
            }
        ]
    }