"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/[paymentId]", "page"); return handleApiResponse(response, "updatePayment"); } 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"); }