- assets/vendor 全量迁移: fontawesome5→assets/fontawesome5, css→assets/css, js→assets/js/runtime(避开 14 个同名自有文件), img 合并去重→assets/img, fonts→assets/fonts; 删除 assets/vendor - libs 五个与 widgets/third-party 字节相同的库不重复入库, loader paths 改指向 - 运行时切换: _win.uri=assets 根, loader baseUrl=/js/runtime, enlighter 注入 路径同步; PHP 7 个引用文件切换并改名 $zibll_*→$runtime_* - 去品牌: Qinver 头注释/品牌 console.log/演示链接清除, 默认文案中性化 - 功能标识符改名: zib__→aksrt__, zib_ajax→aksrt_ajax, window.zib→ window.aksrtLegacy, zibllblock/*→aksrtblock/*, .zib-widget→.aksrt-panel, .zib-slider→.aksrt-slider, zib_menu_options meta 键等 - 自有 CSS 改名: legacy-utilities/compat-vars/footer shell - admin SPA 重建; 契约测试同步(路径/断言/文件更名) - 例外保留: tests/visual 对照环境与 fixtures 前缀(外部 Zibll 产品引用) 验证: php -l 133 个改动文件通过; runtime 86 个 JS node --check 通过; 契约测试 68/70(2 个失败经 stash 对照确认为存量问题)
321 lines
11 KiB
PHP
321 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Credits;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait BalanceServiceCharge {
|
|
|
|
|
|
|
|
/**
|
|
* When a recharge order is paid, credit the wallet.
|
|
*
|
|
* @param string $order_number Order number.
|
|
* @param array<string,mixed> $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;
|
|
}
|
|
|
|
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<string,mixed>
|
|
*/
|
|
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'] ?? ''),
|
|
],
|
|
'pass_charge' => is_array($cfg['pass_charge'] ?? null) ? $cfg['pass_charge'] : [],
|
|
];
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @return array<int,array{key:string,price:float,pay_price:float,title:string,tag:string}>
|
|
*/
|
|
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 — legacy 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<string,mixed>|\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' => \AKSRT\Settings\PaymentRuntime::get_order_expiry_at(),
|
|
]);
|
|
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,
|
|
];
|
|
}
|
|
|
|
}
|