61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
// Create a new Helcim payment plan
|
|
const HELCIM_API_BASE = 'https://api.helcim.com/v2'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
const config = useRuntimeConfig(event)
|
|
const body = await readBody(event)
|
|
|
|
// Validate required fields
|
|
if (!body.name || !body.amount || !body.frequency) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Name, amount, and frequency are required'
|
|
})
|
|
}
|
|
|
|
const helcimToken = config.public.helcimToken || process.env.NUXT_PUBLIC_HELCIM_TOKEN
|
|
|
|
console.log('Creating payment plan:', body.name)
|
|
|
|
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: `Failed to create payment plan: ${errorText}`
|
|
})
|
|
}
|
|
|
|
const planData = await response.json()
|
|
console.log('Payment plan created:', planData)
|
|
|
|
return {
|
|
success: true,
|
|
plan: planData
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Error creating Helcim payment plan:', error)
|
|
throw createError({
|
|
statusCode: error.statusCode || 500,
|
|
statusMessage: error.message || 'Failed to create payment plan'
|
|
})
|
|
}
|
|
})
|