refactor(helcim): use centralized helper in 5 simple endpoints

This commit is contained in:
Jennie Robinson Faber 2026-04-08 21:44:18 +01:00
parent 07e005ebfc
commit 7b4b6feb51
5 changed files with 66 additions and 296 deletions

View file

@ -2,43 +2,15 @@
import { requireAuth } from '../../utils/auth.js'
import { validateBody } from '../../utils/validateBody.js'
import { paymentVerifySchema } from '../../utils/schemas.js'
const HELCIM_API_BASE = 'https://api.helcim.com/v2'
import { listHelcimCustomerCards } from '../../utils/helcim.js'
export default defineEventHandler(async (event) => {
try {
await requireAuth(event)
const config = useRuntimeConfig(event)
const body = await validateBody(event, paymentVerifySchema)
const helcimToken = config.helcimApiToken
if (!helcimToken) {
throw createError({
statusCode: 500,
statusMessage: 'Helcim API token not configured'
})
}
// Verify the card token by fetching the customer's cards from Helcim
const response = await fetch(`${HELCIM_API_BASE}/customers/${body.customerId}/cards`, {
method: 'GET',
headers: {
'accept': 'application/json',
'api-token': helcimToken
}
})
if (!response.ok) {
const errorText = await response.text()
console.error('Payment verification failed:', response.status, errorText)
throw createError({
statusCode: 502,
statusMessage: 'Payment verification failed with Helcim'
})
}
const cards = await response.json()
const cards = await listHelcimCustomerCards(body.customerId)
// Verify the card token exists for this customer
const cardExists = Array.isArray(cards) && cards.some(card =>