I'm encountering Error: [TypeError: Order.findOneAndUpdate is not a function] while updating the status [NextJS 14]

26 Views Asked by At

So i was integrating payment gateway into a Nextjs14 pages router website and I was checking the status of payment to redirect the user to success or failure pages, and it works fine. The problem is that I'm trying to update the status of payment in the DB of that specific order to true (as in payment successful) but there is this error always popping up.

Error: [TypeError: Order.findOneAndUpdate is not a function]

This is the code of the API status check from the gateway.

//api/status/[id].js
import sha256 from "crypto-js/sha256";
import { NextResponse } from "next/server";
import { Order } from "@/models/Order";

export default async function POST(req, res) {
    const data = await req.formData();

    const status = data.get("code");
    const merchantId = data.get("merchantId");
    const transactionId = data.get("transactionId");

    const st = `/pg/v1/status/${merchantId}/${transactionId}` + process.env.NEXT_PUBLIC_SALT_KEY;
    const dataSha256 = sha256(st);
    const checksum = dataSha256 + "###" + process.env.NEXT_PUBLIC_SALT_INDEX;

    const url = `https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status/${merchantId}/${transactionId}`;

    const options = {
        method: "GET",
        headers: {
            accept: "application/json",
            "Content-Type": "application/json",
            "X-VERIFY": checksum,
            "X-MERCHANT-ID": `${merchantId}`,
        },
    };

    try {
        const response = await fetch(url, options);
        const responseData = await response.json();

        if (responseData.code === "PAYMENT_SUCCESS") {
            // Find the order based on transactionId
            const order = await Order.findOneAndUpdate(
                { transactionid: transactionId },
                { paid: true }, // Update the 'paid' field to true
                { new: true } // Return the updated document
            );

            if (!order) {
                console.error("Order not found with transaction ID:", transactionId);
                // Handle case where order is not found (optional)
            }

            return NextResponse.redirect("http://localhost:3000/success", {
                status: 302, // Use a valid redirect status code
            });
        } else {
            return NextResponse.redirect("http://localhost:3000/failure", {
                status: 302,
            });
        }
    } catch (error) {
        console.error("Error:", error);
        return NextResponse.error(error);
    }
}

export const config = {
    runtime: "edge"
};

The status is returned successfully when there is no updation in the DB is added to the code.

and here is the Schema of the order

//Order.js
import { Schema, model, models } from "mongoose";

const OrderSchema = new Schema(
    {
        line_items: Object,
        name: String,
        mobile: String,
        email: String,
        address: String,
        city: String,
        state: String,
        country: String,
        postal: String,
        amount: Number,
        paid: Boolean,
        transactionid: String
    },
    {
        timestamps: true
    }
);

export const Order = models?.Order || model('Order', OrderSchema);

How can I make the update if the payment is successful or not, if this is not the way to do this.

0

There are 0 best solutions below