feat(donations): add custom donation tier with flexible amount selection

- Updated support page grid layout to accommodate new custom donation card
- Enhanced API route to validate and process custom donation amounts (min/max limits)
- Modified donation card component to include custom amount input field
- Ensures users can donate any amount within specified range, improving donation flexibility
This commit is contained in:
Evandro Fonseca Junior committed 2025-10-26 22:30:46 -03:00
1 parent 8271d1fa31
commit 5ccc9120c4
4 files changed
+232 -40

No files matched your search

+44 -6
View File
@@ -11,6 +11,7 @@ export async function POST(request: NextRequest) {
"coffee",
"snack",
"lunch",
"custom",
];
if (!body.tier || !validTiers.includes(body.tier)) {
return NextResponse.json(
@@ -22,14 +23,49 @@ export async function POST(request: NextRequest) {
const product =
DONATION_PRODUCTS[body.tier as keyof typeof DONATION_PRODUCTS];
// Se for custom, validar valor enviado
let finalAmount: number;
let finalDescription: string;
if (body.tier === "custom") {
if (!body.customAmount || typeof body.customAmount !== "number") {
return NextResponse.json(
{ success: false, error: "Valor customizado inválido" },
{ status: 400 }
);
}
const minAmount = DONATION_PRODUCTS.custom.minAmount;
const maxAmount = DONATION_PRODUCTS.custom.maxAmount;
if (body.customAmount < minAmount || body.customAmount > maxAmount) {
return NextResponse.json(
{
success: false,
error: `Valor deve estar entre R$ ${minAmount} e R$ ${maxAmount}`,
},
{ status: 400 }
);
}
// Converter para centavos
finalAmount = Math.round(body.customAmount * 100);
finalDescription = `Doação Personalizada - R$ ${body.customAmount.toFixed(
2
)}`;
} else {
finalAmount = product.price;
finalDescription = product.name;
}
let pixData;
// Fluxo: Anônimo ou Identificado
if (body.anonymous === true) {
// Doação anônima - sem customer
pixData = await abacatePay.createPixQrCode({
amount: product.price,
description: product.name,
amount: finalAmount,
description: finalDescription,
expiresIn: 600,
});
@@ -38,8 +74,9 @@ export async function POST(request: NextRequest) {
console.log("📝 Registro de doação anônima:", {
type: "donation_created",
pixId: pixData.id,
amount: product.price,
amount: finalAmount,
tier: body.tier,
customAmount: body.tier === "custom" ? body.customAmount : undefined,
anonymous: true,
createdAt: new Date().toISOString(),
});
@@ -87,8 +124,8 @@ export async function POST(request: NextRequest) {
}
pixData = await abacatePay.createPixQrCode({
amount: product.price,
description: product.name,
amount: finalAmount,
description: finalDescription,
expiresIn: 600,
customer: {
name: body.donorName.trim(),
@@ -103,8 +140,9 @@ export async function POST(request: NextRequest) {
console.log("📝 Registro de doação identificada:", {
type: "donation_created",
pixId: pixData.id,
amount: product.price,
amount: finalAmount,
tier: body.tier,
customAmount: body.tier === "custom" ? body.customAmount : undefined,
anonymous: false,
donor: {
name: body.donorName,