fixes #142 Resource path: - Admin: /secure - Payment Cashier: /payment Eliminate ambiguity: */static/ => */assets/
507 lines
18 KiB
JavaScript
507 lines
18 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
let countdownTimer = null;
|
|
let statusCheckTimer = null;
|
|
let totalSeconds = null;
|
|
let paymentConfig = {};
|
|
let i18nInitialized = false;
|
|
|
|
function initI18n() {
|
|
if (i18nInitialized) {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
let currentLang = localStorage.getItem('payment_language') || navigator.language.split('-')[0] || 'en';
|
|
if (currentLang !== 'zh' && currentLang !== 'en') {
|
|
currentLang = 'en';
|
|
}
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
i18next.init({
|
|
lng: currentLang,
|
|
debug: false,
|
|
resources: {}
|
|
}, function (err, t) {
|
|
if (err) {
|
|
console.error('i18next initialization failed:', err);
|
|
reject(err);
|
|
return;
|
|
}
|
|
|
|
// 只加载当前需要的语言文件
|
|
fetch('/payment/assets/locales/' + currentLang + '.json')
|
|
.then(function (r) {
|
|
return r.json();
|
|
})
|
|
.then(function (translations) {
|
|
i18next.addResourceBundle(currentLang, 'translation', translations);
|
|
i18nInitialized = true;
|
|
updateContent();
|
|
const languageSwitcher = document.getElementById('languageSwitcher');
|
|
if (languageSwitcher) {
|
|
languageSwitcher.value = currentLang;
|
|
}
|
|
resolve();
|
|
})
|
|
.catch(function (error) {
|
|
console.error('Failed to load language file:', error);
|
|
reject(error);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function getPaymentConfig() {
|
|
const container = document.querySelector('.payment-container');
|
|
if (!container) return {};
|
|
|
|
return {
|
|
token: container.getAttribute('data-token') || 'USDT',
|
|
network: container.getAttribute('data-network') || 'TRC20',
|
|
networkName: container.getAttribute('data-network-name') || 'TRON',
|
|
warningToken: container.getAttribute('data-warning-token') || 'TRX'
|
|
};
|
|
}
|
|
|
|
function replacePlaceholders(text) {
|
|
const config = getPaymentConfig();
|
|
return text
|
|
.replace(/\{\{token\}\}/g, config.token)
|
|
.replace(/\{\{network\}\}/g, config.network)
|
|
.replace(/\{\{networkName\}\}/g, config.networkName)
|
|
.replace(/\{\{warningToken\}\}/g, config.warningToken);
|
|
}
|
|
|
|
function updateContent() {
|
|
document.querySelectorAll('[data-i18n]').forEach(function (element) {
|
|
const key = element.getAttribute('data-i18n');
|
|
if (key.startsWith('[')) {
|
|
const matches = key.match(/\[(.+?)\](.+)/);
|
|
if (matches) {
|
|
const attr = matches[1];
|
|
const transKey = matches[2];
|
|
let translation = i18next.t(transKey);
|
|
translation = replacePlaceholders(translation);
|
|
if (attr === 'html') {
|
|
element.innerHTML = translation;
|
|
} else {
|
|
element.setAttribute(attr, translation);
|
|
}
|
|
}
|
|
} else {
|
|
let translation = i18next.t(key);
|
|
translation = replacePlaceholders(translation);
|
|
if (element.tagName === 'INPUT' || element.tagName === 'TEXTAREA') {
|
|
element.placeholder = translation;
|
|
} else {
|
|
element.innerHTML = translation;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function changeLanguage(lang) {
|
|
// 检查是否已经加载了该语言包
|
|
const hasLanguage = i18next.hasResourceBundle(lang, 'translation');
|
|
|
|
if (!hasLanguage) {
|
|
// 如果没有加载,先加载该语言文件
|
|
fetch('/payment/assets/locales/' + lang + '.json')
|
|
.then(function (r) {
|
|
return r.json();
|
|
})
|
|
.then(function (translations) {
|
|
i18next.addResourceBundle(lang, 'translation', translations);
|
|
return i18next.changeLanguage(lang);
|
|
})
|
|
.then(function () {
|
|
localStorage.setItem('payment_language', lang);
|
|
updateContent();
|
|
})
|
|
.catch(function (error) {
|
|
console.error('Failed to load language file:', error);
|
|
});
|
|
} else {
|
|
// 如果已经加载,直接切换
|
|
i18next.changeLanguage(lang, function (err, t) {
|
|
if (err) {
|
|
console.error('Language change failed:', err);
|
|
return;
|
|
}
|
|
localStorage.setItem('payment_language', lang);
|
|
updateContent();
|
|
});
|
|
}
|
|
}
|
|
|
|
function initConfig(config) {
|
|
paymentConfig = config;
|
|
totalSeconds = config.expire;
|
|
}
|
|
|
|
function generateQRCode() {
|
|
const qrContainer = document.getElementById('qrcode');
|
|
const containerWidth = qrContainer.offsetWidth;
|
|
const qrSize = Math.min(containerWidth - 10, 150);
|
|
|
|
$('#qrcode').qrcode({
|
|
text: paymentConfig.address,
|
|
width: qrSize,
|
|
height: qrSize,
|
|
foreground: "#000000",
|
|
background: "#ffffff",
|
|
typeNumber: -1
|
|
});
|
|
}
|
|
|
|
function copyToClipboard(text, element, isButton) {
|
|
if (!text) return;
|
|
|
|
navigator.clipboard.writeText(text).then(function () {
|
|
showCopySuccess(element, isButton);
|
|
}).catch(function (err) {
|
|
console.error('复制失败: ', err);
|
|
fallbackCopy(text, element, isButton);
|
|
});
|
|
}
|
|
|
|
function showCopySuccess(element, isButton) {
|
|
const t = window.i18next ? window.i18next.t.bind(window.i18next) : function (key) {
|
|
const translations = {
|
|
'payment.copied': '已复制!',
|
|
'payment.copySuccess': '✓ 已复制!'
|
|
};
|
|
return translations[key] || key;
|
|
};
|
|
|
|
if (isButton) {
|
|
const originalText = element.textContent;
|
|
element.textContent = t('payment.copied');
|
|
element.style.background = '#48bb78';
|
|
setTimeout(function () {
|
|
element.textContent = originalText;
|
|
element.style.background = '#667eea';
|
|
}, 2000);
|
|
} else {
|
|
const originalText = element.textContent;
|
|
const originalColor = element.style.color;
|
|
element.textContent = t('payment.copySuccess');
|
|
element.style.color = '#48bb78';
|
|
setTimeout(function () {
|
|
element.textContent = originalText;
|
|
element.style.color = originalColor;
|
|
}, 2000);
|
|
}
|
|
}
|
|
|
|
function fallbackCopy(text, element, isButton) {
|
|
const t = window.i18next ? window.i18next.t.bind(window.i18next) : function (key) {
|
|
return '复制失败,请手动复制';
|
|
};
|
|
|
|
const textArea = document.createElement('textarea');
|
|
textArea.value = text;
|
|
textArea.style.position = 'fixed';
|
|
textArea.style.opacity = '0';
|
|
document.body.appendChild(textArea);
|
|
textArea.select();
|
|
try {
|
|
document.execCommand('copy');
|
|
showCopySuccess(element, isButton);
|
|
} catch (err) {
|
|
alert(t('payment.copyFailed'));
|
|
}
|
|
document.body.removeChild(textArea);
|
|
}
|
|
|
|
function copyAddress() {
|
|
const btn = document.querySelector('.copy-btn');
|
|
copyToClipboard(paymentConfig.address, btn, true);
|
|
}
|
|
|
|
function copyAmount() {
|
|
const amountEl = document.getElementById('payAmount');
|
|
copyToClipboard(paymentConfig.amount, amountEl, false);
|
|
}
|
|
|
|
function startCountdown() {
|
|
const minutesEl = document.getElementById('minutes');
|
|
const secondsEl = document.getElementById('seconds');
|
|
|
|
function updateDisplay() {
|
|
const minutes = Math.floor(totalSeconds / 60);
|
|
const seconds = totalSeconds % 60;
|
|
|
|
if (totalSeconds <= 0) {
|
|
minutesEl.textContent = '00';
|
|
secondsEl.textContent = '00';
|
|
} else {
|
|
minutesEl.textContent = minutes.toString().padStart(2, '0');
|
|
secondsEl.textContent = seconds.toString().padStart(2, '0');
|
|
}
|
|
|
|
if (totalSeconds <= 300) {
|
|
document.querySelector('.countdown-banner').style.background = 'linear-gradient(135deg, #ff4757, #ff3838)';
|
|
}
|
|
|
|
if (totalSeconds <= 60) {
|
|
document.querySelector('.countdown-banner').style.animation = 'urgentBlink 1s infinite';
|
|
}
|
|
}
|
|
|
|
countdownTimer = setInterval(function () {
|
|
totalSeconds--;
|
|
updateDisplay();
|
|
|
|
if (totalSeconds <= 0) {
|
|
clearInterval(countdownTimer);
|
|
clearInterval(statusCheckTimer);
|
|
showTimeoutMessage();
|
|
}
|
|
}, 1000);
|
|
|
|
updateDisplay();
|
|
}
|
|
|
|
function showTimeoutMessage() {
|
|
const t = window.i18next ? window.i18next.t.bind(window.i18next) : function (key) {
|
|
const translations = {
|
|
'payment.paymentTimeout': '支付时间已过期',
|
|
'payment.timeoutMessage': '很抱歉,支付时间已超时。<br>请重新发起支付或联系客服处理。',
|
|
'payment.returnToMerchant': '返回商户平台'
|
|
};
|
|
return translations[key] || key;
|
|
};
|
|
|
|
const overlay = document.createElement('div');
|
|
overlay.style.cssText = `
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
background: rgba(0, 0, 0, 0.8);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
z-index: 9999;
|
|
`;
|
|
|
|
const modal = document.createElement('div');
|
|
modal.style.cssText = `
|
|
background: white;
|
|
padding: 30px;
|
|
border-radius: 12px;
|
|
text-align: center;
|
|
max-width: 400px;
|
|
width: 90%;
|
|
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
|
|
`;
|
|
|
|
modal.innerHTML = `
|
|
<div style="font-size: 48px; margin-bottom: 20px;">⏰</div>
|
|
<h3 style="color: #e53e3e; margin-bottom: 15px;">${t('payment.paymentTimeout')}</h3>
|
|
<p style="color: #666; margin-bottom: 25px; line-height: 1.5;">
|
|
${t('payment.timeoutMessage')}
|
|
</p>
|
|
<button onclick="location.href='${paymentConfig.return_url}'" style="
|
|
background: #667eea;
|
|
color: white;
|
|
border: none;
|
|
padding: 12px 24px;
|
|
border-radius: 6px;
|
|
cursor: pointer;
|
|
font-size: 14px;
|
|
margin-right: 10px;
|
|
">${t('payment.returnToMerchant')}</button>
|
|
`;
|
|
|
|
overlay.appendChild(modal);
|
|
document.body.appendChild(overlay);
|
|
}
|
|
|
|
function showWaitingConfirmation(data) {
|
|
if (document.getElementById('waiting-overlay')) {
|
|
return;
|
|
}
|
|
|
|
const t = window.i18next ? window.i18next.t.bind(window.i18next) : function (key) {
|
|
const translations = {
|
|
'payment.waitingConfirmation': '正在等待网络确认',
|
|
'payment.confirmationMessage': '检测到付款请求,区块网络确认中...',
|
|
'payment.confirmationNote': '请耐心等待,系统会自动检测确认状态',
|
|
'payment.estimatedTime': '预计确认时间:1-3分钟'
|
|
};
|
|
return translations[key] || key;
|
|
};
|
|
|
|
const overlay = document.createElement('div');
|
|
overlay.id = 'waiting-overlay';
|
|
overlay.style.cssText = `
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
background: rgba(0, 0, 0, 0.8);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
z-index: 9999;
|
|
`;
|
|
|
|
const modal = document.createElement('div');
|
|
modal.style.cssText = `
|
|
background: white;
|
|
padding: 30px;
|
|
border-radius: 12px;
|
|
text-align: center;
|
|
max-width: 400px;
|
|
width: 90%;
|
|
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
|
|
`;
|
|
|
|
modal.innerHTML = `
|
|
<div style="font-size: 48px; margin-bottom: 20px; animation: spin 2s linear infinite;">⏳</div>
|
|
<h3 style="color: #667eea; margin-bottom: 15px;">${t('payment.waitingConfirmation')}</h3>
|
|
<p style="color: #666; margin-bottom: 20px; line-height: 1.5;">
|
|
<span style="color: #059669; font-weight: 600; background: linear-gradient(135deg, #ecfdf5, #f0fdf4); padding: 8px 12px; border-radius: 6px; border-left: 3px solid #10b981;">${t('payment.confirmationMessage')}</span><br>
|
|
<small style="color: #999;">${t('payment.confirmationNote')}</small>
|
|
</p>
|
|
<div style="display: flex; justify-content: center; margin-bottom: 20px;">
|
|
<div style="width: 40px; height: 40px; border: 3px solid #f3f3f3; border-top: 3px solid #667eea; border-radius: 50%; animation: spin 1s linear infinite;"></div>
|
|
</div>
|
|
<p style="color: #999; font-size: 12px;">
|
|
${t('payment.estimatedTime')}
|
|
</p>
|
|
`;
|
|
|
|
const style = document.createElement('style');
|
|
style.textContent = `
|
|
@keyframes spin {
|
|
0% { transform: rotate(0deg); }
|
|
100% { transform: rotate(360deg); }
|
|
}
|
|
`;
|
|
document.head.appendChild(style);
|
|
|
|
overlay.appendChild(modal);
|
|
document.body.appendChild(overlay);
|
|
|
|
statusCheckTimer = setInterval(checkPaymentStatus, 5000);
|
|
}
|
|
|
|
function showSuccessMessage(data) {
|
|
const waitingOverlay = document.getElementById('waiting-overlay');
|
|
if (waitingOverlay) {
|
|
waitingOverlay.remove();
|
|
}
|
|
|
|
const t = window.i18next ? window.i18next.t.bind(window.i18next) : function (key) {
|
|
const translations = {
|
|
'payment.paymentSuccess': '支付成功!',
|
|
'payment.transactionHash': '您的支付已确认,交易哈希:',
|
|
'payment.processing': '处理中...',
|
|
'payment.returnToMerchant': '返回商户平台'
|
|
};
|
|
return translations[key] || key;
|
|
};
|
|
|
|
const overlay = document.createElement('div');
|
|
overlay.style.cssText = `
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
background: rgba(0, 0, 0, 0.8);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
z-index: 9999;
|
|
`;
|
|
|
|
const modal = document.createElement('div');
|
|
modal.style.cssText = `
|
|
background: white;
|
|
padding: 30px;
|
|
border-radius: 12px;
|
|
text-align: center;
|
|
max-width: 400px;
|
|
width: 90%;
|
|
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
|
|
`;
|
|
|
|
modal.innerHTML = `
|
|
<div style="font-size: 48px; margin-bottom: 20px;">✅</div>
|
|
<h3 style="color: #48bb78; margin-bottom: 15px;">${t('payment.paymentSuccess')}</h3>
|
|
<p style="color: #666; margin-bottom: 20px; line-height: 1.5;">
|
|
${t('payment.transactionHash')}<br>
|
|
<code style="background: #e6f0fa; color: #2563eb; padding: 4px 8px; border-radius: 4px; font-size: 12px; word-break: break-all;">
|
|
${data.trade_hash || t('payment.processing')}
|
|
</code>
|
|
</p>
|
|
<button onclick="location.href='${data.return_url || '/'}'" style="
|
|
background: #48bb78;
|
|
color: white;
|
|
border: none;
|
|
padding: 12px 24px;
|
|
border-radius: 6px;
|
|
cursor: pointer;
|
|
font-size: 14px;
|
|
">${t('payment.returnToMerchant')}</button>
|
|
`;
|
|
|
|
overlay.appendChild(modal);
|
|
document.body.appendChild(overlay);
|
|
|
|
if (countdownTimer) clearInterval(countdownTimer);
|
|
if (statusCheckTimer) clearInterval(statusCheckTimer);
|
|
}
|
|
|
|
function checkPaymentStatus() {
|
|
$.ajax({
|
|
type: "GET",
|
|
dataType: "json",
|
|
url: "/pay/check-status/" + paymentConfig.trade_id,
|
|
success: function (data) {
|
|
if (data.status === 1) {
|
|
return setTimeout(checkPaymentStatus, 5000);
|
|
}
|
|
if (data.status === 2) {
|
|
return showSuccessMessage(data);
|
|
}
|
|
if (data.status === 3) {
|
|
return showTimeoutMessage();
|
|
}
|
|
if (data.status === 5) {
|
|
return showWaitingConfirmation(data);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function init(config) {
|
|
initConfig(config);
|
|
initI18n().then(function () {
|
|
generateQRCode();
|
|
startCountdown();
|
|
setTimeout(checkPaymentStatus, 2000);
|
|
}).catch(function (err) {
|
|
console.error('Failed to initialize i18n:', err);
|
|
generateQRCode();
|
|
startCountdown();
|
|
setTimeout(checkPaymentStatus, 2000);
|
|
});
|
|
}
|
|
|
|
window.copyAddress = copyAddress;
|
|
window.copyAmount = copyAmount;
|
|
window.changeLanguage = changeLanguage;
|
|
window.Payment = {
|
|
init: init
|
|
};
|
|
})();
|
|
|