$order Order row. */ public function on_order_paid(string $order_number, array $order = []): void { if ($order === [] && class_exists('\AKSRT\Payment\OrderRepository')) { $order = \AKSRT\Payment\OrderRepository::get_instance()->get($order_number) ?: []; } if (!$order) { return; } $type = sanitize_key((string) ($order['product_type'] ?? '')); $user_id = (int) ($order['user_id'] ?? 0); if ($user_id <= 0) { return; } // Points purchase fulfill (zibll order_type=9). if ($type === \AKSRT\Core\DomainModel::ORDER_PRODUCT_POINTS_BUY) { if (!class_exists('AKSRT\\Credits\\Credits')) { return; } $note = json_decode((string) ($order['note'] ?? ''), true); if (!is_array($note)) { $note = []; } if (!empty($note['points_credited'])) { return; } $rate = 100; if (class_exists('\AKSRT\Settings\Settings')) { $rate = max(1, (int) (\AKSRT\Settings\Settings::get_all()['points_rate'] ?? 100)); } $points = isset($note['points_amount']) ? (int) $note['points_amount'] : (int) round((float) ($order['amount'] ?? 0) * $rate); if ($points <= 0) { return; } global $wpdb; if ($wpdb->query('START TRANSACTION') === false) { return; } $add = Credits::add( $user_id, $points, 'points_buy', ['type' => 'order', 'order' => $order_number], sprintf(__('购买积分订单 %s', 'aksrt'), $order_number), false ); if (is_wp_error($add)) { $wpdb->query('ROLLBACK'); return; } $note['points_credited'] = 1; $note['credited_points'] = $points; $updated = $wpdb->update( $wpdb->prefix . 'aksrt_order', ['note' => wp_json_encode($note, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)], ['order_number' => $order_number], ['%s'], ['%s'] ); if ($updated === false) { $wpdb->query('ROLLBACK'); wp_cache_delete($user_id, 'user_meta'); return; } if ($wpdb->query('COMMIT') === false) { $wpdb->query('ROLLBACK'); wp_cache_delete($user_id, 'user_meta'); return; } do_action('aksrt_credits_added', $user_id, $points, 'points_buy', (int) $add); if (class_exists('\AKSRT\Payment\OrderRepository')) { \AKSRT\Payment\OrderRepository::get_instance()->update_status( $order_number, \AKSRT\Payment\Order::STATUS_COMPLETED, ['note' => __('购买积分已到账。', 'aksrt')] ); } return; } if ($type !== \AKSRT\Core\DomainModel::ORDER_PRODUCT_RECHARGE) { return; } if (!class_exists('AKSRT\\Credits\\Balance')) { return; } // Honor pay_auto_to_balance (default on). When off, leave order paid without wallet credit. if (class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'get_payment_recharge_settings')) { $recharge_cfg = \AKSRT\Settings\Settings::get_payment_recharge_settings(); if (isset($recharge_cfg['auto_to_balance']) && empty($recharge_cfg['auto_to_balance'])) { return; } } $note = json_decode((string) ($order['note'] ?? ''), true); if (!is_array($note)) { $note = []; } if (!empty($note['balance_credited'])) { return; } $credit_amount = isset($note['credit_amount']) ? round((float) $note['credit_amount'], 2) : round((float) ($order['amount'] ?? 0), 2); if ($credit_amount <= 0) { return; } global $wpdb; if ($wpdb->query('START TRANSACTION') === false) { return; } $result = Balance::add( $user_id, $credit_amount, 'recharge', ['type' => 'order', 'order' => $order_number], sprintf(__('余额充值订单 %s', 'aksrt'), $order_number), false ); if (is_wp_error($result)) { $wpdb->query('ROLLBACK'); return; } $note['balance_credited'] = 1; $note['credited_amount'] = $credit_amount; $updated = $wpdb->update( $wpdb->prefix . 'aksrt_order', ['note' => wp_json_encode($note, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)], ['order_number' => $order_number], ['%s'], ['%s'] ); if ($updated === false) { $wpdb->query('ROLLBACK'); wp_cache_delete($user_id, 'user_meta'); return; } if ($wpdb->query('COMMIT') === false) { $wpdb->query('ROLLBACK'); wp_cache_delete($user_id, 'user_meta'); return; } do_action('aksrt_balance_added', $user_id, $credit_amount, 'recharge', (float) $result); if (class_exists('\AKSRT\Payment\OrderRepository')) { \AKSRT\Payment\OrderRepository::get_instance()->update_status( $order_number, \AKSRT\Payment\Order::STATUS_COMPLETED, ['note' => __('充值金额已到账。', 'aksrt')] ); } } /** * @return array */ public function get_charge_view(int $user_id): array { $cfg = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_payment_recharge_settings() : []; $packages = $this->parse_packages((string) ($cfg['packages_raw'] ?? '')); $custom_s = !isset($cfg['product']['custom_s']) || !empty($cfg['product']['custom_s']); $custom_limit = max(1, (int) ($cfg['product']['custom_limit'] ?? 9999)); $methods = []; if (function_exists('aksrt_list_cash_payment_methods')) { // Exclude balance gateway for recharge (cannot pay wallet with wallet). foreach (aksrt_list_cash_payment_methods(true) as $m) { $id = is_array($m) ? (string) ($m['id'] ?? '') : (string) $m; if ($id === '' || $id === 'balance') { continue; } $methods[] = is_array($m) ? $m : ['id' => $id, 'name' => $id]; } } return [ 'enabled' => !empty($cfg['enabled']), 'desc' => (string) ($cfg['desc'] ?? ''), 'wallet' => class_exists('AKSRT\\Credits\\Balance') ? Balance::balance($user_id) : 0.0, 'packages' => $packages, 'custom_s' => $custom_s, 'custom_limit' => $custom_limit, 'currency_mark' => (string) ($cfg['product']['tag'] ?? '元'), 'payment_methods' => $methods, 'transfer' => [ 'enabled' => !empty($cfg['transfer']['enabled']), 'service_charge' => (int) ($cfg['transfer']['service_charge'] ?? 0), 'desc' => (string) ($cfg['transfer']['desc'] ?? ''), ], 'exchange' => [ 'enabled' => !empty($cfg['exchange']['enabled']), 'rate' => (int) ($cfg['exchange']['rate'] ?? 100), 'desc' => (string) ($cfg['exchange']['desc'] ?? ''), ], 'pass_charge' => is_array($cfg['pass_charge'] ?? null) ? $cfg['pass_charge'] : [], ]; } /** * @return array */ public function parse_packages(string $raw): array { $out = []; $lines = preg_split('/\r\n|\r|\n/', $raw) ?: []; $i = 0; foreach ($lines as $line) { $line = trim((string) $line); if ($line === '') { continue; } // pay|credit|title OR credit|pay|title — Zibll uses price(credit)|pay_price|tag // Admin help: 支付金额|到账金额|标题 $parts = array_map('trim', explode('|', $line)); if (count($parts) < 2) { continue; } $pay = (float) $parts[0]; $credit = (float) $parts[1]; $title = $parts[2] ?? ''; if ($pay <= 0 || $credit <= 0) { continue; } $out[] = [ 'key' => (string) $i, 'pay_price' => round($pay, 2), 'price' => round($credit, 2), 'title' => $title !== '' ? $title : sprintf(__('充值 %s 元', 'aksrt'), (string) $credit), 'tag' => $title, ]; $i++; } return $out; } /** * @return array|\WP_Error */ public function create_charge_order(int $user_id, string $package_key, float $custom_price, string $payment_method) { if ($user_id <= 0) { return new \WP_Error('aksrt_need_login', __('请先登录', 'aksrt'), ['status' => 401]); } if (!class_exists('\AKSRT\Payment\OrderRepository') || !class_exists('\AKSRT\Payment\Order')) { return new \WP_Error('aksrt_pay_unavailable', __('订单系统不可用', 'aksrt'), ['status' => 500]); } $view = $this->get_charge_view($user_id); $credit_amount = 0.0; $pay_amount = 0.0; $title = __('余额充值', 'aksrt'); if ($package_key === 'custom' || $package_key === '') { if (empty($view['custom_s'])) { return new \WP_Error('aksrt_custom_closed', __('未开放自定义金额', 'aksrt'), ['status' => 400]); } $custom_price = round($custom_price, 2); $min = 1.0; $max = (float) $view['custom_limit']; if ($custom_price < $min || $custom_price > $max) { return new \WP_Error('aksrt_custom_limit', sprintf(__('充值金额需在 %s ~ %s 之间', 'aksrt'), (string) $min, (string) $max), ['status' => 400]); } $pay_amount = $custom_price; $credit_amount = $custom_price; $title = sprintf(__('自定义充值 %s 元', 'aksrt'), (string) $custom_price); $package_key = 'custom'; } else { $found = null; foreach ((array) $view['packages'] as $pkg) { if ((string) ($pkg['key'] ?? '') === (string) $package_key) { $found = $pkg; break; } } if (!$found) { return new \WP_Error('aksrt_invalid_package', __('充值套餐无效', 'aksrt'), ['status' => 400]); } $pay_amount = (float) $found['pay_price']; $credit_amount = (float) $found['price']; $title = (string) $found['title']; } if ($pay_amount <= 0 || $credit_amount <= 0) { return new \WP_Error('aksrt_invalid_price', __('金额无效', 'aksrt'), ['status' => 400]); } $payment_method = sanitize_key($payment_method); if ($payment_method === '' || $payment_method === 'balance') { // Prefer first non-balance method. $methods = $view['payment_methods'] ?? []; $payment_method = !empty($methods[0]['id']) ? (string) $methods[0]['id'] : 'alipay'; } $note = wp_json_encode([ 'balance_recharge' => 1, 'package' => $package_key, 'credit_amount' => $credit_amount, 'pay_amount' => $pay_amount, 'title' => $title, ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $order_number = \AKSRT\Payment\OrderRepository::get_instance()->create([ 'user_id' => $user_id, 'product_type' => \AKSRT\Core\DomainModel::ORDER_PRODUCT_RECHARGE, 'product_id' => 0, 'amount' => $pay_amount, 'currency' => 'CNY', 'status' => \AKSRT\Payment\Order::STATUS_PENDING, 'payment_method' => $payment_method, 'note' => is_string($note) ? $note : '', 'expires_at' => gmdate('Y-m-d H:i:s', time() + 30 * MINUTE_IN_SECONDS), ]); if ($order_number === '') { return new \WP_Error('aksrt_order_create_failed', __('创建订单失败', 'aksrt'), ['status' => 500]); } $order = \AKSRT\Payment\OrderRepository::get_instance()->get($order_number); if (!$order) { return new \WP_Error('aksrt_order_create_failed', __('创建订单失败', 'aksrt'), ['status' => 500]); } $order['subject'] = $title; $order['payment_method'] = $payment_method; if (!class_exists('\AKSRT\Payment\Gateway')) { return new \WP_Error('aksrt_pay_unavailable', __('支付网关不可用', 'aksrt'), ['status' => 500]); } $resolved = \AKSRT\Payment\PaymentOrchestrator::resolve_gateway($payment_method); if (empty($resolved['gateway'])) { return new \WP_Error( (string) ($resolved['error_code'] ?? 'aksrt_gateway_unavailable'), (string) ($resolved['message'] ?? __('当前支付方式不可用', 'aksrt')), ['status' => 400, 'order_number' => $order_number] ); } $payment_result = \AKSRT\Payment\PaymentOrchestrator::pay($resolved['gateway'], $order, [ 'paid_note' => __('余额充值支付完成', 'aksrt'), ]); if (empty($payment_result['success'])) { return new \WP_Error( (string) ($payment_result['code'] ?? 'aksrt_pay_failed'), (string) ($payment_result['message'] ?? __('支付失败', 'aksrt')), ['status' => 402, 'order_number' => $order_number] ); } if (!empty($payment_result['redirect'])) { return [ 'redirect' => true, 'payment_url' => (string) ($payment_result['payment_url'] ?? ''), 'qr_content' => (string) ($payment_result['qr_content'] ?? ''), 'order_number' => $order_number, 'amount' => $pay_amount, 'credit_amount' => $credit_amount, 'message' => $payment_result['message'], 'reload' => false, 'poll' => true, ]; } // Synchronous gateway: PaymentOrchestrator's paid transition already fired // aksrt_order_paid, so fulfillment has run through the single hook path. return [ 'redirect' => false, 'order_number' => $order_number, 'amount' => $pay_amount, 'credit_amount' => $credit_amount, 'wallet' => Balance::balance($user_id), 'message' => __('充值成功', 'aksrt'), 'reload' => true, ]; } }