Stripe payment issues and incomplete payment somehow being stored to db

The payment gateway is working fine and all, but sometimes even if I get the message that payment was successful, there’s no entry in my db. I check the webhook screen and there are bunch of incomplete payments. When I come to check my db later on, I see bunch of payment entries in the db even when I am not using it, which I am guessing is because I go back from the payment screen? Even if payment is successful and it gets inserted in db, I am seeing two entries in the db at the same time. What is the problem here?

//Payment page
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string)

const BuyProduct = async({params}: BuyProductProps) => {
  const product = await prisma.product.findUnique({where: {id: params.id}})
  const {getUser, isAuthenticated} = getKindeServerSession()

  const user = await getUser()

  if(user == null) {
    redirect("/")
  }

  if(product == null) {
    return <div>Product not found.</div>
  }

  const paymentIntent = await stripe.paymentIntents.create({
    amount: product?.price!,
    currency: "inr",
    metadata: {productId: product?.id, userId: user?.id}
  })

  if(paymentIntent.client_secret == null) {
    throw Error("Stripe failed to create payment link.")
  }
  return (
    <CheckoutForm product={product as Product} clientSecret={paymentIntent.client_secret} />
  )
}
export default BuyProduct
//checkout form

"use client";

const stripePromise = loadStripe(
  process.env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY as string
);

const CheckoutForm = ({ product, clientSecret }: CheckoutFormProps) => {
  const [quantity, setQuantity] = useState(1);

  return (
    <div className="pb-16 lg:p-2">
      <h1 className="font-bold text-red-500 text-center tracking-wide">This is in experimentation. May not work as intended.</h1>
      <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
        <div className="flex flex-col px-2 pt-20 h-full items-center justify-center">
          <div className="relative h-[200px] w-full">
            <Image
              alt={product?.name}
              src={`${process.env.NEXT_PUBLIC_SUPABASE_URL}/storage/v1/object/public/Zephyr-products/${product?.image}`}
              fill
              className="object-contain"
            />
          </div>
          <div className="my-8">
            <h1 className="text-2xl font-bold">{`${product?.name} (x${quantity})`}</h1>
          </div>
          <div className="flex items-center gap-4">
            <Button
              disabled={quantity === 1}
              onClick={() => setQuantity((prev) => prev - 1)}
              variant="ghost"
              className="border border-border bg-accent hover:opacity-75 duration-100 transition-opacity ease-in-out"
            >
              -
            </Button>
            <div className="text-bold text-red-500 text-md">{quantity}</div>
            <Button
              disabled={quantity === 10}
              onClick={() => setQuantity((prev) => prev + 1)}
              variant="ghost"
              className="border border-border bg-accent hover:opacity-75 duration-100 transition-opacity ease-in-out"
            >
              +
            </Button>
          </div>
        </div>
        <div className="flex px-4 pt-20 h-full items-center justify-center">
          <Elements
            options={{ clientSecret, appearance: { theme: "night" } }}
            stripe={stripePromise}
          >
            <Form price={product?.price} quantity={quantity} />
          </Elements>
        </div>
      </div>
    </div>
  );
};

function Form({ price, quantity }: { price: number; quantity: number }) {
  const stripe = useStripe();
  const elements = useElements();
  const [loading, setLoading] = useState(false);
  const [errors, setError] = useState<string>();

  const finalPrice = new Intl.NumberFormat("en-IN").format(price * quantity);

  function handleSubmit(e: FormEvent) {
    e.preventDefault();
    if (stripe == null || elements == null) return;
    setLoading(true);

    stripe
      .confirmPayment({
        elements,
        confirmParams: {
          return_url: `${process.env.NEXT_PUBLIC_PAYMENT_REDIRECT_URL}/stripe/purchase-success`,
        },
      })
      .then(({ error }) => {
        if (error.type === "card_error" || error.type === "validation_error") {
          setError(error.message);
        } else {
          console.log(error);
          setError("An unexpected error occurred.");
        }
      })
      .finally(() => {
        setLoading(false);
      });
  }

  return (
    <form onSubmit={handleSubmit} className="w-full">
      //styling
    </form>
  );
}
export default CheckoutForm;
//webhook

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string)
export async function POST(req: NextRequest ) {
  const payload = await req.text()
  const response = JSON.parse(payload)
  const sig = req.headers.get("Stripe-Signature")

  const event = stripe.webhooks.constructEvent(
    payload,
    sig!,
    process.env.STRIPE_WEBHOOK_SECRET!
  );
  
  if(event.type === "charge.succeeded") {
    const charge = event.data.object

    const productId = charge.metadata.productId
    const userId = charge.metadata.userId
    const email = charge.billing_details.email
    const price = charge.amount

    const product = await prisma.product.findUnique({where: {id: productId}})

    if(product == null || email == null) {
      return new NextResponse("Bad Request", {status: 400})
    }

    await prisma.purchased.create({
      data: {
        id: product.id,
        kindeAuth: userId,
        pricePaid: price,
      }
    })
  }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật