Files
Pay2Gateway/app/api/merchants/route.ts

60 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
export async function POST(req: NextRequest) {
try {
const { name, webhook_url, payment_provider, provider_config, fee_percent } = await req.json();
if (!name) {
return NextResponse.json(
{ error: 'Firma adı zorunludur.' },
{ status: 400 }
);
}
// Generate a 8-character short ID (e.g., P2C-A1B2C3)
const generateShortId = () => {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let result = '';
for (let i = 0; i < 8; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
};
const shortId = generateShortId();
const provider = payment_provider || 'stripe';
const configStr = provider_config ? JSON.stringify(provider_config) : '{}';
const result = await db.query(
`INSERT INTO merchants (name, webhook_url, short_id, payment_provider, provider_config, fee_percent)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`,
[name, webhook_url, shortId, provider, configStr, fee_percent || 1.0]
);
if (result.rows.length === 0) {
throw new Error('Could not insert merchant');
}
return NextResponse.json(result.rows[0]);
} catch (err: any) {
console.error('Internal Error:', err);
return NextResponse.json(
{ error: `Internal Server Error: ${err.message}` },
{ status: 500 }
);
}
}
export async function GET() {
try {
const result = await db.query('SELECT * FROM merchants ORDER BY created_at DESC');
return NextResponse.json(result.rows);
} catch (err: any) {
return NextResponse.json(
{ error: `Internal Server Error: ${err.message}` },
{ status: 500 }
);
}
}