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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const admin = require('firebase-admin');
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
app.use(express.json());
|
| 6 |
+
|
| 7 |
+
// ==========================================
|
| 8 |
+
// ১. ফায়ারবেজ মাল্টিপল অ্যাডমিন সেটআপ
|
| 9 |
+
// ==========================================
|
| 10 |
+
// Note: Environment Variable এ FIREBASE_ADMIN_1, FIREBASE_ADMIN_3, FIREBASE_ADMIN_4 এবং
|
| 11 |
+
// তাদের নিজ নিজ ডাটাবেজ URL (FIREBASE_DB_URL_1, 3, 4) থাকতে হবে।
|
| 12 |
+
const initFirebase = (envAdmin, envUrl, appName) => {
|
| 13 |
+
if (!process.env[envAdmin] || !process.env[envUrl]) {
|
| 14 |
+
console.warn(`Warning: ${envAdmin} or ${envUrl} is missing.`);
|
| 15 |
+
return null;
|
| 16 |
+
}
|
| 17 |
+
return admin.initializeApp({
|
| 18 |
+
credential: admin.credential.cert(JSON.parse(process.env[envAdmin])),
|
| 19 |
+
databaseURL: process.env[envUrl]
|
| 20 |
+
}, appName);
|
| 21 |
+
};
|
| 22 |
+
|
| 23 |
+
// অ্যাপগুলো ইনিশিয়ালাইজ করা হচ্ছে
|
| 24 |
+
const app1 = initFirebase('FIREBASE_ADMIN_1', 'FIREBASE_DB_URL_1', 'Admin1');
|
| 25 |
+
// Admin 2 এর কথা লজিকে নেই, তাই ইনিশিয়ালাইজ করলাম না। প্রয়োজন হলে একই নিয়মে করতে পারবে।
|
| 26 |
+
const app3 = initFirebase('FIREBASE_ADMIN_3', 'FIREBASE_DB_URL_3', 'Admin3');
|
| 27 |
+
const app4 = initFirebase('FIREBASE_ADMIN_4', 'FIREBASE_DB_URL_4', 'Admin4');
|
| 28 |
+
|
| 29 |
+
const db1 = app1 ? app1.database() : null;
|
| 30 |
+
const db3 = app3 ? app3.database() : null;
|
| 31 |
+
const db4 = app4 ? app4.database() : null;
|
| 32 |
+
|
| 33 |
+
// ==========================================
|
| 34 |
+
// ২. হেলথ চেক রাউটস (যেকোনো রিকোয়েস্ট)
|
| 35 |
+
// ==========================================
|
| 36 |
+
const healthHandler = (req, res) => {
|
| 37 |
+
res.send('Ok, yes, done');
|
| 38 |
+
};
|
| 39 |
+
app.all('/health', healthHandler);
|
| 40 |
+
app.all('/ch', healthHandler);
|
| 41 |
+
app.all('/unps', healthHandler);
|
| 42 |
+
|
| 43 |
+
// ==========================================
|
| 44 |
+
// ৩. মিডলওয়্যার: শুধুমাত্র POST রিকোয়েস্ট পারমিট করা
|
| 45 |
+
// ==========================================
|
| 46 |
+
const checkPostMethod = (req, res, next) => {
|
| 47 |
+
if (req.method !== 'POST') {
|
| 48 |
+
return res.status(405).json({ error: 'Method Not Allowed. Only POST is accepted.' });
|
| 49 |
+
}
|
| 50 |
+
next();
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
// ==========================================
|
| 54 |
+
// ৪. API: ক্রিয়েট অ্যাকাউন্ট (/creataccoumt)
|
| 55 |
+
// ==========================================
|
| 56 |
+
app.post('/creataccoumt', checkPostMethod, async (req, res) => {
|
| 57 |
+
const { user_id, refer_id, name, username, profile_pic_url } = req.body;
|
| 58 |
+
|
| 59 |
+
if (!user_id) return res.status(400).json({ error: 'user_id is required' });
|
| 60 |
+
|
| 61 |
+
try {
|
| 62 |
+
const userRef = db1.ref('user').child(user_id);
|
| 63 |
+
const snapshot = await userRef.once('value');
|
| 64 |
+
|
| 65 |
+
// যদি অ্যাকাউন্ট আগে থেকেই থাকে, রিকোয়েস্ট ক্লোজ
|
| 66 |
+
if (snapshot.exists()) {
|
| 67 |
+
return res.status(200).json({ message: 'Account already exists. No action taken.' });
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// নতুন অ্যাকাউন্ট তৈরি
|
| 71 |
+
const newUserData = {
|
| 72 |
+
user_id,
|
| 73 |
+
refer_id: refer_id || null,
|
| 74 |
+
name: name || null,
|
| 75 |
+
username: username || null,
|
| 76 |
+
profile_pic_url: profile_pic_url || null,
|
| 77 |
+
balance: 0,
|
| 78 |
+
total_withdraw: 0,
|
| 79 |
+
total_withdraw_amount: 0,
|
| 80 |
+
join_date: admin.database.ServerValue.TIMESTAMP,
|
| 81 |
+
total_refer: 0
|
| 82 |
+
};
|
| 83 |
+
await userRef.set(newUserData);
|
| 84 |
+
|
| 85 |
+
// রেফারেল লজিক (যদি refer_id থাকে)
|
| 86 |
+
if (refer_id) {
|
| 87 |
+
const referUserRef = db1.ref('user').child(refer_id);
|
| 88 |
+
const referUserSnap = await referUserRef.once('value');
|
| 89 |
+
|
| 90 |
+
if (referUserSnap.exists()) {
|
| 91 |
+
// Admin 1 এ total refer +1 করা
|
| 92 |
+
await referUserRef.child('total_refer').transaction((currentValue) => {
|
| 93 |
+
return (currentValue || 0) + 1;
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
// Admin 3 তে refer/uid এ ডাটা যুক্ত করা
|
| 97 |
+
const referListRef = db3.ref('refer').child(refer_id);
|
| 98 |
+
await referListRef.push(user_id); // নতুন user_id লিস্টে পুশ করা হলো
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
res.status(201).json({ message: 'Account created successfully!' });
|
| 103 |
+
} catch (error) {
|
| 104 |
+
console.error('Account Creation Error:', error);
|
| 105 |
+
res.status(500).json({ error: 'Internal Server Error' });
|
| 106 |
+
}
|
| 107 |
+
});
|
| 108 |
+
|
| 109 |
+
// ==========================================
|
| 110 |
+
// ৫. API: উইথড্র মেইন ব্যালেন্স (/withdraw-main)
|
| 111 |
+
// ==========================================
|
| 112 |
+
app.post('/withdraw-main', checkPostMethod, async (req, res) => {
|
| 113 |
+
const { user_id, method, address, time, amount } = req.body;
|
| 114 |
+
const allowedMethods = ['bkash', 'mobile-gp', 'mobile-ar', 'mobile-ro', 'mobile-bl', 'mobile-tk', 'nagad', 'binance', 'usdt', 'ton'];
|
| 115 |
+
|
| 116 |
+
if (!user_id || !method || !address || !amount) {
|
| 117 |
+
return res.status(400).json({ error: 'Missing required fields' });
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
if (!allowedMethods.includes(method)) {
|
| 121 |
+
return res.status(400).json({ error: 'Invalid withdrawal method' });
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
try {
|
| 125 |
+
const userRef = db1.ref('user').child(user_id);
|
| 126 |
+
const snapshot = await userRef.once('value');
|
| 127 |
+
|
| 128 |
+
if (!snapshot.exists()) return res.status(404).json({ error: 'User not found' });
|
| 129 |
+
|
| 130 |
+
const userData = snapshot.val();
|
| 131 |
+
const currentBalance = Number(userData.balance || 0);
|
| 132 |
+
const requestAmount = Number(amount);
|
| 133 |
+
|
| 134 |
+
// ব্যালেন্স চেক
|
| 135 |
+
if (currentBalance < requestAmount) {
|
| 136 |
+
return res.status(400).json({ error: 'Insufficient balance' });
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
// Admin 1 থেকে ব্যালেন্স কাটা
|
| 140 |
+
await userRef.child('balance').set(currentBalance - requestAmount);
|
| 141 |
+
|
| 142 |
+
// Admin 3 তে পেন্ডিং উইথড্র লিস্টে যুক্ত করা
|
| 143 |
+
const pendingRef = db3.ref('withdrow/painding').child(user_id);
|
| 144 |
+
await pendingRef.push({
|
| 145 |
+
method,
|
| 146 |
+
address,
|
| 147 |
+
time: time || admin.database.ServerValue.TIMESTAMP,
|
| 148 |
+
amount: requestAmount,
|
| 149 |
+
status: 'pending'
|
| 150 |
+
});
|
| 151 |
+
|
| 152 |
+
res.status(200).json({ message: 'Withdrawal request successful' });
|
| 153 |
+
} catch (error) {
|
| 154 |
+
console.error('Withdraw Main Error:', error);
|
| 155 |
+
res.status(500).json({ error: 'Internal Server Error' });
|
| 156 |
+
}
|
| 157 |
+
});
|
| 158 |
+
|
| 159 |
+
// ==========================================
|
| 160 |
+
// ৬. API: অ্যাড ব্যালেন্স উইথড্র (Admin 4 -> Admin 1)
|
| 161 |
+
// ==========================================
|
| 162 |
+
const adWithdrawPaths = [
|
| 163 |
+
'/withdraw-link-visit', '/withdraw-adsgram-1', '/withdraw-adsgram-2',
|
| 164 |
+
'/withdraw-gigapub-1', '/withdraw-gigapub-2', '/withdraw-monetag-1', '/withdraw-monetag-2'
|
| 165 |
+
];
|
| 166 |
+
|
| 167 |
+
app.post(adWithdrawPaths, checkPostMethod, async (req, res) => {
|
| 168 |
+
const { user_id, amount, method } = req.body;
|
| 169 |
+
|
| 170 |
+
if (!user_id || !amount || method !== 'wallet') {
|
| 171 |
+
return res.status(400).json({ error: 'Invalid request. Method must be wallet and amount is required.' });
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
// Path থেকে অরিজিনাল অ্যাড নেটওয়ার্কের নাম বের করা (যেমন: link-visit)
|
| 175 |
+
const adNetworkName = req.path.replace('/withdraw-', '');
|
| 176 |
+
const requestAmount = Number(amount);
|
| 177 |
+
|
| 178 |
+
try {
|
| 179 |
+
// Admin 4 (আগের সার্ভার) এর নির্দিষ্ট পাথে ব্যালেন্স চেক
|
| 180 |
+
const adRef = db4.ref(adNetworkName).child(user_id);
|
| 181 |
+
const adSnapshot = await adRef.once('value');
|
| 182 |
+
|
| 183 |
+
if (!adSnapshot.exists()) return res.status(404).json({ error: 'No ad earnings found for this user' });
|
| 184 |
+
|
| 185 |
+
// ধরে নিচ্ছি আগের সার্ভারে ইউজারের মোট ব্যালেন্স `balance` নোডে আছে।
|
| 186 |
+
// (যদি আগের সিস্টেমে ব্যালেন্স পুশ করা লিস্ট হিসেবে থাকে, তবে এখানে সেটা যোগ করার লজিক লিখতে হবে)
|
| 187 |
+
let adCurrentBalance = Number(adSnapshot.val().balance || 0);
|
| 188 |
+
|
| 189 |
+
if (adCurrentBalance < requestAmount) {
|
| 190 |
+
return res.status(400).json({ error: 'Insufficient ad balance' });
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
// Admin 4 থেকে ব্যালেন্স মাইনাস করা
|
| 194 |
+
await adRef.child('balance').set(adCurrentBalance - requestAmount);
|
| 195 |
+
|
| 196 |
+
// Admin 1 এর মেইন ব্যালেন্সে ট্রান্সফার (যোগ) করা
|
| 197 |
+
const mainUserRef = db1.ref('user').child(user_id);
|
| 198 |
+
await mainUserRef.child('balance').transaction((currentMainBalance) => {
|
| 199 |
+
return (currentMainBalance || 0) + requestAmount;
|
| 200 |
+
});
|
| 201 |
+
|
| 202 |
+
res.status(200).json({ message: `Success! ${requestAmount} transferred to main balance from ${adNetworkName}` });
|
| 203 |
+
} catch (error) {
|
| 204 |
+
console.error('Ad Withdraw Error:', error);
|
| 205 |
+
res.status(500).json({ error: 'Internal Server Error' });
|
| 206 |
+
}
|
| 207 |
+
});
|
| 208 |
+
|
| 209 |
+
// সার্ভার স্টার্ট
|
| 210 |
+
const PORT = process.env.PORT || 7860;
|
| 211 |
+
app.listen(PORT, () => {
|
| 212 |
+
console.log(`Server is running on port ${PORT}`);
|
| 213 |
+
});
|