diff --git a/actions/payment.ts b/actions/payment.ts index 9b0eef9..82f97df 100644 --- a/actions/payment.ts +++ b/actions/payment.ts @@ -179,24 +179,16 @@ export async function cancelTopup({ id }: { id: string }) { export async function cancelPayment({ id }: { id: string }) { const session = await getServerSession(authOptions); const response = await fetch( - `${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}/delete/`, + `${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}/cancel/`, { - method: "DELETE", + method: "PATCH", headers: { "Content-Type": "application/json", Authorization: `Token ${session?.apiToken}`, }, }, ); - if (!response.ok) { - const errorData = (await response.json()) as ApiError; - const errorMessage = - errorData.message || errorData.detail || "An error occurred."; - const error = new Error(errorMessage); - (error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object - throw error; - } - return { message: "Payment successfully canceled." }; + return handleApiResponse(response, "cancelPayment"); } type UpdatePayment = { diff --git a/app/(dashboard)/payments/[paymentId]/page.tsx b/app/(dashboard)/payments/[paymentId]/page.tsx index 9f53c3b..1e0b151 100644 --- a/app/(dashboard)/payments/[paymentId]/page.tsx +++ b/app/(dashboard)/payments/[paymentId]/page.tsx @@ -1,11 +1,12 @@ +import { redirect } from "next/navigation"; import { getPayment, getProfile } from "@/actions/payment"; import CancelPaymentButton from "@/components/billing/cancel-payment-button"; +import ExpiryCountDown from "@/components/billing/expiry-time-countdown"; import ClientErrorMessage from "@/components/client-error-message"; import DevicesToPay from "@/components/devices-to-pay"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { tryCatch } from "@/utils/tryCatch"; -import { redirect } from "next/navigation"; export default async function PaymentPage({ params, }: { @@ -40,13 +41,24 @@ export default async function PaymentPage({ {payment?.paid ? "Paid" : "Pending"} {!payment.paid && ( - + payment.is_expired ? ( + + ) : ( + + ) )} - + {!payment.paid && ( + + )}
{!topup.paid && ( - + )}
{ setLoading(true); - const [error, x] = await tryCatch(cancelPayment({ id: paymentId })); - console.log(x); + const [error, payment] = await tryCatch(cancelPayment({ id: paymentId })); + console.log(payment); if (error) { toast.error(error.message); setLoading(false); } else { + toast.success("Payment cancelled successfully!", { + description: `Your payment of ${payment?.amount} MVR has been cancelled.`, + closeButton: true, + }); router.replace("/devices"); } }} diff --git a/components/billing/expiry-time-countdown.tsx b/components/billing/expiry-time-countdown.tsx index a5a7188..59e0ab4 100644 --- a/components/billing/expiry-time-countdown.tsx +++ b/components/billing/expiry-time-countdown.tsx @@ -15,7 +15,7 @@ const HumanizeTimeLeft = (seconds: number) => { return `${minutes}m ${remainingSeconds}s` } -export default function ExpiryCountDown({ expiresAt }: { expiresAt: string }) { +export default function ExpiryCountDown({ expiresAt, expiryLabel }: { expiresAt: string, expiryLabel: string }) { const [timeLeft, setTimeLeft] = useState(calculateTimeLeft(expiresAt)) const [mounted, setMounted] = useState(false) const router = useRouter() @@ -47,7 +47,7 @@ export default function ExpiryCountDown({ expiresAt }: { expiresAt: string }) { {timeLeft ? ( Time left: {HumanizeTimeLeft(timeLeft)} ) : ( - Top up has expired. Please make another topup to add balance to your wallet. + {expiryLabel} has expired. )} {timeLeft > 0 && ( diff --git a/components/payments-table.tsx b/components/payments-table.tsx index 014593a..4b79ec5 100644 --- a/components/payments-table.tsx +++ b/components/payments-table.tsx @@ -59,7 +59,7 @@ export async function PaymentsTable({ Details Duration - + Status Amount @@ -84,6 +84,8 @@ export async function PaymentsTable({ month: "short", day: "2-digit", year: "numeric", + minute: "2-digit", + hour: "2-digit", }, )} @@ -98,16 +100,6 @@ export async function PaymentsTable({ View Details - - {payment.paid ? "Paid" : "Unpaid"} -

Devices

@@ -124,9 +116,30 @@ export async function PaymentsTable({
+ {payment.number_of_months} Months + + + {payment.paid ? ( + + {payment.status} + + ) : payment.is_expired ? ( + Expired + ) : ( + {payment.status} + )} + + {payment.amount.toFixed(2)} @@ -185,6 +198,8 @@ function MobilePaymentDetails({ payment }: { payment: Payment }) { month: "short", day: "2-digit", year: "numeric", + minute: "2-digit", + hour: "2-digit", })} @@ -198,16 +213,6 @@ function MobilePaymentDetails({ payment }: { payment: Payment }) { View Details - - {payment.paid ? "Paid" : "Unpaid"} -

Devices

@@ -226,9 +231,29 @@ function MobilePaymentDetails({ payment }: { payment: Payment }) {

Amount

- - {payment.amount.toFixed(2)} MVR - +
+ + {payment.amount.toFixed(2)} MVR + + + {payment.paid ? ( + + {payment.status} + + ) : payment.is_expired ? ( + Expired + ) : ( + {payment.status} + )} + +
diff --git a/components/topups-table.tsx b/components/topups-table.tsx index f7fb0ab..f85e3eb 100644 --- a/components/topups-table.tsx +++ b/components/topups-table.tsx @@ -103,19 +103,6 @@ export async function TopupsTable({ View Details - {!topup.is_expired && - topup.status !== "CANCELLED" && ( - - {topup.paid ? "Paid" : "Unpaid"} - - )}
@@ -203,18 +190,6 @@ function MobileTopupDetails({ topup }: { topup: Topup }) { View Details - {!topup.is_expired && topup.status !== "CANCELLED" && ( - - {topup.paid ? "Paid" : "Unpaid"} - - )}
diff --git a/lib/backend-types.ts b/lib/backend-types.ts index a42e8f8..1cdfcd5 100644 --- a/lib/backend-types.ts +++ b/lib/backend-types.ts @@ -81,9 +81,12 @@ export interface Payment { paid: boolean; paid_at: string | null; method: string; - expires_at: string | null; + expires_at: string; + is_expired: boolean; created_at: string; updated_at: string; + status: "CANCELLED" | "PENDING" | "PAID"; + mib_reference: string | null; user: number; }