mirror of
https://github.com/pezkuwichain/pezkuwi-telegram-miniapp.git
synced 2026-04-22 00:47:55 +00:00
910610491f
- All 17 edge functions now check both TELEGRAM_BOT_TOKEN and TELEGRAM_BOT_TOKEN_KRD for session verification - Add perPage:1000 to listUsers calls to prevent pagination issues - Fix offer button label: Buy tab shows "Al" (green), Sell tab shows "Sat" (red) - Fix active tab highlight with cyan color for visibility - Fix modal transparency (add --card CSS variable) - Fix withdraw tab sync (useEffect on modal open)
194 lines
5.9 KiB
TypeScript
194 lines
5.9 KiB
TypeScript
import { serve } from 'https://deno.land/std@0.177.0/http/server.ts';
|
|
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2';
|
|
import { createHmac } from 'https://deno.land/std@0.177.0/node/crypto.ts';
|
|
|
|
// CORS - Production domain only
|
|
const ALLOWED_ORIGINS = [
|
|
'https://telegram.pezkuwichain.io',
|
|
'https://telegram.pezkiwi.app',
|
|
'https://t.me',
|
|
];
|
|
|
|
function getCorsHeaders(origin: string | null): Record<string, string> {
|
|
const allowedOrigin =
|
|
origin && ALLOWED_ORIGINS.some((o) => origin.startsWith(o)) ? origin : ALLOWED_ORIGINS[0];
|
|
|
|
return {
|
|
'Access-Control-Allow-Origin': allowedOrigin,
|
|
'Access-Control-Allow-Headers':
|
|
'authorization, x-client-info, apikey, content-type, x-supabase-client-platform',
|
|
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
|
};
|
|
}
|
|
|
|
interface GetPaymentMethodsRequest {
|
|
sessionToken: string;
|
|
currency?: string;
|
|
}
|
|
|
|
// Session token secret (derived from bot token)
|
|
function getSessionSecret(botToken: string): Uint8Array {
|
|
return createHmac('sha256', 'SessionTokenSecret').update(botToken).digest();
|
|
}
|
|
|
|
// Verify HMAC-signed session token
|
|
function verifySessionToken(token: string, botToken: string): number | null {
|
|
try {
|
|
const parts = token.split('.');
|
|
if (parts.length !== 2) {
|
|
// Try legacy format for backwards compatibility
|
|
return verifyLegacyToken(token);
|
|
}
|
|
|
|
const [payloadB64, signature] = parts;
|
|
|
|
// Verify signature
|
|
const secret = getSessionSecret(botToken);
|
|
const expectedSig = createHmac('sha256', secret).update(payloadB64).digest('hex');
|
|
|
|
if (signature !== expectedSig) {
|
|
return null;
|
|
}
|
|
|
|
// Parse payload
|
|
const payload = JSON.parse(atob(payloadB64));
|
|
|
|
// Check expiration
|
|
if (Date.now() > payload.exp) {
|
|
return null;
|
|
}
|
|
|
|
return payload.tgId;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Legacy token format (Base64 only) - for backwards compatibility
|
|
function verifyLegacyToken(token: string): number | null {
|
|
try {
|
|
const decoded = atob(token);
|
|
const [telegramId, timestamp] = decoded.split(':');
|
|
const ts = parseInt(timestamp);
|
|
// Token valid for 7 days
|
|
if (Date.now() - ts > 7 * 24 * 60 * 60 * 1000) {
|
|
return null;
|
|
}
|
|
return parseInt(telegramId);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
serve(async (req) => {
|
|
const origin = req.headers.get('origin');
|
|
const corsHeaders = getCorsHeaders(origin);
|
|
|
|
// Handle CORS
|
|
if (req.method === 'OPTIONS') {
|
|
return new Response('ok', { headers: corsHeaders });
|
|
}
|
|
|
|
try {
|
|
const body: GetPaymentMethodsRequest = await req.json();
|
|
const { sessionToken, currency } = body;
|
|
|
|
// Get bot tokens for session verification (dual bot support)
|
|
const botTokens: string[] = [];
|
|
const _mainToken = Deno.env.get('TELEGRAM_BOT_TOKEN');
|
|
const _krdToken = Deno.env.get('TELEGRAM_BOT_TOKEN_KRD');
|
|
if (_mainToken) botTokens.push(_mainToken);
|
|
if (_krdToken) botTokens.push(_krdToken);
|
|
if (botTokens.length === 0) {
|
|
return new Response(JSON.stringify({ error: 'Server configuration error' }), {
|
|
status: 500,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
// Validate session token
|
|
if (!sessionToken) {
|
|
return new Response(JSON.stringify({ error: 'Missing session token' }), {
|
|
status: 401,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
let telegramId: number | null = null;
|
|
for (const bt of botTokens) {
|
|
telegramId = verifySessionToken(sessionToken, bt);
|
|
if (telegramId) break;
|
|
}
|
|
if (!telegramId) {
|
|
return new Response(JSON.stringify({ error: 'Invalid or expired session' }), {
|
|
status: 401,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
// Create Supabase admin client (bypasses RLS)
|
|
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
|
|
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
|
|
const supabase = createClient(supabaseUrl, supabaseServiceKey);
|
|
|
|
// Get auth user ID for this telegram user
|
|
const telegramEmail = `telegram_${telegramId}@pezkuwichain.io`;
|
|
const {
|
|
data: { users: authUsers },
|
|
} = await supabase.auth.admin.listUsers({ perPage: 1000 });
|
|
const authUser = authUsers?.find((u: { email?: string }) => u.email === telegramEmail);
|
|
|
|
if (!authUser) {
|
|
return new Response(JSON.stringify({ error: 'User not found. Please authenticate first.' }), {
|
|
status: 404,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
// Build query for payment_methods
|
|
let query = supabase
|
|
.from('payment_methods')
|
|
.select(
|
|
'id, currency, country, method_name, method_type, logo_url, fields, min_trade_amount, max_trade_amount, processing_time_minutes'
|
|
)
|
|
.eq('is_active', true)
|
|
.order('display_order', { ascending: true });
|
|
|
|
// Apply optional currency filter
|
|
if (currency) {
|
|
query = query.eq('currency', currency);
|
|
}
|
|
|
|
const { data: methods, error: methodsError } = await query;
|
|
|
|
if (methodsError) {
|
|
console.error('Payment methods query error:', methodsError);
|
|
return new Response(
|
|
JSON.stringify({ error: 'Failed to fetch payment methods: ' + methodsError.message }),
|
|
{
|
|
status: 500,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
|
}
|
|
);
|
|
}
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
methods: methods || [],
|
|
}),
|
|
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
);
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
const origin = req.headers.get('origin');
|
|
return new Response(
|
|
JSON.stringify({ error: error instanceof Error ? error.message : 'Internal server error' }),
|
|
{
|
|
status: 500,
|
|
headers: { ...getCorsHeaders(origin), 'Content-Type': 'application/json' },
|
|
}
|
|
);
|
|
}
|
|
});
|