fix(payment): close creator income contract gaps
This commit is contained in:
1 parent
cfb0a1368c
commit
7cca472fe9
14 files changed
+70
-52
No files matched your search
+33
-28
@@ -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];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user