diff --git a/assets/js/cms-download.js b/assets/js/cms-download.js index eec22a88..47d1ad1d 100644 --- a/assets/js/cms-download.js +++ b/assets/js/cms-download.js @@ -140,13 +140,29 @@ title = tt('need_login', '请先登录'); msg = tt('login_hint', '登录后即可继续下载,访客不可使用此资源。'); extra = '' + tt('go_login', '前往登录') + ''; - } else if (code === 'aksrt_credits_insufficient') { + } else if (code === 'aksrt_need_purchase') { + title = tt('need_purchase', '请先购买'); + msg = msg || tt('purchase_hint', '请先完成购买后再下载该资源。'); + extra = ''; + } else if (code === 'aksrt_need_vip') { + title = tt('need_vip', '会员专属'); + msg = msg || tt('vip_hint', '该资源仅限会员购买/下载。'); + if (data && data.vip_url) { + extra = '' + tt('go_vip', '开通会员') + ''; + } + } else if (code === 'aksrt_credits_insufficient' || code === 'aksrt_balance_insufficient') { title = tt('credit_low', '积分不够'); const balance = typeof (data && data.balance) === 'number' ? data.balance : 0; const cost = typeof (data && data.cost) === 'number' ? data.cost : 0; msg = tt('credit_hint', '当前余额 ' + balance + ' 积分,需要 ' + cost + ' 积分。可通过签到、阅读、邀请好友等方式获取积分。' ); + if (data && data.credits_url) { + extra = '' + tt('my_points', '我的积分') + ''; + } + } else if (code === 'aksrt_captcha_failed') { + title = tt('captcha_fail', '验证失败'); + msg = msg || tt('captcha_retry', '人机验证失败,请重试。'); } else if (data && data.login_url) { extra = '' + tt('go_login', '前往登录') + ''; } @@ -157,6 +173,15 @@ (extra ? '
' + extra + '
' : ''), returnFocusEl ); + + const openCashierBtn = document.querySelector('[data-aksrt-open-cashier]'); + if (openCashierBtn) { + openCashierBtn.addEventListener('click', function () { + const pid = parseInt(openCashierBtn.getAttribute('data-aksrt-open-cashier'), 10) || 0; + close(); + if (pid) openCashier(pid, returnFocusEl); + }); + } } // ===== 成功渲染 ===== @@ -303,6 +328,189 @@ renderError('', { balance: balance, cost: cost }, button, 'aksrt_credits_insufficient'); } + // ===== 收银台(内容购买) ===== + function restHeaders() { + return { + 'Content-Type': 'application/json', + 'X-WP-Nonce': window.aksrtData.restNonce || '', + }; + } + + function openCashier(postId, returnFocusEl) { + if (!postId) return; + open( + '

' + tt('cashier_loading', '确认购买') + '

' + + '

' + tt('loading', '加载中…') + '

', + returnFocusEl + ); + + fetch(REST_BASE + '/pay/content/' + postId, { + method: 'GET', + credentials: 'same-origin', + headers: restHeaders(), + }) + .then(function (resp) { return resp.json().then(function (j) { return { ok: resp.ok, status: resp.status, json: j }; }); }) + .then(function (r) { + if (!r.ok || (r.json && r.json.code)) { + const code = r.json && r.json.code ? r.json.code : ''; + const data = (r.json && r.json.data) || {}; + if (code === 'aksrt_need_login' || r.status === 401) { + renderError(r.json.message || '', data, returnFocusEl, 'aksrt_need_login'); + return; + } + if (code === 'aksrt_need_vip') { + renderError(r.json.message || '', data, returnFocusEl, 'aksrt_need_vip'); + return; + } + if (code === 'aksrt_already_paid') { + window.location.reload(); + return; + } + renderError((r.json && r.json.message) || tt('default_err', '请稍后重试'), data, returnFocusEl, code); + return; + } + renderCashier(r.json, returnFocusEl); + }) + .catch(function () { + renderError(tt('network_err', '网络错误,请稍后重试'), {}, returnFocusEl); + }); + } + + function renderCashier(preview, returnFocusEl) { + if (!preview) return; + if (preview.need_login) { + renderError('', { login_url: preview.login_url }, returnFocusEl, 'aksrt_need_login'); + return; + } + if (preview.need_vip) { + renderError('', { vip_url: preview.vip_url }, returnFocusEl, 'aksrt_need_vip'); + return; + } + if (preview.has_paid_access || preview.is_free) { + window.location.reload(); + return; + } + + const isPoints = preview.pay_modo === 'points'; + const amountLabel = escapeHtml(preview.amount_label || ''); + const baseLabel = escapeHtml(preview.base_label || ''); + const title = escapeHtml(preview.title || ''); + const typeLabel = escapeHtml(preview.pay_type_label || ''); + const balance = typeof preview.balance === 'number' ? preview.balance : 0; + const methods = Array.isArray(preview.payment_methods) ? preview.payment_methods : []; + + let priceRows = ''; + if (!preview.pay_limit) { + priceRows += '
' + tt('price', '价格') + '' + baseLabel + '
'; + } + if (preview.amount !== preview.base) { + priceRows += '
' + tt('vip_price', '会员价') + '' + amountLabel + '
'; + } + priceRows += '
' + tt('pay_amount', '支付金额') + '' + amountLabel + '
'; + + let body = ''; + body += '
' + typeLabel + '' + title + '
'; + body += '
' + priceRows + '
'; + + if (isPoints) { + body += '
' + tt('my_points', '我的积分') + '' + balance + '
'; + if (!preview.balance_enough) { + body += '
' + tt('points_short', '抱歉,您的积分不足,暂时无法支付') + '
'; + body += '
'; + if (preview.credits_url) { + body += '' + tt('my_points', '我的积分') + ''; + } + body += '
'; + open('

' + tt('confirm_buy', '确认购买') + '

' + body, returnFocusEl); + return; + } + body += '
'; + body += ''; + body += '
'; + } else { + let methodHtml = ''; + methods.forEach(function (m, idx) { + const id = escapeAttr(m.id || ''); + const name = escapeHtml(m.name || id); + methodHtml += ''; + }); + if (methodHtml) { + body += '
' + tt('pay_method', '支付方式') + '
' + methodHtml + '
'; + } + body += '
'; + body += ''; + body += '
'; + } + + open('

' + tt('confirm_buy', '确认购买') + '

' + body, returnFocusEl); + + const submitBtn = document.querySelector('[data-aksrt-pay-submit]'); + if (submitBtn) { + submitBtn.addEventListener('click', function () { + const mode = submitBtn.getAttribute('data-mode') || (isPoints ? 'points' : 'cash'); + let paymentMethod = 'balance'; + const checked = document.querySelector('input[name="aksrt_pay_method"]:checked'); + if (checked) paymentMethod = checked.value; + submitPurchase(preview.post_id, mode, paymentMethod, submitBtn, returnFocusEl); + }); + } + } + + function submitPurchase(postId, mode, paymentMethod, button, returnFocusEl) { + if (!postId) return; + const original = button.innerHTML; + button.disabled = true; + button.innerHTML = tt('processing', '处理中…'); + + const payload = { mode: mode }; + if (mode === 'cash') payload.payment_method = paymentMethod || 'balance'; + + fetch(REST_BASE + '/pay/content/' + postId, { + method: 'POST', + credentials: 'same-origin', + headers: restHeaders(), + body: JSON.stringify(payload), + }) + .then(function (resp) { return resp.json().then(function (j) { return { ok: resp.ok, status: resp.status, json: j }; }); }) + .then(function (r) { + button.disabled = false; + button.innerHTML = original; + if (!r.ok || (r.json && r.json.code && !r.json.success)) { + const code = r.json && r.json.code ? r.json.code : ''; + const data = (r.json && r.json.data) || {}; + if (code === 'aksrt_already_paid') { + window.location.reload(); + return; + } + renderError((r.json && r.json.message) || tt('default_err', '请稍后重试'), data, returnFocusEl, code); + return; + } + const data = (r.json && r.json.data) ? r.json.data : r.json; + if (data && data.redirect && (data.payment_url || data.qr_content)) { + if (data.payment_url) { + window.location.href = data.payment_url; + return; + } + open( + '

' + tt('scan_pay', '扫码支付') + '

' + + '

' + escapeHtml(data.message || tt('scan_hint', '请使用手机扫码完成支付,支付后刷新页面。')) + '

' + + '

' + escapeHtml(data.qr_content || '') + '

' + + '
', + returnFocusEl + ); + const reloadBtn = document.querySelector('[data-aksrt-reload]'); + if (reloadBtn) reloadBtn.addEventListener('click', function () { window.location.reload(); }); + return; + } + window.location.reload(); + }) + .catch(function () { + button.disabled = false; + button.innerHTML = original; + renderError(tt('network_err', '网络错误,请稍后重试'), {}, returnFocusEl); + }); + } + // ===== 事件绑定 ===== document.addEventListener('click', function (e) { const lowBtn = e.target.closest('[data-action="aksrt-need-credits"]'); @@ -312,6 +520,28 @@ return; } + const buyBtn = e.target.closest('[data-action="aksrt-need-purchase"], [data-aksrt-pay-content]'); + if (buyBtn) { + e.preventDefault(); + const postId = parseInt( + buyBtn.getAttribute('data-aksrt-pay-content') || + buyBtn.getAttribute('data-post-id') || + (buyBtn.closest('[data-post-id]') && buyBtn.closest('[data-post-id]').getAttribute('data-post-id')) || + '0', + 10 + ); + if (postId) openCashier(postId, buyBtn); + return; + } + + const cashierLink = e.target.closest('.cashier-link'); + if (cashierLink && cashierLink.hasAttribute('data-aksrt-pay-content')) { + e.preventDefault(); + const postId = parseInt(cashierLink.getAttribute('data-aksrt-pay-content') || '0', 10); + if (postId) openCashier(postId, cashierLink); + return; + } + const btn = e.target.closest('[data-action="aksrt-download"]'); if (!btn) return; if (btn.disabled || btn.getAttribute('aria-disabled') === 'true') return; @@ -338,9 +568,25 @@ '.aksrt-dl-modal__copy:hover{background:#dbeafe!important;border-color:#93c5fd!important;color:#1d4ed8!important;-webkit-text-fill-color:#1d4ed8!important;}' + '.aksrt-dl-modal__copy-text{display:inline-flex!important;align-items:center!important;justify-content:center!important;position:relative!important;z-index:2!important;color:#2563eb!important;-webkit-text-fill-color:#2563eb!important;font-size:13px!important;font-weight:700!important;line-height:1!important;font-family:inherit!important;text-indent:0!important;opacity:1!important;visibility:visible!important;white-space:nowrap!important;}' + '.aksrt-dl-modal__copy:hover .aksrt-dl-modal__copy-text{color:#1d4ed8!important;-webkit-text-fill-color:#1d4ed8!important;}' + - '.aksrt-dl-modal__actions{display:flex;align-items:center;gap:12px;padding-top:4px;}' + - '.aksrt-dl-modal__download{min-height:40px;padding:0 18px;border-radius:9px;display:inline-flex;align-items:center;justify-content:center;text-decoration:none;}' + + '.aksrt-dl-modal__actions{display:flex;align-items:center;gap:12px;padding-top:4px;flex-wrap:wrap;}' + + '.aksrt-dl-modal__download{min-height:40px;padding:0 18px;border-radius:9px;display:inline-flex;align-items:center;justify-content:center;text-decoration:none;border:0;cursor:pointer;}' + '.aksrt-dl-modal__balance{font-size:13px;color:#6b7280;margin-left:auto;white-space:nowrap;}' + + '.aksrt-cashier__product{margin:0 0 12px;font-size:14px;line-height:1.5;}' + + '.aksrt-cashier__tag{display:inline-block;margin-right:8px;padding:2px 8px;border-radius:6px;background:#eff6ff;color:#1d4ed8;font-size:12px;font-weight:700;}' + + '.aksrt-cashier__box{background:#f8fafc;border:1px solid #e5e7eb;border-radius:10px;padding:10px 12px;margin:0 0 12px;}' + + '.aksrt-cashier__row{display:flex;justify-content:space-between;align-items:center;gap:12px;font-size:13px;color:#6b7280;padding:4px 0;}' + + '.aksrt-cashier__row b{color:#111827;font-size:15px;}' + + '.aksrt-cashier__row b.is-sale{color:#dc2626;}' + + '.aksrt-cashier__row b.is-ok{color:#059669;}' + + '.aksrt-cashier__warn{background:#fef2f2;color:#b91c1c;border-radius:8px;padding:10px 12px;margin:0 0 12px;font-size:13px;}' + + '.aksrt-cashier__methods{margin:0 0 14px;}' + + '.aksrt-cashier__methods-title{font-size:13px;font-weight:600;color:#374151;margin:0 0 8px;}' + + '.aksrt-cashier__method{display:flex;align-items:center;gap:8px;padding:8px 10px;border:1px solid #e5e7eb;border-radius:8px;margin:0 0 6px;cursor:pointer;font-size:13px;}' + + '.resource-download-box__attrs{list-style:none;margin:10px 0 0;padding:0;display:grid;gap:6px;}' + + '.resource-download-box__attrs-item{display:flex;justify-content:space-between;gap:12px;font-size:12px;color:#6b7280;}' + + '.resource-download-box__attrs-name{flex:0 0 auto;}' + + '.resource-download-box__attrs-value{color:#111827;text-align:right;}' + + '.resource-download-box__paid-badge{background:#ecfdf5!important;color:#047857!important;}' + '@media(max-width:480px){.aksrt-dl-modal__panel{padding:24px 20px 22px;}.aksrt-dl-modal__row{grid-template-columns:1fr 72px;gap:8px 12px;}.aksrt-dl-modal__label{grid-column:1 / -1;}.aksrt-dl-modal__code{font-size:17px;}.aksrt-dl-modal__actions{align-items:flex-start;flex-direction:column;}.aksrt-dl-modal__download{width:100%;}.aksrt-dl-modal__balance{margin-left:0;}}' + 'body.aksrt-dl-modal-open{overflow:hidden;}'; document.head.appendChild(style); diff --git a/functions.php b/functions.php index b87578bd..ef40e883 100644 --- a/functions.php +++ b/functions.php @@ -174,6 +174,9 @@ if (class_exists('AKSRT_Creator_Revenue')) { if (class_exists('AKSRT_Download')) { AKSRT_Download::get_instance()->init(); } +if (class_exists('AKSRT_Content_Pay')) { + AKSRT_Content_Pay::get_instance()->init(); +} // Download State & Docs Presenter are pure service singletons (no hooks) if (class_exists('AKSRT_Download_State')) { AKSRT_Download_State::get_instance(); diff --git a/inc/bootstrap.php b/inc/bootstrap.php index ea49c56f..94933f55 100644 --- a/inc/bootstrap.php +++ b/inc/bootstrap.php @@ -217,6 +217,9 @@ require_once AKSRT_DIR . '/inc/classes/class-aksrt-invite.php'; // 加载下载服务(CMS / 资源 token 流程) require_once AKSRT_DIR . '/inc/classes/class-aksrt-download.php'; +// 付费内容收银台(post/_aksrt_pay → resource 订单) +require_once AKSRT_DIR . '/inc/classes/class-aksrt-content-pay.php'; + // 下载状态统一解析器 require_once AKSRT_DIR . '/inc/classes/class-aksrt-download-state.php'; diff --git a/inc/classes/class-aksrt-content-pay.php b/inc/classes/class-aksrt-content-pay.php new file mode 100644 index 00000000..67132d40 --- /dev/null +++ b/inc/classes/class-aksrt-content-pay.php @@ -0,0 +1,582 @@ +\\d+)', [ + [ + 'methods' => 'GET', + 'callback' => [$this, 'rest_preview'], + 'permission_callback' => [$this, 'check_rest_nonce'], + ], + [ + 'methods' => 'POST', + 'callback' => [$this, 'rest_purchase'], + 'permission_callback' => [$this, 'check_rest_nonce'], + ], + ]); + } + + /** + * REST nonce for logged-in users (wp_rest). Guests get 401 on purchase paths. + * + * @param WP_REST_Request $request + * @return true|WP_Error + */ + public function check_rest_nonce(WP_REST_Request $request) { + $nonce = $request->get_header('x-wp-nonce'); + if (!$nonce) { + return new WP_Error('aksrt_rest_no_nonce', __('缺少安全令牌', 'aksrt'), ['status' => 403]); + } + if (!wp_verify_nonce($nonce, 'wp_rest')) { + return new WP_Error('aksrt_rest_bad_nonce', __('安全令牌已过期,请刷新页面', 'aksrt'), ['status' => 403]); + } + return true; + } + + /** + * GET /pay/content/{id} + */ + public function rest_preview(WP_REST_Request $request) { + $post_id = (int) $request['id']; + $uid = get_current_user_id(); + $preview = $this->preview($post_id, $uid); + if (is_wp_error($preview)) { + return $preview; + } + return new WP_REST_Response($preview, 200); + } + + /** + * POST /pay/content/{id} + * body: { mode?: points|cash, payment_method?: balance|alipay|... } + */ + public function rest_purchase(WP_REST_Request $request) { + $post_id = (int) $request['id']; + $uid = get_current_user_id(); + if ($uid <= 0) { + $post = get_post($post_id); + return new WP_Error('aksrt_need_login', __('请先登录后再购买。', 'aksrt'), [ + 'status' => 401, + 'login_url' => wp_login_url($post ? get_permalink($post) : home_url('/')), + ]); + } + + $params = $request->get_json_params(); + if (!is_array($params)) { + $params = $request->get_params(); + } + + $gate = $this->assert_purchasable($post_id, $uid); + if (is_wp_error($gate)) { + return $gate; + } + + $priced = $gate['priced']; + $pay_modo = (($priced['modo'] ?? '0') === 'points') ? 'points' : '0'; + + $mode = sanitize_key((string) ($params['mode'] ?? '')); + if ($mode === '') { + $mode = $pay_modo === 'points' ? 'points' : 'cash'; + } + if ($mode === 'points' || $pay_modo === 'points') { + return $this->rest_wrap_result($this->create_and_pay_points($post_id, $uid)); + } + + $payment_method = sanitize_key((string) ($params['payment_method'] ?? 'balance')); + if ($payment_method === '' || $payment_method === 'cash') { + $payment_method = 'balance'; + } + return $this->rest_wrap_result($this->create_cash_order($post_id, $uid, $payment_method)); + } + + /** + * @param array|WP_Error $result + * @return WP_REST_Response|WP_Error + */ + private function rest_wrap_result($result) { + if (is_wp_error($result)) { + return $result; + } + if (empty($result['success'])) { + $code = sanitize_key((string) ($result['code'] ?? 'aksrt_pay_failed')); + $status = (int) ($result['status'] ?? 400); + return new WP_Error($code !== '' ? $code : 'aksrt_pay_failed', (string) ($result['message'] ?? __('支付失败', 'aksrt')), array_merge( + ['status' => $status > 0 ? $status : 400], + is_array($result['data'] ?? null) ? $result['data'] : [] + )); + } + return new WP_REST_Response($result, 200); + } + + /** + * Cashier preview payload. + * + * @return array|WP_Error + */ + public function preview(int $post_id, int $user_id = 0) { + $post = get_post($post_id); + if (!($post instanceof WP_Post) || $post->post_status !== 'publish') { + return new WP_Error('aksrt_invalid', __('内容不存在', 'aksrt'), ['status' => 404]); + } + + $meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : []; + $pay_type = AKSRT_Domain_Model::normalize_pay_type((string) ($meta['pay_type'] ?? AKSRT_Domain_Model::PAY_TYPE_NO)); + if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_NO) { + return new WP_Error('aksrt_not_paid', __('该内容无需购买', 'aksrt'), ['status' => 400]); + } + + $uid = $user_id > 0 ? $user_id : get_current_user_id(); + $priced = function_exists('aksrt_get_user_pay_price') + ? aksrt_get_user_pay_price($post, $uid) + : ['amount' => 0.0, 'base' => 0.0, 'modo' => '0', 'currency' => '¥', 'is_free' => true, 'pay_limit' => 0, 'vip_level' => 0, 'original' => 0.0, 'vip1' => 0.0, 'vip2' => 0.0]; + + $pay_modo = (($priced['modo'] ?? '0') === 'points') ? 'points' : '0'; + $pay_limit = (int) ($priced['pay_limit'] ?? 0); + $vip_level = (int) ($priced['vip_level'] ?? 0); + $amount = (float) ($priced['amount'] ?? 0); + $base = (float) ($priced['base'] ?? $amount); + + $has_paid = false; + if ($uid > 0 && class_exists('AKSRT_Order')) { + $has_paid = AKSRT_Order::get_instance()->has_paid_access( + $uid, + AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE, + $post_id + ); + } + + $need_vip = $pay_limit > 0 && $vip_level < $pay_limit; + $currency = (string) ($priced['currency'] ?? ($pay_modo === 'points' + ? (class_exists('AKSRT_Settings') ? AKSRT_Settings::get_currency_name() : __('积分', 'aksrt')) + : '¥')); + + $balance = ($uid > 0 && class_exists('AKSRT_Credits')) ? AKSRT_Credits::balance($uid) : 0; + $methods = $this->available_payment_methods($pay_modo); + + $amount_label = $this->format_amount_label($amount, $pay_modo, $currency); + $base_label = $this->format_amount_label($base, $pay_modo, $currency); + + $vip_page = function_exists('aksrt_get_user_center_url') + ? aksrt_get_user_center_url() + : home_url('/'); + + return [ + 'post_id' => $post_id, + 'title' => get_the_title($post), + 'permalink' => get_permalink($post), + 'pay_type' => $pay_type, + 'pay_type_label' => AKSRT_Domain_Model::pay_type_labels()[$pay_type] ?? $pay_type, + 'pay_modo' => $pay_modo, + 'pay_limit' => $pay_limit, + 'vip_level' => $vip_level, + 'amount' => $pay_modo === 'points' ? (int) ceil($amount) : round($amount, 2), + 'base' => $pay_modo === 'points' ? (int) ceil($base) : round($base, 2), + 'original' => (float) ($priced['original'] ?? 0), + 'vip1' => (float) ($priced['vip1'] ?? 0), + 'vip2' => (float) ($priced['vip2'] ?? 0), + 'currency' => $currency, + 'amount_label' => $amount_label, + 'base_label' => $base_label, + 'is_free' => !empty($priced['is_free']) || $amount <= 0, + 'has_paid_access' => $has_paid, + 'need_login' => $uid <= 0, + 'need_vip' => $need_vip, + 'need_purchase' => !$has_paid && !$need_vip && $amount > 0, + 'logged_in' => $uid > 0, + 'balance' => $balance, + 'balance_enough' => $pay_modo !== 'points' || $balance >= (int) ceil($amount), + 'payment_methods' => $methods, + 'login_url' => wp_login_url(get_permalink($post)), + 'vip_url' => $vip_page, + 'credits_url' => $vip_page, + ]; + } + + /** + * Points one-shot purchase → permanent resource order. + * + * @return array|WP_Error + */ + public function create_and_pay_points(int $post_id, int $user_id) { + $assert = $this->assert_purchasable($post_id, $user_id); + if (is_wp_error($assert)) { + return $assert; + } + + $priced = $assert['priced']; + if ((($priced['modo'] ?? '0') !== 'points')) { + return new WP_Error('aksrt_wrong_modo', __('该内容不支持积分支付。', 'aksrt'), ['status' => 400]); + } + + $amount = (int) ceil((float) ($priced['amount'] ?? 0)); + if ($amount <= 0) { + return [ + 'success' => true, + 'data' => [ + 'free' => true, + 'message' => __('无需支付,已可访问。', 'aksrt'), + 'reload' => true, + ], + ]; + } + + if (!class_exists('AKSRT_Order') || !class_exists('AKSRT_Credits')) { + return new WP_Error('aksrt_pay_unavailable', __('支付系统不可用。', 'aksrt'), ['status' => 500]); + } + + $balance = AKSRT_Credits::balance($user_id); + if ($balance < $amount) { + return new WP_Error('aksrt_credits_insufficient', __('积分不足,暂时无法支付。', 'aksrt'), [ + 'status' => 402, + 'balance' => $balance, + 'cost' => $amount, + ]); + } + + $note = wp_json_encode([ + 'content_pay' => 1, + 'pay_modo' => 'points', + 'post_id' => $post_id, + 'vip_level' => (int) ($priced['vip_level'] ?? 0), + 'base' => (float) ($priced['base'] ?? $amount), + 'amount' => $amount, + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + + $order_number = AKSRT_Order_Repository::get_instance()->create([ + 'user_id' => $user_id, + 'product_type' => AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE, + 'product_id' => $post_id, + 'amount' => (float) $amount, + 'currency' => 'points', + 'status' => AKSRT_Order::STATUS_PENDING, + 'payment_method' => 'points', + 'note' => is_string($note) ? $note : '', + 'expires_at' => gmdate('Y-m-d H:i:s', time() + 30 * MINUTE_IN_SECONDS), + ]); + + if ($order_number === '') { + return new WP_Error('aksrt_order_create_failed', __('创建订单失败。', 'aksrt'), ['status' => 500]); + } + + $deducted = AKSRT_Credits::deduct( + $user_id, + $amount, + 'content_points_pay', + ['type' => 'post', 'id' => $post_id, 'order' => $order_number], + sprintf(__('积分购买《%s》', 'aksrt'), get_the_title($post_id)) + ); + + if (is_wp_error($deducted) || $deducted === false) { + AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_CANCELLED, [ + 'note' => __('积分扣除失败,自动取消', 'aksrt'), + ]); + $msg = is_wp_error($deducted) ? $deducted->get_error_message() : __('积分扣除失败。', 'aksrt'); + return new WP_Error('aksrt_credits_deduct_failed', $msg ?: __('积分扣除失败。', 'aksrt'), ['status' => 400]); + } + + $paid = AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_PAID, [ + 'payment_method' => 'points', + 'payment_transaction_id' => 'points_' . $order_number, + 'note' => sprintf(__('积分支付 %d 积分。', 'aksrt'), $amount), + ]); + + if (!$paid) { + AKSRT_Credits::add( + $user_id, + $amount, + 'content_points_pay_refund', + ['type' => 'post', 'id' => $post_id, 'order' => $order_number], + sprintf(__('购买失败退款:%s', 'aksrt'), get_the_title($post_id)) + ); + return new WP_Error('aksrt_order_confirm_failed', __('订单确认失败,已尝试退回积分。', 'aksrt'), ['status' => 500]); + } + + return [ + 'success' => true, + 'data' => [ + 'order_number' => $order_number, + 'amount' => $amount, + 'pay_modo' => 'points', + 'balance' => AKSRT_Credits::balance($user_id), + 'message' => __('支付成功', 'aksrt'), + 'reload' => true, + ], + ]; + } + + /** + * Cash order create + balance/gateway pay. + * + * @return array|WP_Error + */ + public function create_cash_order(int $post_id, int $user_id, string $payment_method = 'balance') { + $assert = $this->assert_purchasable($post_id, $user_id); + if (is_wp_error($assert)) { + return $assert; + } + + $priced = $assert['priced']; + if ((($priced['modo'] ?? '0') === 'points')) { + return new WP_Error('aksrt_wrong_modo', __('该内容仅支持积分支付。', 'aksrt'), ['status' => 400]); + } + + $amount = round((float) ($priced['amount'] ?? 0), 2); + if ($amount <= 0) { + return [ + 'success' => true, + 'data' => [ + 'free' => true, + 'message' => __('无需支付,已可访问。', 'aksrt'), + 'reload' => true, + ], + ]; + } + + if (!class_exists('AKSRT_Order') || !class_exists('AKSRT_Order_Repository')) { + return new WP_Error('aksrt_pay_unavailable', __('支付系统不可用。', 'aksrt'), ['status' => 500]); + } + + $payment_method = sanitize_key($payment_method); + if ($payment_method === '') { + $payment_method = 'balance'; + } + + $note = wp_json_encode([ + 'content_pay' => 1, + 'pay_modo' => '0', + 'post_id' => $post_id, + 'vip_level' => (int) ($priced['vip_level'] ?? 0), + 'base' => (float) ($priced['base'] ?? $amount), + 'amount' => $amount, + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + + $order_number = AKSRT_Order_Repository::get_instance()->create([ + 'user_id' => $user_id, + 'product_type' => AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE, + 'product_id' => $post_id, + 'amount' => $amount, + 'currency' => 'CNY', + 'status' => AKSRT_Order::STATUS_PENDING, + 'payment_method' => $payment_method, + 'note' => is_string($note) ? $note : '', + 'expires_at' => gmdate('Y-m-d H:i:s', time() + 30 * MINUTE_IN_SECONDS), + ]); + + if ($order_number === '') { + return new WP_Error('aksrt_order_create_failed', __('创建订单失败。', 'aksrt'), ['status' => 500]); + } + + $order = AKSRT_Order_Repository::get_instance()->get($order_number); + if (!$order) { + return new WP_Error('aksrt_order_create_failed', __('创建订单失败。', 'aksrt'), ['status' => 500]); + } + + $order['subject'] = sprintf( + /* translators: %s: post title */ + __('购买《%s》', 'aksrt'), + get_the_title($post_id) + ); + + if (!class_exists('AKSRT_Payment_Gateway')) { + return new WP_Error('aksrt_pay_unavailable', __('支付网关不可用。', 'aksrt'), ['status' => 500]); + } + + $gateway_manager = AKSRT_Payment_Gateway::get_instance(); + $gateway = $gateway_manager->get_gateway($payment_method); + if (!$gateway) { + AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_CANCELLED, [ + 'note' => __('支付方式不可用,自动取消', 'aksrt'), + ]); + return new WP_Error('aksrt_gateway_unavailable', __('当前支付方式不可用。', 'aksrt'), ['status' => 400]); + } + if (method_exists($gateway, 'is_configured') && !$gateway->is_configured()) { + AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_CANCELLED, [ + 'note' => __('支付方式未配置,自动取消', 'aksrt'), + ]); + return new WP_Error('aksrt_gateway_unconfigured', __('该支付方式尚未配置,请联系站长。', 'aksrt'), ['status' => 400]); + } + + $payment_result = $gateway->create_payment($order); + if (empty($payment_result['success'])) { + AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_CANCELLED, [ + 'note' => (string) ($payment_result['message'] ?? __('支付失败,自动取消', 'aksrt')), + ]); + $msg = (string) ($payment_result['message'] ?? __('支付失败。', 'aksrt')); + $code = ($payment_method === 'balance' && str_contains($msg, '余额')) + ? 'aksrt_balance_insufficient' + : 'aksrt_pay_failed'; + return new WP_Error($code, $msg, [ + 'status' => 402, + 'order_number' => $order_number, + ]); + } + + $code_url = (string) ($payment_result['payment_data']['code_url'] ?? ''); + $payment_url = (string) ($payment_result['payment_url'] ?? ''); + if ($payment_url !== '' || $code_url !== '') { + return [ + 'success' => true, + 'data' => [ + 'redirect' => true, + 'payment_url' => $payment_url, + 'qr_content' => $code_url, + 'order_number' => $order_number, + 'amount' => $amount, + 'pay_modo' => '0', + 'message' => $payment_result['message'] ?? __('请在新页面完成支付。', 'aksrt'), + 'reload' => false, + ], + ]; + } + + // Sync gateway (balance): mark paid. + $paid = AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_PAID, [ + 'payment_method' => $payment_method, + 'payment_transaction_id' => sanitize_text_field((string) ($payment_result['transaction_id'] ?? '')), + 'note' => __('用户完成内容购买支付。', 'aksrt'), + ]); + if (!$paid) { + return new WP_Error('aksrt_order_confirm_failed', __('订单确认失败。', 'aksrt'), ['status' => 500]); + } + + return [ + 'success' => true, + 'data' => [ + 'order_number' => $order_number, + 'amount' => $amount, + 'pay_modo' => '0', + 'message' => $payment_result['message'] ?? __('支付成功', 'aksrt'), + 'reload' => true, + ], + ]; + } + + /** + * @return array{post:WP_Post,meta:array,priced:array}|WP_Error + */ + private function assert_purchasable(int $post_id, int $user_id) { + $post = get_post($post_id); + if (!($post instanceof WP_Post) || $post->post_status !== 'publish') { + return new WP_Error('aksrt_invalid', __('内容不存在', 'aksrt'), ['status' => 404]); + } + if ($user_id <= 0) { + return new WP_Error('aksrt_need_login', __('请先登录后再购买。', 'aksrt'), [ + 'status' => 401, + 'login_url' => wp_login_url(get_permalink($post)), + ]); + } + + $meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : []; + $pay_type = AKSRT_Domain_Model::normalize_pay_type((string) ($meta['pay_type'] ?? AKSRT_Domain_Model::PAY_TYPE_NO)); + if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_NO) { + return new WP_Error('aksrt_not_paid', __('该内容无需购买', 'aksrt'), ['status' => 400]); + } + if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_VIP) { + return new WP_Error('aksrt_need_vip', __('该内容为 VIP 专属,请开通会员。', 'aksrt'), ['status' => 403]); + } + + $priced = function_exists('aksrt_get_user_pay_price') + ? aksrt_get_user_pay_price($post, $user_id) + : ['amount' => 0.0, 'modo' => '0', 'pay_limit' => 0, 'vip_level' => 0, 'is_free' => true, 'base' => 0.0]; + + $pay_limit = (int) ($priced['pay_limit'] ?? 0); + $vip_level = (int) ($priced['vip_level'] ?? 0); + if ($pay_limit > 0 && $vip_level < $pay_limit) { + return new WP_Error('aksrt_need_vip', sprintf( + /* translators: %d: required VIP level */ + __('该资源仅限 VIP%s 及以上会员购买/下载。', 'aksrt'), + max(1, $pay_limit) + ), [ + 'status' => 403, + 'pay_limit' => $pay_limit, + ]); + } + + if (class_exists('AKSRT_Order') && AKSRT_Order::get_instance()->has_paid_access( + $user_id, + AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE, + $post_id + )) { + return new WP_Error('aksrt_already_paid', __('您已购买该内容。', 'aksrt'), [ + 'status' => 200, + 'has_paid_access' => true, + 'reload' => true, + ]); + } + + return [ + 'post' => $post, + 'meta' => $meta, + 'priced' => $priced, + ]; + } + + /** + * @return array + */ + private function available_payment_methods(string $pay_modo): array { + if ($pay_modo === 'points') { + return [ + ['id' => 'points', 'name' => __('积分支付', 'aksrt')], + ]; + } + + $methods = []; + if (!class_exists('AKSRT_Payment_Gateway')) { + return [['id' => 'balance', 'name' => __('站内余额', 'aksrt')]]; + } + $gw = AKSRT_Payment_Gateway::get_instance(); + foreach (['balance', 'alipay', 'wechatpay', 'paypal', 'stripe'] as $id) { + $g = $gw->get_gateway($id); + if (!$g) { + continue; + } + if (method_exists($g, 'is_configured') && !$g->is_configured()) { + continue; + } + $methods[] = [ + 'id' => $id, + 'name' => method_exists($g, 'get_name') ? (string) $g->get_name() : $id, + ]; + } + if ($methods === []) { + $methods[] = ['id' => 'balance', 'name' => __('站内余额', 'aksrt')]; + } + return $methods; + } + + private function format_amount_label(float $amount, string $pay_modo, string $currency): string { + if ($amount <= 0) { + return (string) __('免费', 'aksrt'); + } + $n = function_exists('aksrt_format_price') ? aksrt_format_price($amount) : (string) $amount; + if ($pay_modo === 'points') { + return $n . ' ' . $currency; + } + return $currency . $n; + } +} diff --git a/inc/classes/class-aksrt-content-visibility.php b/inc/classes/class-aksrt-content-visibility.php index 1257580d..0a1a72af 100644 --- a/inc/classes/class-aksrt-content-visibility.php +++ b/inc/classes/class-aksrt-content-visibility.php @@ -121,38 +121,41 @@ class AKSRT_Content_Visibility { // Member-only purchase restriction (Zibll pay_limit). if ($pay_limit > 0 && $pay_type !== AKSRT_Domain_Model::PAY_TYPE_NO) { - $state['need_vip'] = true; + $state['need_vip'] = $uid <= 0 || $user_vip_level < $pay_limit; $allowed_member = $uid > 0 && $user_vip_level >= $pay_limit; if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_DOWNLOAD) { - $download_cost = function_exists('aksrt_resource_download_cost') - ? (int) aksrt_resource_download_cost($resource_post) - : (int) ceil($price); - // If member is free via VIP price, cost is 0. + // Cash: order gate only. Points: cashier once → paid order zeros cost. + $download_cost = 0; + if ($pay_modo === 'points' && $price > 0 && !$has_paid_order) { + $download_cost = (int) ceil($price); + } if ($allowed_member && $price <= 0) { $download_cost = 0; } $state['download_cost'] = max(0, $download_cost); $state['need_login'] = $uid <= 0; - $state['need_purchase'] = $allowed_member && $state['download_cost'] > 0 && !$has_paid_order; - $state['has_paid_access'] = $allowed_member && ($has_paid_order || $state['download_cost'] <= 0); + $state['need_purchase'] = $allowed_member && $price > 0 && !$has_paid_order; + $state['has_paid_access'] = $allowed_member && ($has_paid_order || $price <= 0); $state['can_view'] = true; - $state['can_download'] = $allowed_member && ($uid > 0) && ($has_paid_order || $state['download_cost'] <= 0); + $state['can_download'] = $allowed_member && ($uid > 0) && ($has_paid_order || $price <= 0); if (!$allowed_member) { $state['can_download'] = false; $state['has_paid_access'] = false; $state['need_purchase'] = false; + $state['need_vip'] = true; } return $state; } - $state['has_paid_access'] = ($allowed_member && $price <= 0) || $has_paid_order || ($allowed_member && $has_vip_access && $pay_type === AKSRT_Domain_Model::PAY_TYPE_VIP); - // For reading/gallery/video with pay_limit: members still need to purchase unless free. + // reading/gallery/video with pay_limit if ($allowed_member) { $state['has_paid_access'] = $has_paid_order || $price <= 0; $state['need_purchase'] = !$state['has_paid_access']; + $state['need_vip'] = false; } else { $state['has_paid_access'] = false; $state['need_purchase'] = false; + $state['need_vip'] = true; } $state['can_view'] = $state['has_paid_access']; $state['can_download'] = false; @@ -160,34 +163,23 @@ class AKSRT_Content_Visibility { } if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_DOWNLOAD) { - $download_cost = function_exists('aksrt_resource_download_cost') - ? (int) aksrt_resource_download_cost($resource_post) - : (int) ceil($price); - + // Zibll: cash = order unlock; points = one-shot cashier order (not per-download deduct). + $download_cost = 0; + if ($pay_modo === 'points' && $price > 0 && !$has_paid_order) { + $download_cost = (int) ceil($price); + } $state['download_cost'] = max(0, $download_cost); - // Zibll pay_free_logged_show: free download still requires login. + $free_logged_show = !empty($paid_settings['free_logged_show']); $state['need_login'] = !empty($paid_settings['download_login']) || !empty($pay_meta['require_login']) - || ($free_logged_show && $state['download_cost'] <= 0); + || ($free_logged_show && $price <= 0); $state['require_captcha'] = !empty($paid_settings['download_captcha']) || !empty($pay_meta['require_captcha']); - // Cash mode with cost handled by orders: need purchase when no order and base price > 0. - $needs_pay = $state['download_cost'] > 0 || ($pay_modo === '0' && $price > 0 && !$has_paid_order && empty($paid_settings['download_credit'])); - if ($pay_modo === '0' && $price > 0 && empty($paid_settings['download_credit'])) { - $state['need_purchase'] = !$has_paid_order && !($has_vip_access && $price <= 0); - $state['has_paid_access'] = $has_paid_order || $price <= 0; - // Download still gated by login/credit path for token; cash unlock is order-based. - $state['can_view'] = true; - $state['can_download'] = ($has_paid_order || $price <= 0) && (!$state['need_login'] || $uid > 0); - return $state; - } - $state['need_purchase'] = !$has_paid_order && $state['download_cost'] > 0 && !$has_free_access; - // VIP with free tier amount already reflected in download_cost. - $state['has_paid_access'] = $has_paid_order || $state['download_cost'] <= 0 || $has_free_access; + $state['need_purchase'] = !$has_paid_order && $price > 0; + $state['has_paid_access'] = $has_paid_order || $price <= 0 || $has_free_access; $state['can_view'] = true; - $state['can_download'] = (!$state['need_login'] || $uid > 0) && ($has_paid_order || $state['download_cost'] <= 0 || true); - // can_download true only means "may request token"; rest_request still deducts credits. + $state['can_download'] = (!$state['need_login'] || $uid > 0) && ($has_paid_order || $price <= 0); return $state; } @@ -270,14 +262,19 @@ class AKSRT_Content_Visibility { ]); } - // Cash purchase required (order path). - if (!empty($state['need_purchase']) && empty($state['has_paid_access']) && $cost <= 0 && (($state['pay_modo'] ?? '0') === '0')) { + // Cash or points: require cashier purchase (resource order) before download token. + if (!empty($state['need_purchase']) && empty($state['has_paid_access'])) { + $pay_modo = (string) ($state['pay_modo'] ?? '0'); + $price = (float) ($state['price'] ?? 0); return new WP_Error('aksrt_need_purchase', __('请先购买后再下载。', 'aksrt'), [ 'status' => 402, + 'pay_modo' => $pay_modo, + 'amount' => $pay_modo === 'points' ? (int) ceil($price) : $price, + 'post_id' => (int) ($state['post_id'] ?? 0), ]); } - // Zibll daily free-download quota. + // Zibll daily free-download quota (only when free / already unlocked). if ($cost <= 0 && function_exists('aksrt_check_free_download_quota')) { $quota = aksrt_check_free_download_quota($uid, $cost); if (is_wp_error($quota)) { @@ -285,22 +282,6 @@ class AKSRT_Content_Visibility { } } - $pay_modo = (string) ($state['pay_modo'] ?? '0'); - $need_credit_check = $cost > 0 && ( - $pay_modo === 'points' - || !empty($paid_settings['download_credit']) - ); - if ($need_credit_check) { - $balance = class_exists('AKSRT_Credits') ? AKSRT_Credits::balance($uid) : 0; - if ($balance < $cost) { - return new WP_Error('aksrt_credits_insufficient', __('积分不足,无法下载。', 'aksrt'), [ - 'status' => 402, - 'balance' => $balance, - 'cost' => $cost, - ]); - } - } - return true; } diff --git a/inc/classes/class-aksrt-download.php b/inc/classes/class-aksrt-download.php index 498b6393..766f166f 100644 --- a/inc/classes/class-aksrt-download.php +++ b/inc/classes/class-aksrt-download.php @@ -120,26 +120,37 @@ class AKSRT_Download { : ((($meta['pay_modo'] ?? '0') === 'points') ? 'points' : '0'); $display_price = is_array($priced) ? (float) ($priced['amount'] ?? 0) : (float) ($meta['price'] ?? 0); + $access = class_exists('AKSRT_Content_Visibility') + ? AKSRT_Content_Visibility::get_post_access_state($post, $uid) + : []; + $need_captcha = !empty($paid_settings['download_captcha']) || !empty($meta['require_captcha']); if ($need_captcha && function_exists('aksrt_is_captcha_enabled_for') && !aksrt_is_captcha_enabled_for('download')) { $need_captcha = false; } return new WP_REST_Response([ - 'post_id' => $post_id, - 'price' => $display_price, - 'base_price' => is_array($priced) ? (float) ($priced['base'] ?? $display_price) : (float) ($meta['price'] ?? 0), - 'pay_modo' => $pay_modo, - 'pay_limit' => (int) ($meta['pay_limit'] ?? 0), - 'cost' => $cost, - 'require_login' => !empty($paid_settings['download_login']) || !empty($meta['require_login']) || (!empty($paid_settings['free_logged_show']) && $cost <= 0), - 'require_captcha' => $need_captcha, - 'logged_in' => $uid > 0, - 'balance' => $uid > 0 && class_exists('AKSRT_Credits') ? AKSRT_Credits::balance($uid) : 0, - 'free_limit' => $uid > 0 && function_exists('aksrt_get_user_free_download_limit') ? aksrt_get_user_free_download_limit($uid) : 0, - 'free_used' => $uid > 0 && function_exists('aksrt_get_user_free_downloaded_number') ? aksrt_get_user_free_downloaded_number($uid) : 0, - 'free_surplus' => $uid > 0 && function_exists('aksrt_get_free_download_surplus') ? aksrt_get_free_download_surplus($uid) : null, - 'sources' => array_map(function ($row, $idx) { + 'post_id' => $post_id, + 'price' => $display_price, + 'base_price' => is_array($priced) ? (float) ($priced['base'] ?? $display_price) : (float) ($meta['price'] ?? 0), + 'pay_modo' => $pay_modo, + 'pay_limit' => (int) ($meta['pay_limit'] ?? 0), + 'cost' => $cost, + 'require_login' => !empty($paid_settings['download_login']) || !empty($meta['require_login']) || (!empty($paid_settings['free_logged_show']) && $cost <= 0 && $display_price <= 0), + 'require_captcha' => $need_captcha, + 'logged_in' => $uid > 0, + 'balance' => $uid > 0 && class_exists('AKSRT_Credits') ? AKSRT_Credits::balance($uid) : 0, + 'has_paid_access' => !empty($access['has_paid_access']), + 'need_purchase' => !empty($access['need_purchase']), + 'need_vip' => !empty($access['need_vip']), + 'need_login' => !empty($access['need_login']) && $uid <= 0, + 'can_download' => !empty($access['can_download']), + 'login_url' => wp_login_url(get_permalink($post)), + 'vip_level' => (int) ($access['user_vip_level'] ?? 0), + 'free_limit' => $uid > 0 && function_exists('aksrt_get_user_free_download_limit') ? aksrt_get_user_free_download_limit($uid) : 0, + 'free_used' => $uid > 0 && function_exists('aksrt_get_user_free_downloaded_number') ? aksrt_get_user_free_downloaded_number($uid) : 0, + 'free_surplus' => $uid > 0 && function_exists('aksrt_get_free_download_surplus') ? aksrt_get_free_download_surplus($uid) : null, + 'sources' => array_map(function ($row, $idx) { $extract_code = trim((string) ($row['extract_code'] ?? '')); return [ 'index' => $idx, @@ -198,16 +209,9 @@ class AKSRT_Download { } } - if ($cost > 0 && $uid > 0 && class_exists('AKSRT_Credits')) { - $deducted = AKSRT_Credits::deduct($uid, $cost, 'download', ['type' => 'post', 'id' => $post_id]); - if (is_wp_error($deducted)) return $deducted; - } - - $token = $this->issue_token($uid, $post_id, $source_index, $cost); + // Purchase is one-shot via cashier (resource order). Token issue does not re-deduct. + $token = $this->issue_token($uid, $post_id, $source_index, 0); if (is_wp_error($token)) { - if ($cost > 0 && $uid > 0 && class_exists('AKSRT_Credits')) { - AKSRT_Credits::add($uid, $cost, 'refund', ['type' => 'post', 'id' => $post_id]); - } return $token; } diff --git a/inc/classes/scripts-traits/trait-aksrt-scripts-part-1b.php b/inc/classes/scripts-traits/trait-aksrt-scripts-part-1b.php index 51d3a187..3f9eab3e 100644 --- a/inc/classes/scripts-traits/trait-aksrt-scripts-part-1b.php +++ b/inc/classes/scripts-traits/trait-aksrt-scripts-part-1b.php @@ -140,6 +140,24 @@ trait AKSRT_Scripts_Part_1b { $is_single_resource = (function_exists('aksrt_is_resource_post') && is_singular() && aksrt_is_resource_post(get_queried_object_id())); // 普通博客 single(排除资源型 post) $is_single_post = is_singular('post') && !$is_single_resource; + // 付费下载/付费墙:任意 singular 需收银台/下载脚本 + $is_paid_content_single = false; + if (is_singular()) { + $qid = (int) get_queried_object_id(); + if ($qid > 0) { + if (function_exists('aksrt_is_downloadable_content') && aksrt_is_downloadable_content($qid)) { + $is_paid_content_single = true; + } elseif (function_exists('aksrt_is_paid_post') && aksrt_is_paid_post($qid)) { + $is_paid_content_single = true; + } elseif (function_exists('aksrt_get_pay_meta')) { + $pm = aksrt_get_pay_meta($qid); + $pt = (string) ($pm['pay_type'] ?? 'no'); + $is_paid_content_single = ($pt !== '' && $pt !== 'no'); + } + } + } + // 独立下载页模板 + $is_download_file_page = is_page_template('templates/pages/page-download-file.php'); // 资源虚拟列表 /resources/ $is_resource_archive = function_exists('aksrt_is_resource_archive') && aksrt_is_resource_archive(); // /user/{nicename}/ 自定义重写路由(class-aksrt-user.php 注册) @@ -643,13 +661,13 @@ trait AKSRT_Scripts_Part_1b { $enqueue_module('resource-archive'); } - // 单篇资源:下载脚本 - if ($is_single_resource) { + // 单篇资源 / 付费内容 / 独立下载页:下载 + 收银台脚本 + if ($is_single_resource || $is_paid_content_single || $is_download_file_page) { $cms_download_js_path = get_template_directory() . '/assets/js/cms-download.js'; wp_enqueue_script( 'aksrt-cms-download', $base_uri . '/assets/js/cms-download.js', - ['aksrt-script'], + ['aksrt-script', 'aksrt-runtime'], file_exists($cms_download_js_path) ? (string) filemtime($cms_download_js_path) : $version, true ); diff --git a/inc/helpers/aksrt-pay.php b/inc/helpers/aksrt-pay.php index 632448a4..54782c8b 100644 --- a/inc/helpers/aksrt-pay.php +++ b/inc/helpers/aksrt-pay.php @@ -481,6 +481,7 @@ if (!function_exists('aksrt_get_pay_price_label')) { $pay_limit = is_array($priced) ? (int) ($priced['pay_limit'] ?? 0) : (int) ($meta['pay_limit'] ?? 0); + $vip_level = is_array($priced) ? (int) ($priced['vip_level'] ?? 0) : 0; $currency = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_currency_name() : __('积分', 'aksrt'); $cash_symbol = '¥'; if (class_exists('AKSRT_Settings')) { @@ -488,7 +489,10 @@ if (!function_exists('aksrt_get_pay_price_label')) { $cash_symbol = (string) ($paid['display']['currency_symbol'] ?? '¥'); } - if ($meta['pay_type'] === AKSRT_PAY_TYPE_VIP || $pay_limit > 0) { + // pay_type VIP-only or pay_limit not met → member-exclusive badge. + $member_blocked = ($meta['pay_type'] === AKSRT_PAY_TYPE_VIP) + || ($pay_limit > 0 && $vip_level < $pay_limit); + if ($member_blocked) { return [ 'text' => __('会员专属', 'aksrt'), 'free' => false, @@ -511,7 +515,7 @@ if (!function_exists('aksrt_get_pay_price_label')) { 'amount' => 0, 'base' => $base, 'modo' => $pay_modo, - 'pay_limit' => 0, + 'pay_limit' => $pay_limit, ]; } @@ -522,7 +526,7 @@ if (!function_exists('aksrt_get_pay_price_label')) { 'amount' => $price, 'base' => $base, 'modo' => 'points', - 'pay_limit' => 0, + 'pay_limit' => $pay_limit, ]; } @@ -532,7 +536,7 @@ if (!function_exists('aksrt_get_pay_price_label')) { 'amount' => $price, 'base' => $base, 'modo' => '0', - 'pay_limit' => 0, + 'pay_limit' => $pay_limit, ]; } } @@ -646,6 +650,57 @@ if (!function_exists('aksrt_get_demo_link_html')) { } } +/** + * Product attributes row (Zibll zibpay_get_product_attributes). + */ +if (!function_exists('aksrt_get_pay_attributes_html')) { + function aksrt_get_pay_attributes_html($post = null, string $class = 'resource-download-box__attrs'): string { + $p = get_post($post); + if (!($p instanceof WP_Post)) { + return ''; + } + $meta = aksrt_get_pay_meta($p); + $attrs = is_array($meta['attributes'] ?? null) ? $meta['attributes'] : []; + // Built-in version/size/license/compat as attributes when set. + $builtin = [ + 'version' => __('版本', 'aksrt'), + 'size' => __('大小', 'aksrt'), + 'license' => __('授权', 'aksrt'), + 'compat' => __('兼容', 'aksrt'), + ]; + $rows = []; + foreach ($builtin as $key => $label) { + $val = trim((string) ($meta[$key] ?? '')); + if ($val !== '') { + $rows[] = ['name' => $label, 'value' => $val]; + } + } + foreach ($attrs as $row) { + if (!is_array($row)) { + continue; + } + $name = trim((string) ($row['name'] ?? $row['key'] ?? $row['label'] ?? '')); + $value = trim((string) ($row['value'] ?? $row['val'] ?? '')); + if ($name === '' || $value === '') { + continue; + } + $rows[] = ['name' => $name, 'value' => $value]; + } + if ($rows === []) { + return ''; + } + $html = ''; + return $html; + } +} + // ============================================================ // 付费墙 HTML // ============================================================ @@ -657,6 +712,7 @@ if (!function_exists('aksrt_get_paywall_html')) { $priced = function_exists('aksrt_get_user_pay_price') ? aksrt_get_user_pay_price($post) : null; $pay_modo = is_array($priced) ? ((($priced['modo'] ?? '0') === 'points') ? 'points' : '0') : ((($meta['pay_modo'] ?? '0') === 'points') ? 'points' : '0'); $price = is_array($priced) ? (float) ($priced['amount'] ?? 0) : (float) ($meta['price'] ?? 0); + $vip_level = is_array($priced) ? (int) ($priced['vip_level'] ?? 0) : 0; $currency = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_currency_name() : __('积分', 'aksrt'); $cash_symbol = '¥'; if (class_exists('AKSRT_Settings')) { @@ -679,6 +735,8 @@ if (!function_exists('aksrt_get_paywall_html')) { $original_price = (float) ($meta['original_price'] ?? 0); $has_original = $original_price > 0 && $original_price > $price; $pay_limit = (int) ($meta['pay_limit'] ?? 0); + $member_blocked = $pay_limit > 0 && $vip_level < $pay_limit; + $vip_url = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url() : home_url('/'); ob_start(); ?> @@ -692,7 +750,7 @@ if (!function_exists('aksrt_get_paywall_html')) {

- 0) : ?> + 0) : ?> @@ -704,13 +762,13 @@ if (!function_exists('aksrt_get_paywall_html')) {
- + - 0) : ?> - + + - + 0) : ?>
- + get_error_message() : __('暂无下载权限', 'aksrt')); ?>
diff --git a/templates/parts/resource-download-box.php b/templates/parts/resource-download-box.php index 153c9eac..1b431205 100644 --- a/templates/parts/resource-download-box.php +++ b/templates/parts/resource-download-box.php @@ -45,32 +45,34 @@ $paid_settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_paid_content_settings() : ['download_credit' => true, 'download_captcha' => false]; -$cost = function_exists('aksrt_get_download_cost') ? aksrt_get_download_cost($post_id) : (int) ceil((float) ($meta['price'] ?? 0)); -$credit_active = !empty($paid_settings['download_credit']) && $cost > 0; -$pay_modo = (($meta['pay_modo'] ?? '0') === 'points') ? 'points' : '0'; -if (function_exists('aksrt_get_user_pay_price')) { - $priced = aksrt_get_user_pay_price($post_id); - $pay_modo = (($priced['modo'] ?? $pay_modo) === 'points') ? 'points' : '0'; -} -// Points mode always uses credit deduct path when cost > 0. -if ($pay_modo === 'points' && $cost > 0) { - $credit_active = true; -} +$access = class_exists('AKSRT_Content_Visibility') + ? AKSRT_Content_Visibility::get_post_access_state($post_id) + : []; +$has_paid_access = !empty($access['has_paid_access']); +$priced = function_exists('aksrt_get_user_pay_price') ? aksrt_get_user_pay_price($post_id) : null; +$pay_modo = is_array($priced) + ? ((($priced['modo'] ?? '0') === 'points') ? 'points' : '0') + : ((($meta['pay_modo'] ?? '0') === 'points') ? 'points' : '0'); +$user_amount = is_array($priced) ? (float) ($priced['amount'] ?? 0) : (float) ($meta['price'] ?? 0); +$cost = function_exists('aksrt_resource_download_cost') ? (int) aksrt_resource_download_cost($post_id) : 0; + $captcha_active = !empty($paid_settings['download_captcha']) || !empty($meta['require_captcha']); if ($captcha_active && function_exists('aksrt_is_captcha_enabled_for') && !aksrt_is_captcha_enabled_for('download')) { $captcha_active = false; } -$balance = (int) ($gate_data['balance'] ?? 0); +$balance = (int) ($gate_data['balance'] ?? (is_user_logged_in() && class_exists('AKSRT_Credits') ? AKSRT_Credits::balance(get_current_user_id()) : 0)); $login_url = (string) ($gate_data['login_url'] ?? wp_login_url(get_permalink($post_id))); +$vip_url = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url() : home_url('/'); $is_need_login = $gate_blocked && $gate_code === 'aksrt_need_login'; -$is_low_credit = $gate_blocked && $gate_code === 'aksrt_credits_insufficient'; $is_need_vip = $gate_blocked && $gate_code === 'aksrt_need_vip'; $is_need_purchase = $gate_blocked && $gate_code === 'aksrt_need_purchase'; +// Points unpaid may also surface as need_purchase; treat low balance only after cashier. +$is_low_credit = false; // Zibll pay_free_logged_show: free downloads still require login. $free_logged_show = !empty($paid_settings['free_logged_show']); -$price_amount = (float) ($price_label['amount'] ?? $meta['price'] ?? 0); +$price_amount = (float) ($price_label['amount'] ?? $user_amount); if ($price_amount <= 0 && $free_logged_show && !is_user_logged_in()) { $is_need_login = true; $gate_blocked = true; @@ -82,7 +84,7 @@ $alone_url = ''; if ($alone_page && function_exists('aksrt_get_single_download_page_url')) { $alone_url = aksrt_get_single_download_page_url($post_id); } -$use_alone_entry = $alone_page && $alone_url !== '' && !$is_need_login && !$is_low_credit && !$is_need_vip && !$is_need_purchase; +$use_alone_entry = $alone_page && $alone_url !== '' && !$is_need_login && !$is_need_vip && !$is_need_purchase && !$gate_blocked; if (!empty($GLOBALS['aksrt_on_download_file_page'])) { $use_alone_entry = false; } @@ -105,6 +107,8 @@ if ($pay_modo === 'points') { $has_original = $original_price > 0 && $original_price > $base_price; $has_vip = ($vip1_price > 0 && $vip1_price < $base_price) || ($vip2_price > 0 && $vip2_price < $base_price); $pay_limit = (int) ($meta['pay_limit'] ?? 0); +$user_vip_level = is_array($priced) ? (int) ($priced['vip_level'] ?? 0) : 0; +$member_blocked = $pay_limit > 0 && $user_vip_level < $pay_limit; // 销量 $download_count = (int) ($meta['download_count'] ?? 0); @@ -129,18 +133,34 @@ $format_vip = static function (float $amount) use ($pay_modo, $currency, $cash_s $n = function_exists('aksrt_format_price') ? aksrt_format_price($amount) : (string) $amount; return $pay_modo === 'points' ? ($n . ' ' . $currency) : ($cash_symbol . $n); }; + +$purchase_label = ''; +if ($is_need_purchase && $user_amount > 0) { + $purchase_label = $pay_modo === 'points' + ? sprintf(__('立即购买(%s %s)', 'aksrt'), aksrt_format_price($user_amount), $currency) + : sprintf(__('立即购买(%s%s)', 'aksrt'), $cash_symbol, aksrt_format_price($user_amount)); +} elseif ($is_need_purchase) { + $purchase_label = __('购买后下载', 'aksrt'); +} ?> -
- 0) : ?> + 0) : ?> + + + + + 0) : ?>

- +

@@ -175,10 +195,16 @@ $format_vip = static function (float $amount) use ($pay_modo, $currency, $cash_s
- - + + 0) { + esc_html_e('已解锁', 'aksrt'); + } else { + echo esc_html((string) ($price_label['text'] ?? '')); + } + ?> - +
- +
0 && $vip1_price < $base_price) : ?> @@ -215,16 +241,42 @@ $format_vip = static function (float $amount) use ($pay_modo, $currency, $cash_s
+ +

- +
+ +
+ - - - - -
@@ -272,26 +317,13 @@ $format_vip = static function (float $amount) use ($pay_modo, $currency, $cash_s $btn_text = __('登录后下载', 'aksrt'); $btn_icon = 'fa-sign-in-alt'; $btn_modifier = ' resource-download-box__btn--login'; - } elseif ($is_need_purchase) { - $btn_tag = 'button'; - $btn_text = __('购买后下载', 'aksrt'); - $btn_icon = 'fa-shopping-cart'; - $btn_modifier = ' resource-download-box__btn--purchase'; - } elseif ($is_low_credit) { - $btn_tag = 'button'; - $btn_text = sprintf(__('解锁下载(%d 积分)', 'aksrt'), $cost); - $btn_icon = 'fa-coins'; - $btn_modifier = ' resource-download-box__btn--credit-low'; - } elseif ($credit_active && $cost > 0) { - $btn_tag = 'button'; - $btn_text = sprintf(__('下载(%d 积分)', 'aksrt'), $cost); - $btn_icon = 'fa-download'; - $btn_modifier = ' resource-download-box__btn--paid'; + $btn_action = ''; } else { $btn_tag = 'button'; $btn_text = __('立即下载', 'aksrt'); $btn_icon = 'fa-download'; - $btn_modifier = ''; + $btn_modifier = $has_paid_access ? ' resource-download-box__btn--owned' : ''; + $btn_action = 'aksrt-download'; } ?>
  • @@ -313,7 +345,7 @@ $format_vip = static function (float $amount) use ($pay_modo, $currency, $cash_s
  • - + + ' . $demo . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + } + } + ?> 0) { $dl_used = function_exists('aksrt_get_user_free_downloaded_number') ? aksrt_get_user_free_downloaded_number() : 0; @@ -376,7 +410,7 @@ $format_vip = static function (float $amount) use ($pay_modo, $currency, $cash_s

    - +

    get_error_message() : __('该资源仅限会员购买/下载。', 'aksrt')); ?> @@ -386,11 +420,6 @@ $format_vip = static function (float $amount) use ($pay_modo, $currency, $cash_s

    - -

    - - -