- 新增 AKSRT_Creator_Revenue:监听 aksrt_order_paid,按 share_ratio 把商品作者分成写入既有 aksrt_revenue 台账(幂等、跳过自购),闭合此前"结算引擎"延后项;管理端 /payment/revenue 与 /shop 概览自动读取。 - page-verify-email 套用 sign 壳层与站点 logo,和 reset/bind 对齐。 - modal-auth 补 close_signup 门控,与全页登录页一致。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
146 lines
5.1 KiB
PHP
146 lines
5.1 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT Creator Revenue Settlement.
|
|
*
|
|
* Wires order-paid + non-shop paid content into the pre-existing revenue ledger
|
|
* (`aksrt_revenue` table + AKSRT_Revenue service). This closes the deferred
|
|
* "结算引擎" gap: the ledger, service and share-ratio settings already existed
|
|
* but nothing credited creator revenue when an order was paid.
|
|
*
|
|
* Scope (kept intentionally narrow):
|
|
* - Only shop-product orders and post-level paid content credit revenue.
|
|
* - Share ratio comes from AKSRT_Settings::get_payment_revenue_settings().
|
|
* - Buyer == author is skipped (no self-dealing).
|
|
* - Idempotent per order_number via a guard row lookup.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
if (!class_exists('AKSRT_Creator_Revenue')) {
|
|
|
|
class AKSRT_Creator_Revenue {
|
|
|
|
private static ?AKSRT_Creator_Revenue $instance = null;
|
|
|
|
public static function get_instance(): self {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function init(): void {
|
|
add_action('aksrt_order_paid', [$this, 'handle_order_paid'], 20, 2);
|
|
}
|
|
|
|
/**
|
|
* Whether creator revenue settlement is enabled.
|
|
*/
|
|
private function is_enabled(): bool {
|
|
if (!class_exists('AKSRT_Revenue') || !class_exists('AKSRT_Settings')) {
|
|
return false;
|
|
}
|
|
$settings = AKSRT_Settings::get_payment_revenue_settings();
|
|
return !empty($settings['enabled']);
|
|
}
|
|
|
|
/**
|
|
* Resolve share ratio (percentage 0-100) for a given product author.
|
|
*/
|
|
private function resolve_share_ratio(int $author_id): float {
|
|
$settings = AKSRT_Settings::get_payment_revenue_settings();
|
|
$ratio = (float) ($settings['share_ratio'] ?? 0);
|
|
|
|
// VIP-tier author overrides, if configured (>0 wins).
|
|
if ($author_id > 0 && function_exists('aksrt_get_user_vip_level')) {
|
|
$level = (int) aksrt_get_user_vip_level($author_id);
|
|
if ($level === 1 && (float) ($settings['ratio_vip1'] ?? 0) > 0) {
|
|
$ratio = (float) $settings['ratio_vip1'];
|
|
} elseif ($level >= 2 && (float) ($settings['ratio_vip2'] ?? 0) > 0) {
|
|
$ratio = (float) $settings['ratio_vip2'];
|
|
}
|
|
}
|
|
|
|
return max(0.0, min(100.0, $ratio));
|
|
}
|
|
|
|
/**
|
|
* Guard: has this order already been settled into revenue?
|
|
*/
|
|
private function already_settled(string $order_number): bool {
|
|
if ($order_number === '' || !class_exists('AKSRT_DB_Migrator')) {
|
|
return false;
|
|
}
|
|
global $wpdb;
|
|
$table = AKSRT_DB_Migrator::table('revenue');
|
|
if ($table === '') {
|
|
return false;
|
|
}
|
|
$count = (int) $wpdb->get_var($wpdb->prepare(
|
|
"SELECT COUNT(*) FROM {$table} WHERE order_number = %s AND type = %s",
|
|
$order_number,
|
|
'order'
|
|
));
|
|
return $count > 0;
|
|
}
|
|
|
|
/**
|
|
* aksrt_order_paid listener: credit creators their share.
|
|
*
|
|
* @param string $order_number Paid order number.
|
|
* @param array $order Order row (status=paid).
|
|
*/
|
|
public function handle_order_paid(string $order_number, array $order): void {
|
|
if (!$this->is_enabled() || $order_number === '') {
|
|
return;
|
|
}
|
|
if ($this->already_settled($order_number)) {
|
|
return;
|
|
}
|
|
|
|
$buyer_id = (int) ($order['user_id'] ?? 0);
|
|
|
|
$items = [];
|
|
if (class_exists('AKSRT_Shop')) {
|
|
$shop = AKSRT_Shop::get_instance();
|
|
if (method_exists($shop, 'get_order_items')) {
|
|
$items = (array) $shop->get_order_items($order_number);
|
|
}
|
|
}
|
|
|
|
if (empty($items)) {
|
|
return; // Only structured shop-product orders settle creator revenue here.
|
|
}
|
|
|
|
$revenue = AKSRT_Revenue::get_instance();
|
|
|
|
foreach ($items as $item) {
|
|
$product_id = (int) ($item['product_id'] ?? 0);
|
|
$pay_price = (float) ($item['pay_price'] ?? $item['total_price'] ?? 0);
|
|
if ($product_id <= 0 || $pay_price <= 0) {
|
|
continue;
|
|
}
|
|
|
|
$author_id = (int) get_post_field('post_author', $product_id);
|
|
if ($author_id <= 0 || $author_id === $buyer_id) {
|
|
continue; // No author, or buyer is the author.
|
|
}
|
|
|
|
$ratio = $this->resolve_share_ratio($author_id);
|
|
if ($ratio <= 0) {
|
|
continue;
|
|
}
|
|
|
|
$amount = round($pay_price * $ratio / 100, 2);
|
|
if ($amount <= 0) {
|
|
continue;
|
|
}
|
|
|
|
$revenue->add($author_id, $amount, $order_number, 'order');
|
|
}
|
|
}
|
|
}
|
|
}
|