From 130e5bfa9f8e89b31987596aa9381efcb9bb5e63 Mon Sep 17 00:00:00 2001 From: Jennie Robinson Faber Date: Wed, 8 Apr 2026 22:11:25 +0100 Subject: [PATCH] refactor(helcim): use helper in unused admin endpoints --- server/api/helcim/create-plan.post.js | 36 ++++--------------- server/api/helcim/plans.get.js | 30 ++++------------ server/api/helcim/subscriptions.get.js | 30 ++++------------ server/api/helcim/update-billing.post.js | 46 ++++++------------------ 4 files changed, 29 insertions(+), 113 deletions(-) diff --git a/server/api/helcim/create-plan.post.js b/server/api/helcim/create-plan.post.js index c1565e6..346d9aa 100644 --- a/server/api/helcim/create-plan.post.js +++ b/server/api/helcim/create-plan.post.js @@ -1,42 +1,18 @@ // Create a new Helcim payment plan -const HELCIM_API_BASE = 'https://api.helcim.com/v2' +import { createHelcimPlan } from '../../utils/helcim.js' export default defineEventHandler(async (event) => { try { await requireAdmin(event) - const config = useRuntimeConfig(event) const body = await validateBody(event, helcimCreatePlanSchema) - const helcimToken = config.helcimApiToken - - - 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' - }) + const planData = await createHelcimPlan({ + planName: body.name, + planAmount: parseFloat(body.amount), + planFrequency: body.frequency, + 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 diff --git a/server/api/helcim/plans.get.js b/server/api/helcim/plans.get.js index 6621987..b2be82e 100644 --- a/server/api/helcim/plans.get.js +++ b/server/api/helcim/plans.get.js @@ -1,35 +1,17 @@ // Get Helcim payment plans -const HELCIM_API_BASE = 'https://api.helcim.com/v2' +import { listHelcimPlans } from '../../utils/helcim.js' export default defineEventHandler(async (event) => { try { await requireAdmin(event) - const config = useRuntimeConfig(event) - const helcimToken = config.helcimApiToken - - const response = await fetch(`${HELCIM_API_BASE}/payment-plans`, { - method: 'GET', - headers: { - 'accept': 'application/json', - 'api-token': helcimToken - } - }) - - if (!response.ok) { - console.error('Failed to fetch payment plans:', response.status, response.statusText) - throw createError({ - statusCode: response.status, - statusMessage: 'Failed to fetch payment plans' - }) - } - - const plansData = await response.json() - + + const plansData = await listHelcimPlans() + return { success: true, plans: plansData } - + } catch (error) { if (error.statusCode) throw error console.error('Error fetching Helcim payment plans:', error) @@ -38,4 +20,4 @@ export default defineEventHandler(async (event) => { statusMessage: 'An unexpected error occurred' }) } -}) \ No newline at end of file +}) diff --git a/server/api/helcim/subscriptions.get.js b/server/api/helcim/subscriptions.get.js index 6636503..5105979 100644 --- a/server/api/helcim/subscriptions.get.js +++ b/server/api/helcim/subscriptions.get.js @@ -1,35 +1,17 @@ // Get existing Helcim subscriptions to understand the format -const HELCIM_API_BASE = 'https://api.helcim.com/v2' +import { listHelcimSubscriptions } from '../../utils/helcim.js' export default defineEventHandler(async (event) => { try { await requireAdmin(event) - const config = useRuntimeConfig(event) - const helcimToken = config.helcimApiToken - - const response = await fetch(`${HELCIM_API_BASE}/subscriptions`, { - method: 'GET', - headers: { - 'accept': 'application/json', - 'api-token': helcimToken - } - }) - - if (!response.ok) { - console.error('Failed to fetch subscriptions:', response.status, response.statusText) - throw createError({ - statusCode: response.status, - statusMessage: 'Failed to fetch subscriptions' - }) - } - - const subscriptionsData = await response.json() - + + const subscriptionsData = await listHelcimSubscriptions() + return { success: true, subscriptions: subscriptionsData } - + } catch (error) { if (error.statusCode) throw error console.error('Error fetching Helcim subscriptions:', error) @@ -38,4 +20,4 @@ export default defineEventHandler(async (event) => { statusMessage: 'An unexpected error occurred' }) } -}) \ No newline at end of file +}) diff --git a/server/api/helcim/update-billing.post.js b/server/api/helcim/update-billing.post.js index 5c4fdac..cffa9f1 100644 --- a/server/api/helcim/update-billing.post.js +++ b/server/api/helcim/update-billing.post.js @@ -1,48 +1,24 @@ // Update customer billing address import { requireAuth } from '../../utils/auth.js' - -const HELCIM_API_BASE = 'https://api.helcim.com/v2' +import { updateHelcimCustomer } from '../../utils/helcim.js' 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.helcimApiToken - - // 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 - } - }) + const customerData = await updateHelcimCustomer(body.customerId, { + 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, @@ -56,4 +32,4 @@ export default defineEventHandler(async (event) => { statusMessage: 'An unexpected error occurred' }) } -}) \ No newline at end of file +})