integratedhub/routes/api/retrieve/payment_methods.ts
2026-01-20 09:02:26 -06:00

44 lines
1.1 KiB
TypeScript

// routes/api/retrieve/payment_methods.ts
import { define } from "../../../utils.ts";
import {
getDefaultPaymentMethod,
getPaymentMethodsByUser,
} from "../../../services/paymentService.ts";
import { SessionCookie } from "../../auth/callback.ts";
export const handler = define.handlers({
async GET(ctx) {
try {
const stateData: SessionCookie = ctx.state.data;
const { user_id } = stateData;
const url = new URL(ctx.req.url);
const default_only = url.searchParams.get("default_only") === "true";
let result;
if (default_only) {
result = await getDefaultPaymentMethod(user_id);
result = result ? [result] : [];
} else {
result = await getPaymentMethodsByUser(user_id);
}
return new Response(
JSON.stringify({
success: true,
payment_methods: result,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} catch (e) {
return new Response(JSON.stringify({ error: e }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
},
});