2024-12-07 14:09:53 +05:00
|
|
|
"use server";
|
|
|
|
|
|
|
|
import prisma from "@/lib/db";
|
|
|
|
import type { PaymentType } from "@/lib/types";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
|
|
|
|
|
|
export async function createPayment(data: PaymentType) {
|
2024-12-09 22:59:13 +05:00
|
|
|
console.log("data", data);
|
2024-12-07 14:09:53 +05:00
|
|
|
const payment = await prisma.payment.create({
|
|
|
|
data: {
|
|
|
|
amount: data.amount,
|
|
|
|
numberOfMonths: data.numberOfMonths,
|
|
|
|
paid: data.paid,
|
|
|
|
userId: data.userId,
|
|
|
|
devices: {
|
|
|
|
connect: data.deviceIds.map((id) => {
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
revalidatePath("/devices");
|
|
|
|
return payment;
|
|
|
|
}
|
2024-12-09 22:59:13 +05:00
|
|
|
|
|
|
|
type VerifyPaymentType = {
|
|
|
|
paymentId?: string;
|
|
|
|
benefName: string;
|
|
|
|
accountNo?: string;
|
|
|
|
absAmount: string;
|
|
|
|
time: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function verifyPayment(data: VerifyPaymentType) {
|
|
|
|
console.log({ data });
|
|
|
|
try {
|
|
|
|
const payment = await prisma.payment.findUnique({
|
|
|
|
where: {
|
|
|
|
id: data.paymentId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
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 (json.success === true) {
|
|
|
|
await prisma.payment.update({
|
|
|
|
where: {
|
|
|
|
id: payment?.id,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
paid: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
revalidatePath("/payment[paymentId]");
|
|
|
|
return json;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|