main: add many things to app :)

Signed-off-by: nfel <nfilsaraee@gmail.com>
This commit is contained in:
2025-12-30 01:30:31 +03:30
parent 91c149e92e
commit 895afc44af
26 changed files with 2180 additions and 249 deletions
+73
View File
@@ -0,0 +1,73 @@
import { NextRequest, NextResponse } from 'next/server';
import { ZarinPal } from 'zarinpal-node-sdk';
import { getDatabase } from '@/lib/db';
const zarinpal = new ZarinPal({
merchantId: process.env.ZARINPAL_MERCHANT_ID || 'test-merchant-id',
sandbox: process.env.NODE_ENV !== 'production',
});
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { albumId, amount, customerName, email, phoneNumber } = body;
if (!albumId || !amount) {
return NextResponse.json(
{ error: 'Album ID and amount are required' },
{ status: 400 }
);
}
// Clean phone number: remove +98, spaces, and any non-digits
// ZarinPal expects format: 09XXXXXXXXX (11 digits starting with 0)
const cleanPhone = phoneNumber.replace(/\D/g, ''); // Remove all non-digits
const mobileNumber = cleanPhone.startsWith('98')
? '0' + cleanPhone.slice(2) // +98 9390084053 -> 09390084053
: cleanPhone.startsWith('9')
? '0' + cleanPhone // 9390084053 -> 09390084053
: cleanPhone; // Already in correct format
// Get the base URL for callback
const protocol = request.headers.get('x-forwarded-proto') || 'http';
const host = request.headers.get('host') || 'localhost:3000';
const callback_url = `${protocol}://${host}/payment/callback`;
// Initiate payment with ZarinPal
const response = await zarinpal.payments.create({
amount: amount,
callback_url: callback_url,
description: `Purchase album: ${albumId}`,
mobile: mobileNumber,
email: email,
});
if (response.data && response.data.code === 100) {
const authority = response.data.authority;
// Store payment authority in database
const db = getDatabase();
db.prepare(`
INSERT INTO payment_authorities (authority, albumId, amount, customerName, email, phoneNumber, status)
VALUES (?, ?, ?, ?, ?, ?, 'pending')
`).run(authority, albumId, amount, customerName, email, mobileNumber);
return NextResponse.json({
success: true,
authority: authority,
paymentUrl: `https://sandbox.zarinpal.com/pg/StartPay/${authority}`,
});
} else {
return NextResponse.json(
{ error: 'Failed to initiate payment', code: response.data?.code },
{ status: 400 }
);
}
} catch (error: any) {
console.error('Payment initiation error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to initiate payment' },
{ status: 500 }
);
}
}
+104
View File
@@ -0,0 +1,104 @@
import { NextRequest, NextResponse } from 'next/server';
import { ZarinPal } from 'zarinpal-node-sdk';
import { getDatabase } from '@/lib/db';
const zarinpal = new ZarinPal({
merchantId: process.env.ZARINPAL_MERCHANT_ID || 'test-merchant-id',
sandbox: process.env.NODE_ENV !== 'production',
});
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { authority } = body;
if (!authority) {
return NextResponse.json(
{ error: 'Authority code is required' },
{ status: 400 }
);
}
const db = getDatabase();
// Get payment details from database
const paymentRecord = db.prepare(`
SELECT * FROM payment_authorities WHERE authority = ?
`).get(authority) as any;
if (!paymentRecord) {
return NextResponse.json(
{ error: 'No matching transaction found for this authority code' },
{ status: 404 }
);
}
// Check if already verified
if (paymentRecord.status === 'verified') {
return NextResponse.json({
success: true,
refId: paymentRecord.refId,
message: 'Payment already verified',
});
}
// Verify payment with ZarinPal
const response = await zarinpal.verifications.verify({
amount: paymentRecord.amount,
authority: authority,
});
if (response.data.code === 100 || response.data.code === 101) {
const refId = response.data.ref_id;
const cardPan = response.data.card_pan;
const fee = response.data.fee;
// Update payment authority status
db.prepare(`
UPDATE payment_authorities
SET status = 'verified', refId = ?, cardPan = ?, fee = ?, verifiedAt = ?
WHERE authority = ?
`).run(refId, cardPan, fee, Date.now(), authority);
// Create purchase record (auto-approved for IPG payments)
db.prepare(`
INSERT INTO purchases (albumId, transactionId, customerName, email, phoneNumber, txReceipt, purchaseDate, approvalStatus, paymentMethod)
VALUES (?, ?, ?, ?, ?, ?, ?, 'approved', 'ipg')
`).run(
paymentRecord.albumId,
refId.toString(),
paymentRecord.customerName,
paymentRecord.email,
paymentRecord.phoneNumber,
`ZarinPal-${refId}`,
Date.now()
);
return NextResponse.json({
success: true,
refId: refId,
cardPan: cardPan,
fee: fee,
message: response.data.code === 101 ? 'Payment already verified' : 'Payment verified successfully',
});
} else {
// Update payment authority status to failed
db.prepare(`
UPDATE payment_authorities
SET status = 'failed'
WHERE authority = ?
`).run(authority);
return NextResponse.json(
{ error: `Transaction failed with code: ${response.data.code}` },
{ status: 400 }
);
}
} catch (error: any) {
console.error('Payment verification error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to verify payment' },
{ status: 500 }
);
}
}