Merge pull request 'feat(pay): sales volume, paid order meta, pending resume, limit-over buy' (#121) from worktree-hero-banner-chain into main

Reviewed-on: CYL/wordpress-theme-aksrt#121
This commit is contained in:
2026-07-14 12:07:24 +08:00
8 changed files with 397 additions and 72 deletions
+58 -13
View File
@@ -160,6 +160,12 @@
if (data && data.credits_url) {
extra = '<a class="btn-primary btn-sm" href="' + escapeAttr(data.credits_url) + '">' + tt('my_points', '我的积分') + '</a>';
}
} else if (code === 'aksrt_download_limit') {
title = tt('quota_over', '今日免费额度已用完');
msg = msg || tt('quota_hint', '您今日下载免费资源个数已超限,请明日再下载。');
if (data && data.allow_limit_over && data.post_id) {
extra = '<button type="button" class="btn-primary btn-sm" data-aksrt-open-cashier="' + escapeAttr(data.post_id) + '" data-price-type="download_limit_over">' + tt('buy_over', '立即购买') + '</button>';
}
} else if (code === 'aksrt_captcha_failed') {
title = tt('captcha_fail', '验证失败');
msg = msg || tt('captcha_retry', '人机验证失败,请重试。');
@@ -178,8 +184,9 @@
if (openCashierBtn) {
openCashierBtn.addEventListener('click', function () {
const pid = parseInt(openCashierBtn.getAttribute('data-aksrt-open-cashier'), 10) || 0;
const pt = openCashierBtn.getAttribute('data-price-type') || '';
close();
if (pid) openCashier(pid, returnFocusEl);
if (pid) openCashier(pid, returnFocusEl, pt);
});
}
}
@@ -321,6 +328,29 @@
});
}
function pollOrderPaid(orderNumber, returnFocusEl) {
if (!orderNumber) return;
let tries = 0;
const maxTries = 40;
const timer = setInterval(function () {
tries += 1;
fetch(REST_BASE + '/pay/content/order/' + encodeURIComponent(orderNumber), {
method: 'GET',
credentials: 'same-origin',
headers: restHeaders(),
})
.then(function (resp) { return resp.json().then(function (j) { return { ok: resp.ok, json: j }; }); })
.then(function (r) {
if (r.ok && r.json && r.json.paid) {
clearInterval(timer);
window.location.reload();
}
})
.catch(function () { /* ignore */ });
if (tries >= maxTries) clearInterval(timer);
}, 3000);
}
// ===== 积分不足按钮(前置拦截,不发请求) =====
function showLowCreditModal(button) {
const balance = parseInt(button.getAttribute('data-balance'), 10) || 0;
@@ -336,15 +366,18 @@
};
}
function openCashier(postId, returnFocusEl) {
function openCashier(postId, returnFocusEl, priceType) {
if (!postId) return;
priceType = priceType || '';
open(
'<h3 class="aksrt-dl-modal__title">' + tt('cashier_loading', '确认购买') + '</h3>' +
'<p class="aksrt-dl-modal__msg">' + tt('loading', '加载中…') + '</p>',
returnFocusEl
);
fetch(REST_BASE + '/pay/content/' + postId, {
var previewUrl = REST_BASE + '/pay/content/' + postId;
if (priceType) previewUrl += (previewUrl.indexOf('?') >= 0 ? '&' : '?') + 'price_type=' + encodeURIComponent(priceType);
fetch(previewUrl, {
method: 'GET',
credentials: 'same-origin',
headers: restHeaders(),
@@ -369,7 +402,9 @@
renderError((r.json && r.json.message) || tt('default_err', '请稍后重试'), data, returnFocusEl, code);
return;
}
renderCashier(r.json, returnFocusEl);
var prev = r.json || {};
if (priceType) prev._price_type = priceType;
renderCashier(prev, returnFocusEl);
})
.catch(function () {
renderError(tt('network_err', '网络错误,请稍后重试'), {}, returnFocusEl);
@@ -392,8 +427,15 @@
}
const isPoints = preview.pay_modo === 'points';
const amountLabel = escapeHtml(preview.amount_label || '');
const baseLabel = escapeHtml(preview.base_label || '');
const priceType = (preview._price_type || preview.price_type || '');
let amountLabel = escapeHtml(preview.amount_label || '');
let baseLabel = escapeHtml(preview.base_label || '');
if (priceType === 'download_limit_over' && preview.download_limit_over_label) {
amountLabel = escapeHtml(preview.download_limit_over_label);
baseLabel = amountLabel;
preview.amount = preview.download_limit_over_price;
preview.base = preview.download_limit_over_price;
}
const title = escapeHtml(preview.title || '');
const typeLabel = escapeHtml(preview.pay_type_label || '');
const balance = typeof preview.balance === 'number' ? preview.balance : 0;
@@ -425,7 +467,7 @@
return;
}
body += '<div class="aksrt-dl-modal__actions">';
body += '<button type="button" class="btn-primary aksrt-dl-modal__download" data-aksrt-pay-submit data-mode="points">' + tt('pay_now', '立即支付') + ' ' + amountLabel + '</button>';
body += '<button type="button" class="btn-primary aksrt-dl-modal__download" data-aksrt-pay-submit data-mode="points" data-price-type="' + escapeAttr(priceType) + '">' + tt('pay_now', '立即支付') + ' ' + amountLabel + '</button>';
body += '</div>';
} else {
let methodHtml = '';
@@ -438,7 +480,7 @@
body += '<div class="aksrt-cashier__methods"><div class="aksrt-cashier__methods-title">' + tt('pay_method', '支付方式') + '</div>' + methodHtml + '</div>';
}
body += '<div class="aksrt-dl-modal__actions">';
body += '<button type="button" class="btn-primary aksrt-dl-modal__download" data-aksrt-pay-submit data-mode="cash">' + tt('pay_now', '立即支付') + ' ' + amountLabel + '</button>';
body += '<button type="button" class="btn-primary aksrt-dl-modal__download" data-aksrt-pay-submit data-mode="cash" data-price-type="' + escapeAttr(priceType) + '">' + tt('pay_now', '立即支付') + ' ' + amountLabel + '</button>';
body += '</div>';
}
@@ -464,6 +506,8 @@
const payload = { mode: mode };
if (mode === 'cash') payload.payment_method = paymentMethod || 'balance';
const pt = button.getAttribute('data-price-type') || '';
if (pt) payload.price_type = pt;
fetch(REST_BASE + '/pay/content/' + postId, {
method: 'POST',
@@ -488,18 +532,18 @@
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;
window.open(data.payment_url, '_blank');
}
open(
'<h3 class="aksrt-dl-modal__title">' + tt('scan_pay', '扫码支付') + '</h3>' +
'<p class="aksrt-dl-modal__msg">' + escapeHtml(data.message || tt('scan_hint', '请使用手机扫码完成支付,支付后刷新页面。')) + '</p>' +
'<p class="aksrt-dl-modal__msg"><code>' + escapeHtml(data.qr_content || '') + '</code></p>' +
(data.qr_content ? ('<p class="aksrt-dl-modal__msg"><code>' + escapeHtml(data.qr_content || '') + '</code></p>') : '') +
'<div class="aksrt-dl-modal__actions"><button type="button" class="btn-primary btn-sm" data-aksrt-reload>' + tt('paid_refresh', '我已支付') + '</button></div>',
returnFocusEl
);
const reloadBtn = document.querySelector('[data-aksrt-reload]');
if (reloadBtn) reloadBtn.addEventListener('click', function () { window.location.reload(); });
if (data.order_number) pollOrderPaid(data.order_number, returnFocusEl);
return;
}
window.location.reload();
@@ -530,7 +574,8 @@
'0',
10
);
if (postId) openCashier(postId, buyBtn);
const priceType = buyBtn.getAttribute('data-price-type') || '';
if (postId) openCashier(postId, buyBtn, priceType);
return;
}
@@ -586,7 +631,7 @@
'.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;}' +
'.resource-download-box__paid-badge{background:#ecfdf5!important;color:#047857!important;}.resource-download-box__paid-meta{display:grid;gap:4px;margin:8px 0 12px;padding:10px 12px;background:#f0fdf4;border:1px solid #bbf7d0;border-radius:10px;font-size:12px;color:#166534;}' +
'@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);
+205 -52
View File
@@ -23,6 +23,7 @@ class AKSRT_Content_Pay {
public function init(): void {
add_action('rest_api_init', [$this, 'register_routes']);
add_action('aksrt_order_paid', [$this, 'on_order_paid'], 15, 2);
}
public function register_routes(): void {
@@ -38,6 +39,12 @@ class AKSRT_Content_Pay {
'permission_callback' => [$this, 'check_rest_nonce'],
],
]);
register_rest_route('aksrt/v1', '/pay/content/order/(?P<order_number>[A-Za-z0-9_-]+)', [
'methods' => 'GET',
'callback' => [$this, 'rest_order_status'],
'permission_callback' => [$this, 'check_rest_nonce'],
]);
}
/**
@@ -72,7 +79,7 @@ class AKSRT_Content_Pay {
/**
* POST /pay/content/{id}
* body: { mode?: points|cash, payment_method?: balance|alipay|... }
* body: { mode?: points|cash, payment_method?: balance|alipay|..., price_type?: ''|download_limit_over }
*/
public function rest_purchase(WP_REST_Request $request) {
$post_id = (int) $request['id'];
@@ -90,7 +97,12 @@ class AKSRT_Content_Pay {
$params = $request->get_params();
}
$gate = $this->assert_purchasable($post_id, $uid);
$price_type = sanitize_key((string) ($params['price_type'] ?? ''));
if ($price_type !== 'download_limit_over') {
$price_type = '';
}
$gate = $this->assert_purchasable($post_id, $uid, $price_type);
if (is_wp_error($gate)) {
return $gate;
}
@@ -103,14 +115,73 @@ class AKSRT_Content_Pay {
$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));
return $this->rest_wrap_result($this->create_and_pay_points($post_id, $uid, $price_type));
}
$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));
return $this->rest_wrap_result($this->create_cash_order($post_id, $uid, $payment_method, $price_type));
}
/**
* GET /pay/content/order/{order_number}
*/
public function rest_order_status(WP_REST_Request $request) {
$uid = get_current_user_id();
if ($uid <= 0) {
return new WP_Error('aksrt_need_login', __('请先登录。', 'aksrt'), ['status' => 401]);
}
$order_number = sanitize_text_field((string) $request['order_number']);
if ($order_number === '' || !class_exists('AKSRT_Order_Repository')) {
return new WP_Error('aksrt_invalid', __('订单不存在', 'aksrt'), ['status' => 404]);
}
$order = AKSRT_Order_Repository::get_instance()->get_user_order($uid, $order_number);
if (!$order) {
return new WP_Error('aksrt_invalid', __('订单不存在', 'aksrt'), ['status' => 404]);
}
$status = (string) ($order['status'] ?? '');
$paid = class_exists('AKSRT_Domain_Model') && AKSRT_Domain_Model::is_paid_order_status($status);
return new WP_REST_Response([
'order_number' => $order_number,
'status' => $status,
'paid' => $paid,
'amount' => (float) ($order['amount'] ?? 0),
'currency' => (string) ($order['currency'] ?? ''),
'product_id' => (int) ($order['product_id'] ?? 0),
'product_type' => (string) ($order['product_type'] ?? ''),
'reload' => $paid,
], 200);
}
/**
* Bump content sales when a resource order is paid.
*
* @param string $order_number
* @param array $paid_order
*/
public function on_order_paid(string $order_number, array $paid_order): void {
$product_type = sanitize_key((string) ($paid_order['product_type'] ?? ''));
if ($product_type !== AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE) {
return;
}
$post_id = (int) ($paid_order['product_id'] ?? 0);
if ($post_id <= 0) {
return;
}
// Only count normal posts (content pay), not necessarily forum topics.
$post = get_post($post_id);
if (!($post instanceof WP_Post) || $post->post_type !== 'post') {
return;
}
if (function_exists('aksrt_bump_content_sales_volume')) {
aksrt_bump_content_sales_volume($post_id, 1);
} else {
$key = AKSRT_Domain_Model::RESOURCE_SALES_VOLUME_KEY;
$cur = (int) get_post_meta($post_id, $key, true);
update_post_meta($post_id, $key, $cur + 1);
}
}
/**
@@ -159,14 +230,24 @@ class AKSRT_Content_Pay {
$vip_level = (int) ($priced['vip_level'] ?? 0);
$amount = (float) ($priced['amount'] ?? 0);
$base = (float) ($priced['base'] ?? $amount);
$over_price = max(0.0, (float) ($meta['download_limit_over_price'] ?? 0));
$has_paid = false;
$paid_order = null;
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
);
if ($has_paid && function_exists('aksrt_get_user_content_paid_order')) {
$paid_order = aksrt_get_user_content_paid_order($post_id, $uid);
}
}
$pending_order = null;
if ($uid > 0 && !$has_paid && class_exists('AKSRT_Order_Repository')) {
$pending_order = $this->find_pending_content_order($uid, $post_id);
}
$need_vip = $pay_limit > 0 && $vip_level < $pay_limit;
@@ -179,11 +260,18 @@ class AKSRT_Content_Pay {
$amount_label = $this->format_amount_label($amount, $pay_modo, $currency);
$base_label = $this->format_amount_label($base, $pay_modo, $currency);
$over_label = $over_price > 0 ? $this->format_amount_label($over_price, $pay_modo, $currency) : '';
$vip_page = function_exists('aksrt_get_user_center_url')
? aksrt_get_user_center_url()
: home_url('/');
$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;
$quota_over = $free_limit > 0 && $free_used >= $free_limit;
return [
'post_id' => $post_id,
'title' => get_the_title($post),
@@ -203,6 +291,19 @@ class AKSRT_Content_Pay {
'base_label' => $base_label,
'is_free' => !empty($priced['is_free']) || $amount <= 0,
'has_paid_access' => $has_paid,
'paid_order' => $paid_order ? [
'order_number' => (string) ($paid_order['order_number'] ?? ''),
'paid_at' => (string) ($paid_order['paid_at'] ?? $paid_order['created_at'] ?? ''),
'amount' => (float) ($paid_order['amount'] ?? 0),
'currency' => (string) ($paid_order['currency'] ?? ''),
'payment_method' => (string) ($paid_order['payment_method'] ?? ''),
] : null,
'pending_order' => $pending_order ? [
'order_number' => (string) ($pending_order['order_number'] ?? ''),
'amount' => (float) ($pending_order['amount'] ?? 0),
'payment_method' => (string) ($pending_order['payment_method'] ?? ''),
'created_at' => (string) ($pending_order['created_at'] ?? ''),
] : null,
'need_login' => $uid <= 0,
'need_vip' => $need_vip,
'need_purchase' => !$has_paid && !$need_vip && $amount > 0,
@@ -213,6 +314,12 @@ class AKSRT_Content_Pay {
'login_url' => wp_login_url(get_permalink($post)),
'vip_url' => $vip_page,
'credits_url' => $vip_page,
'download_limit_over_price' => $pay_modo === 'points' ? (int) ceil($over_price) : round($over_price, 2),
'download_limit_over_label' => $over_label,
'allow_limit_over' => $over_price > 0,
'quota_over' => $quota_over,
'free_limit' => $free_limit,
'free_used' => $free_used,
];
}
@@ -221,8 +328,8 @@ class AKSRT_Content_Pay {
*
* @return array|WP_Error
*/
public function create_and_pay_points(int $post_id, int $user_id) {
$assert = $this->assert_purchasable($post_id, $user_id);
public function create_and_pay_points(int $post_id, int $user_id, string $price_type = '') {
$assert = $this->assert_purchasable($post_id, $user_id, $price_type);
if (is_wp_error($assert)) {
return $assert;
}
@@ -261,6 +368,7 @@ class AKSRT_Content_Pay {
'content_pay' => 1,
'pay_modo' => 'points',
'post_id' => $post_id,
'price_type' => $price_type,
'vip_level' => (int) ($priced['vip_level'] ?? 0),
'base' => (float) ($priced['base'] ?? $amount),
'amount' => $amount,
@@ -333,8 +441,8 @@ class AKSRT_Content_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);
public function create_cash_order(int $post_id, int $user_id, string $payment_method = 'balance', string $price_type = '') {
$assert = $this->assert_purchasable($post_id, $user_id, $price_type);
if (is_wp_error($assert)) {
return $assert;
}
@@ -360,46 +468,60 @@ class AKSRT_Content_Pay {
return new WP_Error('aksrt_pay_unavailable', __('支付系统不可用。', 'aksrt'), ['status' => 500]);
}
$payment_method = sanitize_key($payment_method);
// Resume existing pending content order for this post when amount matches.
$pending = $this->find_pending_content_order($user_id, $post_id);
if ($pending && abs((float) ($pending['amount'] ?? 0) - $amount) < 0.001) {
$order_number = (string) ($pending['order_number'] ?? '');
$order = $pending;
} else {
$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,
'price_type' => $price_type,
'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]);
}
}
$payment_method = sanitize_key($payment_method ?: (string) ($order['payment_method'] ?? 'balance'));
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)
);
$order['payment_method'] = $payment_method;
if (!class_exists('AKSRT_Payment_Gateway')) {
return new WP_Error('aksrt_pay_unavailable', __('支付网关不可用。', 'aksrt'), ['status' => 500]);
@@ -408,25 +530,22 @@ class AKSRT_Content_Pay {
$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,
'order_number' => $order_number,
]);
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,
'order_number' => $order_number,
]);
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, '余额'))
$code = ($payment_method === 'balance' && (str_contains($msg, '余额') || str_contains($msg, '不足')))
? 'aksrt_balance_insufficient'
: 'aksrt_pay_failed';
return new WP_Error($code, $msg, [
@@ -449,6 +568,7 @@ class AKSRT_Content_Pay {
'pay_modo' => '0',
'message' => $payment_result['message'] ?? __('请在新页面完成支付。', 'aksrt'),
'reload' => false,
'poll' => true,
],
];
}
@@ -478,7 +598,7 @@ class AKSRT_Content_Pay {
/**
* @return array{post:WP_Post,meta:array,priced:array}|WP_Error
*/
private function assert_purchasable(int $post_id, int $user_id) {
private function assert_purchasable(int $post_id, int $user_id, string $price_type = '') {
$post = get_post($post_id);
if (!($post instanceof WP_Post) || $post->post_status !== 'publish') {
return new WP_Error('aksrt_invalid', __('内容不存在', 'aksrt'), ['status' => 404]);
@@ -492,7 +612,7 @@ class AKSRT_Content_Pay {
$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) {
if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_NO && $price_type !== 'download_limit_over') {
return new WP_Error('aksrt_not_paid', __('该内容无需购买', 'aksrt'), ['status' => 400]);
}
if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_VIP) {
@@ -503,9 +623,21 @@ class AKSRT_Content_Pay {
? 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];
// Free daily quota overbuy: override amount, ignore VIP discount (zibll).
if ($price_type === 'download_limit_over') {
$over = max(0.0, (float) ($meta['download_limit_over_price'] ?? 0));
if ($over <= 0) {
return new WP_Error('aksrt_no_overbuy', __('未开放超额下载购买。', 'aksrt'), ['status' => 400]);
}
$priced['amount'] = $over;
$priced['base'] = $over;
$priced['is_free'] = false;
$priced['vip_amount'] = -1.0;
}
$pay_limit = (int) ($priced['pay_limit'] ?? 0);
$vip_level = (int) ($priced['vip_level'] ?? 0);
if ($pay_limit > 0 && $vip_level < $pay_limit) {
if ($pay_limit > 0 && $vip_level < $pay_limit && $price_type !== 'download_limit_over') {
return new WP_Error('aksrt_need_vip', sprintf(
/* translators: %d: required VIP level */
__('该资源仅限 VIP%s 及以上会员购买/下载。', 'aksrt'),
@@ -535,6 +667,27 @@ class AKSRT_Content_Pay {
];
}
/**
* @return array<string,mixed>|null
*/
private function find_pending_content_order(int $user_id, int $post_id): ?array {
if ($user_id <= 0 || $post_id <= 0 || !class_exists('AKSRT_Order_Repository')) {
return null;
}
$orders = AKSRT_Order_Repository::get_instance()->get_user_orders($user_id, [
'product_type' => AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE,
'status' => AKSRT_Domain_Model::ORDER_STATUS_PENDING,
'limit' => 20,
'offset' => 0,
]);
foreach ($orders as $order) {
if ((int) ($order['product_id'] ?? 0) === $post_id) {
return $order;
}
}
return null;
}
/**
* @return array<int, array{id:string,name:string}>
*/
+14 -1
View File
@@ -276,9 +276,22 @@ class AKSRT_Content_Visibility {
// Zibll daily free-download quota (only when free / already unlocked).
if ($cost <= 0 && function_exists('aksrt_check_free_download_quota')) {
// Provide per-post overbuy price to quota error.
$over_cb = static function ($over, $uid) use ($resource_post) {
if (!($resource_post instanceof WP_Post) || !function_exists('aksrt_get_pay_meta')) {
return $over;
}
$m = aksrt_get_pay_meta($resource_post);
return max(0.0, (float) ($m['download_limit_over_price'] ?? 0));
};
add_filter('aksrt_download_limit_over_price', $over_cb, 10, 2);
$quota = aksrt_check_free_download_quota($uid, $cost);
remove_filter('aksrt_download_limit_over_price', $over_cb, 10);
if (is_wp_error($quota)) {
return $quota;
$data = (array) $quota->get_error_data();
$data['post_id'] = (int) ($state['post_id'] ?? 0);
$data['pay_modo'] = (string) ($state['pay_modo'] ?? '0');
return new WP_Error($quota->get_error_code(), $quota->get_error_message(), $data);
}
}
@@ -299,6 +299,14 @@ class AKSRT_Resource_Meta_Box {
</div>
</div>
<div class="aksrt-pmb__row">
<label for="aksrt_pay_limit_over_price"><?php esc_html_e('超额下载补价', 'aksrt'); ?></label>
<div>
<input type="number" id="aksrt_pay_limit_over_price" name="aksrt_pay_limit_over_price" value="<?php echo esc_attr((string) ($meta['download_limit_over_price'] ?? 0)); ?>" min="0" step="0.01">
<p class="aksrt-pmb__hint"><?php esc_html_e('免费日限额用尽后允许购买的补价(现金/积分随支付方式)。0 = 不允许超额购买。对齐 zibll download_limit_over_price。', 'aksrt'); ?></p>
</div>
</div>
<div class="aksrt-pmb__row">
<label for="aksrt_pay_stock"><?php esc_html_e('库存', 'aksrt'); ?></label>
<div>
@@ -497,6 +505,7 @@ class AKSRT_Resource_Meta_Box {
'card_keys' => isset($_POST['aksrt_pay_card_keys']) ? array_filter(array_map('trim', explode("\n", (string) $_POST['aksrt_pay_card_keys']))) : [],
'free_preview' => $_POST['aksrt_pay_free_preview'] ?? '',
'show_sales' => !empty($_POST['aksrt_pay_show_sales']),
'download_limit_over_price' => max(0, (float) ($_POST['aksrt_pay_limit_over_price'] ?? 0)),
]);
update_post_meta($post_id, AKSRT_Domain_Model::PAY_META_KEY, $pay_meta);
+4
View File
@@ -21,6 +21,7 @@ class AKSRT_Domain_Model {
public const RESOURCE_MARKER_KEY = '_aksrt_is_resource';
public const RESOURCE_DOWNLOAD_COUNT_KEY = '_aksrt_res_download_count';
public const RESOURCE_VIEW_COUNT_KEY = '_aksrt_res_view_count';
public const RESOURCE_SALES_VOLUME_KEY = '_aksrt_sales_volume';
public const POST_VIEW_COUNT_KEY = 'aksrt_post_views';
public const ORDER_MIGRATED_OPTION = 'aksrt_order_migrated_to_table';
@@ -134,6 +135,8 @@ class AKSRT_Domain_Model {
'purchase_limit' => 0,
'free_preview' => '',
'show_sales' => true,
// Zibll: free daily quota overbuy price (cash or points by pay_modo).
'download_limit_over_price' => 0.0,
];
}
@@ -333,6 +336,7 @@ class AKSRT_Domain_Model {
$meta['purchase_limit'] = max(0, (int) ($raw['purchase_limit'] ?? 0));
$meta['free_preview'] = wp_kses_post((string) ($raw['free_preview'] ?? ''));
$meta['show_sales'] = !isset($raw['show_sales']) || !empty($raw['show_sales']);
$meta['download_limit_over_price'] = max(0, (float) ($raw['download_limit_over_price'] ?? 0));
return $meta;
}
+52 -1
View File
@@ -150,12 +150,14 @@ if (!function_exists('aksrt_get_download_box_meta')) {
$download_count = (int) get_post_meta($p->ID, AKSRT_Domain_Model::RESOURCE_DOWNLOAD_COUNT_KEY, true);
$view_count = (int) get_post_meta($p->ID, AKSRT_Domain_Model::POST_VIEW_COUNT_KEY, true);
$virtual_sales = max(0, (int) ($pay_meta['virtual_sales'] ?? 0));
$sales_volume = (int) get_post_meta($p->ID, AKSRT_Domain_Model::RESOURCE_SALES_VOLUME_KEY, true);
$show_sales = !isset($pay_meta['show_sales']) || !empty($pay_meta['show_sales']);
$pay_modo = (($pay_meta['pay_modo'] ?? '0') === 'points') ? 'points' : '0';
$pay_limit = (int) ($pay_meta['pay_limit'] ?? 0);
if (!in_array($pay_limit, [0, 1, 2], true)) {
$pay_limit = 0;
}
$over_price = max(0.0, (float) ($pay_meta['download_limit_over_price'] ?? 0));
return [
'version' => (string) ($pay_meta['version'] ?? ''),
@@ -177,8 +179,11 @@ if (!function_exists('aksrt_get_download_box_meta')) {
'download_count' => max(0, $download_count),
'view_count' => max(0, $view_count),
'virtual_sales' => $virtual_sales,
'sales_count' => $show_sales ? ($virtual_sales + max(0, $download_count)) : 0,
'sales_volume' => max(0, $sales_volume),
// Zibll: virtual (pay_cuont) + real sales_volume; fall back to downloads when no sales yet.
'sales_count' => $show_sales ? ($virtual_sales + max(0, $sales_volume, $download_count)) : 0,
'show_sales' => $show_sales,
'download_limit_over_price' => $over_price,
'require_login' => !empty($pay_meta['require_login']),
'require_captcha' => !empty($pay_meta['require_captcha']),
'subject_label' => __('内容', 'aksrt'),
@@ -186,6 +191,52 @@ if (!function_exists('aksrt_get_download_box_meta')) {
}
}
/**
* Latest paid content order for a user/post (resource product_type).
*
* @return array<string,mixed>|null
*/
if (!function_exists('aksrt_get_user_content_paid_order')) {
function aksrt_get_user_content_paid_order(int $post_id, int $user_id = 0): ?array {
$uid = $user_id > 0 ? $user_id : get_current_user_id();
if ($uid <= 0 || $post_id <= 0 || !class_exists('AKSRT_Order_Repository')) {
return null;
}
$orders = AKSRT_Order_Repository::get_instance()->get_user_orders($uid, [
'product_type' => AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE,
'status' => '',
'limit' => 20,
'offset' => 0,
]);
foreach ($orders as $order) {
if ((int) ($order['product_id'] ?? 0) !== $post_id) {
continue;
}
$status = (string) ($order['status'] ?? '');
if (AKSRT_Domain_Model::is_paid_order_status($status)) {
return $order;
}
}
return null;
}
}
/**
* Increment real sales volume for a paid content post (idempotent per order via order meta note flag optional).
*/
if (!function_exists('aksrt_bump_content_sales_volume')) {
function aksrt_bump_content_sales_volume(int $post_id, int $delta = 1): int {
if ($post_id <= 0 || $delta === 0) {
return 0;
}
$key = AKSRT_Domain_Model::RESOURCE_SALES_VOLUME_KEY;
$cur = (int) get_post_meta($post_id, $key, true);
$next = max(0, $cur + $delta);
update_post_meta($post_id, $key, $next);
return $next;
}
}
/**
* Resolve the amount a given user should pay for a post (Zibll VIP tier min).
*
+13 -5
View File
@@ -270,14 +270,22 @@ if (!function_exists('aksrt_check_free_download_quota')) {
$used = aksrt_get_user_free_downloaded_number($user_id);
if ($used >= $limit) {
$data = [
'status' => 429,
'limit' => $limit,
'used' => $used,
];
// Optional overbuy price for free-quota exhausted (Zibll download_limit_over_price).
// Callers may pass post context via filter.
$over = apply_filters('aksrt_download_limit_over_price', 0.0, $user_id);
if ($over > 0) {
$data['download_limit_over_price'] = $over;
$data['allow_limit_over'] = true;
}
return new WP_Error(
'aksrt_download_limit',
__('您今日下载免费资源个数已超限,请明日再下载。', 'aksrt'),
[
'status' => 429,
'limit' => $limit,
'used' => $used,
]
$data
);
}
+42
View File
@@ -170,6 +170,13 @@ if ($is_need_purchase && $user_amount > 0) {
</span>
<?php endif; ?>
<?php
$paid_order = null;
if ($has_paid_access && is_user_logged_in() && function_exists('aksrt_get_user_content_paid_order')) {
$paid_order = aksrt_get_user_content_paid_order((int) $post_id);
}
?>
<div class="resource-download-box__body">
<?php if ($has_thumbnail) : ?>
<div class="resource-download-box__thumb-col">
@@ -194,6 +201,31 @@ if ($is_need_purchase && $user_amount > 0) {
</span>
</header>
<?php if ($has_paid_access && $user_amount > 0) : ?>
<div class="resource-download-box__pricing">
<span class="resource-download-box__price is-free"><?php esc_html_e('已解锁', 'aksrt'); ?></span>
<?php echo $pay_type_badge && !$has_thumbnail ? $pay_type_badge : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<?php if (is_array($paid_order) && !empty($paid_order['order_number'])) : ?>
<div class="resource-download-box__paid-meta">
<span><?php printf(esc_html__('订单号:%s', 'aksrt'), esc_html((string) $paid_order['order_number'])); ?></span>
<?php if (!empty($paid_order['paid_at'])) : ?>
<span><?php printf(esc_html__('支付时间:%s', 'aksrt'), esc_html((string) $paid_order['paid_at'])); ?></span>
<?php endif; ?>
<span>
<?php
$paid_amt = (float) ($paid_order['amount'] ?? 0);
$paid_cur = (string) ($paid_order['currency'] ?? '');
if ($paid_cur === 'points') {
printf(esc_html__('实付:%s %s', 'aksrt'), esc_html(function_exists('aksrt_format_price') ? aksrt_format_price($paid_amt) : (string) $paid_amt), esc_html($currency));
} else {
printf(esc_html__('实付:%s%s', 'aksrt'), esc_html($cash_symbol), esc_html(function_exists('aksrt_format_price') ? aksrt_format_price($paid_amt) : (string) $paid_amt));
}
?>
</span>
</div>
<?php endif; ?>
<?php else : ?>
<div class="resource-download-box__pricing">
<span class="resource-download-box__price<?php echo !empty($price_label['free']) || $has_paid_access ? ' is-free' : ''; ?>">
<?php
@@ -217,6 +249,7 @@ if ($is_need_purchase && $user_amount > 0) {
<?php endif; ?>
<?php echo $pay_type_badge && !$has_thumbnail ? $pay_type_badge : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
<?php endif; ?>
<?php if ($has_vip && !$member_blocked && !$has_paid_access) : ?>
<div class="resource-download-box__vip-pricing">
@@ -380,8 +413,17 @@ if ($is_need_purchase && $user_amount > 0) {
$dl_used = function_exists('aksrt_get_user_free_downloaded_number') ? aksrt_get_user_free_downloaded_number() : 0;
$dl_surplus = max(0, $dl_limit - $dl_used);
$vip_lv = function_exists('aksrt_get_user_vip_level') ? aksrt_get_user_vip_level() : 0;
$over_price = max(0.0, (float) ($meta['download_limit_over_price'] ?? 0));
if ($dl_surplus <= 0) {
echo '<div class="resource-download-box__quota-notice"><span class="badg c-red btn-block">' . esc_html__('您今日下载免费资源个数已超限,请明日再下载。', 'aksrt') . '</span></div>';
if ($over_price > 0 && !$has_paid_access) {
$over_label = $pay_modo === 'points'
? sprintf(__('立即购买(%s %s)', 'aksrt'), function_exists('aksrt_format_price') ? aksrt_format_price($over_price) : (string) $over_price, $currency)
: sprintf(__('立即购买(%s%s)', 'aksrt'), $cash_symbol, function_exists('aksrt_format_price') ? aksrt_format_price($over_price) : (string) $over_price);
echo '<div class="mt10 resource-download-box__purchase-gate">';
echo '<button type="button" class="resource-download-box__btn btn-primary resource-download-box__btn--purchase cashier-link" data-action="aksrt-need-purchase" data-aksrt-pay-content="' . esc_attr((string) $post_id) . '" data-post-id="' . esc_attr((string) $post_id) . '" data-price-type="download_limit_over" data-pay-modo="' . esc_attr($pay_modo) . '" data-amount="' . esc_attr((string) $over_price) . '">';
echo '<i class="fas fa-shopping-cart" aria-hidden="true"></i><span>' . esc_html($over_label) . '</span></button></div>';
}
} else {
$who = $vip_lv > 0 ? esc_html__('您是尊贵的会员,', 'aksrt') : esc_html__('您', 'aksrt');
echo '<div class="resource-download-box__quota-notice"><span class="badg c-red btn-block">' . $who . esc_html(sprintf(__('今日还可下载 %s 个免费资源', 'aksrt'), number_format_i18n($dl_surplus))) . '</span></div>';