mika-agent-assist/supabase/functions/get-paddle-price/index.ts
gpt-engineer-app[bot] 5ef5bab121 Changes
Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
2026-04-17 17:47:29 +00:00

42 lines
1.3 KiB
TypeScript

import { gatewayFetch, type PaddleEnv } from '../_shared/paddle.ts';
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
'Content-Type': 'application/json',
};
Deno.serve(async (req) => {
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
try {
const { priceId, environment } = await req.json();
if (!priceId) {
return new Response(JSON.stringify({ error: 'priceId required' }), {
status: 400,
headers: corsHeaders,
});
}
const env = (environment || 'sandbox') as PaddleEnv;
const response = await gatewayFetch(env, `/prices?external_id=${encodeURIComponent(priceId)}`);
const data = await response.json();
if (!data.data?.length) {
return new Response(JSON.stringify({ error: 'Price not found' }), {
status: 404,
headers: corsHeaders,
});
}
return new Response(JSON.stringify({ paddleId: data.data[0].id }), { headers: corsHeaders });
} catch (e) {
console.error('get-paddle-price error:', e);
return new Response(JSON.stringify({ error: (e as Error).message }), {
status: 500,
headers: corsHeaders,
});
}
});