{
    "name": "Stripe Payment Handler",
    "nodes": [
        {
            "parameters": {
                "httpMethod": "POST",
                "path": "stripe-webhook",
                "options": {}
            },
            "type": "n8n-nodes-base.webhook",
            "typeVersion": 1,
            "position": [
                0,
                0
            ],
            "id": "webhook-stripe",
            "name": "Webhook Stripe"
        },
        {
            "parameters": {
                "conditions": {
                    "string": [
                        {
                            "value1": "={{ $json.body.type }}",
                            "value2": "checkout.session.completed"
                        }
                    ]
                }
            },
            "type": "n8n-nodes-base.if",
            "typeVersion": 1,
            "position": [
                200,
                0
            ],
            "id": "check-event-type",
            "name": "Is Checkout Completed?"
        },
        {
            "parameters": {
                "operation": "executeQuery",
                "query": "SELECT id FROM profiles WHERE email = '{{ $json.body.data.object.customer_details.email }}' LIMIT 1"
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 1,
            "position": [
                450,
                -100
            ],
            "id": "lookup-user",
            "name": "Find User by Email",
            "credentials": {
                "postgres": {
                    "id": "2JDD2OJz4cAsWb0J",
                    "name": "foodsnap_supabase"
                }
            }
        },
        {
            "parameters": {
                "operation": "executeQuery",
                "query": "INSERT INTO payments (user_id, amount_cents, currency, status, stripe_payment_id, plan_type) VALUES ('{{ $json.id }}', {{ $node[\"Webhook Stripe\"].json.body.data.object.amount_total }}, '{{ $node[\"Webhook Stripe\"].json.body.data.object.currency }}', 'succeeded', '{{ $node[\"Webhook Stripe\"].json.body.data.object.payment_intent }}', 'pro') RETURNING id"
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 1,
            "position": [
                650,
                -100
            ],
            "id": "log-payment",
            "name": "Log Payment"
        },
        {
            "parameters": {
                "operation": "executeQuery",
                "query": "INSERT INTO user_entitlements (user_id, entitlement_code, is_active, is_trial, valid_until, plan_type) VALUES ('{{ $node[\"Find User by Email\"].json.id }}', 'pro', true, false, NOW() + INTERVAL '30 days', 'pro') ON CONFLICT (user_id) DO UPDATE SET is_active = true, valid_until = NOW() + INTERVAL '30 days', entitlement_code = 'pro', updated_at = NOW();"
            },
            "type": "n8n-nodes-base.postgres",
            "typeVersion": 1,
            "position": [
                850,
                -100
            ],
            "id": "activate-plan",
            "name": "Activate Plan"
        }
    ],
    "connections": {
        "Webhook Stripe": {
            "main": [
                [
                    {
                        "node": "Is Checkout Completed?",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Is Checkout Completed?": {
            "main": [
                [
                    {
                        "node": "Find User by Email",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Find User by Email": {
            "main": [
                [
                    {
                        "node": "Log Payment",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Log Payment": {
            "main": [
                [
                    {
                        "node": "Activate Plan",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        }
    }
}