ghostguild-org/server/api/helcim/update-billing.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

59 lines
No EOL
1.7 KiB
JavaScript

// Update customer billing address
import { requireAuth } from '../../utils/auth.js'
const HELCIM_API_BASE = 'https://api.helcim.com/v2'
export default defineEventHandler(async (event) => {
try {
await requireAuth(event)
const config = useRuntimeConfig(event)
const body = await validateBody(event, helcimUpdateBillingSchema)
const { billingAddress } = body
const helcimToken = config.public.helcimToken || process.env.NUXT_PUBLIC_HELCIM_TOKEN
// Update customer billing address in Helcim
const response = await fetch(`${HELCIM_API_BASE}/customers/${body.customerId}`, {
method: 'PATCH',
headers: {
'accept': 'application/json',
'content-type': 'application/json',
'api-token': helcimToken
},
body: JSON.stringify({
billingAddress: {
name: billingAddress.name,
street1: billingAddress.street,
city: billingAddress.city,
province: billingAddress.province || billingAddress.state,
country: billingAddress.country,
postalCode: billingAddress.postalCode
}
})
})
if (!response.ok) {
const errorText = await response.text()
console.error('Billing address update failed:', response.status, errorText)
throw createError({
statusCode: response.status,
statusMessage: 'Billing update failed'
})
}
const customerData = await response.json()
return {
success: true,
customer: customerData
}
} catch (error) {
if (error.statusCode) throw error
console.error('Error updating billing address:', error)
throw createError({
statusCode: 500,
statusMessage: 'An unexpected error occurred'
})
}
})