|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { useRouter, useSearchParams } from "next/navigation"; |
| 5 | +import axios from "axios"; |
| 6 | +import { BeatLoader } from "react-spinners"; |
| 7 | +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; |
| 8 | +import Link from "next/link"; |
| 9 | +import { toast } from "sonner"; |
| 10 | +export default function PaymentSuccess() { |
| 11 | + const router = useRouter(); |
| 12 | + const searchParams = useSearchParams(); |
| 13 | + const orderId = searchParams.get("order_id"); |
| 14 | + const [status, setStatus] = useState<string>(""); |
| 15 | + const [isLoading, setIsLoading] = useState<boolean>(false); |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + if (!orderId) { |
| 19 | + setStatus("FAILED"); |
| 20 | + toast.error( |
| 21 | + <span className="text-rose-400"> |
| 22 | + Order ID not found. Please contact support. |
| 23 | + </span> |
| 24 | + ); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const verifyPayment = async () => { |
| 29 | + setIsLoading(true); |
| 30 | + |
| 31 | + try { |
| 32 | + const res = await axios.post("/api/verify-payment", { |
| 33 | + order_id: orderId, |
| 34 | + }); |
| 35 | + console.log("Data from verify-payment: ", res.data); |
| 36 | + |
| 37 | + if (res?.data?.success && res?.data?.data?.order_status === "PAID") { |
| 38 | + setStatus("VERIFIED"); |
| 39 | + setIsLoading(false); |
| 40 | + toast.message( |
| 41 | + <span className="text-rose-400"> |
| 42 | + ✅ Payment Verified! Redirecting to Dashboard... |
| 43 | + </span>, |
| 44 | + { |
| 45 | + description: ( |
| 46 | + <span className="text-slate-500"> |
| 47 | + We have verified your payment. |
| 48 | + </span> |
| 49 | + ), |
| 50 | + } |
| 51 | + ); |
| 52 | + setTimeout(() => router.push("/dashboard"), 2000); |
| 53 | + } else { |
| 54 | + setStatus("FAILED"); |
| 55 | + setIsLoading(false); |
| 56 | + toast.error("Payment verification failed. Please contact support."); |
| 57 | + setTimeout(() => router.push("/dashboard"), 5000); |
| 58 | + } |
| 59 | + } catch (error) { |
| 60 | + setStatus("FAILED"); |
| 61 | + toast.error( |
| 62 | + <span className="text-rose-400"> |
| 63 | + Payment verification failed. Please contact us. |
| 64 | + </span> |
| 65 | + ); |
| 66 | + |
| 67 | + let errorMessage = |
| 68 | + "Something went wrong! Please try again or contact support ⚠️"; |
| 69 | + |
| 70 | + if (axios.isAxiosError(error)) { |
| 71 | + if (error.response) { |
| 72 | + // Handle specific HTTP error codes |
| 73 | + if (error.response.status === 404) { |
| 74 | + errorMessage = "Order not found. Please check your order ID."; |
| 75 | + } else if (error.response.status === 400) { |
| 76 | + errorMessage = "Invalid request. Please try again."; |
| 77 | + } else if (error.response.status === 401) { |
| 78 | + errorMessage = "Authentication failed. Please contact support."; |
| 79 | + } else { |
| 80 | + errorMessage = "Payment verification failed. Please try again."; |
| 81 | + } |
| 82 | + } else if (error.request) { |
| 83 | + errorMessage = |
| 84 | + "Network issue! Unable to connect to the server. Please check your internet connection."; |
| 85 | + } else { |
| 86 | + errorMessage = "Unexpected error occurred while verifying payment."; |
| 87 | + } |
| 88 | + } |
| 89 | + setIsLoading(false); |
| 90 | + |
| 91 | + setStatus("FAILED"); |
| 92 | + toast.error(<span className="text-rose-400">{errorMessage}</span>); |
| 93 | + setTimeout(() => router.push("/dashboard"), 5000); |
| 94 | + } finally { |
| 95 | + setIsLoading(false); |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + verifyPayment(); |
| 100 | + }, [orderId]); |
| 101 | + |
| 102 | + return ( |
| 103 | + <div className="flex p-4 flex-col items-center justify-start pt-12 sm:pt-24 min-h-screen"> |
| 104 | + <Card className="w-full max-w-md flex flex-col space-y-4 p-4 py-10"> |
| 105 | + <CardHeader> |
| 106 | + <h3 className="text-2xl md:text-3xl text-center bg-linear-to-r from-slate-600 via-rose-500 to-orange-600 bg-clip-text text-transparent font-bold"> |
| 107 | + Verifying Payment |
| 108 | + </h3> |
| 109 | + <hr /> |
| 110 | + </CardHeader> |
| 111 | + <CardContent className="space-y-4"> |
| 112 | + <div className="w-full text-center"> |
| 113 | + {status === "VERIFIED" ? ( |
| 114 | + <div className="space-y-4"> |
| 115 | + <h3 className="text-xl font-medium text-green-600"> |
| 116 | + Your Payment has been verified successfully. |
| 117 | + </h3> |
| 118 | + <p className="text-muted-foreground text-sm"> |
| 119 | + You will be redirected to your dashboard shortly. |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + ) : status === "FAILED" ? ( |
| 123 | + <div className="space-y-4"> |
| 124 | + <h3 className="text-xl font-medium text-rose-600"> |
| 125 | + Payment Verification Failed. |
| 126 | + </h3> |
| 127 | + <p className="text-muted-foreground text-sm"> |
| 128 | + You will be redirected to your dashboard shortly. |
| 129 | + </p> |
| 130 | + </div> |
| 131 | + ) : ( |
| 132 | + <BeatLoader color="#de407e" size={20} /> |
| 133 | + )} |
| 134 | + </div> |
| 135 | + {isLoading && ( |
| 136 | + <h4 className="text-sm text-rose-500 animate-pulse font-semibold text-center"> |
| 137 | + Hold on for some time |
| 138 | + </h4> |
| 139 | + )} |
| 140 | + <p className="text-base text-gray-600 font-medium text-center bg-gradient-to-r from-slate-100 to-emerald-50 px-2 py-3 rounded-lg shadow-md hover:shadow-2xl transition-all duration-300"> |
| 141 | + Once your payment is being verified, you'll be redirected to your |
| 142 | + dashboard. If not then click{" "} |
| 143 | + <span className="text-indigo-600 underline cursor-pointer"> |
| 144 | + <Link href={"/dashboard"}>here</Link> |
| 145 | + </span> |
| 146 | + </p> |
| 147 | + </CardContent> |
| 148 | + </Card> |
| 149 | + </div> |
| 150 | + ); |
| 151 | +} |
0 commit comments