feat: implement platform treasury model with merchant balance tracking and API docs
This commit is contained in:
@@ -95,27 +95,37 @@ export async function POST(request: Request) {
|
||||
});
|
||||
}
|
||||
|
||||
// 6. Proceed to Sweep
|
||||
// 6. Proceed to Sweep (100% to platform treasury)
|
||||
const sweepResult = await cryptoEngine.sweepFunds(
|
||||
tempWalletConfig.privateKey,
|
||||
merchantAddress,
|
||||
platformAddress,
|
||||
selectedToken,
|
||||
feePercent
|
||||
selectedToken
|
||||
);
|
||||
|
||||
if (!sweepResult.success) {
|
||||
throw new Error("Süpürme işlemi başarısız oldu.");
|
||||
}
|
||||
|
||||
// 6. Update transaction status
|
||||
// 6.1 Calculate Merchant's credit (Amount - Platform Fee)
|
||||
// Note: transaction.amount is the gross amount paid by customer
|
||||
const grossAmount = parseFloat(transaction.amount);
|
||||
const feeAmount = (grossAmount * feePercent) / 100;
|
||||
const merchantNetCredit = grossAmount - feeAmount;
|
||||
|
||||
// 6.2 Update Merchant's virtual balance in DB
|
||||
await db.query(`
|
||||
UPDATE merchants
|
||||
SET available_balance = available_balance + $1
|
||||
WHERE id = $2
|
||||
`, [merchantNetCredit, transaction.merchant_id]);
|
||||
|
||||
// 6.3 Update transaction status
|
||||
await db.query(`UPDATE transactions SET status = 'succeeded' WHERE id = $1`, [transaction.id]);
|
||||
|
||||
// 7. Automated Webhook Notification
|
||||
if (transaction.callback_url) {
|
||||
console.log(`[Webhook] Notifying merchant at ${transaction.callback_url}`);
|
||||
try {
|
||||
// In production, sign this payload and use a more robust delivery system
|
||||
fetch(transaction.callback_url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -123,7 +133,7 @@ export async function POST(request: Request) {
|
||||
status: 'success',
|
||||
txId: transaction.stripe_pi_id,
|
||||
orderRef: transaction.source_ref_id,
|
||||
hashes: sweepResult
|
||||
hashes: { txHash: sweepResult.txHash }
|
||||
})
|
||||
}).catch(e => console.error("Webhook fetch failed:", e.message));
|
||||
} catch (webhookError: any) {
|
||||
@@ -133,11 +143,11 @@ export async function POST(request: Request) {
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: `Ödeme ${selectedNetwork} ağında ${selectedToken} ile başarıyla doğrulandı ve süpürüldü.`,
|
||||
message: `Ödeme ${selectedNetwork} ağında ${selectedToken} ile başarıyla doğrulandı, hazineye aktarıldı ve merchant bakiyesi güncellendi.`,
|
||||
hashes: {
|
||||
platform: sweepResult.platformTx,
|
||||
merchant: sweepResult.merchantTx
|
||||
}
|
||||
txHash: sweepResult.txHash
|
||||
},
|
||||
merchantCredit: merchantNetCredit
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
|
||||
Reference in New Issue
Block a user