feat(payment): separate creator income settlement

This commit is contained in:
2026-08-07 12:39:32 +08:00
parent cd93ea1656
commit e20418e909
17 changed files with 451 additions and 445 deletions
+21 -37
View File
@@ -48,11 +48,6 @@ class PaymentController extends ControllerBase {
'callback' => [$this, 'settle_creator_income'],
'permission_callback' => [$this, 'check_permission'],
]);
register_rest_route(self::API_NAMESPACE, '/payment/creator-income/order/(?P<order_id>[a-zA-Z0-9_-]+)/reverse', [
'methods' => 'POST',
'callback' => [$this, 'reverse_creator_income'],
'permission_callback' => [$this, 'check_permission'],
]);
register_rest_route(self::API_NAMESPACE, '/payment/withdrawals', [
'methods' => 'GET',
@@ -160,7 +155,10 @@ class PaymentController extends ControllerBase {
* GET /payment/revenue — revenue stats with optional time-series
*/
public function get_revenue(\WP_REST_Request $request): \WP_REST_Response {
$revenue_stats = ['total' => 0, 'pending' => 0, 'settled' => 0, 'count' => 0];
$revenue_stats = [
'cash' => ['total' => 0, 'pending' => 0, 'settled' => 0, 'count' => 0],
'points' => ['total' => 0, 'pending' => 0, 'settled' => 0, 'count' => 0],
];
if (class_exists('\AKSRT\Payment\Revenue')) {
$revenue_stats = \AKSRT\Payment\Revenue::get_instance()->get_stats();
}
@@ -194,12 +192,14 @@ class PaymentController extends ControllerBase {
$date_to = sanitize_text_field((string) $request->get_param('date_to'));
$granularity = sanitize_text_field((string) $request->get_param('granularity'));
// Default: last 30 days
// Default: last 30 days in the WordPress site timezone.
$timezone = function_exists('wp_timezone') ? wp_timezone() : new \DateTimeZone('UTC');
$now = new \DateTimeImmutable('now', $timezone);
if (!$date_from) {
$date_from = gmdate('Y-m-d', strtotime('-30 days'));
$date_from = $now->modify('-30 days')->format('Y-m-d');
}
if (!$date_to) {
$date_to = gmdate('Y-m-d');
$date_to = $now->format('Y-m-d');
}
// Granularity: day, week, month
@@ -212,27 +212,27 @@ class PaymentController extends ControllerBase {
$table = $prefix . 'aksrt_revenue';
$results = $wpdb->get_results($wpdb->prepare(
"SELECT DATE_FORMAT(created_at, %s) AS period,
"SELECT currency, DATE_FORMAT(created_at, %s) AS period,
SUM(amount) AS total_amount,
COUNT(*) AS order_count
FROM {$table}
WHERE created_at >= %s AND created_at < DATE_ADD(%s, INTERVAL 1 DAY)
GROUP BY period
WHERE status IN ('pending', 'settled')
AND created_at >= %s AND created_at < DATE_ADD(%s, INTERVAL 1 DAY)
GROUP BY currency, period
ORDER BY period ASC",
$date_format,
$date_from,
$date_to
));
$series = [];
if ($results) {
foreach ($results as $row) {
$series[] = [
'date' => (string) $row->period,
'amount' => (float) $row->total_amount,
'count' => (int) $row->order_count,
];
}
$series = ['cash' => [], 'points' => []];
foreach ((array) $results as $row) {
$key = (string) ($row->currency ?? '') === 'points' ? 'points' : 'cash';
$series[$key][] = [
'date' => (string) $row->period,
'amount' => (float) $row->total_amount,
'count' => (int) $row->order_count,
];
}
return $series;
@@ -277,22 +277,6 @@ class PaymentController extends ControllerBase {
return $this->success(['message' => __('创作分成已结算', 'aksrt')]);
}
/**
* POST /payment/creator-income/order/{order_id}/reverse — cancel or reverse order income.
*/
public function reverse_creator_income(\WP_REST_Request $request): \WP_REST_Response {
if (!class_exists('\AKSRT\Payment\Revenue')) {
return $this->error(__('创作分成模块未启用', 'aksrt'), 500);
}
$order_id = sanitize_text_field((string) $request->get_param('order_id'));
$params = $this->get_json_params($request);
$reason = sanitize_textarea_field((string) ($params['reason'] ?? ''));
if ($order_id === '' || !\AKSRT\Payment\Revenue::get_instance()->cancel_or_reverse($order_id, $reason)) {
return $this->error(__('冲正失败', 'aksrt'));
}
return $this->success(['message' => __('创作分成已处理退款/冲正', 'aksrt')]);
}
public function get_withdrawals(\WP_REST_Request $request): \WP_REST_Response {
if (!class_exists('\AKSRT\Payment\Withdrawal')) {
return $this->success(['data' => ['items' => [], 'total' => 0, 'page' => 1, 'total_pages' => 1, 'stats' => []]]);