feat(pay): content cashier, resource orders at VIP price, purchase/download JS loop

Wire zibll-style one-shot points/cash cashier for post+_aksrt_pay downloads:
server amount from aksrt_get_user_pay_price, ORDER_PRODUCT_RESOURCE unlock,
frontend need-purchase opens modal, download box purchased/VIP/purchase CTAs.
This commit is contained in:
kate522 committed 2026-07-14 11:52:44 +08:00
1 parent abe2f57417
commit 770205623d
11 files changed
+1142 -170

No files matched your search

+3
View File
@@ -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';
+582
View File
@@ -0,0 +1,582 @@
<?php
/**
* AKSRT Content Pay — post/_aksrt_pay cashier + resource orders.
*
* Server-side amount always from aksrt_get_user_pay_price().
* product_type = ORDER_PRODUCT_RESOURCE (post_id).
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
class AKSRT_Content_Pay {
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
add_action('rest_api_init', [$this, 'register_routes']);
}
public function register_routes(): void {
register_rest_route('aksrt/v1', '/pay/content/(?P<id>\\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<int, array{id:string,name:string}>
*/
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;
}
}
+31 -50
View File
@@ -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;
}
+27 -23
View File
@@ -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;
}
@@ -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
);
+67 -9
View File
@@ -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 = '<ul class="' . esc_attr($class) . '">';
foreach ($rows as $row) {
$html .= '<li class="' . esc_attr($class) . '-item">';
$html .= '<span class="' . esc_attr($class) . '-name">' . esc_html($row['name']) . '</span>';
$html .= '<span class="' . esc_attr($class) . '-value">' . esc_html($row['value']) . '</span>';
$html .= '</li>';
}
$html .= '</ul>';
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')) {
<h3 class="aksrt-paywall__title"><?php echo esc_html($type_info['label']); ?></h3>
<p class="aksrt-paywall__desc"><?php echo esc_html($type_info['desc']); ?></p>
<div class="aksrt-paywall__price">
<?php if ($pay_limit > 0) : ?>
<?php if ($member_blocked) : ?>
<span class="aksrt-paywall__price-member"><?php esc_html_e('会员专属', 'aksrt'); ?></span>
<?php elseif ($price > 0) : ?>
<span class="aksrt-paywall__price-amount"><?php echo esc_html($price_text); ?></span>
@@ -704,13 +762,13 @@ if (!function_exists('aksrt_get_paywall_html')) {
<?php endif; ?>
</div>
<?php if (!$is_logged_in) : ?>
<a href="<?php echo esc_url($login_url); ?>" class="but jb-red padding-lg cashier-link aksrt-paywall__btn">
<a href="<?php echo esc_url($login_url); ?>" class="but jb-red padding-lg aksrt-paywall__btn">
<i class="fa fa-sign-in" aria-hidden="true"></i> <?php esc_html_e('登录后购买', 'aksrt'); ?>
</a>
<?php elseif ($pay_limit > 0) : ?>
<span class="but jb-vip padding-lg aksrt-paywall__btn is-disabled">
<?php elseif ($member_blocked) : ?>
<a href="<?php echo esc_url($vip_url); ?>" class="but jb-vip padding-lg aksrt-paywall__btn">
<i class="fas fa-crown" aria-hidden="true"></i> <?php esc_html_e('开通会员后可查看', 'aksrt'); ?>
</span>
</a>
<?php elseif ($price > 0) : ?>
<button type="button" class="but <?php echo $pay_modo === 'points' ? 'jb-yellow' : 'jb-red'; ?> padding-lg cashier-link aksrt-paywall__btn" data-aksrt-pay-content="<?php echo esc_attr((string) $post->ID); ?>">
<i class="fa fa-shopping-cart" aria-hidden="true"></i> <?php printf(esc_html__('立即购买(%s)', 'aksrt'), $price_text); ?>
+8 -8
View File
@@ -486,8 +486,8 @@ if (!function_exists('aksrt_resource_download_cost')) {
$pay_modo = (($pay['pay_modo'] ?? '0') === 'points') ? 'points' : '0';
}
// Cash mode: order/checkout path unless legacy download_credit is on.
if ($pay_modo !== 'points' && empty($paid_settings['download_credit'])) {
// Cash mode always unlocks via resource order (cashier), never per-download credits.
if ($pay_modo !== 'points') {
return 0;
}
@@ -495,6 +495,10 @@ if (!function_exists('aksrt_resource_download_cost')) {
return 0;
}
// Points mode: one-time purchase via cashier. Unpaid amount is not deducted here —
// can_download returns need_purchase; after order, has_paid_access zeros cost above.
// Return display/gate cost only for free-tier (0) detection; unpaid still shows amount
// but rest_request must not deduct without paid order (see Content_Visibility).
if (is_array($priced)) {
$price = (float) ($priced['amount'] ?? 0);
if ($price > 0) {
@@ -505,15 +509,11 @@ if (!function_exists('aksrt_resource_download_cost')) {
// Fallback without helper.
$meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : aksrt_get_resource_meta($post);
if ($pay_modo === 'points') {
$price = (float) ($meta['points_price'] ?? $meta['price'] ?? 0);
} else {
$price = (float) ($meta['price'] ?? 0);
}
$price = (float) ($meta['points_price'] ?? $meta['price'] ?? 0);
if ($price > 0) {
return (int) ceil($price);
}
return max(0, (int) ($paid_settings['default_cost'] ?? 0));
return 0;
}
}