From 07349cda056c5ab63bcb5ceec2ba0cb873830327 Mon Sep 17 00:00:00 2001 From: i701 Date: Fri, 30 May 2025 23:53:08 +0500 Subject: [PATCH] Refactor payment verification and add MAC address guide - Updated payment verification logic in `actions/payment.ts` to remove unused code and improve clarity. - Enhanced `DevicesToPay` component to handle separate states for wallet and transfer payment verification. - Introduced a new `GetMacAccordion` component to guide users on finding their MAC addresses. - Created a reusable accordion component in `ui/accordion.tsx` for better UI consistency. - Integrated the MAC address guide into the device addition dialog. - Updated Tailwind CSS configuration to include animations for the accordion component. - Added Radix UI Accordion dependency to package.json and package-lock.json. - Improved error handling in API response utility to log unauthorized responses. --- actions/payment.ts | 122 +------- components/devices-to-pay.tsx | 41 ++- components/how-to-get-mac.tsx | 60 ++++ components/ui/accordion.tsx | 66 +++++ components/user/add-device-dialog.tsx | 2 + package-lock.json | 387 +++++++++++++++++++++++++- package.json | 1 + tailwind.config.ts | 156 ++++++----- utils/tryCatch.ts | 1 + 9 files changed, 625 insertions(+), 211 deletions(-) create mode 100644 components/how-to-get-mac.tsx create mode 100644 components/ui/accordion.tsx diff --git a/actions/payment.ts b/actions/payment.ts index 7bcbd89..a2a156b 100644 --- a/actions/payment.ts +++ b/actions/payment.ts @@ -138,19 +138,10 @@ export async function verifyPayment({ id, method }: UpdatePayment) { }), }, ); - revalidatePath("/payments/[paymentsId]"); + revalidatePath("/payments/[paymentId]", "page"); return handleApiResponse(response, "updatePayment"); } -type VerifyPaymentType = { - userId: string; - paymentId?: string; - benefName: string; - accountNo?: string; - absAmount: string; - time: string; - type?: "TRANSFER" | "WALLET"; -}; export async function getProfile() { const session = await getServerSession(authOptions); @@ -167,114 +158,3 @@ export async function getProfile() { return handleApiResponse(response, "getProfile"); } - -type VerifyPaymentResponse = - | { - success: boolean; - message: string; - } - | { - success: boolean; - message: string; - transaction: { - ref: string; - sourceBank: string; - trxDate: string; - }; - }; - -// async function verifyExternalPayment( -// data: VerifyPaymentType, -// payment: PaymentWithDevices | null, -// ): Promise { -// console.log("payment verify data ->", data); -// const response = await fetch( -// "https://verifypaymentsapi.baraveli.dev/verify-payment", -// { -// method: "POST", -// headers: { "Content-Type": "application/json" }, -// body: JSON.stringify(data), -// }, -// ); - -// const json = await response.json(); -// console.log(json); -// if (!payment) { -// throw new Error("Payment verification failed or payment not found"); -// } - -// if (json.success) { -// const expiryDate = new Date(); -// expiryDate.setMonth(expiryDate.getMonth() + payment.numberOfMonths); -// await prisma.payment.update({ -// where: { id: payment.id }, -// data: { -// paid: true, -// paidAt: new Date(), -// method: "TRANSFER", -// devices: { -// updateMany: payment.devices.map((device) => ({ -// where: { id: device.id }, -// data: { -// isActive: true, -// expiryDate: expiryDate, -// }, -// })), -// }, -// }, -// }); -// } - -// return json; -// } - -// async function updateDevices(payment: PaymentWithDevices | null) { -// if (!payment) return; - -// const newDevices = payment.devices.map((d) => ({ -// name: d.name, -// macAddress: formatMacAddress(d.mac), -// })); - -// return await addDevicesToGroup({ -// groupId: process.env.OMADA_GROUP_ID, -// siteId: process.env.OMADA_SITE_ID, -// newDevices, -// }); -// } - -// export async function verifyPayment(data: VerifyPaymentType) { -// try { -// const [payment, user] = await Promise.all([ -// prisma.payment.findUnique({ -// where: { id: data.paymentId }, -// include: { devices: true }, -// }), -// prisma.user.findUnique({ -// where: { id: data.userId }, -// }), -// ]); - -// if (data.type === "WALLET") { -// console.log("WALLET"); -// await processWalletPayment(user, payment, Number(data.absAmount)); -// redirect("/payments"); -// } -// if (data.type === "TRANSFER") { -// console.log({ data, payment }); -// const verificationResult = await verifyExternalPayment(data, payment); -// await updateDevices(payment); - -// revalidatePath("/payment[paymentId]"); - -// return verificationResult; -// } -// } catch (error) { -// console.error("Payment verification failed:", error); -// throw error; // Re-throw to handle at a higher level -// } -// } - -// export async function addDevicesToOmada() { -// console.log("hi"); -// } diff --git a/components/devices-to-pay.tsx b/components/devices-to-pay.tsx index d7eb4d0..80a9c19 100644 --- a/components/devices-to-pay.tsx +++ b/components/devices-to-pay.tsx @@ -25,8 +25,9 @@ export default function DevicesToPay({ payment, user, }: { payment?: Payment; user?: User }) { - console.log(user); - const [verifying, setVerifying] = useState(false); + const [verifyingWalletPayment, setVerifyingWalletPayment] = useState(false); + const [verifyingTransferPayment, setVerifyingTransferPayment] = useState(false); + const devices = payment?.devices; if (devices?.length === 0) { @@ -81,37 +82,47 @@ export default function DevicesToPay({
{isWalletPayVisible && ( )}