mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-07 06:36:30 +00:00
refactor: enhance topup payment verification with improved state management and error handling 🔨
This commit is contained in:
@ -225,14 +225,37 @@ export async function verifyPayment({ id, method }: UpdatePayment) {
|
|||||||
return handleApiResponse<Payment>(response, "updatePayment");
|
return handleApiResponse<Payment>(response, "updatePayment");
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateTopupPayment = {
|
|
||||||
id: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function verifyTopupPayment({ id }: UpdateTopupPayment) {
|
export type VerifyTopupPaymentState = {
|
||||||
|
transaction?: {
|
||||||
|
sourceBank: string;
|
||||||
|
trxDate: string;
|
||||||
|
};
|
||||||
|
message: string;
|
||||||
|
success: boolean;
|
||||||
|
fieldErrors: Record<string, string>;
|
||||||
|
payload?: FormData;
|
||||||
|
}
|
||||||
|
export async function verifyTopupPayment(
|
||||||
|
_prevState: VerifyTopupPaymentState,
|
||||||
|
formData: FormData
|
||||||
|
): Promise<VerifyTopupPaymentState> {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
|
|
||||||
|
// Get the topup ID from the form data or use a hidden input
|
||||||
|
const topupId = formData.get('topupId') as string;
|
||||||
|
|
||||||
|
if (!topupId) {
|
||||||
|
return {
|
||||||
|
message: "Topup ID is required",
|
||||||
|
success: false,
|
||||||
|
fieldErrors: { topupId: "Topup ID is required" },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/topup/${id}/verify/`,
|
`${process.env.SARLINK_API_BASE_URL}/api/billing/topup/${topupId}/verify/`,
|
||||||
{
|
{
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: {
|
headers: {
|
||||||
@ -241,8 +264,32 @@ export async function verifyTopupPayment({ id }: UpdateTopupPayment) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const result = await handleApiResponse<TopupResponse>(response, "verifyTopupPayment");
|
||||||
|
|
||||||
revalidatePath("/top-ups/[topupId]", "page");
|
revalidatePath("/top-ups/[topupId]", "page");
|
||||||
return handleApiResponse<TopupResponse>(response, "verifyTopupPayment");
|
|
||||||
|
return {
|
||||||
|
message: result.message || "Topup payment verified successfully",
|
||||||
|
success: true,
|
||||||
|
fieldErrors: {},
|
||||||
|
transaction: result.transaction,
|
||||||
|
};
|
||||||
|
} catch (error: unknown) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
return {
|
||||||
|
message: error.message || "Please check your payment details and try again.",
|
||||||
|
success: false,
|
||||||
|
fieldErrors: {},
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
message: "Topup verification failed.",
|
||||||
|
success: false,
|
||||||
|
fieldErrors: {},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getProfile() {
|
export async function getProfile() {
|
||||||
|
@ -5,9 +5,9 @@ import {
|
|||||||
ClipboardCheck,
|
ClipboardCheck,
|
||||||
Loader2,
|
Loader2,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useState } from "react";
|
import { useActionState, useEffect, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { verifyTopupPayment } from "@/actions/payment";
|
import { type VerifyTopupPaymentState, verifyTopupPayment } from "@/actions/payment";
|
||||||
import {
|
import {
|
||||||
Table,
|
Table,
|
||||||
TableBody,
|
TableBody,
|
||||||
@ -19,9 +19,32 @@ import {
|
|||||||
import type { Topup } from "@/lib/backend-types";
|
import type { Topup } from "@/lib/backend-types";
|
||||||
import { Button } from "./ui/button";
|
import { Button } from "./ui/button";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const initialState: VerifyTopupPaymentState = {
|
||||||
|
message: "",
|
||||||
|
success: false,
|
||||||
|
fieldErrors: {},
|
||||||
|
};
|
||||||
export default function TopupToPay({ topup, disabled }: { topup?: Topup, disabled?: boolean }) {
|
export default function TopupToPay({ topup, disabled }: { topup?: Topup, disabled?: boolean }) {
|
||||||
const [verifyingTransferPayment, setVerifyingTransferPayment] =
|
const [state, formAction, isPending] = useActionState(verifyTopupPayment, initialState);
|
||||||
useState(false);
|
|
||||||
|
// Handle toast notifications based on state changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (state.success && state.message) {
|
||||||
|
toast.success("Topup successful!", {
|
||||||
|
closeButton: true,
|
||||||
|
description: state.transaction
|
||||||
|
? `Your topup payment has been verified successfully using ${state.transaction.sourceBank} bank transfer on ${state.transaction.trxDate}.`
|
||||||
|
: state.message,
|
||||||
|
});
|
||||||
|
} else if (!state.success && state.message && state.message !== initialState.message) {
|
||||||
|
toast.error("Topup Payment Verification Failed", {
|
||||||
|
closeButton: true,
|
||||||
|
description: state.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [state]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
@ -45,45 +68,25 @@ export default function TopupToPay({ topup, disabled }: { topup?: Topup, disable
|
|||||||
</Button>
|
</Button>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
|
<form action={formAction}>
|
||||||
|
<input type="hidden" name="topupId" value={topup?.id ?? ""} />
|
||||||
<Button
|
<Button
|
||||||
disabled={disabled || verifyingTransferPayment}
|
disabled={disabled || isPending}
|
||||||
onClick={async () => {
|
type="submit"
|
||||||
setVerifyingTransferPayment(true);
|
|
||||||
try {
|
|
||||||
const response = await verifyTopupPayment({
|
|
||||||
id: topup?.id ?? "",
|
|
||||||
});
|
|
||||||
toast.success("Topup successful!", {
|
|
||||||
closeButton: true,
|
|
||||||
description:
|
|
||||||
`Your topup payment has been verified successfully using ${response.transaction?.sourceBank} bank transfer on ${response.transaction?.trxDate}.` || response.message,
|
|
||||||
});
|
|
||||||
} catch (error: unknown) {
|
|
||||||
if (error instanceof Error) {
|
|
||||||
toast.error("Topup Payment Verification Failed", {
|
|
||||||
closeButton: true,
|
|
||||||
description:
|
|
||||||
error.message || "Please check your payment details and try again.",
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
toast.error("Topup verification failed.");
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
setVerifyingTransferPayment(false);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
size={"lg"}
|
size={"lg"}
|
||||||
className="mb-4"
|
className="mb-4"
|
||||||
>
|
>
|
||||||
{verifyingTransferPayment
|
{isPending
|
||||||
? "Processing payment..."
|
? "Processing payment..."
|
||||||
: "I have paid"}
|
: "I have paid"}
|
||||||
{verifyingTransferPayment ? (
|
{isPending ? (
|
||||||
<Loader2 className="animate-spin" />
|
<Loader2 className="animate-spin" />
|
||||||
) : (
|
) : (
|
||||||
<BadgeDollarSign />
|
<BadgeDollarSign />
|
||||||
)}
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
|
</form>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user