feat(credits): exchange balance for points
Replace the legacy points-to-balance and external points purchase flows with direct integer balance-to-points exchange. Enforce exchange and points-transfer limits and capability checks across the UI and REST API.
This commit is contained in:
@@ -49,27 +49,10 @@ trait BalanceServiceRest {
|
||||
],
|
||||
]);
|
||||
|
||||
register_rest_route('aksrt/v1', '/pay/balance/exchange', [
|
||||
register_rest_route('aksrt/v1', '/pay/balance/points-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'],
|
||||
'callback' => [$this, 'rest_exchange_balance_for_points'],
|
||||
'permission_callback' => static function () {
|
||||
return is_user_logged_in();
|
||||
},
|
||||
@@ -208,21 +191,28 @@ trait BalanceServiceRest {
|
||||
}
|
||||
|
||||
// points transfer
|
||||
$points_transfer_on = !empty(\AKSRT\Settings\Settings::get_all()['points_transfer_s']);
|
||||
if (!class_exists('\AKSRT\Settings\Settings') || !\AKSRT\Settings\Settings::credits_enabled() || !$points_transfer_on) {
|
||||
if (!class_exists('\AKSRT\Settings\Settings') || !\AKSRT\Settings\Settings::credits_enabled()) {
|
||||
return new \WP_Error('aksrt_transfer_closed', __('积分转账未开启', 'aksrt'), ['status' => 403]);
|
||||
}
|
||||
$points_settings = \AKSRT\Settings\Settings::get_all();
|
||||
if (empty($points_settings['points_transfer_s'])) {
|
||||
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) {
|
||||
if (filter_var($amount_raw, FILTER_VALIDATE_INT) === false || (int) $amount_raw <= 0) {
|
||||
return new \WP_Error('aksrt_transfer_amount', __('请输入有效积分', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
$amount = (int) $amount_raw;
|
||||
$min_amount = max(1, (int) ($points_settings['points_transfer_min'] ?? 1));
|
||||
if ($amount < $min_amount) {
|
||||
return new \WP_Error('aksrt_transfer_minimum', sprintf(__('单笔转账至少需要 %d 积分', 'aksrt'), $min_amount), ['status' => 400]);
|
||||
}
|
||||
if (!class_exists('AKSRT\\Credits\\Credits')) {
|
||||
return new \WP_Error('aksrt_credits_unavailable', __('积分系统不可用', 'aksrt'), ['status' => 500]);
|
||||
}
|
||||
$fee_rate = max(0, min(50, (int) (\AKSRT\Settings\Settings::get_all()['points_service_charge'] ?? 0)));
|
||||
$fee_rate = max(0, min(50, (int) ($points_settings['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);
|
||||
@@ -236,13 +226,26 @@ trait BalanceServiceRest {
|
||||
if (is_wp_error($deduct)) {
|
||||
return $deduct;
|
||||
}
|
||||
Credits::add(
|
||||
$add = Credits::add(
|
||||
$recipient,
|
||||
$credit,
|
||||
'transfer_in',
|
||||
['type' => 'user', 'id' => $from_id],
|
||||
sprintf(__('来自用户[%s]的转账', 'aksrt'), $from_user->display_name ?: (string) $from_id)
|
||||
);
|
||||
if (is_wp_error($add)) {
|
||||
$refund = Credits::add(
|
||||
$from_id,
|
||||
$amount,
|
||||
'transfer_refund',
|
||||
['type' => 'user', 'id' => $recipient],
|
||||
__('收款积分入账失败,已退回转账积分', 'aksrt')
|
||||
);
|
||||
if (is_wp_error($refund)) {
|
||||
return new \WP_Error('aksrt_transfer_recovery_failed', __('收款积分入账失败,转账积分退回失败,请联系管理员', 'aksrt'), ['status' => 500]);
|
||||
}
|
||||
return $add;
|
||||
}
|
||||
return rest_ensure_response([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
@@ -283,102 +286,82 @@ trait BalanceServiceRest {
|
||||
|
||||
|
||||
/**
|
||||
* Exchange points → cash wallet (pay_balance_exchange_*).
|
||||
* Exchange integer cash-wallet balance for points.
|
||||
*/
|
||||
public function rest_exchange_from_credits(\WP_REST_Request $request) {
|
||||
public function rest_exchange_balance_for_points(\WP_REST_Request $request) {
|
||||
if (!class_exists('AKSRT\\Credits\\Balance') || !Balance::enabled()) {
|
||||
return new \WP_Error('aksrt_balance_disabled', __('余额功能未启用', 'aksrt'), ['status' => 403]);
|
||||
}
|
||||
$cfg = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\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\\Credits') || !\AKSRT\Settings\Settings::credits_enabled()) {
|
||||
if (!class_exists('AKSRT\\Credits\\Credits') || !class_exists('AKSRT\\Settings\\Settings') || !\AKSRT\Settings\Settings::credits_enabled()) {
|
||||
return new \WP_Error('aksrt_credits_disabled', __('积分功能未启用', 'aksrt'), ['status' => 403]);
|
||||
}
|
||||
$cfg = \AKSRT\Settings\Settings::get_payment_recharge_settings();
|
||||
$exchange = is_array($cfg['points_exchange'] ?? null) ? $cfg['points_exchange'] : [];
|
||||
if (empty($exchange['enabled'])) {
|
||||
return new \WP_Error('aksrt_exchange_closed', __('余额兑换积分未开启', '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]);
|
||||
$raw_amount = $params['amount'] ?? 0;
|
||||
if (filter_var($raw_amount, FILTER_VALIDATE_INT) === false || (int) $raw_amount <= 0) {
|
||||
return new \WP_Error('aksrt_exchange_amount', __('兑换余额必须为正整数', 'aksrt'), ['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]);
|
||||
$amount = (int) $raw_amount;
|
||||
$rate = max(1, (int) ($exchange['rate'] ?? 100));
|
||||
$daily_max = max(1, (int) ($exchange['daily_max_points'] ?? 100000));
|
||||
if ($amount > intdiv($daily_max, $rate)) {
|
||||
return new \WP_Error('aksrt_exchange_daily_limit', sprintf(__('今日购买积分上限为 %d', 'aksrt'), $daily_max), ['status' => 400]);
|
||||
}
|
||||
$points = $amount * $rate;
|
||||
$min_points = max(1, (int) ($exchange['min_points'] ?? 100));
|
||||
if ($points < $min_points) {
|
||||
return new \WP_Error('aksrt_exchange_min', sprintf(__('单笔至少购买 %d 积分', 'aksrt'), $min_points), ['status' => 400]);
|
||||
}
|
||||
$user_id = get_current_user_id();
|
||||
$deduct = Credits::deduct(
|
||||
$user_id,
|
||||
$cost,
|
||||
'exchange_to_balance',
|
||||
['type' => 'balance'],
|
||||
sprintf(__('积分兑换余额 %d 元', 'aksrt'), $yuan)
|
||||
);
|
||||
if (is_wp_error($deduct)) {
|
||||
return $deduct;
|
||||
$today_points = $this->today_points_exchange_total($user_id);
|
||||
if ($today_points + $points > $daily_max) {
|
||||
return new \WP_Error('aksrt_exchange_daily_limit', sprintf(__('今日购买积分上限为 %d,当前还可购买 %d', 'aksrt'), $daily_max, max(0, $daily_max - $today_points)), ['status' => 400]);
|
||||
}
|
||||
$add = Balance::add(
|
||||
$user_id,
|
||||
(float) $yuan,
|
||||
'exchange_from_credits',
|
||||
['type' => 'credits'],
|
||||
sprintf(__('积分兑换:消耗 %d 积分', 'aksrt'), $cost)
|
||||
);
|
||||
try {
|
||||
$exchange_ref = random_int(1, PHP_INT_MAX);
|
||||
} catch (\Throwable $e) {
|
||||
$exchange_ref = (int) round(microtime(true) * 1000000);
|
||||
}
|
||||
$ref = ['type' => 'points_exchange', 'id' => $exchange_ref];
|
||||
$deduct = Balance::deduct($user_id, (float) $amount, 'points_exchange_out', $ref, sprintf(__('余额兑换 %d 积分', 'aksrt'), $points));
|
||||
if (is_wp_error($deduct)) return $deduct;
|
||||
$add = Credits::add($user_id, $points, 'points_exchange_in', $ref, sprintf(__('余额兑换:扣除 %d 余额', 'aksrt'), $amount));
|
||||
if (is_wp_error($add)) {
|
||||
// best-effort refund points
|
||||
Credits::add($user_id, $cost, 'exchange_refund', [], __('兑换失败退回积分', 'aksrt'));
|
||||
$refund = Balance::add($user_id, (float) $amount, 'points_exchange_refund', $ref, __('积分入账失败,已退回兑换余额', 'aksrt'));
|
||||
if (is_wp_error($refund)) return new \WP_Error('aksrt_exchange_recovery_failed', __('积分入账失败,余额退回失败,请联系管理员', 'aksrt'), ['status' => 500]);
|
||||
return $add;
|
||||
}
|
||||
return rest_ensure_response([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'message' => sprintf(__('兑换成功:+%d 元', 'aksrt'), $yuan),
|
||||
'wallet_balance' => Balance::balance($user_id),
|
||||
'credit_balance' => Credits::balance($user_id),
|
||||
'reload' => true,
|
||||
],
|
||||
]);
|
||||
return rest_ensure_response(['success' => true, 'data' => [
|
||||
'message' => sprintf(__('兑换成功,获得 %d 积分', 'aksrt'), $points),
|
||||
'wallet_balance' => Balance::balance($user_id),
|
||||
'credit_balance' => Credits::balance($user_id),
|
||||
'points' => $points,
|
||||
'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()),
|
||||
]);
|
||||
private function today_points_exchange_total(int $user_id): int {
|
||||
if ($user_id <= 0 || !class_exists('AKSRT\\Core\\DBMigrator')) return 0;
|
||||
global $wpdb;
|
||||
$table = \AKSRT\Core\DBMigrator::table('credit_log');
|
||||
if ($table === '') return 0;
|
||||
$site_now = current_datetime();
|
||||
$start = (clone $site_now)->setTime(0, 0, 0);
|
||||
$end = (clone $start)->modify('+1 day');
|
||||
$utc = new \DateTimeZone('UTC');
|
||||
$sql = "SELECT COALESCE(SUM(delta), 0) FROM {$table} WHERE user_id = %d AND reason = %s AND created_at >= %s AND created_at < %s";
|
||||
$start_gmt = $start->setTimezone($utc)->format('Y-m-d H:i:s');
|
||||
$end_gmt = $end->setTimezone($utc)->format('Y-m-d H:i:s');
|
||||
return max(0, (int) $wpdb->get_var($wpdb->prepare($sql, $user_id, 'points_exchange_in', $start_gmt, $end_gmt)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
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\\Credits\\Balance') || !Balance::enabled()) {
|
||||
return new \WP_Error('aksrt_balance_disabled', __('余额功能未启用', 'aksrt'), ['status' => 403]);
|
||||
|
||||
Reference in New Issue
Block a user