feat(pay): sales volume, paid order meta, pending resume, limit-over buy

Bump content sales on resource order paid; show order number/time in download
box; poll pending gateway payments; support download_limit_over_price overbuy.
This commit is contained in:
2026-07-14 12:04:59 +08:00
parent 770205623d
commit 8f15d16d35
8 changed files with 397 additions and 72 deletions
+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
);
}