submitZoho.js
1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
export default async function handler(req, res) {
  if (req.method !== "POST") {
    return res.status(405).json({ error: "Method not allowed" });
  }
  try {
    const zohoFormUrl =
      "https://forms.zohopublic.in/services10/form/ContactUs/formperma/bJVh0UBQ6gjwfELMYAzkqMf5pfIyed3eJcjfSaEXmVg/htmlRecords/submit";
    // Ensure values exist
    const {
      name = "",
      email = "",
      mobilenumber = "",
      service = "",
      //   AdditionalMessage = "-",
      message = "-",
    } = req.body || {};
    console.log(req.body);
    // Build form data (URL encoded, required by Zoho)
    const formData = new URLSearchParams();
    formData.append("zf_referrer_name", "Website Contact Form");
    formData.append("zf_redirect_url", ""); // leave blank if no redirect
    formData.append("zc_gad", "");
    formData.append("Name_First", name);
    formData.append("Name_Last", "-");
    formData.append("Email", email);
    formData.append("SingleLine", service);
    formData.append("SingleLine1", mobilenumber);
    formData.append("MultiLine", message);
    const zohoResponse = await fetch(zohoFormUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      body: formData.toString(),
      redirect: "manual", // prevents auto-following 302
    });
    // Zoho usually replies 302 (redirect), we treat that as success
    if (zohoResponse.status === 200 || zohoResponse.status === 302) {
      return res
        .status(200)
        .json({ success: true, status: zohoResponse.status });
    } else {
      const text = await zohoResponse.text();
      console.error("Zoho response not OK:", zohoResponse.status, text);
      return res.status(zohoResponse.status).json({
        success: false,
        status: zohoResponse.status,
        message: text,
      });
    }
  } catch (error) {
    console.error("Zoho submission error:", error);
    return res.status(500).json({ success: false, error: error.message });
  }
}