fix(pay): unify download gate on cashier unlock not per-download credits

Route can_download/Download_State/permission card through Content_Visibility need_purchase; drop download_credit as a second toll; clarify admin labels for points purchase mode.
This commit is contained in:
kate522 committed 2026-07-14 20:38:17 +08:00
1 parent 1039b26efd
commit 79f8c43970
5 files changed
+311 -291

No files matched your search

+156 -111
View File
@@ -3,8 +3,7 @@
* AKSRT Download State
*
* Unified download state resolver for resource pages.
* Replaces scattered per-template download status checks
* with a single service that returns a normalized state object.
* Driven by AKSRT_Content_Visibility (cashier unlock), not per-download credits.
*
* @package AKSRT
*/
@@ -15,15 +14,15 @@ class AKSRT_Download_State {
private static $instance = null;
/**
* State constants
*/
const STATE_NOT_LOGGED_IN = 'not_logged_in';
const STATE_NO_PERMISSION = 'no_permission';
const STATE_INSUFFICIENT_CREDITS = 'insufficient_credits';
const STATE_OWNED = 'owned';
const STATE_AVAILABLE = 'available';
const STATE_PENDING_DELIVERY = 'pending_delivery';
const STATE_NOT_LOGGED_IN = 'not_logged_in';
const STATE_NO_PERMISSION = 'no_permission';
const STATE_NEED_PURCHASE = 'need_purchase';
const STATE_NEED_VIP = 'need_vip';
const STATE_OWNED = 'owned';
const STATE_AVAILABLE = 'available';
const STATE_PENDING_DELIVERY = 'pending_delivery';
/** @deprecated use STATE_NEED_PURCHASE — kept for old templates */
const STATE_INSUFFICIENT_CREDITS = 'need_purchase';
private function __construct() {}
@@ -38,66 +37,57 @@ class AKSRT_Download_State {
* Resolve the current user's download state for a given resource post.
*
* @param int $post_id Resource post ID.
* @return array {
* @type string $state One of the STATE_* constants.
* @type bool $can_download Whether the user can initiate download.
* @type string $message Human-readable status message.
* @type string $action_label Suggested button label.
* @type string $action_url Suggested button URL (if applicable).
* @type int $cost Download cost in credits.
* @type int $balance Current user credit balance.
* @type string $login_url Login URL (if not logged in).
* @return array{
* state:string,can_download:bool,message:string,action_label:string,
* action_url:string,cost:int,balance:int,login_url:string,pay_modo:string,
* amount:float,need_purchase:bool,need_vip:bool,has_paid_access:bool
* }
*/
public function resolve(int $post_id): array {
$meta = function_exists('aksrt_get_resource_meta') ? aksrt_get_resource_meta($post_id) : [];
$price = (float) ($meta['price'] ?? 0);
$downloads = is_array($meta['downloads'] ?? null) ? $meta['downloads'] : [];
$login_url = wp_login_url(get_permalink($post_id));
$is_logged_in = is_user_logged_in();
$user_id = $is_logged_in ? get_current_user_id() : 0;
$login_url = wp_login_url(get_permalink($post_id));
// No downloads available
if (empty($downloads)) {
$meta = function_exists('aksrt_get_download_box_meta')
? aksrt_get_download_box_meta($post_id)
: (function_exists('aksrt_get_resource_meta') ? aksrt_get_resource_meta($post_id) : []);
$downloads = is_array($meta['downloads'] ?? null) ? $meta['downloads'] : [];
if ($downloads === []) {
return $this->build_result(
self::STATE_NO_PERMISSION,
false,
__('该资源暂未提供下载链接。', 'aksrt'),
__('暂无下载', 'aksrt'),
'',
0, 0, $login_url
0,
0,
$login_url
);
}
// Not logged in
if (!$is_logged_in) {
$requires_login = false;
if (class_exists('AKSRT_Settings')) {
$paid = AKSRT_Settings::get_paid_content_settings();
$requires_login = !empty($paid['download_requires_login']);
}
if ($requires_login) {
return $this->build_result(
self::STATE_NOT_LOGGED_IN,
false,
__('登录后可下载此资源。', 'aksrt'),
__('登录下载', 'aksrt'),
$login_url,
0, 0, $login_url
);
}
}
$access = class_exists('AKSRT_Content_Visibility')
? AKSRT_Content_Visibility::get_post_access_state($post_id, $user_id)
: [];
$priced = function_exists('aksrt_get_user_pay_price')
? aksrt_get_user_pay_price($post_id, $user_id)
: null;
$pay_modo = is_array($priced)
? ((($priced['modo'] ?? '0') === 'points') ? 'points' : '0')
: '0';
$amount = is_array($priced) ? (float) ($priced['amount'] ?? 0) : 0.0;
$cost = function_exists('aksrt_resource_download_cost')
? (int) aksrt_resource_download_cost($post_id)
: 0;
$balance = ($user_id > 0 && class_exists('AKSRT_Credits'))
? AKSRT_Credits::balance($user_id)
: 0;
// Check gate (credits, permission, etc.)
$gate = function_exists('aksrt_can_download') ? aksrt_can_download($post_id) : true;
$gate = function_exists('aksrt_can_download') ? aksrt_can_download($post_id, $user_id) : true;
if (is_wp_error($gate)) {
$code = $gate->get_error_code();
$data = (array) $gate->get_error_data();
$balance = (int) ($data['balance'] ?? 0);
$cost = function_exists('aksrt_resource_download_cost')
? aksrt_resource_download_cost($post_id)
: (int) ceil($price);
$msg = $gate->get_error_message() ?: __('无下载权限。', 'aksrt');
if ($code === 'aksrt_need_login') {
return $this->build_result(
@@ -106,83 +96,132 @@ class AKSRT_Download_State {
__('登录后可下载此资源。', 'aksrt'),
__('登录下载', 'aksrt'),
(string) ($data['login_url'] ?? $login_url),
$cost, $balance, (string) ($data['login_url'] ?? $login_url)
$cost,
$balance,
(string) ($data['login_url'] ?? $login_url),
[
'pay_modo' => $pay_modo,
'amount' => $amount,
'need_purchase' => false,
'need_vip' => false,
'has_paid_access' => !empty($access['has_paid_access']),
]
);
}
if ($code === 'aksrt_credits_insufficient') {
if ($code === 'aksrt_need_vip') {
$vip_url = function_exists('aksrt_get_user_center_tab_url')
? aksrt_get_user_center_tab_url('credits')
: $login_url;
return $this->build_result(
self::STATE_INSUFFICIENT_CREDITS,
self::STATE_NEED_VIP,
false,
sprintf(
/* translators: 1: current balance, 2: required cost */
__('当前余额 %1$d 积分,需要 %2$d 积分。', 'aksrt'),
$balance,
$cost
),
sprintf(__('付费下载(%d 积分)', 'aksrt'), $cost),
$msg,
__('开通会员', 'aksrt'),
$vip_url,
$cost,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => $amount,
'need_purchase' => false,
'need_vip' => true,
'has_paid_access' => false,
]
);
}
// need_purchase / legacy insufficient credits → cashier.
if (in_array($code, ['aksrt_need_purchase', 'aksrt_credits_insufficient'], true)) {
$label = $pay_modo === 'points'
? sprintf(__('购买(%s 积分)', 'aksrt'), (string) (int) ceil($amount > 0 ? $amount : $cost))
: __('去购买', 'aksrt');
return $this->build_result(
self::STATE_NEED_PURCHASE,
false,
$msg ?: __('请先购买后再下载。', 'aksrt'),
$label,
'',
$cost, $balance, $login_url
$cost,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => $amount > 0 ? $amount : (float) $cost,
'need_purchase' => true,
'need_vip' => false,
'has_paid_access' => false,
]
);
}
// Other gate error — treat as no permission
return $this->build_result(
self::STATE_NO_PERMISSION,
false,
$gate->get_error_message() ?: __('无下载权限。', 'aksrt'),
$msg,
__('无权限', 'aksrt'),
'',
0, 0, $login_url
$cost,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => $amount,
'need_purchase' => !empty($access['need_purchase']),
'need_vip' => !empty($access['need_vip']),
'has_paid_access' => !empty($access['has_paid_access']),
]
);
}
// Check if user already owns/downloads
$has_downloaded = false;
if ($is_logged_in) {
$downloaded = get_user_meta($user_id, '_aksrt_downloaded_resources', true);
$downloaded = is_array($downloaded) ? $downloaded : [];
$has_downloaded = in_array($post_id, $downloaded, true);
}
$cost = function_exists('aksrt_resource_download_cost')
? aksrt_resource_download_cost($post_id)
: (int) ceil($price);
$balance = 0;
if ($is_logged_in && class_exists('AKSRT_Credits')) {
$balance = AKSRT_Credits::balance($user_id);
}
if ($has_downloaded) {
// Paid / free unlock.
$has_paid = !empty($access['has_paid_access']);
if ($has_paid || $amount <= 0) {
$owned = $has_paid && $amount > 0;
return $this->build_result(
self::STATE_OWNED,
$owned ? self::STATE_OWNED : self::STATE_AVAILABLE,
true,
__('已下载,可再次下载。', 'aksrt'),
__('再次下载', 'aksrt'),
$owned
? __('已购买,可下载。', 'aksrt')
: __('此资源可免费下载。', 'aksrt'),
$owned ? __('立即下载', 'aksrt') : __('立即下载', 'aksrt'),
'',
$cost, $balance, $login_url
0,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => 0.0,
'need_purchase' => false,
'need_vip' => false,
'has_paid_access' => true,
]
);
}
// Available
$label = $cost > 0
? sprintf(__('下载(%d 积分)', 'aksrt'), $cost)
: __('立即下载', 'aksrt');
// Gate true but still priced unpaid (should be rare).
return $this->build_result(
self::STATE_AVAILABLE,
true,
$cost > 0
? sprintf(__('下载此资源将消耗 %d 积分。', 'aksrt'), $cost)
: __('此资源可免费下载。', 'aksrt'),
$label,
self::STATE_NEED_PURCHASE,
false,
__('请先购买后再下载。', 'aksrt'),
__('去购买', 'aksrt'),
'',
$cost, $balance, $login_url
$cost,
$balance,
$login_url,
[
'pay_modo' => $pay_modo,
'amount' => $amount,
'need_purchase' => true,
'need_vip' => false,
'has_paid_access' => false,
]
);
}
/**
* Build a normalized result array.
* @param array<string,mixed> $extra
*/
private function build_result(
string $state,
@@ -192,17 +231,23 @@ class AKSRT_Download_State {
string $action_url,
int $cost,
int $balance,
string $login_url
string $login_url,
array $extra = []
): array {
return [
'state' => $state,
'can_download' => $can_download,
'message' => $message,
'action_label' => $action_label,
'action_url' => $action_url,
'cost' => $cost,
'balance' => $balance,
'login_url' => $login_url,
];
return array_merge([
'state' => $state,
'can_download' => $can_download,
'message' => $message,
'action_label' => $action_label,
'action_url' => $action_url,
'cost' => $cost,
'balance' => $balance,
'login_url' => $login_url,
'pay_modo' => '0',
'amount' => 0.0,
'need_purchase' => false,
'need_vip' => false,
'has_paid_access' => false,
], $extra);
}
}
+18 -16
View File
@@ -653,9 +653,8 @@ if (!function_exists('aksrt_can_download')) {
/**
* 下载授权门禁 —— 单一合同。
*
* 实际判定全部交给 AKSRT_Content_Visibility::can_download_resource()
* (由 inc/helpers/aksrt-resource.php 的同名 helper 在更早加载时定义)。
* 此处仅作为该 helper 不可用时的兜底,避免出现第二套下载真相。
* 实际判定交给 AKSRT_Content_Visibility::can_download_resource()。
* 不再使用 download_credit 做按次扣积分门禁。
*
* @param int|WP_Post|null $post
* @param int $user_id 0 表示当前用户
@@ -672,13 +671,13 @@ if (!function_exists('aksrt_can_download')) {
}
$meta = aksrt_get_pay_meta($p);
if ($meta['pay_type'] !== AKSRT_PAY_TYPE_DOWNLOAD) {
if (($meta['pay_type'] ?? '') !== AKSRT_PAY_TYPE_DOWNLOAD) {
return new WP_Error('aksrt_not_downloadable', __('该内容不是可下载资源。', 'aksrt'));
}
$paid_settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_paid_content_settings()
: ['download_login' => true, 'download_credit' => true];
: ['download_login' => true];
$uid = $user_id > 0 ? $user_id : get_current_user_id();
@@ -687,18 +686,21 @@ if (!function_exists('aksrt_can_download')) {
return new WP_Error('aksrt_need_login', __('请先登录。', 'aksrt'), ['login_url' => $login_url]);
}
if ($uid > 0 && aksrt_has_paid_access($p, $uid)) return true;
if ($uid > 0 && function_exists('aksrt_is_vip_exempt') && aksrt_is_vip_exempt($uid)) return true;
if ($uid > 0 && function_exists('aksrt_has_paid_access') && aksrt_has_paid_access($p, $uid)) {
return true;
}
if ($uid > 0 && function_exists('aksrt_is_vip_exempt') && aksrt_is_vip_exempt($uid)) {
return true;
}
$cost = aksrt_get_download_cost($p);
if ($cost > 0 && !empty($paid_settings['download_credit'])) {
$balance = function_exists('aksrt_user_credits') ? aksrt_user_credits($uid) : 0;
if ($balance < $cost) {
return new WP_Error('aksrt_credits_insufficient', __('积分不足。', 'aksrt'), [
'balance' => $balance,
'cost' => $cost,
]);
}
// Fallback without Content_Visibility: require purchase when priced.
$cost = function_exists('aksrt_get_download_cost') ? aksrt_get_download_cost($p) : 0;
if ($cost > 0) {
return new WP_Error('aksrt_need_purchase', __('请先购买后再下载。', 'aksrt'), [
'status' => 402,
'cost' => $cost,
'post_id'=> (int) $p->ID,
]);
}
return true;
+20 -62
View File
@@ -383,9 +383,10 @@ if (!function_exists('aksrt_is_download_credit_exempt_user')) {
if (!function_exists('aksrt_can_download')) {
/**
* 是否可下载(P3 阶段:登录 + 积分校验已接入;P5 接入验证码)。
* 是否可下载。
*
* 返回 WP_Error 表示拒绝;返回 true 表示放行。
* 单一合同:委托 AKSRT_Content_Visibility(收银台订单解锁 / VIP / 登录 / 日限额)。
* 不再用 cms_features.download_credit 做「按次扣积分」门禁。
*
* @param int|WP_Post|null $post
* @param int $user_id 0 表示当前用户
@@ -397,68 +398,40 @@ if (!function_exists('aksrt_can_download')) {
}
$p = get_post($post);
if (!($p instanceof WP_Post) || !aksrt_is_resource_post($p)) {
if (!($p instanceof WP_Post)) {
return new WP_Error('aksrt_not_resource', __('该内容不是可下载资源。', 'aksrt'));
}
$meta = aksrt_get_resource_meta($p);
$uid = $user_id > 0 ? $user_id : get_current_user_id();
$paid_settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_paid_content_settings()
: [
'download_login' => true,
'download_credit' => true,
];
: ['download_login' => true];
// 登录门槛:全局开关 OR 单条覆盖
$need_login = !empty($paid_settings['download_login']) || !empty($meta['require_login']);
$uid = $user_id > 0 ? $user_id : get_current_user_id();
if ($need_login && $uid <= 0) {
if (!empty($paid_settings['download_login']) && $uid <= 0) {
return new WP_Error('aksrt_need_login', __('请先登录后再下载。', 'aksrt'), [
'status' => 401,
'login_url' => wp_login_url(get_permalink($p)),
]);
}
// 积分门槛:仅在 cms_features.download_credit 开启 + 实际成本 > 0 + 登录态时才校验
$credit_enabled = !empty($paid_settings['download_credit']);
if ($credit_enabled) {
$cost = (int) aksrt_resource_download_cost($p);
if ($cost > 0) {
if ($uid <= 0) {
return new WP_Error('aksrt_need_login', __('请先登录后再下载。', 'aksrt'), [
'status' => 401,
'login_url' => wp_login_url(get_permalink($p)),
]);
}
$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,
]);
}
}
}
// P5 阶段:验证码校验(在请求时校验,不在 can_download 静态判定中拦截)
// Without Content_Visibility, only login gate remains.
return true;
}
}
if (!function_exists('aksrt_resource_download_cost')) {
/**
* 取实际需要扣除的积分(向上取整),未启用积分门槛时返回 0。
* 下载侧展示用「未解锁积分价」。
*
* 优先级:单条 _aksrt_res_price > 全局 download_default_cost。
* 现金 pay_modo:永远 0(走订单解锁,不按次扣积分)。
* 积分 pay_modo:未购时返回积分价(用于 UI);已购订单后 0。
* 实际扣费只在收银台创建 resource 订单时发生,下载 token 不再扣积分。
*/
function aksrt_resource_download_cost($post = null): int {
$p = get_post($post);
$uid = function_exists('get_current_user_id') ? (int) get_current_user_id() : 0;
// Paid order → no further credit deduct. Avoid has_paid_access() here (recursion via access state).
// Paid order → unlocked.
if ($p instanceof WP_Post && $uid > 0 && class_exists('AKSRT_Order')) {
if (AKSRT_Order::get_instance()->has_paid_access(
$uid,
@@ -468,13 +441,9 @@ if (!function_exists('aksrt_resource_download_cost')) {
return 0;
}
}
$paid_settings = class_exists('AKSRT_Settings')
? AKSRT_Settings::get_paid_content_settings()
: [
'download_credit' => true,
'default_cost' => 0,
];
if ($p instanceof WP_Post && $uid <= 0 && function_exists('aksrt_guest_has_paid_access') && aksrt_guest_has_paid_access((int) $p->ID)) {
return 0;
}
$priced = function_exists('aksrt_get_user_pay_price')
? aksrt_get_user_pay_price($post, $uid)
@@ -486,7 +455,7 @@ if (!function_exists('aksrt_resource_download_cost')) {
$pay_modo = (($pay['pay_modo'] ?? '0') === 'points') ? 'points' : '0';
}
// Cash mode always unlocks via resource order (cashier), never per-download credits.
// Cash mode: order unlock only.
if ($pay_modo !== 'points') {
return 0;
}
@@ -495,25 +464,14 @@ 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) {
return (int) ceil($price);
}
return 0;
return $price > 0 ? (int) ceil($price) : 0;
}
// Fallback without helper.
$meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : aksrt_get_resource_meta($post);
$meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : [];
$price = (float) ($meta['points_price'] ?? $meta['price'] ?? 0);
if ($price > 0) {
return (int) ceil($price);
}
return 0;
return $price > 0 ? (int) ceil($price) : 0;
}
}