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:
1 parent
1039b26efd
commit
79f8c43970
5 files changed
+311
-291
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user