- Trait-split content-pay, vip-service, balance-service, order-repository - Procedural split of user module into user-parts/* - Split scripts enqueue_public into part-1b/1c; settings bulk trait - Bootstrap: explicit notifications, widgets glob-only, home-modules init - ADR: freeze domain placement for pay/revenue/notify boundaries
450 lines
16 KiB
PHP
450 lines
16 KiB
PHP
<?php
|
|
/**
|
|
* Extracted trait: AKSRT_Balance_Service_Rest
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait AKSRT_Balance_Service_Rest {
|
|
|
|
|
|
|
|
public function register_routes(): void {
|
|
register_rest_route('aksrt/v1', '/pay/balance/charge', [
|
|
[
|
|
'methods' => WP_REST_Server::READABLE,
|
|
'callback' => [$this, 'rest_charge_options'],
|
|
'permission_callback' => static function () {
|
|
return is_user_logged_in();
|
|
},
|
|
],
|
|
[
|
|
'methods' => WP_REST_Server::CREATABLE,
|
|
'callback' => [$this, 'rest_charge_create'],
|
|
'permission_callback' => static function () {
|
|
return is_user_logged_in();
|
|
},
|
|
],
|
|
]);
|
|
|
|
register_rest_route('aksrt/v1', '/pay/transfer', [
|
|
[
|
|
'methods' => WP_REST_Server::CREATABLE,
|
|
'callback' => [$this, 'rest_transfer'],
|
|
'permission_callback' => static function () {
|
|
return is_user_logged_in();
|
|
},
|
|
],
|
|
]);
|
|
|
|
register_rest_route('aksrt/v1', '/pay/transfer/search', [
|
|
[
|
|
'methods' => WP_REST_Server::READABLE,
|
|
'callback' => [$this, 'rest_transfer_search'],
|
|
'permission_callback' => static function () {
|
|
return is_user_logged_in();
|
|
},
|
|
],
|
|
]);
|
|
|
|
register_rest_route('aksrt/v1', '/pay/balance/exchange', [
|
|
[
|
|
'methods' => WP_REST_Server::CREATABLE,
|
|
'callback' => [$this, 'rest_exchange_from_credits'],
|
|
'permission_callback' => static function () {
|
|
return is_user_logged_in();
|
|
},
|
|
],
|
|
]);
|
|
|
|
register_rest_route('aksrt/v1', '/pay/points/buy', [
|
|
[
|
|
'methods' => WP_REST_Server::READABLE,
|
|
'callback' => [$this, 'rest_points_buy_options'],
|
|
'permission_callback' => static function () {
|
|
return is_user_logged_in();
|
|
},
|
|
],
|
|
[
|
|
'methods' => WP_REST_Server::CREATABLE,
|
|
'callback' => [$this, 'rest_points_buy_create'],
|
|
'permission_callback' => static function () {
|
|
return is_user_logged_in();
|
|
},
|
|
],
|
|
]);
|
|
|
|
register_rest_route('aksrt/v1', '/pay/balance/withdraw', [
|
|
[
|
|
'methods' => WP_REST_Server::CREATABLE,
|
|
'callback' => [$this, 'rest_balance_withdraw'],
|
|
'permission_callback' => static function () {
|
|
return is_user_logged_in();
|
|
},
|
|
],
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* GET charge options (packages + methods + limits).
|
|
*/
|
|
public function rest_charge_options(WP_REST_Request $request) {
|
|
if (!class_exists('AKSRT_Balance') || !AKSRT_Balance::enabled()) {
|
|
return new WP_Error('aksrt_balance_disabled', __('余额功能未启用', 'aksrt'), ['status' => 403]);
|
|
}
|
|
$user_id = get_current_user_id();
|
|
return rest_ensure_response([
|
|
'success' => true,
|
|
'data' => $this->get_charge_view($user_id),
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* POST create recharge order and initiate payment.
|
|
*/
|
|
public function rest_charge_create(WP_REST_Request $request) {
|
|
if (!class_exists('AKSRT_Balance') || !AKSRT_Balance::enabled()) {
|
|
return new WP_Error('aksrt_balance_disabled', __('余额功能未启用', 'aksrt'), ['status' => 403]);
|
|
}
|
|
$user_id = get_current_user_id();
|
|
$params = $request->get_json_params();
|
|
if (!is_array($params)) {
|
|
$params = $request->get_params();
|
|
}
|
|
$package_key = sanitize_text_field((string) ($params['package'] ?? 'custom'));
|
|
$custom_price = isset($params['custom_price']) ? (float) $params['custom_price'] : 0.0;
|
|
$payment_method = sanitize_key((string) ($params['payment_method'] ?? 'alipay'));
|
|
|
|
$result = $this->create_charge_order($user_id, $package_key, $custom_price, $payment_method);
|
|
if (is_wp_error($result)) {
|
|
return $result;
|
|
}
|
|
return rest_ensure_response(['success' => true, 'data' => $result]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Transfer balance or points between users.
|
|
*/
|
|
public function rest_transfer(WP_REST_Request $request) {
|
|
$params = $request->get_json_params();
|
|
if (!is_array($params)) {
|
|
$params = $request->get_params();
|
|
}
|
|
$type = sanitize_key((string) ($params['type'] ?? 'balance'));
|
|
$recipient = (int) ($params['recipient'] ?? 0);
|
|
$amount_raw = $params['amount'] ?? $params['price'] ?? 0;
|
|
$from_id = get_current_user_id();
|
|
|
|
if (!in_array($type, ['balance', 'points'], true)) {
|
|
return new WP_Error('aksrt_transfer_type', __('转账类型无效', 'aksrt'), ['status' => 400]);
|
|
}
|
|
if ($from_id <= 0) {
|
|
return new WP_Error('aksrt_need_login', __('请先登录', 'aksrt'), ['status' => 401]);
|
|
}
|
|
if ($recipient <= 0 || $recipient === $from_id) {
|
|
return new WP_Error('aksrt_transfer_user', __('请选择有效的收款用户', 'aksrt'), ['status' => 400]);
|
|
}
|
|
if (!get_userdata($recipient)) {
|
|
return new WP_Error('aksrt_transfer_user', __('收款用户不存在', 'aksrt'), ['status' => 404]);
|
|
}
|
|
|
|
$cfg = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_payment_recharge_settings() : [];
|
|
if ($type === 'balance') {
|
|
if (empty($cfg['enabled']) || empty($cfg['transfer']['enabled'])) {
|
|
return new WP_Error('aksrt_transfer_closed', __('余额转账未开启', 'aksrt'), ['status' => 403]);
|
|
}
|
|
if (class_exists('AKSRT_Settings_Capabilities') && !AKSRT_Settings_Capabilities::user_can('balance_transfer', $from_id)) {
|
|
return new WP_Error('aksrt_transfer_forbidden', __('当前账号无权转账余额', 'aksrt'), ['status' => 403]);
|
|
}
|
|
$amount = round((float) $amount_raw, 2);
|
|
if ($amount <= 0) {
|
|
return new WP_Error('aksrt_transfer_amount', __('请输入有效金额', 'aksrt'), ['status' => 400]);
|
|
}
|
|
if (!class_exists('AKSRT_Balance')) {
|
|
return new WP_Error('aksrt_balance_unavailable', __('余额系统不可用', 'aksrt'), ['status' => 500]);
|
|
}
|
|
$from_bal = AKSRT_Balance::balance($from_id);
|
|
if ($from_bal < $amount) {
|
|
return new WP_Error('aksrt_balance_insufficient', __('余额不足', 'aksrt'), ['status' => 402]);
|
|
}
|
|
$fee_rate = max(0, min(50, (int) ($cfg['transfer']['service_charge'] ?? 0)));
|
|
$credit = $fee_rate > 0 ? round($amount * (100 - $fee_rate) / 100, 2) : $amount;
|
|
|
|
$from_user = wp_get_current_user();
|
|
$to_user = get_userdata($recipient);
|
|
$deduct = AKSRT_Balance::deduct(
|
|
$from_id,
|
|
$amount,
|
|
'transfer_out',
|
|
['type' => 'user', 'id' => $recipient],
|
|
sprintf(__('转账给用户[%s]', 'aksrt'), $to_user ? $to_user->display_name : (string) $recipient)
|
|
);
|
|
if (is_wp_error($deduct)) {
|
|
return $deduct;
|
|
}
|
|
AKSRT_Balance::add(
|
|
$recipient,
|
|
$credit,
|
|
'transfer_in',
|
|
['type' => 'user', 'id' => $from_id],
|
|
sprintf(__('来自用户[%s]的转账', 'aksrt'), $from_user->display_name ?: (string) $from_id)
|
|
);
|
|
return rest_ensure_response([
|
|
'success' => true,
|
|
'data' => [
|
|
'message' => __('转账成功', 'aksrt'),
|
|
'reload' => true,
|
|
'balance' => AKSRT_Balance::balance($from_id),
|
|
],
|
|
]);
|
|
}
|
|
|
|
// points transfer
|
|
$points_transfer_on = !empty(AKSRT_Settings::get_all()['points_transfer_s']);
|
|
if (!class_exists('AKSRT_Settings') || !AKSRT_Settings::credits_enabled() || !$points_transfer_on) {
|
|
return new WP_Error('aksrt_transfer_closed', __('积分转账未开启', 'aksrt'), ['status' => 403]);
|
|
}
|
|
if (class_exists('AKSRT_Settings_Capabilities') && !AKSRT_Settings_Capabilities::user_can('points_transfer', $from_id)) {
|
|
return new WP_Error('aksrt_transfer_forbidden', __('当前账号无权转账积分', 'aksrt'), ['status' => 403]);
|
|
}
|
|
$amount = (int) $amount_raw;
|
|
if ($amount <= 0) {
|
|
return new WP_Error('aksrt_transfer_amount', __('请输入有效积分', 'aksrt'), ['status' => 400]);
|
|
}
|
|
if (!class_exists('AKSRT_Credits')) {
|
|
return new WP_Error('aksrt_credits_unavailable', __('积分系统不可用', 'aksrt'), ['status' => 500]);
|
|
}
|
|
$fee_rate = max(0, min(50, (int) (AKSRT_Settings::get_all()['points_service_charge'] ?? 0)));
|
|
$credit = $fee_rate > 0 ? (int) floor($amount * (100 - $fee_rate) / 100) : $amount;
|
|
$from_user = wp_get_current_user();
|
|
$to_user = get_userdata($recipient);
|
|
$deduct = AKSRT_Credits::deduct(
|
|
$from_id,
|
|
$amount,
|
|
'transfer_out',
|
|
['type' => 'user', 'id' => $recipient],
|
|
sprintf(__('转账给用户[%s]', 'aksrt'), $to_user ? $to_user->display_name : (string) $recipient)
|
|
);
|
|
if (is_wp_error($deduct)) {
|
|
return $deduct;
|
|
}
|
|
AKSRT_Credits::add(
|
|
$recipient,
|
|
$credit,
|
|
'transfer_in',
|
|
['type' => 'user', 'id' => $from_id],
|
|
sprintf(__('来自用户[%s]的转账', 'aksrt'), $from_user->display_name ?: (string) $from_id)
|
|
);
|
|
return rest_ensure_response([
|
|
'success' => true,
|
|
'data' => [
|
|
'message' => __('转账成功', 'aksrt'),
|
|
'reload' => true,
|
|
'balance' => AKSRT_Credits::balance($from_id),
|
|
],
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
public function rest_transfer_search(WP_REST_Request $request) {
|
|
$s = sanitize_text_field((string) $request->get_param('s'));
|
|
if (mb_strlen($s) < 1) {
|
|
return rest_ensure_response(['success' => true, 'data' => ['users' => []]]);
|
|
}
|
|
$exclude = [get_current_user_id()];
|
|
$q = new WP_User_Query([
|
|
'search' => '*' . $s . '*',
|
|
'search_columns' => ['user_login', 'user_nicename', 'display_name', 'user_email'],
|
|
'exclude' => $exclude,
|
|
'number' => 20,
|
|
'fields' => ['ID', 'display_name', 'user_login'],
|
|
]);
|
|
$users = [];
|
|
foreach ((array) $q->get_results() as $u) {
|
|
$users[] = [
|
|
'id' => (int) $u->ID,
|
|
'display_name' => (string) $u->display_name,
|
|
'user_login' => (string) $u->user_login,
|
|
'avatar' => get_avatar_url((int) $u->ID, ['size' => 48]),
|
|
];
|
|
}
|
|
return rest_ensure_response(['success' => true, 'data' => ['users' => $users]]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Exchange points → cash wallet (pay_balance_exchange_*).
|
|
*/
|
|
public function rest_exchange_from_credits(WP_REST_Request $request) {
|
|
if (!class_exists('AKSRT_Balance') || !AKSRT_Balance::enabled()) {
|
|
return new WP_Error('aksrt_balance_disabled', __('余额功能未启用', 'aksrt'), ['status' => 403]);
|
|
}
|
|
$cfg = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_payment_recharge_settings() : [];
|
|
if (empty($cfg['exchange']['enabled'])) {
|
|
return new WP_Error('aksrt_exchange_closed', __('积分兑换余额未开启', 'aksrt'), ['status' => 403]);
|
|
}
|
|
if (!class_exists('AKSRT_Credits') || !AKSRT_Settings::credits_enabled()) {
|
|
return new WP_Error('aksrt_credits_disabled', __('积分功能未启用', 'aksrt'), ['status' => 403]);
|
|
}
|
|
$params = $request->get_json_params();
|
|
if (!is_array($params)) {
|
|
$params = $request->get_params();
|
|
}
|
|
$points = (int) ($params['points'] ?? 0);
|
|
$rate = max(1, (int) ($cfg['exchange']['rate'] ?? 100)); // N points = 1 yuan
|
|
if ($points < $rate) {
|
|
return new WP_Error('aksrt_exchange_min', sprintf(__('最少兑换 %d 积分', 'aksrt'), $rate), ['status' => 400]);
|
|
}
|
|
// Floor to whole yuan.
|
|
$yuan = (int) floor($points / $rate);
|
|
$cost = $yuan * $rate;
|
|
if ($yuan <= 0 || $cost <= 0) {
|
|
return new WP_Error('aksrt_exchange_amount', __('兑换数量无效', 'aksrt'), ['status' => 400]);
|
|
}
|
|
$user_id = get_current_user_id();
|
|
$deduct = AKSRT_Credits::deduct(
|
|
$user_id,
|
|
$cost,
|
|
'exchange_to_balance',
|
|
['type' => 'balance'],
|
|
sprintf(__('积分兑换余额 %d 元', 'aksrt'), $yuan)
|
|
);
|
|
if (is_wp_error($deduct)) {
|
|
return $deduct;
|
|
}
|
|
$add = AKSRT_Balance::add(
|
|
$user_id,
|
|
(float) $yuan,
|
|
'exchange_from_credits',
|
|
['type' => 'credits'],
|
|
sprintf(__('积分兑换:消耗 %d 积分', 'aksrt'), $cost)
|
|
);
|
|
if (is_wp_error($add)) {
|
|
// best-effort refund points
|
|
AKSRT_Credits::add($user_id, $cost, 'exchange_refund', [], __('兑换失败退回积分', 'aksrt'));
|
|
return $add;
|
|
}
|
|
return rest_ensure_response([
|
|
'success' => true,
|
|
'data' => [
|
|
'message' => sprintf(__('兑换成功:+%d 元', 'aksrt'), $yuan),
|
|
'wallet_balance' => AKSRT_Balance::balance($user_id),
|
|
'credit_balance' => AKSRT_Credits::balance($user_id),
|
|
'reload' => true,
|
|
],
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
public function rest_points_buy_options(WP_REST_Request $request) {
|
|
if (!$this->points_buy_enabled()) {
|
|
return new WP_Error('aksrt_points_buy_disabled', __('购买积分未开启', 'aksrt'), ['status' => 403]);
|
|
}
|
|
return rest_ensure_response([
|
|
'success' => true,
|
|
'data' => $this->get_points_buy_view(get_current_user_id()),
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
public function rest_points_buy_create(WP_REST_Request $request) {
|
|
if (!$this->points_buy_enabled()) {
|
|
return new WP_Error('aksrt_points_buy_disabled', __('购买积分未开启', 'aksrt'), ['status' => 403]);
|
|
}
|
|
$params = $request->get_json_params();
|
|
if (!is_array($params)) {
|
|
$params = $request->get_params();
|
|
}
|
|
$package_key = sanitize_text_field((string) ($params['package'] ?? 'custom'));
|
|
$custom_points = (int) ($params['custom_points'] ?? $params['points'] ?? 0);
|
|
$payment_method = sanitize_key((string) ($params['payment_method'] ?? 'alipay'));
|
|
$result = $this->create_points_buy_order(get_current_user_id(), $package_key, $custom_points, $payment_method);
|
|
if (is_wp_error($result)) {
|
|
return $result;
|
|
}
|
|
return rest_ensure_response(['success' => true, 'data' => $result]);
|
|
}
|
|
|
|
|
|
|
|
public function rest_balance_withdraw(WP_REST_Request $request) {
|
|
if (!class_exists('AKSRT_Balance') || !AKSRT_Balance::enabled()) {
|
|
return new WP_Error('aksrt_balance_disabled', __('余额功能未启用', 'aksrt'), ['status' => 403]);
|
|
}
|
|
$settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_all() : [];
|
|
if (empty($settings['pay_balance_withdraw_s'])) {
|
|
return new WP_Error('aksrt_withdraw_closed', __('余额提现未开启', 'aksrt'), ['status' => 403]);
|
|
}
|
|
if (!class_exists('AKSRT_Withdrawal')) {
|
|
return new WP_Error('aksrt_withdraw_unavailable', __('提现系统不可用', 'aksrt'), ['status' => 500]);
|
|
}
|
|
$params = $request->get_json_params();
|
|
if (!is_array($params)) {
|
|
$params = $request->get_params();
|
|
}
|
|
$user_id = get_current_user_id();
|
|
if (class_exists('AKSRT_Settings_Capabilities') && !AKSRT_Settings_Capabilities::user_can('balance_withdraw', $user_id)) {
|
|
return new WP_Error('aksrt_withdraw_forbidden', __('当前账号无权提现', 'aksrt'), ['status' => 403]);
|
|
}
|
|
$amount = round((float) ($params['amount'] ?? 0), 2);
|
|
$method = sanitize_key((string) ($params['method'] ?? 'alipay'));
|
|
$account = sanitize_textarea_field((string) ($params['account_info'] ?? $params['account'] ?? ''));
|
|
$lowest = max(1, (float) ($settings['pay_balance_withdraw_lowest_money'] ?? $settings['pay_rebate_withdraw_lowest_money'] ?? 50));
|
|
if ($amount < $lowest) {
|
|
return new WP_Error('aksrt_withdraw_min', sprintf(__('最低提现 %s 元', 'aksrt'), (string) $lowest), ['status' => 400]);
|
|
}
|
|
if ($account === '') {
|
|
return new WP_Error('aksrt_withdraw_account', __('请填写收款账号信息', 'aksrt'), ['status' => 400]);
|
|
}
|
|
// One open balance withdrawal at a time (Zibll parity).
|
|
if (AKSRT_Withdrawal::get_instance()->has_open_withdrawal($user_id)) {
|
|
return new WP_Error('aksrt_withdraw_pending', __('您有处理中的提现申请,请等待完成后再申请', 'aksrt'), ['status' => 409]);
|
|
}
|
|
$wallet = AKSRT_Balance::balance($user_id);
|
|
if ($wallet < $amount) {
|
|
return new WP_Error('aksrt_balance_insufficient', __('余额不足', 'aksrt'), ['status' => 402]);
|
|
}
|
|
$deduct = AKSRT_Balance::deduct($user_id, $amount, 'withdraw_hold', ['type' => 'withdraw'], __('余额提现申请冻结', 'aksrt'));
|
|
if (is_wp_error($deduct)) {
|
|
return $deduct;
|
|
}
|
|
$fee_rate = max(0, min(50, (int) ($settings['withdraw_service_charge'] ?? 0)));
|
|
$net = $fee_rate > 0 ? round($amount * (100 - $fee_rate) / 100, 2) : $amount;
|
|
$account_payload = wp_json_encode([
|
|
'account' => $account,
|
|
'gross_amount' => $amount,
|
|
'fee_rate' => $fee_rate,
|
|
'source' => 'balance',
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: $account;
|
|
$wid = AKSRT_Withdrawal::get_instance()->create($user_id, $net, $method !== '' ? $method : 'alipay', $account_payload);
|
|
if ($wid <= 0) {
|
|
AKSRT_Balance::add($user_id, $amount, 'withdraw_refund', [], __('提现申请失败退回', 'aksrt'));
|
|
return new WP_Error('aksrt_withdraw_failed', __('提现申请失败', 'aksrt'), ['status' => 500]);
|
|
}
|
|
return rest_ensure_response([
|
|
'success' => true,
|
|
'data' => [
|
|
'message' => __('提现申请已提交,请等待处理', 'aksrt'),
|
|
'withdrawal_id' => $wid,
|
|
'amount' => $amount,
|
|
'net_amount' => $net,
|
|
'wallet_balance' => AKSRT_Balance::balance($user_id),
|
|
'reload' => true,
|
|
],
|
|
]);
|
|
}
|
|
|
|
}
|