fix(payment): close creator income contract gaps

This commit is contained in:
2026-08-07 13:30:37 +08:00
parent cfb0a1368c
commit 7cca472fe9
14 changed files with 70 additions and 52 deletions
+11 -5
View File
@@ -187,10 +187,13 @@ class PayIncome {
}
}
$ids = array_values(array_unique($ids));
$ok = !empty($ids)
&& class_exists('\\AKSRT\\Payment\\Revenue')
&& \AKSRT\Payment\Revenue::get_instance()->settle($ids, get_current_user_id());
wp_safe_redirect($this->page_url('aksrt_income', ['aksrt_pay_msg' => $ok ? 'income_batch_settled' : 'income_failed']));
$batch_result = !empty($ids) && class_exists('\\AKSRT\\Payment\\Revenue')
? \AKSRT\Payment\Revenue::get_instance()->settle_batch($ids, get_current_user_id())
: ['settled' => [], 'failed' => $ids];
$message = empty($batch_result['settled'])
? 'income_failed'
: (!empty($batch_result['failed']) ? 'income_batch_partial' : 'income_batch_settled');
wp_safe_redirect($this->page_url('aksrt_income', ['aksrt_pay_msg' => $message]));
exit;
}
@@ -218,12 +221,15 @@ class PayIncome {
$map = [
'income_settled' => __('分成已结算。', 'aksrt'),
'income_batch_settled' => __('选中分成已批量结算。', 'aksrt'),
'income_batch_partial' => __('部分分成已结算,失败记录可稍后重试。', 'aksrt'),
'income_failed' => __('分成结算失败。', 'aksrt'),
];
if (!isset($map[$code])) {
return;
}
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
$class = $code === 'income_batch_partial'
? 'notice-warning'
: (str_contains($code, 'failed') ? 'notice-error' : 'notice-success');
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($map[$code]) . '</p></div>';
}
+2 -2
View File
@@ -163,11 +163,11 @@ class SchemaPayment {
],
'revenue' => [
'key' => 'revenue',
'title' => '创作分成',
'title' => '创作中心',
'version' => '2.0.0',
'option_key' => 'aksrt_settings',
'groups' => [
['key' => 'basic', 'title' => '创作分成设置', 'fields' => [
['key' => 'basic', 'title' => '创作中心设置', 'fields' => [
['key' => 'pay_income_s', 'label' => '启用创作分成', 'type' => 'switch', 'default' => false],
['key' => 'payment_revenue_cycle_days', 'label' => '结算周期(天)', 'type' => 'number', 'default' => 30, 'min' => 1, 'max' => 365, 'dependsOn' => ['key' => 'pay_income_s', 'value' => true]],
['key' => 'payment_revenue_note', 'label' => '结算备注文案', 'type' => 'textarea', 'sanitize_type' => 'html', 'default' => '<p>订单完成并经过结算周期后,作者分成会自动转入对应的钱包。</p>', 'dependsOn' => ['key' => 'pay_income_s', 'value' => true]],
+1 -1
View File
@@ -601,7 +601,7 @@ class SchemaUsers {
'option_key' => 'aksrt_settings',
'groups' => [
['key' => 'center', 'title' => '用户中心', 'fields' => [
['key' => 'user_center_tabs', 'label' => '用户中心 Tab', 'type' => 'input', 'default' => 'profile,level,posts,comments,favorites,vip,credits,revenue,auth', 'help' => '逗号分隔。level=我的等级;revenue=收益中心;auth=官方认证;等级、VIP、订单、收益中心和认证会按功能开关自动注入'],
['key' => 'user_center_tabs', 'label' => '用户中心 Tab', 'type' => 'input', 'default' => 'profile,level,comments,favorites,vip,credits,revenue,auth', 'help' => '逗号分隔。level=我的等级;creator=创作中心;revenue=推广收益中心;auth=官方认证;等级、VIP、订单、创作中心、收益中心和认证会按功能开关自动注入'],
['key' => 'user_cover_upload', 'label' => '封面上传', 'type' => 'switch', 'default' => true],
['key' => 'user_social_links', 'label' => '社交链接', 'type' => 'switch', 'default' => true],
['key' => 'user_desc_std', 'label' => '默认签名', 'type' => 'input', 'default' => '这家伙很懒…', 'desc' => '用户未设置个人简介时显示的默认文案'],
+33 -28
View File
@@ -30,11 +30,23 @@ class Revenue {
public function create_from_paid_order(array $order): int {
global $wpdb;
if (!$this->is_enabled()) {
return 0;
}
$order_number = sanitize_text_field((string) ($order['order_number'] ?? ''));
if ($order_number === '' || (string) ($order['status'] ?? '') !== 'completed') {
return 0;
}
$existing = $wpdb->get_var($wpdb->prepare(
"SELECT id FROM {$this->table()} WHERE order_number = %s ORDER BY id ASC LIMIT 1",
$order_number
));
if ((int) $existing > 0) {
return (int) $existing;
}
$meta = $this->decode_order_meta($order);
if (empty($meta['content_pay'])) {
return 0;
@@ -95,8 +107,7 @@ class Revenue {
if ($inserted === false) {
// Duplicate callbacks are a successful no-op by contract.
$existing = $wpdb->get_var($wpdb->prepare(
"SELECT id FROM {$this->table()} WHERE author_id = %d AND order_number = %s LIMIT 1",
$author_id,
"SELECT id FROM {$this->table()} WHERE order_number = %s ORDER BY id ASC LIMIT 1",
$order_number
));
return (int) $existing;
@@ -113,28 +124,6 @@ class Revenue {
return $id;
}
/**
* Legacy generic insertion retained as the native income page entry point.
*/
public function add(int $user_id, float $amount, string $order_number = '', string $type = 'income'): int {
global $wpdb;
if ($user_id <= 0 || $amount <= 0) {
return 0;
}
$order_number = sanitize_text_field($order_number);
$wpdb->insert($this->table(), [
'author_id' => $user_id,
'order_number' => $order_number,
'amount' => round($amount, 2),
'original_amount' => round($amount, 2),
'currency' => 'CNY',
'type' => sanitize_key($type),
'status' => 'pending',
'created_at' => current_time('mysql'),
], ['%d', '%s', '%f', '%f', '%s', '%s', '%s', '%s']);
return (int) $wpdb->insert_id;
}
/**
* Settle one or more pending records through matching atomic balance services.
*
@@ -142,18 +131,34 @@ class Revenue {
* @param int $actor_id
*/
public function settle($record_ids, int $actor_id = 0): bool {
$result = $this->settle_batch($record_ids, $actor_id);
return !empty($result['settled']) && empty($result['failed']);
}
/**
* Settle a batch while preserving per-record outcomes for operational feedback.
*
* @param int|array<int,int> $record_ids
* @param int $actor_id
* @return array{settled:array<int,int>,failed:array<int,int>}
*/
public function settle_batch($record_ids, int $actor_id = 0): array {
$ids = is_array($record_ids) ? $record_ids : [$record_ids];
$ids = array_values(array_unique(array_filter(array_map('intval', $ids), static fn (int $id): bool => $id > 0)));
if (!$ids || !$this->is_enabled()) {
return false;
return ['settled' => [], 'failed' => $ids];
}
$settled = [];
$failed = [];
foreach ($ids as $revenue_id) {
if (!$this->settle_one($revenue_id, $actor_id)) {
return false;
if ($this->settle_one($revenue_id, $actor_id)) {
$settled[] = $revenue_id;
} else {
$failed[] = $revenue_id;
}
}
return true;
return ['settled' => $settled, 'failed' => $failed];
}
/**
+4 -4
View File
@@ -83,14 +83,14 @@ class UserRuntime {
*/
public static function get_user_center_settings(): array {
$settings = Settings::get_all();
$raw_tabs = $settings['user_center_tabs'] ?? 'profile,level,posts,comments,credits,revenue,favorites,auth';
$raw_tabs = $settings['user_center_tabs'] ?? 'profile,level,comments,credits,revenue,favorites,auth';
$tabs = is_array($raw_tabs)
? $raw_tabs
: preg_split('/[\s,]+/', (string) $raw_tabs, -1, PREG_SPLIT_NO_EMPTY);
// Built-in switchable panels. VIP is always auto-injected when enabled
// by AKSRT\User\Center — keep it allowed for explicit config too.
$allowed_tabs = ['profile', 'level', 'posts', 'comments', 'favorites', 'credits', 'revenue', 'vip', 'orders', 'auth', 'settings'];
$allowed_tabs = ['profile', 'level', 'comments', 'favorites', 'credits', 'revenue', 'vip', 'orders', 'auth', 'settings'];
$normalized_tabs = ['profile'];
foreach ((array) $tabs as $tab) {
@@ -104,8 +104,8 @@ class UserRuntime {
}
}
if (!in_array('posts', $normalized_tabs, true) && !in_array('comments', $normalized_tabs, true)) {
$normalized_tabs[] = 'posts';
if (!in_array('comments', $normalized_tabs, true)) {
$normalized_tabs[] = 'comments';
}
// Ensure VIP appears in configured tabs when membership is on, even if
+4 -2
View File
@@ -13,6 +13,7 @@ class Metrics {
public const META_COMMENT_LIKE_SUM = 'aksrt_comment_like_sum';
public const META_POSTS_VIEWS_SUM = 'aksrt_posts_views_sum';
public const META_PAY_INCOME = 'aksrt_pay_income_sum';
public const META_PAY_INCOME_POINTS = 'aksrt_pay_income_points_sum';
public const META_PAY_REBATE = 'aksrt_pay_rebate_sum';
public const META_CHECKIN_ALL_DAY = 'aksrt_checkin_all_day';
@@ -132,13 +133,14 @@ class Metrics {
* @param array $context
*/
public function on_creator_income_added($user_id, $amount, $context = []): void {
unset($context);
$currency = sanitize_key((string) ($context['currency'] ?? 'CNY'));
$user_id = (int) $user_id;
$amount = (float) $amount;
if ($user_id <= 0 || $amount <= 0) {
return;
}
self::bump_user_meta($user_id, self::META_PAY_INCOME, $amount);
$meta_key = $currency === 'points' ? self::META_PAY_INCOME_POINTS : self::META_PAY_INCOME;
self::bump_user_meta($user_id, $meta_key, $amount);
self::reevaluate_medals($user_id);
}