d3ro-voice/server/supabase/functions/billing-catalog/index.ts

43 lines
1.4 KiB
TypeScript

import { handleCorsPreflightRequest } from '../_shared/cors.ts'
import { requireUser, authErrorResponse, type AuthError } from '../_shared/auth.ts'
import { buildJsonHeaders, createJsonResponder } from '../_shared/json-response.ts'
import {
createBillingCatalog,
type BillingCatalogTier,
} from '../_shared/billing-catalog.ts'
import {
getPaypleConfig,
PaypleConfigurationError,
TIER_PRICE,
} from '../_shared/payple.ts'
const NO_STORE_HEADERS = buildJsonHeaders({ noStore: true, noSniff: true })
const jsonResponse = createJsonResponder({ noStore: true, noSniff: true })
Deno.serve(async (req: Request) => {
const preflight = handleCorsPreflightRequest(req)
if (preflight) return preflight
if (req.method !== 'POST') return jsonResponse({ error: 'method_not_allowed' }, 405)
try {
await requireUser(req)
let payple: Record<BillingCatalogTier, number> | null = null
try {
getPaypleConfig()
payple = {
pro: TIER_PRICE.pro,
pro_plus: TIER_PRICE.pro_plus,
}
} catch (error) {
if (!(error instanceof PaypleConfigurationError)) throw error
}
return jsonResponse(createBillingCatalog({ payple }))
} catch (error) {
if (error && typeof error === 'object' && 'status' in error && 'message' in error) {
return authErrorResponse(error as AuthError, NO_STORE_HEADERS)
}
return jsonResponse({ error: 'billing_catalog_unavailable' }, 503)
}
})