101 lines
3.2 KiB
TypeScript
101 lines
3.2 KiB
TypeScript
// routes/api/create/payment_transaction.ts
|
|
import { define } from "../../../utils.ts";
|
|
import { createPaymentTransaction } from "../../../services/paymentService.ts";
|
|
//import { SessionCookie } from "../../auth/callback.ts";
|
|
|
|
export const handler = define.handlers({
|
|
async POST(ctx) {
|
|
try {
|
|
//const stateData: SessionCookie = ctx.state.data;
|
|
|
|
const contentType = ctx.req.headers.get("content-type") || "";
|
|
let data: Record<string, string | number> = {};
|
|
let sender: string | null = null;
|
|
let isJsonRequest = false;
|
|
|
|
// Handle both JSON and form data
|
|
if (contentType.includes("application/json")) {
|
|
isJsonRequest = true;
|
|
const jsonData = await ctx.req.json();
|
|
data = jsonData;
|
|
} else {
|
|
const formData = await ctx.req.formData();
|
|
sender = formData.get("sender")?.toString() || null;
|
|
|
|
data = {
|
|
lease_id: formData.get("lease_id")?.toString() || "",
|
|
amount: parseFloat(formData.get("amount")?.toString() || "0"),
|
|
payment_date: parseInt(
|
|
formData.get("payment_date")?.toString() || Date.now().toString(),
|
|
),
|
|
payment_method: formData.get("payment_method")?.toString() || "",
|
|
transaction_status: formData.get("transaction_status")?.toString() ||
|
|
"pending",
|
|
stripe_payment_id: formData.get("stripe_payment_id")?.toString() ||
|
|
"",
|
|
};
|
|
}
|
|
|
|
const lease_id = data.lease_id as string;
|
|
const amount = data.amount as number;
|
|
const payment_date = data.payment_date as number;
|
|
const payment_method = data.payment_method as string;
|
|
const transaction_status = data.transaction_status as string || "pending";
|
|
const stripe_payment_id = data.stripe_payment_id as string;
|
|
|
|
if (!lease_id || !amount || !payment_method) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: "Lease ID, amount, and payment method are required",
|
|
}),
|
|
{
|
|
status: 400,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
}
|
|
|
|
const result = await createPaymentTransaction(
|
|
lease_id,
|
|
amount,
|
|
payment_date,
|
|
payment_method,
|
|
transaction_status,
|
|
stripe_payment_id || undefined,
|
|
);
|
|
|
|
// Return appropriate response based on request type
|
|
if (isJsonRequest) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
transaction: result[0],
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
} else {
|
|
// Redirect back to the sender URL
|
|
const senderPath = sender?.toString() || "/";
|
|
let redirectUrl = senderPath;
|
|
try {
|
|
if (senderPath.startsWith("/")) {
|
|
const url = new URL(ctx.req.url);
|
|
redirectUrl = url.origin + senderPath;
|
|
}
|
|
} catch (_) {
|
|
// fallback: just use senderPath
|
|
}
|
|
return Response.redirect(redirectUrl, 303);
|
|
}
|
|
} catch (e) {
|
|
return new Response(JSON.stringify({ error: e }), {
|
|
status: 500,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
},
|
|
});
|