"use server"; import { authOptions } from "@/app/auth"; import type { ApiError, ApiResponse, NewPayment, Payment, } from "@/lib/backend-types"; import type { User } from "@/lib/types/user"; import { checkSession } from "@/utils/session"; import { handleApiResponse, tryCatch } from "@/utils/tryCatch"; import { getServerSession } from "next-auth"; import { revalidatePath } from "next/cache"; import { redirect } from "next/navigation"; export async function createPayment(data: NewPayment) { const session = await getServerSession(authOptions); console.log("data", data); const response = await fetch( `${ process.env.SARLINK_API_BASE_URL // }); }/api/billing/payment/`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Token ${session?.apiToken}`, }, body: JSON.stringify(data), }, ); 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; } const payment = (await response.json()) as Payment; revalidatePath("/devices"); return payment; } export async function getPayment({ id }: { id: string }) { const session = await getServerSession(authOptions); const response = await fetch( `${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}`, { method: "GET", 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; } const data = (await response.json()) as Payment; return data; } export async function getPayments() { const session = await getServerSession(authOptions); const response = await fetch( `${process.env.SARLINK_API_BASE_URL}/api/billing/payment/`, { method: "GET", 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; } const data = (await response.json()) as ApiResponse; return data; } 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/`, { method: "DELETE", 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." }; } type UpdatePayment = { id: string; method: "TRANSFER" | "WALLET"; benefName?: string; accountNo?: string; absAmount?: string; time?: string; }; export async function verifyPayment({ id, method }: UpdatePayment) { const session = await getServerSession(authOptions); const response = await fetch( `${process.env.SARLINK_API_BASE_URL}/api/billing/payment/${id}/verify/`, { method: "PUT", headers: { "Content-Type": "application/json", Authorization: `Token ${session?.apiToken}`, }, body: JSON.stringify({ method, }), }, ); revalidatePath("/payments/[paymentsId]"); 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); const response = await fetch( `${process.env.SARLINK_API_BASE_URL}/api/auth/profile/`, { method: "GET", headers: { "Content-Type": "application/json", Authorization: `Token ${session?.apiToken}`, }, }, ); 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"); // }