admin管理员组

文章数量:1122832

I am getting a TypeError of Cannot read properties of undefined (reading 'create') when i try to run my code, i am getting this error from the Initialize payment line, the const response = await flutterwave.Payment.create(payload)

const paymentFlutterwave = async (req, res) => {
  try {
    const { appointmentId } = req.body;

    const appointmentData = await appointmentModel.findById(appointmentId);

    if (!appointmentData || appointmentData.cancelled) {
      return res.json({
        success: false,
        message: "Appointment Cancelled or not found",
      });
    }

    // Payload for Flutterwave payment
    const payload = {
      tx_ref: `tx_${appointmentId}_${Date.now()}`, 
      amount: appointmentData.amount,
      currency: process.env.CURRENCY || "NGN", 
      redirect_url: process.env.REDIRECT_URL, 
      payment_options: "card, banktransfer",
      customer: {
        email: "[email protected]",
        phonenumber: "750678786898",
        name: "test customer",
      },
      customizations: {
        title: "Appointment Payment",
        description: `Payment for appointment ID ${appointmentId}`,
        logo: ".png",
      },
    };

    // Initialize payment
    const response = await flutterwave.Payment.create(payload);

    if (response.status === "success") {
      res.json({ success: true, data: response });
    } else {
      res.json({ success: false, message: response.message });
    }
  } catch (error) {
    console.error(error);
    res.json({ success: false, message: error.message });
  }
};

const paymentCallback = async (req, res) => {
    try {
      const transactionId = req.query.transaction_id;
  
      if (!transactionId) {
        return res.status(400).send("Transaction ID is required.");
      }
  
      const response = await flutterwave.Transaction.verify({ id: transactionId });
  
      if (response.status === "success" && response.data.status === "successful") {
        const appointmentId = response.data.tx_ref.split('_')[1];
        await appointmentModel.findByIdAndUpdate(appointmentId, { paid: true });
        res.status(200).send("Payment verified successfully!");
      } else {
        res.status(400).send("Payment verification failed.");
      }
    } catch (error) {
      console.error("Error verifying payment:", error);
      res.status(500).send("An error occurred during payment verification.");
    }
  };

i tried to apply flutterwave to my backend and its not working

I am getting a TypeError of Cannot read properties of undefined (reading 'create') when i try to run my code, i am getting this error from the Initialize payment line, the const response = await flutterwave.Payment.create(payload)

const paymentFlutterwave = async (req, res) => {
  try {
    const { appointmentId } = req.body;

    const appointmentData = await appointmentModel.findById(appointmentId);

    if (!appointmentData || appointmentData.cancelled) {
      return res.json({
        success: false,
        message: "Appointment Cancelled or not found",
      });
    }

    // Payload for Flutterwave payment
    const payload = {
      tx_ref: `tx_${appointmentId}_${Date.now()}`, 
      amount: appointmentData.amount,
      currency: process.env.CURRENCY || "NGN", 
      redirect_url: process.env.REDIRECT_URL, 
      payment_options: "card, banktransfer",
      customer: {
        email: "[email protected]",
        phonenumber: "750678786898",
        name: "test customer",
      },
      customizations: {
        title: "Appointment Payment",
        description: `Payment for appointment ID ${appointmentId}`,
        logo: "https://your-logo-url.com/logo.png",
      },
    };

    // Initialize payment
    const response = await flutterwave.Payment.create(payload);

    if (response.status === "success") {
      res.json({ success: true, data: response });
    } else {
      res.json({ success: false, message: response.message });
    }
  } catch (error) {
    console.error(error);
    res.json({ success: false, message: error.message });
  }
};

const paymentCallback = async (req, res) => {
    try {
      const transactionId = req.query.transaction_id;
  
      if (!transactionId) {
        return res.status(400).send("Transaction ID is required.");
      }
  
      const response = await flutterwave.Transaction.verify({ id: transactionId });
  
      if (response.status === "success" && response.data.status === "successful") {
        const appointmentId = response.data.tx_ref.split('_')[1];
        await appointmentModel.findByIdAndUpdate(appointmentId, { paid: true });
        res.status(200).send("Payment verified successfully!");
      } else {
        res.status(400).send("Payment verification failed.");
      }
    } catch (error) {
      console.error("Error verifying payment:", error);
      res.status(500).send("An error occurred during payment verification.");
    }
  };

i tried to apply flutterwave to my backend and its not working

Share Improve this question asked Nov 22, 2024 at 0:30 UfuomsUfuoms 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Cannot read properties of undefined (reading 'create')

This error means that the object flutterwave.Payment that you're trying to reference here doesn't exist.

    const response = await flutterwave.Payment.create(payload);

What documentation are you using for Flutterwave? I'm not familiar with that product, but when I look at the "Usage" documentation for the Node.js SDK, I don't see anything called Payment.

Looking at your payload, it looks like you might be trying to get a payment link. From their documentation, it says you need to make an HTTP call to do that.

  const response = await axios.post(
    'https://api.flutterwave.com/v3/payments',
    payload,
    {
      headers: {
        Authorization: `Bearer ${process.env.FLW_SECRET_KEY}`,
        'Content-Type': 'application/json'
      }
    }
  );

本文标签: reactjsTrouble integrating Flutterwave Library with node js App on localhostStack Overflow