mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-07 18:56:29 +00:00
refactor: enhance topup payment verification with improved state management and error handling 🔨
This commit is contained in:
@ -225,24 +225,71 @@ export async function verifyPayment({ id, method }: 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 response = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/topup/${id}/verify/`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${session?.apiToken}`,
|
||||
|
||||
// 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(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/topup/${topupId}/verify/`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Token ${session?.apiToken}`,
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
revalidatePath("/top-ups/[topupId]", "page");
|
||||
return handleApiResponse<TopupResponse>(response, "verifyTopupPayment");
|
||||
);
|
||||
|
||||
const result = await handleApiResponse<TopupResponse>(response, "verifyTopupPayment");
|
||||
|
||||
revalidatePath("/top-ups/[topupId]", "page");
|
||||
|
||||
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() {
|
||||
|
Reference in New Issue
Block a user