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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const crypto = require('crypto');
|
| 3 |
+
const bodyParser = require('body-parser');
|
| 4 |
+
const nodemailer = require('nodemailer');
|
| 5 |
+
|
| 6 |
+
const app = express();
|
| 7 |
+
app.use(bodyParser.json());
|
| 8 |
+
|
| 9 |
+
// Temporary storage for order tracking
|
| 10 |
+
const orderTracking = new Map();
|
| 11 |
+
|
| 12 |
+
// Email configuration
|
| 13 |
+
const transporter = nodemailer.createTransport({
|
| 14 |
+
host: 'smtp.gmail.com',
|
| 15 |
+
port: 465,
|
| 16 |
+
secure: true, // use SSL
|
| 17 |
+
auth: {
|
| 18 |
+
user: '[email protected]',
|
| 19 |
+
pass: 'rpymwovddmzfitdp'
|
| 20 |
+
}
|
| 21 |
+
});
|
| 22 |
+
|
| 23 |
+
// Function to send success email with retry logic
|
| 24 |
+
async function sendSuccessEmail(email, orderId, amount, redemptionCode, retryCount = 0) {
|
| 25 |
+
const maxRetries = 5;
|
| 26 |
+
const retryDelay = 3000; // 3 seconds between retries
|
| 27 |
+
|
| 28 |
+
try {
|
| 29 |
+
console.log(`Attempt ${retryCount + 1} to send email to:`, email);
|
| 30 |
+
console.log('Email details:', {
|
| 31 |
+
orderId,
|
| 32 |
+
amount,
|
| 33 |
+
redemptionCode
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
// Verify transporter configuration
|
| 37 |
+
await transporter.verify();
|
| 38 |
+
console.log('SMTP connection verified successfully');
|
| 39 |
+
|
| 40 |
+
const result = await transporter.sendMail({
|
| 41 |
+
from: '"TypeGPT" <[email protected]>',
|
| 42 |
+
to: email,
|
| 43 |
+
subject: 'Your Recharge Code Is Ready! 🎉',
|
| 44 |
+
text: `Your payment of $${amount} for order ${orderId} has been processed successfully.\nRedemption Code: ${redemptionCode}`,
|
| 45 |
+
html: `
|
| 46 |
+
<!DOCTYPE html>
|
| 47 |
+
<html>
|
| 48 |
+
<head>
|
| 49 |
+
<style>
|
| 50 |
+
body {
|
| 51 |
+
font-family: Arial, sans-serif;
|
| 52 |
+
line-height: 1.6;
|
| 53 |
+
color: #333333;
|
| 54 |
+
margin: 0;
|
| 55 |
+
padding: 0;
|
| 56 |
+
}
|
| 57 |
+
.container {
|
| 58 |
+
max-width: 600px;
|
| 59 |
+
margin: 0 auto;
|
| 60 |
+
padding: 20px;
|
| 61 |
+
}
|
| 62 |
+
.header {
|
| 63 |
+
background-color: #f8f9fa;
|
| 64 |
+
padding: 20px;
|
| 65 |
+
text-align: center;
|
| 66 |
+
border-radius: 8px 8px 0 0;
|
| 67 |
+
}
|
| 68 |
+
.content {
|
| 69 |
+
background-color: #ffffff;
|
| 70 |
+
padding: 30px;
|
| 71 |
+
border-radius: 0 0 8px 8px;
|
| 72 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 73 |
+
}
|
| 74 |
+
.code-box {
|
| 75 |
+
background-color: #f8f9fa;
|
| 76 |
+
border: 2px dashed #dee2e6;
|
| 77 |
+
border-radius: 6px;
|
| 78 |
+
padding: 15px;
|
| 79 |
+
margin: 20px 0;
|
| 80 |
+
text-align: center;
|
| 81 |
+
}
|
| 82 |
+
.code {
|
| 83 |
+
font-family: monospace;
|
| 84 |
+
font-size: 24px;
|
| 85 |
+
color: #28a745;
|
| 86 |
+
font-weight: bold;
|
| 87 |
+
letter-spacing: 2px;
|
| 88 |
+
}
|
| 89 |
+
.details {
|
| 90 |
+
margin: 20px 0;
|
| 91 |
+
padding: 15px;
|
| 92 |
+
background-color: #f8f9fa;
|
| 93 |
+
border-radius: 6px;
|
| 94 |
+
}
|
| 95 |
+
.footer {
|
| 96 |
+
text-align: center;
|
| 97 |
+
margin-top: 20px;
|
| 98 |
+
font-size: 12px;
|
| 99 |
+
color: #6c757d;
|
| 100 |
+
}
|
| 101 |
+
.button {
|
| 102 |
+
display: inline-block;
|
| 103 |
+
padding: 10px 20px;
|
| 104 |
+
background-color: #28a745;
|
| 105 |
+
color: #ffffff;
|
| 106 |
+
text-decoration: none;
|
| 107 |
+
border-radius: 5px;
|
| 108 |
+
margin: 20px 0;
|
| 109 |
+
}
|
| 110 |
+
</style>
|
| 111 |
+
</head>
|
| 112 |
+
<body>
|
| 113 |
+
<div class="container">
|
| 114 |
+
<div class="header">
|
| 115 |
+
<h1 style="color: #28a745; margin: 0;">Payment Successful! 🎉</h1>
|
| 116 |
+
</div>
|
| 117 |
+
<div class="content">
|
| 118 |
+
<p>Dear Customer,</p>
|
| 119 |
+
<p>Thank you for your purchase! Your payment has been successfully processed.</p>
|
| 120 |
+
|
| 121 |
+
<div class="code-box">
|
| 122 |
+
<p style="margin: 0; font-weight: bold;">Your Redemption Code:</p>
|
| 123 |
+
<div class="code">${redemptionCode}</div>
|
| 124 |
+
</div>
|
| 125 |
+
|
| 126 |
+
<div class="details">
|
| 127 |
+
<h3 style="margin-top: 0;">Transaction Details:</h3>
|
| 128 |
+
<p><strong>Order ID:</strong> ${orderId}</p>
|
| 129 |
+
<p><strong>Amount Paid:</strong> $${amount}</p>
|
| 130 |
+
<p><strong>API Credits:</strong> $${amount * 25}</p>
|
| 131 |
+
</div>
|
| 132 |
+
|
| 133 |
+
<p><strong>Important:</strong></p>
|
| 134 |
+
<ul>
|
| 135 |
+
<li>Keep this code safe - it's your access to the API credits</li>
|
| 136 |
+
<li>The code can only be used once</li>
|
| 137 |
+
<li>If you have any issues, please contact our support team</li>
|
| 138 |
+
</ul>
|
| 139 |
+
|
| 140 |
+
<div style="text-align: center;">
|
| 141 |
+
<a href="https://fast.typegpt.net/topup" class="button">Top Up Balance</a>
|
| 142 |
+
</div>
|
| 143 |
+
|
| 144 |
+
<div class="footer">
|
| 145 |
+
<p>This is an automated message, please do not reply to this email.</p>
|
| 146 |
+
<p>© ${new Date().getFullYear()} NiansuhAI. All rights reserved.</p>
|
| 147 |
+
</div>
|
| 148 |
+
</div>
|
| 149 |
+
</div>
|
| 150 |
+
</body>
|
| 151 |
+
</html>
|
| 152 |
+
`
|
| 153 |
+
});
|
| 154 |
+
|
| 155 |
+
console.log('Email sent successfully:', result);
|
| 156 |
+
return result;
|
| 157 |
+
} catch (error) {
|
| 158 |
+
console.error('Failed to send email:', error);
|
| 159 |
+
console.error('Error details:', {
|
| 160 |
+
code: error.code,
|
| 161 |
+
command: error.command,
|
| 162 |
+
response: error.response
|
| 163 |
+
});
|
| 164 |
+
|
| 165 |
+
if (retryCount < maxRetries) {
|
| 166 |
+
console.log(`Retrying in ${retryDelay/1000} seconds... (Attempt ${retryCount + 2}/${maxRetries + 1})`);
|
| 167 |
+
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
| 168 |
+
return sendSuccessEmail(email, orderId, amount, redemptionCode, retryCount + 1);
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
throw new Error(`Failed to send email after ${maxRetries + 1} attempts`);
|
| 172 |
+
}
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
// Add function to generate order ID
|
| 176 |
+
function generateOrderId() {
|
| 177 |
+
const randomNum = Math.random().toString().slice(2, 18); // Get 16 digits
|
| 178 |
+
return `hrmn-${randomNum}`;
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
// Update the merchant key constant
|
| 182 |
+
const MERCHANT_KEY = '3H896K-W3LZEF-H4H957-ZDF5S4';
|
| 183 |
+
|
| 184 |
+
// Add the function to create redemption code
|
| 185 |
+
async function createRedemptionCode(orderId, amount) {
|
| 186 |
+
console.log('Creating redemption code...');
|
| 187 |
+
const quota = 25 * amount * 500000; // $1 = 500000 quota
|
| 188 |
+
|
| 189 |
+
try {
|
| 190 |
+
const response = await fetch('https://niansuh-redeem.hf.space/api/create', {
|
| 191 |
+
method: 'POST',
|
| 192 |
+
headers: {
|
| 193 |
+
'Content-Type': 'application/json',
|
| 194 |
+
'x-api-key': 'sk-niansuhaiburewala521'
|
| 195 |
+
},
|
| 196 |
+
body: JSON.stringify({
|
| 197 |
+
username: 'admin',
|
| 198 |
+
name: Math.random().toString(36).substring(2, 12), // Random name
|
| 199 |
+
quota: quota,
|
| 200 |
+
count: 1
|
| 201 |
+
})
|
| 202 |
+
});
|
| 203 |
+
|
| 204 |
+
const result = await response.json();
|
| 205 |
+
console.log('Redemption code created');
|
| 206 |
+
if (result.success && result.data?.data?.[0]) {
|
| 207 |
+
return result.data.data[0]; // Return the redemption code
|
| 208 |
+
} else {
|
| 209 |
+
throw new Error('Invalid response format from redemption code API');
|
| 210 |
+
}
|
| 211 |
+
} catch (error) {
|
| 212 |
+
console.log('Failed to create redemption code:', error);
|
| 213 |
+
throw error;
|
| 214 |
+
}
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
// Update the webhook handler
|
| 218 |
+
const webhookHandler = {
|
| 219 |
+
validateHmacSignature(postData, hmacHeader, type) {
|
| 220 |
+
const apiKey = MERCHANT_KEY; // Use the same key consistently
|
| 221 |
+
|
| 222 |
+
const calculatedHmac = crypto
|
| 223 |
+
.createHmac('sha512', apiKey)
|
| 224 |
+
.update(postData)
|
| 225 |
+
.digest('hex');
|
| 226 |
+
|
| 227 |
+
return calculatedHmac === hmacHeader;
|
| 228 |
+
},
|
| 229 |
+
|
| 230 |
+
handleWaitingPayment(data) {
|
| 231 |
+
console.log('Payment waiting');
|
| 232 |
+
},
|
| 233 |
+
|
| 234 |
+
handleConfirmingPayment(data) {
|
| 235 |
+
console.log('Payment confirming');
|
| 236 |
+
},
|
| 237 |
+
|
| 238 |
+
async handlePaidPayment(data) {
|
| 239 |
+
console.log('Payment received for order:', data.orderId);
|
| 240 |
+
const orderData = orderTracking.get(data.orderId);
|
| 241 |
+
if (orderData) {
|
| 242 |
+
const { email, amount } = orderData;
|
| 243 |
+
console.log(`Processing payment for email: ${email}, amount: $${amount}`);
|
| 244 |
+
|
| 245 |
+
try {
|
| 246 |
+
// Create redemption code and send email
|
| 247 |
+
const redemptionCode = await createRedemptionCode(data.orderId, amount);
|
| 248 |
+
console.log(`Generated redemption code for order ${data.orderId}: ${redemptionCode}`);
|
| 249 |
+
|
| 250 |
+
try {
|
| 251 |
+
await sendSuccessEmail(email, data.orderId, amount, redemptionCode);
|
| 252 |
+
console.log(`Successfully sent email to ${email} with code: ${redemptionCode}`);
|
| 253 |
+
} catch (emailError) {
|
| 254 |
+
// Log failed email send but don't throw error
|
| 255 |
+
console.error(`⚠️ FAILED EMAIL SEND - IMPORTANT DATA:`);
|
| 256 |
+
console.error(`Email: ${email}`);
|
| 257 |
+
console.error(`Order ID: ${data.orderId}`);
|
| 258 |
+
console.error(`Amount: $${amount}`);
|
| 259 |
+
console.error(`Redemption Code: ${redemptionCode}`);
|
| 260 |
+
console.error(`Error:`, emailError);
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
console.log('Payment process completed');
|
| 264 |
+
} catch (error) {
|
| 265 |
+
console.error(`❌ CRITICAL ERROR processing order ${data.orderId}:`, error);
|
| 266 |
+
} finally {
|
| 267 |
+
// Clean up stored data
|
| 268 |
+
orderTracking.delete(data.orderId);
|
| 269 |
+
}
|
| 270 |
+
} else {
|
| 271 |
+
console.error(`❌ No order data found for order ID: ${data.orderId}`);
|
| 272 |
+
}
|
| 273 |
+
},
|
| 274 |
+
|
| 275 |
+
handleExpiredPayment(data) {
|
| 276 |
+
console.log('Payment expired');
|
| 277 |
+
},
|
| 278 |
+
|
| 279 |
+
handleCompletePayout(data) {
|
| 280 |
+
console.log('Payout complete');
|
| 281 |
+
}
|
| 282 |
+
};
|
| 283 |
+
|
| 284 |
+
// Serve the HTML directly from root endpoint
|
| 285 |
+
app.get('/', (req, res) => {
|
| 286 |
+
const html = `
|
| 287 |
+
<html>
|
| 288 |
+
<head>
|
| 289 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 290 |
+
<script src="https://registry.npmmirror.com/vue/3.3.11/files/dist/vue.global.js"></script>
|
| 291 |
+
<meta charset="UTF-8">
|
| 292 |
+
</head>
|
| 293 |
+
<body class="flex items-center justify-center min-h-screen bg-gray-100">
|
| 294 |
+
<div id="app" class="bg-white p-8 rounded-lg shadow-md w-80">
|
| 295 |
+
<h1 class="text-2xl font-bold mb-2">Recharge Balance</h1>
|
| 296 |
+
<p class="text-gray-500 mb-4">Add funds to your account</p>
|
| 297 |
+
|
| 298 |
+
<div v-if="amount" class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 mb-4">
|
| 299 |
+
<p>You will receive \${{ amount * 25 }} in API credits</p>
|
| 300 |
+
</div>
|
| 301 |
+
|
| 302 |
+
<div v-if="showWarning" class="bg-yellow-100 border-l-4 border-yellow-500 text-yellow-700 p-4 mb-4">
|
| 303 |
+
<p>A redemption code will be sent to your email after payment.</p>
|
| 304 |
+
<p class="mt-2 text-sm font-bold">Please make sure to enter a valid email address!</p>
|
| 305 |
+
</div>
|
| 306 |
+
|
| 307 |
+
<label class="block text-gray-700 mb-2">Email</label>
|
| 308 |
+
<input v-model="email" type="email" placeholder="Enter your email" class="w-full p-2 border rounded mb-4">
|
| 309 |
+
|
| 310 |
+
<label class="block text-gray-700 mb-2">Amount</label>
|
| 311 |
+
<input v-model="amount" type="text" placeholder="Enter amount" class="w-full p-2 border rounded mb-4">
|
| 312 |
+
|
| 313 |
+
<div class="flex justify-between mb-4">
|
| 314 |
+
<button @click="setAmount(10)" class="bg-gray-200 text-gray-700 py-2 px-4 rounded">$10</button>
|
| 315 |
+
<button @click="setAmount(20)" class="bg-gray-200 text-gray-700 py-2 px-4 rounded">$20</button>
|
| 316 |
+
<button @click="setAmount(50)" class="bg-gray-200 text-gray-700 py-2 px-4 rounded">$50</button>
|
| 317 |
+
<button @click="setAmount(100)" class="bg-gray-200 text-gray-700 py-2 px-4 rounded">$100</button>
|
| 318 |
+
</div>
|
| 319 |
+
|
| 320 |
+
<button @click="handleButtonClick" class="bg-black text-white py-2 px-4 w-full rounded">
|
| 321 |
+
{{ buttonText }}
|
| 322 |
+
</button>
|
| 323 |
+
</div>
|
| 324 |
+
|
| 325 |
+
<script>
|
| 326 |
+
const { createApp } = Vue;
|
| 327 |
+
createApp({
|
| 328 |
+
data() {
|
| 329 |
+
return {
|
| 330 |
+
email: '',
|
| 331 |
+
amount: '',
|
| 332 |
+
showWarning: false,
|
| 333 |
+
isConfirmed: false
|
| 334 |
+
}
|
| 335 |
+
},
|
| 336 |
+
computed: {
|
| 337 |
+
buttonText() {
|
| 338 |
+
return this.isConfirmed ? 'Confirm' : 'Recharge';
|
| 339 |
+
}
|
| 340 |
+
},
|
| 341 |
+
methods: {
|
| 342 |
+
setAmount(value) {
|
| 343 |
+
this.amount = value;
|
| 344 |
+
},
|
| 345 |
+
handleButtonClick() {
|
| 346 |
+
if (!this.email || !this.amount) {
|
| 347 |
+
alert('Please enter both email and amount');
|
| 348 |
+
return;
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
if (!this.isConfirmed) {
|
| 352 |
+
this.showWarning = true;
|
| 353 |
+
this.isConfirmed = true;
|
| 354 |
+
return;
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
this.submitRecharge();
|
| 358 |
+
},
|
| 359 |
+
async submitRecharge() {
|
| 360 |
+
try {
|
| 361 |
+
const response = await fetch('/create-payment', {
|
| 362 |
+
method: 'POST',
|
| 363 |
+
headers: {
|
| 364 |
+
'Content-Type': 'application/json',
|
| 365 |
+
},
|
| 366 |
+
body: JSON.stringify({
|
| 367 |
+
email: this.email,
|
| 368 |
+
amount: parseFloat(this.amount)
|
| 369 |
+
})
|
| 370 |
+
});
|
| 371 |
+
|
| 372 |
+
const data = await response.json();
|
| 373 |
+
if (data.result === 100) {
|
| 374 |
+
window.location.href = data.payLink;
|
| 375 |
+
} else {
|
| 376 |
+
alert('Payment creation failed: ' + data.message);
|
| 377 |
+
}
|
| 378 |
+
} catch (error) {
|
| 379 |
+
alert('Error creating payment: ' + error.message);
|
| 380 |
+
}
|
| 381 |
+
}
|
| 382 |
+
}
|
| 383 |
+
}).mount('#app');
|
| 384 |
+
</script>
|
| 385 |
+
</body>
|
| 386 |
+
</html>`;
|
| 387 |
+
|
| 388 |
+
res.send(html);
|
| 389 |
+
});
|
| 390 |
+
|
| 391 |
+
// Update payment creation endpoint
|
| 392 |
+
app.post('/create-payment', async (req, res) => {
|
| 393 |
+
const { email, amount } = req.body;
|
| 394 |
+
|
| 395 |
+
if (!email || !amount) {
|
| 396 |
+
return res.status(400).json({ error: 'Email and amount are required' });
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
const baseUrl = `${req.protocol}://${req.get('host')}`;
|
| 400 |
+
const orderId = generateOrderId();
|
| 401 |
+
|
| 402 |
+
// Store email with orderId
|
| 403 |
+
orderTracking.set(orderId, {
|
| 404 |
+
email,
|
| 405 |
+
amount,
|
| 406 |
+
timestamp: Date.now()
|
| 407 |
+
});
|
| 408 |
+
|
| 409 |
+
const paymentData = {
|
| 410 |
+
merchant: MERCHANT_KEY, // Use the same key here
|
| 411 |
+
amount: amount,
|
| 412 |
+
currency: "USD",
|
| 413 |
+
lifeTime: 15,
|
| 414 |
+
feePaidByPayer: 1,
|
| 415 |
+
underPaidCover: "",
|
| 416 |
+
callbackUrl: `${baseUrl}/callback`,
|
| 417 |
+
returnUrl: "https://harmon.tr",
|
| 418 |
+
description: "",
|
| 419 |
+
orderId: orderId,
|
| 420 |
+
email: ""
|
| 421 |
+
};
|
| 422 |
+
|
| 423 |
+
try {
|
| 424 |
+
const response = await fetch('https://harmon.com.tr/cp.php?endpoint=/merchants/request', {
|
| 425 |
+
method: 'POST',
|
| 426 |
+
headers: {
|
| 427 |
+
'accept': '*/*',
|
| 428 |
+
'content-type': 'application/json',
|
| 429 |
+
'origin': 'https://harmon.com.tr',
|
| 430 |
+
'referer': 'https://harmon.com.tr/payment?id=create',
|
| 431 |
+
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/131.0.0.0'
|
| 432 |
+
},
|
| 433 |
+
body: JSON.stringify(paymentData)
|
| 434 |
+
});
|
| 435 |
+
|
| 436 |
+
const responseData = await response.json();
|
| 437 |
+
res.json(responseData);
|
| 438 |
+
} catch (error) {
|
| 439 |
+
console.error('Error creating payment:', error);
|
| 440 |
+
orderTracking.delete(orderId);
|
| 441 |
+
res.status(500).json({ error: 'Failed to create payment' });
|
| 442 |
+
}
|
| 443 |
+
});
|
| 444 |
+
|
| 445 |
+
// Update the callback endpoint
|
| 446 |
+
app.post('/callback', (req, res) => {
|
| 447 |
+
console.log('Callback received:', {
|
| 448 |
+
headers: req.headers,
|
| 449 |
+
body: req.body,
|
| 450 |
+
method: req.method
|
| 451 |
+
});
|
| 452 |
+
|
| 453 |
+
if (req.method !== 'POST') {
|
| 454 |
+
console.log('Invalid request method:', req.method);
|
| 455 |
+
return res.status(200).send('ok');
|
| 456 |
+
}
|
| 457 |
+
|
| 458 |
+
const hmacHeader = req.headers['hmac'];
|
| 459 |
+
if (!hmacHeader) {
|
| 460 |
+
console.log('Missing HMAC signature');
|
| 461 |
+
return res.status(200).send('ok');
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
const postData = JSON.stringify(req.body);
|
| 465 |
+
let data;
|
| 466 |
+
|
| 467 |
+
try {
|
| 468 |
+
data = JSON.parse(postData);
|
| 469 |
+
} catch (error) {
|
| 470 |
+
console.log('Invalid JSON data:', error);
|
| 471 |
+
return res.status(200).send('ok');
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
// Validate HMAC signature
|
| 475 |
+
if (!webhookHandler.validateHmacSignature(postData, hmacHeader, data.type)) {
|
| 476 |
+
console.log('Invalid HMAC signature:', {
|
| 477 |
+
received: hmacHeader,
|
| 478 |
+
data: postData
|
| 479 |
+
});
|
| 480 |
+
return res.status(200).send('ok');
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
// Process different payment statuses
|
| 484 |
+
switch (data.status) {
|
| 485 |
+
case 'Waiting':
|
| 486 |
+
webhookHandler.handleWaitingPayment(data);
|
| 487 |
+
break;
|
| 488 |
+
case 'Confirming':
|
| 489 |
+
webhookHandler.handleConfirmingPayment(data);
|
| 490 |
+
break;
|
| 491 |
+
case 'Paid':
|
| 492 |
+
webhookHandler.handlePaidPayment(data);
|
| 493 |
+
break;
|
| 494 |
+
case 'Expired':
|
| 495 |
+
webhookHandler.handleExpiredPayment(data);
|
| 496 |
+
break;
|
| 497 |
+
case 'Complete':
|
| 498 |
+
webhookHandler.handleCompletePayout(data);
|
| 499 |
+
break;
|
| 500 |
+
default:
|
| 501 |
+
console.log('Unhandled status:', data.status, data);
|
| 502 |
+
}
|
| 503 |
+
|
| 504 |
+
return res.status(200).send('ok');
|
| 505 |
+
});
|
| 506 |
+
|
| 507 |
+
// Health check endpoint
|
| 508 |
+
app.get('/health', (req, res) => {
|
| 509 |
+
res.status(200).json({ status: 'healthy' });
|
| 510 |
+
});
|
| 511 |
+
|
| 512 |
+
// Start the server
|
| 513 |
+
const PORT = process.env.PORT || 3002;
|
| 514 |
+
app.listen(PORT, () => {
|
| 515 |
+
console.log(`Payment server listening on port ${PORT}`);
|
| 516 |
+
});
|
| 517 |
+
|
| 518 |
+
// Error handling - add this before the general error handler
|
| 519 |
+
app.use((req, res, next) => {
|
| 520 |
+
// Handle 404 errors
|
| 521 |
+
res.redirect('/');
|
| 522 |
+
});
|
| 523 |
+
|
| 524 |
+
// General error handling
|
| 525 |
+
app.use((err, req, res, next) => {
|
| 526 |
+
console.error('Server error:', err);
|
| 527 |
+
res.status(500).json({ error: 'Internal server error' });
|
| 528 |
+
});
|
| 529 |
+
|
| 530 |
+
// Cleanup old entries periodically (every hour)
|
| 531 |
+
setInterval(() => {
|
| 532 |
+
const oneHourAgo = Date.now() - (60 * 60 * 1000);
|
| 533 |
+
for (const [orderId, data] of orderTracking.entries()) {
|
| 534 |
+
if (data.timestamp < oneHourAgo) {
|
| 535 |
+
orderTracking.delete(orderId);
|
| 536 |
+
}
|
| 537 |
+
}
|
| 538 |
+
}, 60 * 60 * 1000);
|