ghostguild-org/server/api/helcim/create-plan.post.js
Jennie Robinson Faber 025c1a180f Add Zod validation to all API endpoints and remove debug test route
Adds schema-based input validation across helcim, events, members,
series, admin, and updates API endpoints. Removes the peer-support
debug test endpoint. Adds validation test coverage.
2026-03-01 17:04:26 +00:00

53 lines
1.5 KiB
JavaScript

// Create a new Helcim payment plan
const HELCIM_API_BASE = 'https://api.helcim.com/v2'
export default defineEventHandler(async (event) => {
try {
await requireAdmin(event)
const config = useRuntimeConfig(event)
const body = await validateBody(event, helcimCreatePlanSchema)
const helcimToken = config.public.helcimToken || process.env.NUXT_PUBLIC_HELCIM_TOKEN
const response = await fetch(`${HELCIM_API_BASE}/payment-plans`, {
method: 'POST',
headers: {
'accept': 'application/json',
'content-type': 'application/json',
'api-token': helcimToken
},
body: JSON.stringify({
planName: body.name,
planAmount: parseFloat(body.amount),
planFrequency: body.frequency, // 'monthly', 'weekly', 'biweekly', etc.
planCurrency: body.currency || 'CAD'
})
})
if (!response.ok) {
const errorText = await response.text()
console.error('Failed to create payment plan:', response.status, errorText)
throw createError({
statusCode: response.status,
statusMessage: 'Payment plan creation failed'
})
}
const planData = await response.json()
return {
success: true,
plan: planData
}
} catch (error) {
if (error.statusCode) throw error
console.error('Error creating Helcim payment plan:', error)
throw createError({
statusCode: 500,
statusMessage: 'An unexpected error occurred'
})
}
})