feat(pay): 卡密兑换入口收敛到三个购买弹窗并对齐参考主题
- 移除余额卡片独立「卡密兑换」通用弹窗,兑换入口分布到充值/积分/会员三个弹窗的支付方式中 - 充值弹窗补齐卡密 logo 与仅卡密时的金额选择隐藏逻辑 - 积分购买弹窗 1:1 复刻参考主题 points-pay modal(商品档位/自定义/支付方式/立即支付),REST 改为 product+custom 契约 - 会员弹窗注入卡密渠道:视图模型透传 card_pass,前端渲染卡密盒并直兑 /pay/card-pass/redeem type=vip - 后台卡密兑换拆分为余额/积分/会员三个独立开关,VIP 段旧会员卡密组清理 - REST type 改为必填(balance/vip/credits),三类型独立闸门 - 测试重写为 card-pass-modal-redeem 契约,321 项全部通过
This commit is contained in:
@@ -146,6 +146,21 @@ trait BalanceServiceCharge {
|
||||
$methods[] = is_array($m) ? $m : ['id' => $id, 'name' => $id];
|
||||
}
|
||||
}
|
||||
// 卡密支付渠道:受「启用卡密兑换余额功能」开关控制(开关内部已含收款接口主开关)。
|
||||
$pass_charge = is_array($cfg['pass_charge'] ?? null) ? $cfg['pass_charge'] : [];
|
||||
$card_pass_on = false;
|
||||
if (class_exists('\AKSRT\Payment\ChargeCard')) {
|
||||
$card_opts = \AKSRT\Payment\ChargeCard::get_instance()->get_public_options();
|
||||
$card_pass_on = !empty($card_opts['balance']['enabled']);
|
||||
$pass_charge = [
|
||||
'balance' => is_array($card_opts['balance'] ?? null) ? $card_opts['balance'] : $pass_charge,
|
||||
'credits' => is_array($card_opts['credits'] ?? null) ? $card_opts['credits'] : [],
|
||||
'vip' => is_array($card_opts['vip'] ?? null) ? $card_opts['vip'] : [],
|
||||
];
|
||||
}
|
||||
if ($card_pass_on) {
|
||||
$methods[] = ['id' => 'card_pass', 'name' => __('卡密', 'aksrt')];
|
||||
}
|
||||
return [
|
||||
'enabled' => !empty($cfg['enabled']),
|
||||
'desc' => (string) ($cfg['desc'] ?? ''),
|
||||
@@ -160,7 +175,7 @@ trait BalanceServiceCharge {
|
||||
'service_charge' => (int) ($cfg['transfer']['service_charge'] ?? 0),
|
||||
'desc' => (string) ($cfg['transfer']['desc'] ?? ''),
|
||||
],
|
||||
'pass_charge' => is_array($cfg['pass_charge'] ?? null) ? $cfg['pass_charge'] : [],
|
||||
'pass_charge' => $pass_charge,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -206,6 +221,48 @@ trait BalanceServiceCharge {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 积分商品行解析("积分|支付|标签",参考主题 pay_points_product 同款结构)。
|
||||
*
|
||||
* @return array<int,array{key:string,points:int,pay_price:string,title:string,tag:string}>
|
||||
*/
|
||||
public function parse_points_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;
|
||||
}
|
||||
$parts = array_map('trim', explode('|', $line));
|
||||
if (count($parts) < 2) {
|
||||
continue;
|
||||
}
|
||||
$points = (int) $parts[0];
|
||||
try {
|
||||
$pay = \AKSRT\Ledger\AssetAmount::balance((string) $parts[1]);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
continue;
|
||||
}
|
||||
if ($points <= 0 || $pay->minor() <= 0) {
|
||||
continue;
|
||||
}
|
||||
$title = $parts[2] ?? '';
|
||||
$out[] = [
|
||||
'key' => (string) $i,
|
||||
'points' => $points,
|
||||
'pay_price' => $pay->decimal(),
|
||||
'title' => $title !== '' ? $title : sprintf(__('购买 %d 积分', 'aksrt'), $points),
|
||||
'tag' => $title,
|
||||
];
|
||||
$i++;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return array<string,mixed>|\WP_Error
|
||||
*/
|
||||
|
||||
@@ -68,7 +68,10 @@ trait BalanceServiceRest {
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => [$this, 'rest_exchange_balance_for_points'],
|
||||
'permission_callback' => [$this, 'check_asset_rest'],
|
||||
'args' => ['amount' => ['type' => 'string', 'required' => true, 'pattern' => '^(0|[1-9]\\d*)(?:\\.\\d{1,2})?$']],
|
||||
'args' => [
|
||||
'product' => ['type' => 'string', 'default' => 'custom', 'maxLength' => 30],
|
||||
'custom' => ['type' => 'string', 'default' => '0', 'pattern' => '^\d{1,9}$'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
@@ -311,7 +314,10 @@ trait BalanceServiceRest {
|
||||
|
||||
|
||||
/**
|
||||
* Exchange integer cash-wallet balance for points.
|
||||
* Buy points with cash-wallet balance (积分购买弹窗 · 余额支付).
|
||||
*
|
||||
* Body: { product: 'custom'|<package_key>, custom?: <points int> }
|
||||
* 自定义按汇率折算支付金额;商品按配置销售金额支付(参考主题同款语义)。
|
||||
*/
|
||||
public function rest_exchange_balance_for_points(\WP_REST_Request $request) {
|
||||
if (!$this->balance_feature_enabled()) {
|
||||
@@ -329,32 +335,54 @@ trait BalanceServiceRest {
|
||||
if (!is_array($params)) {
|
||||
$params = $request->get_params();
|
||||
}
|
||||
$raw_amount = $params['amount'] ?? '';
|
||||
try {
|
||||
$amount = \AKSRT\Ledger\AssetAmount::balance(is_string($raw_amount) ? $raw_amount : '');
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return new \WP_Error('aksrt_exchange_amount', __('兑换余额格式无效', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
if ($amount->minor() <= 0) {
|
||||
return new \WP_Error('aksrt_exchange_amount', __('兑换余额必须大于零', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
$rate = max(1, (int) ($exchange['rate'] ?? 100));
|
||||
$daily_max = max(1, (int) ($exchange['daily_max_points'] ?? 100000));
|
||||
$scaled = $this->multiply_non_negative($amount->minor(), $rate);
|
||||
if (is_wp_error($scaled)) {
|
||||
return $scaled;
|
||||
$product_key = sanitize_key((string) ($params['product'] ?? 'custom'));
|
||||
$points = 0;
|
||||
$pay_minor = 0;
|
||||
$product_title = '';
|
||||
if ($product_key !== '' && $product_key !== 'custom') {
|
||||
$found = null;
|
||||
foreach ($this->parse_points_packages((string) ($exchange['products_raw'] ?? '')) as $pkg) {
|
||||
if ((string) ($pkg['key'] ?? '') === $product_key) {
|
||||
$found = $pkg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
return new \WP_Error('aksrt_exchange_product', __('积分商品无效', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
$points = (int) $found['points'];
|
||||
try {
|
||||
$pay_minor = \AKSRT\Ledger\AssetAmount::balance((string) $found['pay_price'])->minor();
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$pay_minor = 0;
|
||||
}
|
||||
$product_title = (string) $found['title'];
|
||||
} else {
|
||||
$product_key = 'custom';
|
||||
$points = absint((string) ($params['custom'] ?? '0'));
|
||||
$min_points = max(1, (int) ($exchange['min_points'] ?? 100));
|
||||
if ($points <= 0) {
|
||||
return new \WP_Error('aksrt_exchange_amount', __('请输入需购买的积分数额', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
if ($points < $min_points) {
|
||||
return new \WP_Error('aksrt_exchange_min', sprintf(__('最低购买%d积分', 'aksrt'), $min_points), ['status' => 400]);
|
||||
}
|
||||
// 自定义按汇率折算支付金额(参考主题语义:积分 * 100 / 汇率 后四舍五入)。
|
||||
// 纯整数实现:intdiv(2a + b, 2b) 等价于 a / b 四舍五入,避免浮点。
|
||||
$pay_minor = intdiv($points * 200 + $rate, $rate * 2);
|
||||
$product_title = sprintf(__('购买 %d 积分', 'aksrt'), $points);
|
||||
}
|
||||
if ($scaled % 100 !== 0) {
|
||||
return new \WP_Error('aksrt_exchange_precision', __('该金额无法兑换为整数积分', 'aksrt'), ['status' => 400]);
|
||||
if ($points <= 0) {
|
||||
return new \WP_Error('aksrt_exchange_product', __('积分商品无效', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
if ($pay_minor <= 0) {
|
||||
return new \WP_Error('aksrt_exchange_precision', __('支付金额无效', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
$points = intdiv($scaled, 100);
|
||||
if ($points > $daily_max) {
|
||||
return new \WP_Error('aksrt_exchange_daily_limit', sprintf(__('今日购买积分上限为 %d', 'aksrt'), $daily_max), ['status' => 400]);
|
||||
}
|
||||
$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();
|
||||
$operation_key = \AKSRT\Ledger\OperationKey::from_request($request, 'points-exchange', $user_id);
|
||||
if (is_wp_error($operation_key)) {
|
||||
@@ -376,10 +404,12 @@ trait BalanceServiceRest {
|
||||
if (!class_exists('\\AKSRT\\Payment\\OrderRepository')) {
|
||||
return new \WP_Error('aksrt_order_unavailable', __('订单服务不可用', 'aksrt'), ['status' => 500]);
|
||||
}
|
||||
$pay_decimal = \AKSRT\Ledger\AssetAmount::decimal_from_minor($pay_minor);
|
||||
$note = wp_json_encode([
|
||||
'points_exchange' => 1,
|
||||
'points' => $points,
|
||||
'product_title' => sprintf(__('购买 %d 积分', 'aksrt'), $points),
|
||||
'product' => $product_key,
|
||||
'product_title' => $product_title,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
$repo = \AKSRT\Payment\OrderRepository::get_instance();
|
||||
$existing_order = $repo->get_by_request_key($operation_key);
|
||||
@@ -389,11 +419,11 @@ trait BalanceServiceRest {
|
||||
$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()
|
||||
|| (string) ($existing_order['amount'] ?? '') !== $pay_decimal
|
||||
|| sanitize_key((string) ($existing_order['currency'] ?? '')) !== 'cny'
|
||||
|| sanitize_key((string) ($existing_order['payment_method'] ?? '')) !== 'balance'
|
||||
|| (int) ($existing_meta['points'] ?? 0) !== $points
|
||||
|| (string) ($existing_meta['product'] ?? '') !== $product_key
|
||||
) {
|
||||
return new \WP_Error('aksrt_ledger_idempotency_conflict', __('相同操作标识已用于其他积分购买请求', 'aksrt'), ['status' => 409]);
|
||||
}
|
||||
@@ -404,7 +434,7 @@ trait BalanceServiceRest {
|
||||
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) {
|
||||
$business_mutation = static function (int $transaction_id) use ($is_new_order, $repo, $user_id, $pay_decimal, $points, $operation_key, $order_number, $note) {
|
||||
if (!$is_new_order) {
|
||||
return true;
|
||||
}
|
||||
@@ -413,7 +443,7 @@ trait BalanceServiceRest {
|
||||
'user_id' => $user_id,
|
||||
'product_type' => \AKSRT\Core\DomainModel::ORDER_PRODUCT_POINTS,
|
||||
'product_id' => 0,
|
||||
'amount' => $amount->decimal(),
|
||||
'amount' => $pay_decimal,
|
||||
'currency' => 'CNY',
|
||||
'status' => \AKSRT\Payment\Order::STATUS_PAID,
|
||||
'payment_method' => 'balance',
|
||||
@@ -426,19 +456,19 @@ trait BalanceServiceRest {
|
||||
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, $order_number);
|
||||
return \AKSRT\Payment\CommissionLedger::get_instance()->record_points_exchange($user_id, $pay_decimal, $points, $operation_key, $order_number);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
$result = \AKSRT\Ledger\LedgerService::get_instance()->exchange(
|
||||
$user_id,
|
||||
$amount->minor(),
|
||||
$pay_minor,
|
||||
$points,
|
||||
$operation_key,
|
||||
'points_exchange',
|
||||
['type' => 'order', 'key' => $order_number],
|
||||
sprintf(__('余额兑换 %d 积分', 'aksrt'), $points),
|
||||
['rate' => $rate],
|
||||
sprintf(__('余额购买 %d 积分', 'aksrt'), $points),
|
||||
['rate' => $rate, 'product' => $product_key],
|
||||
$guard,
|
||||
$business_mutation
|
||||
);
|
||||
@@ -446,7 +476,7 @@ trait BalanceServiceRest {
|
||||
return $result;
|
||||
}
|
||||
return rest_ensure_response(['success' => true, 'data' => [
|
||||
'message' => sprintf(__('兑换成功,获得 %d 积分', 'aksrt'), $points),
|
||||
'message' => sprintf(__('购买成功,获得 %d 积分', 'aksrt'), $points),
|
||||
'transaction_id' => $result->transaction_id,
|
||||
'replayed' => $result->replayed,
|
||||
'wallet_balance' => \AKSRT\Ledger\AssetAmount::from_minor(\AKSRT\Ledger\AssetType::BALANCE, $result->balance($user_id, \AKSRT\Ledger\AssetType::BALANCE))->decimal(),
|
||||
@@ -464,12 +494,4 @@ trait BalanceServiceRest {
|
||||
return (intdiv($amount, 100) * $rate) + intdiv(($amount % 100) * $rate, 100);
|
||||
}
|
||||
|
||||
/** @return int|\WP_Error */
|
||||
private function multiply_non_negative(int $left, int $right) {
|
||||
if ($left < 0 || $right < 0 || ($right > 0 && $left > intdiv(PHP_INT_MAX, $right))) {
|
||||
return new \WP_Error('aksrt_ledger_overflow', __('资产金额超出可用范围', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
return $left * $right;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user