feat(pay): guest cash purchase with signed cookie unlock (zibll-style)
Allow pay_no_logged_in cash content orders (user_id=0), set aksrt_pay_* cookies on paid, accept cookie in access state, and expose guest CTA on paywall/box.
This commit is contained in:
1 parent
8f15d16d35
commit
8e9e05c859
6 files changed
+301
-35
No files matched your search
@@ -84,13 +84,6 @@ class AKSRT_Content_Pay {
|
||||
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)) {
|
||||
@@ -102,6 +95,18 @@ class AKSRT_Content_Pay {
|
||||
$price_type = '';
|
||||
}
|
||||
|
||||
// Guest allowed only for cash + pay_limit=0 + setting on.
|
||||
if ($uid <= 0) {
|
||||
$post = get_post($post_id);
|
||||
$allow_guest = function_exists('aksrt_allow_guest_content_pay') && aksrt_allow_guest_content_pay($post);
|
||||
if (!$allow_guest) {
|
||||
return new WP_Error('aksrt_need_login', __('请先登录后再购买。', 'aksrt'), [
|
||||
'status' => 401,
|
||||
'login_url' => wp_login_url($post ? get_permalink($post) : home_url('/')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$gate = $this->assert_purchasable($post_id, $uid, $price_type);
|
||||
if (is_wp_error($gate)) {
|
||||
return $gate;
|
||||
@@ -115,6 +120,9 @@ class AKSRT_Content_Pay {
|
||||
$mode = $pay_modo === 'points' ? 'points' : 'cash';
|
||||
}
|
||||
if ($mode === 'points' || $pay_modo === 'points') {
|
||||
if ($uid <= 0) {
|
||||
return new WP_Error('aksrt_need_login', __('积分支付请先登录。', 'aksrt'), ['status' => 401]);
|
||||
}
|
||||
return $this->rest_wrap_result($this->create_and_pay_points($post_id, $uid, $price_type));
|
||||
}
|
||||
|
||||
@@ -122,6 +130,10 @@ class AKSRT_Content_Pay {
|
||||
if ($payment_method === '' || $payment_method === 'cash') {
|
||||
$payment_method = 'balance';
|
||||
}
|
||||
// Guests cannot use balance/points gateways.
|
||||
if ($uid <= 0 && in_array($payment_method, ['balance', 'points'], true)) {
|
||||
$payment_method = 'alipay';
|
||||
}
|
||||
return $this->rest_wrap_result($this->create_cash_order($post_id, $uid, $payment_method, $price_type));
|
||||
}
|
||||
|
||||
@@ -130,19 +142,30 @@ class AKSRT_Content_Pay {
|
||||
*/
|
||||
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);
|
||||
$repo = AKSRT_Order_Repository::get_instance();
|
||||
if ($uid > 0) {
|
||||
$order = $repo->get_user_order($uid, $order_number);
|
||||
} else {
|
||||
$order = $repo->get($order_number);
|
||||
// Guest may only poll own guest content orders (user_id=0 + resource).
|
||||
if ($order && ((int) ($order['user_id'] ?? -1) !== 0
|
||||
|| (string) ($order['product_type'] ?? '') !== AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE)) {
|
||||
$order = null;
|
||||
}
|
||||
}
|
||||
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);
|
||||
if ($paid && $uid <= 0 && function_exists('aksrt_set_guest_content_pay_cookies')) {
|
||||
// Ensure cookie set even if notify raced ahead of browser cookie.
|
||||
aksrt_set_guest_content_pay_cookies($order);
|
||||
}
|
||||
return new WP_REST_Response([
|
||||
'order_number' => $order_number,
|
||||
'status' => $status,
|
||||
@@ -182,6 +205,19 @@ class AKSRT_Content_Pay {
|
||||
$cur = (int) get_post_meta($post_id, $key, true);
|
||||
update_post_meta($post_id, $key, $cur + 1);
|
||||
}
|
||||
|
||||
// Guest cookie unlock (user_id=0).
|
||||
$uid = (int) ($paid_order['user_id'] ?? 0);
|
||||
if ($uid <= 0 && function_exists('aksrt_set_guest_content_pay_cookies')) {
|
||||
// Ensure full order row for token material.
|
||||
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_content_pay_cookies($paid_order);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,6 +280,13 @@ class AKSRT_Content_Pay {
|
||||
$paid_order = aksrt_get_user_content_paid_order($post_id, $uid);
|
||||
}
|
||||
}
|
||||
if (!$has_paid && $uid <= 0 && function_exists('aksrt_get_guest_content_paid_order')) {
|
||||
$guest_order = aksrt_get_guest_content_paid_order($post_id);
|
||||
if ($guest_order) {
|
||||
$has_paid = true;
|
||||
$paid_order = $guest_order;
|
||||
}
|
||||
}
|
||||
|
||||
$pending_order = null;
|
||||
if ($uid > 0 && !$has_paid && class_exists('AKSRT_Order_Repository')) {
|
||||
@@ -251,12 +294,13 @@ class AKSRT_Content_Pay {
|
||||
}
|
||||
|
||||
$need_vip = $pay_limit > 0 && $vip_level < $pay_limit;
|
||||
$allow_guest = function_exists('aksrt_allow_guest_content_pay') && aksrt_allow_guest_content_pay($post);
|
||||
$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);
|
||||
$methods = $this->available_payment_methods($pay_modo, $uid > 0);
|
||||
|
||||
$amount_label = $this->format_amount_label($amount, $pay_modo, $currency);
|
||||
$base_label = $this->format_amount_label($base, $pay_modo, $currency);
|
||||
@@ -304,7 +348,8 @@ class AKSRT_Content_Pay {
|
||||
'payment_method' => (string) ($pending_order['payment_method'] ?? ''),
|
||||
'created_at' => (string) ($pending_order['created_at'] ?? ''),
|
||||
] : null,
|
||||
'need_login' => $uid <= 0,
|
||||
'need_login' => $uid <= 0 && !$allow_guest,
|
||||
'allow_guest' => $allow_guest,
|
||||
'need_vip' => $need_vip,
|
||||
'need_purchase' => !$has_paid && !$need_vip && $amount > 0,
|
||||
'logged_in' => $uid > 0,
|
||||
@@ -468,17 +513,20 @@ class AKSRT_Content_Pay {
|
||||
return new WP_Error('aksrt_pay_unavailable', __('支付系统不可用。', 'aksrt'), ['status' => 500]);
|
||||
}
|
||||
|
||||
// Resume existing pending content order for this post when amount matches.
|
||||
$pending = $this->find_pending_content_order($user_id, $post_id);
|
||||
$payment_method = sanitize_key($payment_method);
|
||||
if ($payment_method === '') {
|
||||
$payment_method = $user_id > 0 ? 'balance' : 'alipay';
|
||||
}
|
||||
if ($user_id <= 0 && in_array($payment_method, ['balance', 'points'], true)) {
|
||||
$payment_method = 'alipay';
|
||||
}
|
||||
|
||||
// Resume existing pending content order for this post when amount matches (logged-in only).
|
||||
$pending = $user_id > 0 ? $this->find_pending_content_order($user_id, $post_id) : null;
|
||||
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',
|
||||
@@ -487,10 +535,12 @@ class AKSRT_Content_Pay {
|
||||
'vip_level' => (int) ($priced['vip_level'] ?? 0),
|
||||
'base' => (float) ($priced['base'] ?? $amount),
|
||||
'amount' => $amount,
|
||||
'ip_address' => function_exists('aksrt_pay_client_ip') ? aksrt_pay_client_ip() : (string) ($_SERVER['REMOTE_ADDR'] ?? ''),
|
||||
'guest' => $user_id <= 0 ? 1 : 0,
|
||||
], 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' => $post_id,
|
||||
'amount' => $amount,
|
||||
@@ -513,7 +563,7 @@ class AKSRT_Content_Pay {
|
||||
|
||||
$payment_method = sanitize_key($payment_method ?: (string) ($order['payment_method'] ?? 'balance'));
|
||||
if ($payment_method === '') {
|
||||
$payment_method = 'balance';
|
||||
$payment_method = $user_id > 0 ? 'balance' : 'alipay';
|
||||
}
|
||||
|
||||
$order['subject'] = sprintf(
|
||||
@@ -603,12 +653,6 @@ class AKSRT_Content_Pay {
|
||||
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));
|
||||
@@ -619,6 +663,17 @@ class AKSRT_Content_Pay {
|
||||
return new WP_Error('aksrt_need_vip', __('该内容为 VIP 专属,请开通会员。', 'aksrt'), ['status' => 403]);
|
||||
}
|
||||
|
||||
$pay_modo_meta = (($meta['pay_modo'] ?? '0') === 'points') ? 'points' : '0';
|
||||
$allow_guest = function_exists('aksrt_allow_guest_content_pay') && aksrt_allow_guest_content_pay($post);
|
||||
if ($user_id <= 0) {
|
||||
if ($pay_modo_meta === 'points' || !$allow_guest) {
|
||||
return new WP_Error('aksrt_need_login', __('请先登录后再购买。', 'aksrt'), [
|
||||
'status' => 401,
|
||||
'login_url' => wp_login_url(get_permalink($post)),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$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];
|
||||
@@ -648,7 +703,7 @@ class AKSRT_Content_Pay {
|
||||
]);
|
||||
}
|
||||
|
||||
if (class_exists('AKSRT_Order') && AKSRT_Order::get_instance()->has_paid_access(
|
||||
if ($user_id > 0 && class_exists('AKSRT_Order') && AKSRT_Order::get_instance()->has_paid_access(
|
||||
$user_id,
|
||||
AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE,
|
||||
$post_id
|
||||
@@ -659,6 +714,13 @@ class AKSRT_Content_Pay {
|
||||
'reload' => true,
|
||||
]);
|
||||
}
|
||||
if ($user_id <= 0 && function_exists('aksrt_guest_has_paid_access') && aksrt_guest_has_paid_access($post_id)) {
|
||||
return new WP_Error('aksrt_already_paid', __('您已购买该内容。', 'aksrt'), [
|
||||
'status' => 200,
|
||||
'has_paid_access' => true,
|
||||
'reload' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
'post' => $post,
|
||||
@@ -691,7 +753,7 @@ class AKSRT_Content_Pay {
|
||||
/**
|
||||
* @return array<int, array{id:string,name:string}>
|
||||
*/
|
||||
private function available_payment_methods(string $pay_modo): array {
|
||||
private function available_payment_methods(string $pay_modo, bool $logged_in = true): array {
|
||||
if ($pay_modo === 'points') {
|
||||
return [
|
||||
['id' => 'points', 'name' => __('积分支付', 'aksrt')],
|
||||
@@ -699,11 +761,16 @@ class AKSRT_Content_Pay {
|
||||
}
|
||||
|
||||
$methods = [];
|
||||
$ids = $logged_in
|
||||
? ['balance', 'alipay', 'wechatpay', 'paypal', 'stripe']
|
||||
: ['alipay', 'wechatpay', 'paypal', 'stripe'];
|
||||
if (!class_exists('AKSRT_Payment_Gateway')) {
|
||||
return [['id' => 'balance', 'name' => __('站内余额', 'aksrt')]];
|
||||
return $logged_in
|
||||
? [['id' => 'balance', 'name' => __('站内余额', 'aksrt')]]
|
||||
: [['id' => 'alipay', 'name' => __('支付宝', 'aksrt')]];
|
||||
}
|
||||
$gw = AKSRT_Payment_Gateway::get_instance();
|
||||
foreach (['balance', 'alipay', 'wechatpay', 'paypal', 'stripe'] as $id) {
|
||||
foreach ($ids as $id) {
|
||||
$g = $gw->get_gateway($id);
|
||||
if (!$g) {
|
||||
continue;
|
||||
@@ -717,7 +784,9 @@ class AKSRT_Content_Pay {
|
||||
];
|
||||
}
|
||||
if ($methods === []) {
|
||||
$methods[] = ['id' => 'balance', 'name' => __('站内余额', 'aksrt')];
|
||||
$methods[] = $logged_in
|
||||
? ['id' => 'balance', 'name' => __('站内余额', 'aksrt')]
|
||||
: ['id' => 'alipay', 'name' => __('支付宝', 'aksrt')];
|
||||
}
|
||||
return $methods;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user