进行了一些小优化

This commit is contained in:
2026-08-24 18:08:06 +08:00
parent 10b3e6dd9f
commit f5c60cd425
5 changed files with 127 additions and 24 deletions
+35 -10
View File
@@ -373,17 +373,42 @@ trait BalanceServiceRest {
}
return true;
};
$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]);
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);
$repo = \AKSRT\Payment\OrderRepository::get_instance();
$existing_order = $repo->get_by_request_key($operation_key);
$is_new_order = !is_array($existing_order);
if (is_array($existing_order)) {
$existing_meta = json_decode((string) ($existing_order['note'] ?? ''), true);
$existing_meta = is_array($existing_meta) ? $existing_meta : [];
if ((int) ($existing_order['user_id'] ?? 0) !== $user_id
|| sanitize_key((string) ($existing_order['product_type'] ?? '')) !== \AKSRT\Core\DomainModel::ORDER_PRODUCT_POINTS
|| (int) ($existing_order['product_id'] ?? 0) !== 0
|| (string) ($existing_order['amount'] ?? '') !== $amount->decimal()
|| sanitize_key((string) ($existing_order['currency'] ?? '')) !== 'cny'
|| sanitize_key((string) ($existing_order['payment_method'] ?? '')) !== 'balance'
|| (int) ($existing_meta['points'] ?? 0) !== $points
) {
return new \WP_Error('aksrt_ledger_idempotency_conflict', __('相同操作标识已用于其他积分购买请求', 'aksrt'), ['status' => 409]);
}
$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 = (string) ($existing_order['order_number'] ?? '');
} else {
$order_number = $repo->allocate_order_number();
}
if ($order_number === '') {
return new \WP_Error('aksrt_order_create_failed', __('积分订单创建失败', 'aksrt'), ['status' => 500]);
}
$business_mutation = static function (int $transaction_id) use ($is_new_order, $repo, $user_id, $amount, $points, $operation_key, $order_number, $note) {
if (!$is_new_order) {
return true;
}
$created = $repo->create([
'order_number' => $order_number,
'user_id' => $user_id,
'product_type' => \AKSRT\Core\DomainModel::ORDER_PRODUCT_POINTS,