文件
freeshop/recharge.php
T
2026-08-16 17:03:10 +08:00

135 行
6.9 KiB
PHP
原始文件 Blame 文件历史

此文件含有不可见的 Unicode 字符
此文件含有人类无法区分的不可见的 Unicode 字符,但可以由计算机进行不同的处理。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
<?php
require __DIR__ . '/includes/init.php';
requireLogin('recharge.php');
$pageTitle = '积分充值';
$user = currentUser();
$rechargeEnabled = getSetting('recharge_enabled', '0') === '1';
$codepayEnabled = getSetting('pay_codepay_enabled', '0') === '1';
define('POINTS_PER_YUAN', 100);
$err = '';
$channels = [];
if ($codepayEnabled) {
$channels = [
'alipay' => ['name' => '支付宝', 'icon' => 'fab fa-alipay'],
'wxpay' => ['name' => '微信支付', 'icon' => 'fab fa-weixin'],
];
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $rechargeEnabled) {
verifyCsrf();
$points = (int) ($_POST['points'] ?? 0);
$channel = trim($_POST['channel'] ?? '');
if ($points <= 0) {
$err = '请选择或输入充值积分';
} elseif ($points % POINTS_PER_YUAN !== 0) {
$err = '积分需为 ' . POINTS_PER_YUAN . ' 的整数倍(' . POINTS_PER_YUAN . ' 积分 = 1 元)';
} elseif (!isset($channels[$channel])) {
$err = '请选择支付方式';
} else {
$amount = $points / POINTS_PER_YUAN;
$orderNo = 'RC' . date('Ymd') . strtoupper(substr(md5(uniqid($user['id'], true)), 0, 10));
db()->prepare(
'INSERT INTO ' . tn('recharge_orders') . ' (order_no,user_id,points,amount,channel,status,created_at) VALUES (?,?,?,?,?,?,NOW())'
)->execute([$orderNo, $user['id'], $points, $amount, $channel, 'pending']);
require_once __DIR__ . '/includes/Payment.php';
require_once __DIR__ . '/includes/PaymentCodePay.php';
$gw = new CodePayGateway();
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? '' : '';
$host = $_SERVER['HTTP_HOST'] ?? '';
$order = [
'order_no' => $orderNo,
'amount' => $amount,
'subject' => '积分充值 ' . $points . ' 积分',
'notify_url' => $scheme . $host . '/pay_notify.php',
'return_url' => $scheme . $host . '/pay_return.php?order=' . rawurlencode($orderNo),
'channel' => $channel,
];
$res = $gw->createOrder($order);
if (isset($_GET['debug'])) {
header('Content-Type: text/plain; charset=utf-8');
echo "【调试信息】\n";
echo "type = " . $gw->getType() . "\n";
echo "gateway = " . $gw->getGateway() . "\n";
echo "pid = " . $gw->getPid() . "\n";
echo "order_no= " . $orderNo . "\n";
echo "跳转 URL =\n" . ($res['url'] ?? '(空)') . "\n";
echo "\n(把上面这个 URL 复制到浏览器打开,应能进入支付页)\n";
exit;
}
if (!empty($res['url'])) {
header('Location: ' . $res['url']);
exit;
} elseif (!empty($res['form'])) {
echo $res['form'];
exit;
}
$err = '创建支付失败,请检查码支付配置或稍后重试。';
}
}
require 'includes/header.php';
?>
<style>
.recharge-wrap{max-width:640px;margin:0 auto}
.pkg-grid{display:grid;grid-template-columns:repeat(2,1fr);gap:12px;margin:12px 0}
.pkg{position:relative;cursor:pointer}
.pkg input{position:absolute;opacity:0}
.pkg span{display:block;padding:16px;border:2px solid var(--border);border-radius:10px;text-align:center;transition:all .15s}
.pkg span b{font-size:1.25rem}
.pkg span i{color:var(--muted);font-style:normal;font-size:.85rem}
.pkg input:checked + span{border-color:var(--primary,
.pay-channels{display:flex;gap:12px;flex-wrap:wrap;margin:8px 0 4px}
.pay-ch{display:flex;align-items:center;gap:8px;padding:12px 18px;border:2px solid var(--border);border-radius:10px;cursor:pointer;font-weight:500}
.pay-ch input{accent-color:var(--primary,
.pay-ch i{font-size:1.2rem}
.pkg-custom-input{width:100%;padding:9px 10px;border:1px solid var(--border);border-radius:8px;font-size:.92rem}
.alert-ok{background:
.alert-err{background:
</style>
<section class="section">
<h2 class="sec-title"><i class="fas fa-coins"></i> 积分充值</h2>
<?php if (!$rechargeEnabled): ?>
<p class="alert-err">积分充值暂未开放,请稍后再试或联系管理员。</p>
<?php elseif (!$codepayEnabled): ?>
<p class="alert-err">支付通道未配置,请联系管理员在后台「设置 → 支付接口配置」启用码支付。</p>
<?php else: ?>
<?php if ($err): ?><p class="alert-err"><?= h($err) ?></p><?php endif; ?>
<div class="recharge-wrap">
<p class="muted">当前汇率:<strong>1 元 = <?= POINTS_PER_YUAN ?> 积分</strong> | 当前余额:<strong><?= getUserPoints($user['id']) ?></strong> 积分</p>
<form method="post" action="" class="recharge-form">
<?= csrfField() ?>
<h3>选择充值积分</h3>
<div class="pkg-grid">
<label class="pkg"><input type="radio" name="points" value="100" required><span><b>100</b> 积分<br><i>¥1.00</i></span></label>
<label class="pkg"><input type="radio" name="points" value="500"><span><b>500</b> 积分<br><i>¥5.00</i></span></label>
<label class="pkg"><input type="radio" name="points" value="1000"><span><b>1000</b> 积分<br><i>¥10.00</i></span></label>
<label class="pkg custom"><input type="radio" name="points" value="custom"><span><b>自定义</b><br><i>输入积分</i></span></label>
</div>
<input type="number" name="points_custom" min="100" step="100" placeholder="自定义积分(100 的倍数)" class="pkg-custom-input" style="display:none">
<h3>支付方式</h3>
<div class="pay-channels">
<?php foreach ($channels as $c => $info): ?>
<label class="pay-ch"><input type="radio" name="channel" value="<?= $c ?>" required><i class="<?= $info['icon'] ?>"></i> <?= h($info['name']) ?></label>
<?php endforeach; ?>
</div>
<button type="submit" class="btn btn-primary" style="width:100%;margin-top:18px">立即充值</button>
</form>
</div>
<?php endif; ?>
</section>
<script>
document.querySelectorAll('input[name=points]').forEach(function (el) {
el.addEventListener('change', function () {
var custom = document.querySelector('input[name=points][value=custom]');
document.querySelector('.pkg-custom-input').style.display = (custom && custom.checked) ? 'block' : 'none';
});
});
document.querySelector('.recharge-form').addEventListener('submit', function (e) {
var customRadio = document.querySelector('input[name=points][value=custom]');
if (customRadio.checked) {
var v = parseInt(document.querySelector('.pkg-custom-input').value, 10);
if (!v || v < 100 || v % 100 !== 0) { alert('请输入 100 的倍数'); e.preventDefault(); return; }
customRadio.value = v;
}
});
</script>
<?php require 'includes/footer.php'; ?>