refactor(helcim): use helper in unused admin endpoints
This commit is contained in:
parent
0d792c7c70
commit
130e5bfa9f
4 changed files with 29 additions and 113 deletions
|
|
@ -1,42 +1,18 @@
|
||||||
// Create a new Helcim payment plan
|
// 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) => {
|
export default defineEventHandler(async (event) => {
|
||||||
try {
|
try {
|
||||||
await requireAdmin(event)
|
await requireAdmin(event)
|
||||||
const config = useRuntimeConfig(event)
|
|
||||||
const body = await validateBody(event, helcimCreatePlanSchema)
|
const body = await validateBody(event, helcimCreatePlanSchema)
|
||||||
|
|
||||||
const helcimToken = config.helcimApiToken
|
const planData = await createHelcimPlan({
|
||||||
|
planName: body.name,
|
||||||
|
planAmount: parseFloat(body.amount),
|
||||||
const response = await fetch(`${HELCIM_API_BASE}/payment-plans`, {
|
planFrequency: body.frequency,
|
||||||
method: 'POST',
|
planCurrency: body.currency || 'CAD'
|
||||||
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 {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
plan: planData
|
plan: planData
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,11 @@
|
||||||
// Get Helcim payment plans
|
// Get Helcim payment plans
|
||||||
const HELCIM_API_BASE = 'https://api.helcim.com/v2'
|
import { listHelcimPlans } from '../../utils/helcim.js'
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
try {
|
try {
|
||||||
await requireAdmin(event)
|
await requireAdmin(event)
|
||||||
const config = useRuntimeConfig(event)
|
|
||||||
const helcimToken = config.helcimApiToken
|
|
||||||
|
|
||||||
const response = await fetch(`${HELCIM_API_BASE}/payment-plans`, {
|
const plansData = await listHelcimPlans()
|
||||||
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()
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,11 @@
|
||||||
// Get existing Helcim subscriptions to understand the format
|
// 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) => {
|
export default defineEventHandler(async (event) => {
|
||||||
try {
|
try {
|
||||||
await requireAdmin(event)
|
await requireAdmin(event)
|
||||||
const config = useRuntimeConfig(event)
|
|
||||||
const helcimToken = config.helcimApiToken
|
|
||||||
|
|
||||||
const response = await fetch(`${HELCIM_API_BASE}/subscriptions`, {
|
const subscriptionsData = await listHelcimSubscriptions()
|
||||||
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()
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
|
|
|
||||||
|
|
@ -1,49 +1,25 @@
|
||||||
// Update customer billing address
|
// Update customer billing address
|
||||||
import { requireAuth } from '../../utils/auth.js'
|
import { requireAuth } from '../../utils/auth.js'
|
||||||
|
import { updateHelcimCustomer } from '../../utils/helcim.js'
|
||||||
const HELCIM_API_BASE = 'https://api.helcim.com/v2'
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
try {
|
try {
|
||||||
await requireAuth(event)
|
await requireAuth(event)
|
||||||
const config = useRuntimeConfig(event)
|
|
||||||
const body = await validateBody(event, helcimUpdateBillingSchema)
|
const body = await validateBody(event, helcimUpdateBillingSchema)
|
||||||
|
|
||||||
const { billingAddress } = body
|
const { billingAddress } = body
|
||||||
|
|
||||||
const helcimToken = config.helcimApiToken
|
const customerData = await updateHelcimCustomer(body.customerId, {
|
||||||
|
billingAddress: {
|
||||||
// Update customer billing address in Helcim
|
name: billingAddress.name,
|
||||||
const response = await fetch(`${HELCIM_API_BASE}/customers/${body.customerId}`, {
|
street1: billingAddress.street,
|
||||||
method: 'PATCH',
|
city: billingAddress.city,
|
||||||
headers: {
|
province: billingAddress.province || billingAddress.state,
|
||||||
'accept': 'application/json',
|
country: billingAddress.country,
|
||||||
'content-type': 'application/json',
|
postalCode: billingAddress.postalCode
|
||||||
'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 {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
customer: customerData
|
customer: customerData
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue