修复两个问题

This commit is contained in:
2026-08-24 16:38:03 +08:00
parent 48a82403b2
commit 58f3f42bf8
15 changed files with 173 additions and 23 deletions
+48 -3
View File
@@ -12,6 +12,12 @@ trait BalanceServiceRest {
public function register_routes(): void {
register_rest_route('aksrt/v1', '/pay/assets', [
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, 'rest_asset_snapshot'],
'permission_callback' => [$this, 'check_asset_rest'],
]);
register_rest_route('aksrt/v1', '/pay/balance/charge', [
[
'methods' => \WP_REST_Server::READABLE,
@@ -68,6 +74,17 @@ trait BalanceServiceRest {
}
public function rest_asset_snapshot(\WP_REST_Request $request) {
$user_id = get_current_user_id();
return rest_ensure_response([
'success' => true,
'data' => [
'wallet_balance' => \AKSRT\Ledger\LedgerService::get_instance()->balance_decimal($user_id),
'credit_balance' => (string) \AKSRT\Ledger\LedgerService::get_instance()->balance($user_id, \AKSRT\Ledger\AssetType::POINTS),
],
]);
}
public function check_asset_rest(\WP_REST_Request $request) {
if (!is_user_logged_in()) {
return new \WP_Error('aksrt_need_login', __('请先登录', 'aksrt'), ['status' => 401]);
@@ -356,9 +373,36 @@ trait BalanceServiceRest {
}
return true;
};
$business_mutation = static function () use ($user_id, $amount, $points, $operation_key) {
$order_number = 'PX' . strtoupper(substr(hash('sha256', $operation_key), 0, 30));
$business_mutation = static function (int $transaction_id) use ($user_id, $amount, $points, $operation_key, $order_number) {
if (!class_exists('\\AKSRT\\Payment\\OrderRepository')) {
return new \WP_Error('aksrt_order_unavailable', __('订单服务不可用', 'aksrt'), ['status' => 500]);
}
$note = wp_json_encode([
'points_exchange' => 1,
'points' => $points,
'product_title' => sprintf(__('购买 %d 积分', 'aksrt'), $points),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$created = \AKSRT\Payment\OrderRepository::get_instance()->create([
'order_number' => $order_number,
'user_id' => $user_id,
'product_type' => \AKSRT\Core\DomainModel::ORDER_PRODUCT_POINTS,
'product_id' => 0,
'amount' => $amount->decimal(),
'currency' => 'CNY',
'status' => \AKSRT\Payment\Order::STATUS_COMPLETED,
'payment_method' => 'balance',
'payment_transaction_id' => 'ledger_' . $transaction_id,
'paid_at' => current_time('mysql'),
'completed_at' => current_time('mysql'),
'note' => is_string($note) ? $note : '',
'request_key' => $operation_key,
]);
if ($created !== $order_number) {
return new \WP_Error('aksrt_order_create_failed', __('积分订单创建失败', 'aksrt'), ['status' => 500]);
}
if (class_exists('\\AKSRT\\Payment\\CommissionLedger')) {
return \AKSRT\Payment\CommissionLedger::get_instance()->record_points_exchange($user_id, $amount->decimal(), $points, $operation_key);
return \AKSRT\Payment\CommissionLedger::get_instance()->record_points_exchange($user_id, $amount->decimal(), $points, $operation_key, $order_number);
}
return true;
};
@@ -368,7 +412,7 @@ trait BalanceServiceRest {
$points,
$operation_key,
'points_exchange',
['type' => 'points_exchange', 'key' => $operation_key],
['type' => 'order', 'key' => $order_number],
sprintf(__('余额兑换 %d 积分', 'aksrt'), $points),
['rate' => $rate],
$guard,
@@ -384,6 +428,7 @@ trait BalanceServiceRest {
'wallet_balance' => \AKSRT\Ledger\AssetAmount::from_minor(\AKSRT\Ledger\AssetType::BALANCE, $result->balance($user_id, \AKSRT\Ledger\AssetType::BALANCE))->decimal(),
'credit_balance' => $result->balance($user_id, \AKSRT\Ledger\AssetType::POINTS),
'points' => $points,
'order_number' => $order_number,
'reload' => true,
]]);
}