feat(forum): guest cash purchase for topic and plate follow

- Reuse pay_no_logged_in / pay_cookie_day; generalized guest cookie helpers
- Topic + plate cash: guest preview/purchase (no balance), order.user_id=0, cookie unlock
- View gates: can_view_topic pay + plate follow accept signed guest cookies
- Templates: guest buy/follow CTAs; poll endpoint allows plate_follow guest orders
This commit is contained in:
2026-07-15 13:58:54 +08:00
parent a9150862ef
commit 6b6314b033
9 changed files with 274 additions and 70 deletions
+65 -22
View File
@@ -74,8 +74,9 @@ class AKSRT_Topic_Pay {
public function rest_purchase(WP_REST_Request $request) {
$topic_id = (int) $request['id'];
$uid = get_current_user_id();
if ($uid <= 0) {
$post = get_post($topic_id);
$post = get_post($topic_id);
$allow_guest = function_exists('aksrt_allow_guest_forum_pay') && aksrt_allow_guest_forum_pay($post);
if ($uid <= 0 && !$allow_guest) {
return new WP_Error('aksrt_need_login', __('请先登录后再购买。', 'aksrt'), [
'status' => 401,
'login_url' => wp_login_url($post ? get_permalink($post) : home_url('/')),
@@ -91,6 +92,10 @@ class AKSRT_Topic_Pay {
if ($payment_method === '' || $payment_method === 'cash') {
$payment_method = 'balance';
}
// Guests cannot use balance.
if ($uid <= 0 && in_array($payment_method, ['balance', 'points'], true)) {
$payment_method = 'alipay';
}
$coupon_code = sanitize_text_field((string) ($params['coupon_code'] ?? $params['coupon'] ?? ''));
return $this->rest_wrap_result(
@@ -135,8 +140,9 @@ class AKSRT_Topic_Pay {
$post = $assert['post'];
$uid = $user_id > 0 ? $user_id : get_current_user_id();
$coupon_code = sanitize_text_field($coupon_code);
$allow_guest = function_exists('aksrt_allow_guest_forum_pay') && aksrt_allow_guest_forum_pay($post);
if ($uid <= 0) {
if ($uid <= 0 && !$allow_guest) {
return [
'post_id' => $topic_id,
'title' => get_the_title($post),
@@ -184,16 +190,22 @@ class AKSRT_Topic_Pay {
$list_amount = round((float) ($priced['amount'] ?? 0), 2);
$base = round((float) ($priced['base'] ?? $list_amount), 2);
$vip_free = !empty($priced['is_free']) || ($list_amount <= 0 && !empty($priced['is_vip_price']));
// Guests never get VIP free (vip_level=0).
if ($uid <= 0) {
$vip_free = false;
}
$has_paid = $this->user_has_paid($uid, $topic_id);
$balance = 0;
if (class_exists('AKSRT_Credits')) {
if ($uid > 0 && class_exists('AKSRT_Credits')) {
$balance = (int) AKSRT_Credits::balance($uid);
}
$methods = function_exists('aksrt_list_cash_payment_methods')
? aksrt_list_cash_payment_methods(true)
: [['id' => 'balance', 'name' => __('站内余额', 'aksrt')]];
? aksrt_list_cash_payment_methods($uid > 0)
: ($uid > 0
? [['id' => 'balance', 'name' => __('站内余额', 'aksrt')]]
: [['id' => 'alipay', 'name' => __('支付宝', 'aksrt')]]);
$coupon_allowed = $this->is_coupon_allowed($topic_id);
$coupon_error = '';
@@ -249,11 +261,11 @@ class AKSRT_Topic_Pay {
'has_paid_access' => $has_paid,
'paid_order' => null,
'pending_order' => null,
'need_login' => false,
'allow_guest' => false,
'need_login' => $uid <= 0 && !$allow_guest,
'allow_guest' => $allow_guest,
'need_vip' => false,
'need_purchase' => !$has_paid && !$is_free,
'logged_in' => true,
'logged_in' => $uid > 0,
'balance' => $balance,
'balance_enough' => true,
'payment_methods' => $methods,
@@ -285,10 +297,6 @@ class AKSRT_Topic_Pay {
* @return array|WP_Error
*/
public function create_cash_order(int $topic_id, int $user_id, string $payment_method = 'balance', string $coupon_code = '') {
if ($user_id <= 0) {
return new WP_Error('aksrt_need_login', __('请先登录后再购买。', 'aksrt'), ['status' => 401]);
}
$assert = $this->assert_topic($topic_id);
if (is_wp_error($assert)) {
return $assert;
@@ -296,17 +304,32 @@ class AKSRT_Topic_Pay {
/** @var WP_Post $post */
$post = $assert['post'];
$allow_guest = function_exists('aksrt_allow_guest_forum_pay') && aksrt_allow_guest_forum_pay($post);
if ($user_id <= 0 && !$allow_guest) {
return new WP_Error('aksrt_need_login', __('请先登录后再购买。', 'aksrt'), [
'status' => 401,
'login_url' => wp_login_url(get_permalink($post)),
]);
}
if ($this->user_has_paid($user_id, $topic_id)) {
return new WP_Error('aksrt_already_paid', __('已购买,可直接查看。', 'aksrt'), ['status' => 400]);
return new WP_Error('aksrt_already_paid', __('已购买,可直接查看。', 'aksrt'), [
'status' => 400,
'has_paid_access' => true,
'reload' => true,
]);
}
$priced = $this->resolve_price($topic_id, $user_id);
$list_amount = round((float) ($priced['amount'] ?? 0), 2);
$base = round((float) ($priced['base'] ?? $list_amount), 2);
$vip_free = !empty($priced['is_free']) || ($list_amount <= 0 && !empty($priced['is_vip_price']));
if ($user_id <= 0) {
$vip_free = false;
}
$coupon_code = sanitize_text_field($coupon_code);
// VIP free (no purchase price) — no coupon stack.
// VIP free (no purchase price) — no coupon stack. Guests never hit this.
if ($vip_free) {
return $this->create_free_paid_order($topic_id, $user_id, $post, $priced, [
'list_amount' => 0.0,
@@ -353,7 +376,10 @@ class AKSRT_Topic_Pay {
$payment_method = sanitize_key($payment_method);
if ($payment_method === '') {
$payment_method = 'balance';
$payment_method = $user_id > 0 ? 'balance' : 'alipay';
}
if ($user_id <= 0 && in_array($payment_method, ['balance', 'points'], true)) {
$payment_method = 'alipay';
}
$pending = $this->find_pending_topic_order($user_id, $topic_id);
@@ -385,11 +411,12 @@ class AKSRT_Topic_Pay {
'coupon_code' => $coupon_code_norm,
'coupon_discount' => $coupon_discount,
'coupon' => $coupon_meta,
'guest' => $user_id <= 0 ? 1 : 0,
'ip_address' => function_exists('aksrt_pay_client_ip') ? aksrt_pay_client_ip() : (string) ($_SERVER['REMOTE_ADDR'] ?? ''),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$order_number = AKSRT_Order_Repository::get_instance()->create([
'user_id' => $user_id,
'user_id' => max(0, $user_id),
'product_type' => AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE,
'product_id' => $topic_id,
'amount' => $amount,
@@ -478,7 +505,9 @@ class AKSRT_Topic_Pay {
return new WP_Error('aksrt_order_confirm_failed', __('订单确认失败。', 'aksrt'), ['status' => 500]);
}
$this->mark_unlocked($user_id, $topic_id);
if ($user_id > 0) {
$this->mark_unlocked($user_id, $topic_id);
}
return [
'success' => true,
@@ -493,7 +522,7 @@ class AKSRT_Topic_Pay {
}
/**
* On paid: unlock topic + burn coupon (idempotent).
* On paid: unlock topic (or guest cookie) + burn coupon (idempotent).
*/
public function on_order_paid(string $order_number, array $paid_order): void {
$product_type = sanitize_key((string) ($paid_order['product_type'] ?? ''));
@@ -503,7 +532,7 @@ class AKSRT_Topic_Pay {
$topic_id = (int) ($paid_order['product_id'] ?? 0);
$user_id = (int) ($paid_order['user_id'] ?? 0);
if ($topic_id <= 0 || $user_id <= 0) {
if ($topic_id <= 0) {
return;
}
@@ -516,7 +545,17 @@ class AKSRT_Topic_Pay {
}
}
$this->mark_unlocked($user_id, $topic_id);
if ($user_id > 0) {
$this->mark_unlocked($user_id, $topic_id);
} elseif (function_exists('aksrt_set_guest_pay_cookies')) {
if (class_exists('AKSRT_Order_Repository')) {
$full = AKSRT_Order_Repository::get_instance()->get($order_number);
if (is_array($full)) {
$paid_order = array_merge($full, $paid_order);
}
}
aksrt_set_guest_pay_cookies($paid_order);
}
$this->finalize_topic_order_side_effects($order_number, $paid_order);
}
@@ -559,6 +598,9 @@ class AKSRT_Topic_Pay {
}
private function user_has_paid(int $user_id, int $topic_id): bool {
if ($user_id <= 0) {
return function_exists('aksrt_guest_has_paid_access') && aksrt_guest_has_paid_access($topic_id);
}
if (class_exists('AKSRT_Forum')) {
return AKSRT_Forum::get_instance()->has_user_paid_topic($user_id, $topic_id);
}
@@ -736,11 +778,12 @@ class AKSRT_Topic_Pay {
'coupon_discount' => $coupon_discount,
'coupon' => $coupon_meta,
'free' => 1,
'guest' => $user_id <= 0 ? 1 : 0,
'ip_address' => function_exists('aksrt_pay_client_ip') ? aksrt_pay_client_ip() : (string) ($_SERVER['REMOTE_ADDR'] ?? ''),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$order_number = AKSRT_Order_Repository::get_instance()->create([
'user_id' => $user_id,
'user_id' => max(0, $user_id),
'product_type' => AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE,
'product_id' => $topic_id,
'amount' => 0.0,