export default { async fetch(request) { const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type" }; // ---------------------------- // CORS preflight // ---------------------------- if (request.method === "OPTIONS") { return new Response(null, { headers: corsHeaders }); } // ---------------------------- // Only POST allowed // ---------------------------- if (request.method !== "POST") { return json( { error: "Method not allowed" }, 405, corsHeaders ); } try { const body = await request.json(); const { roomId, plan, months } = body; // ---------------------------- // Basic validation // ---------------------------- if (!roomId || !plan || !months) { return json( { error: "Missing required fields" }, 400, corsHeaders ); } // ---------------------------- // FORWARD TO APPS SCRIPT // ---------------------------- const GAS_URL = "https://script.google.com/macros/s/AKfycbz9yTPGtk4N7vujV_RwMSLYwiHh3OzFz8slxiZLrt2iTgdjUDHKMgSP1qIC1lWRoMqoMQ/exec"; const gasResponse = await fetch(GAS_URL, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }); const data = await gasResponse.json(); // ---------------------------- // RETURN CLEAN RESPONSE // ---------------------------- return json(data, 200, corsHeaders); } catch (err) { return json( { error: err.message }, 500, corsHeaders ); } } }; // ---------------------------- // Helper: JSON response // ---------------------------- function json(data, status = 200, headers = {}) { return new Response(JSON.stringify(data), { status, headers: { "Content-Type": "application/json", ...headers } }); }