914 lines
36 KiB
PHP
914 lines
36 KiB
PHP
<?php
|
|
/**
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Payment;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class ChargeCard {
|
|
|
|
private static $instance = null;
|
|
|
|
public const TYPE_BALANCE = 'balance';
|
|
public const TYPE_VIP = 'vip';
|
|
public const TYPE_CREDITS = 'credits';
|
|
|
|
public const STATUS_ACTIVE = 'active';
|
|
public const STATUS_USED = 'used';
|
|
public const STATUS_EXPIRED = 'expired';
|
|
public const STATUS_DISABLED = 'disabled';
|
|
|
|
public static function get_instance(): self {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function init(): void {
|
|
add_action('rest_api_init', [$this, 'register_public_routes']);
|
|
}
|
|
|
|
private function table(): string {
|
|
return \AKSRT\Core\DBMigrator::table('charge_card');
|
|
}
|
|
|
|
public function register_public_routes(): void {
|
|
register_rest_route('aksrt/v1', '/pay/card-pass/options', [
|
|
'methods' => 'GET',
|
|
'callback' => [$this, 'rest_options'],
|
|
'permission_callback' => [$this, 'check_rest_nonce'],
|
|
]);
|
|
register_rest_route('aksrt/v1', '/pay/card-pass/redeem', [
|
|
'methods' => 'POST',
|
|
'callback' => [$this, 'rest_redeem'],
|
|
'permission_callback' => [$this, 'check_logged_in_rest'],
|
|
'args' => [
|
|
'password' => ['type' => 'string', 'required' => true, 'minLength' => 1, 'maxLength' => 80, 'sanitize_callback' => 'sanitize_text_field'],
|
|
'card' => ['type' => 'string', 'default' => '', 'maxLength' => 60, 'sanitize_callback' => 'sanitize_text_field'],
|
|
'type' => ['type' => 'string', 'default' => '', 'enum' => ['', self::TYPE_BALANCE, self::TYPE_VIP, self::TYPE_CREDITS]],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param \WP_REST_Request $request
|
|
* @return true|\WP_Error
|
|
*/
|
|
public function check_rest_nonce(\WP_REST_Request $request) {
|
|
$nonce = $request->get_header('x-wp-nonce');
|
|
if (!$nonce) {
|
|
return new \WP_Error('aksrt_rest_no_nonce', __('缺少安全令牌', 'aksrt'), ['status' => 403]);
|
|
}
|
|
if (!wp_verify_nonce($nonce, 'wp_rest')) {
|
|
return new \WP_Error('aksrt_rest_bad_nonce', __('安全令牌已过期,请刷新页面', 'aksrt'), ['status' => 403]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param \WP_REST_Request $request
|
|
* @return true|\WP_Error
|
|
*/
|
|
public function check_logged_in_rest(\WP_REST_Request $request) {
|
|
$nonce = $this->check_rest_nonce($request);
|
|
if (is_wp_error($nonce)) {
|
|
return $nonce;
|
|
}
|
|
if (!is_user_logged_in()) {
|
|
return new \WP_Error('aksrt_need_login', __('请先登录', 'aksrt'), ['status' => 401]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function rest_options(\WP_REST_Request $request) {
|
|
return new \WP_REST_Response([
|
|
'success' => true,
|
|
'data' => $this->get_public_options(),
|
|
], 200);
|
|
}
|
|
|
|
public function rest_redeem(\WP_REST_Request $request) {
|
|
$params = $request->get_json_params();
|
|
if (!is_array($params)) {
|
|
$params = $request->get_params();
|
|
}
|
|
$password = sanitize_text_field((string) ($params['password'] ?? ''));
|
|
$card = sanitize_text_field((string) ($params['card'] ?? ''));
|
|
$type = sanitize_key((string) ($params['type'] ?? ''));
|
|
if ($type !== '' && !in_array($type, [self::TYPE_BALANCE, self::TYPE_VIP, self::TYPE_CREDITS], true)) {
|
|
$type = '';
|
|
}
|
|
|
|
$operation_key = \AKSRT\Ledger\OperationKey::from_request($request, 'card-redeem', get_current_user_id());
|
|
if (is_wp_error($operation_key)) {
|
|
return $operation_key;
|
|
}
|
|
|
|
$result = $this->redeem_for_user(get_current_user_id(), $password, $card, $type, $operation_key);
|
|
if (is_wp_error($result)) {
|
|
return $result;
|
|
}
|
|
return new \WP_REST_Response([
|
|
'success' => true,
|
|
'data' => $result,
|
|
], 200);
|
|
}
|
|
|
|
/**
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function get_public_options(): array {
|
|
$labels = [
|
|
'title' => __('卡密兑换', 'aksrt'),
|
|
'password' => __('卡密', 'aksrt'),
|
|
'card' => __('卡号', 'aksrt'),
|
|
'submit_balance' => __('兑换余额', 'aksrt'),
|
|
'submit_vip' => __('兑换会员', 'aksrt'),
|
|
'submit_credits' => __('兑换积分', 'aksrt'),
|
|
'placeholder_pw' => __('请输入卡密', 'aksrt'),
|
|
'placeholder_card' => __('请输入卡号', 'aksrt'),
|
|
];
|
|
|
|
// Master kill-switch on 收款接口 → 卡密充值.
|
|
$channel_on = !class_exists('\AKSRT\Settings\PaymentRuntime')
|
|
|| \AKSRT\Settings\PaymentRuntime::is_gateway_enabled('card_pass');
|
|
if (!$channel_on) {
|
|
$off = ['enabled' => false, 'desc' => '', 'only_password' => true];
|
|
return [
|
|
'balance' => $off,
|
|
'vip' => $off,
|
|
'credits' => $off,
|
|
'labels' => $labels,
|
|
];
|
|
}
|
|
|
|
$balance = ['enabled' => false, 'desc' => '', 'only_password' => true];
|
|
$vip = ['enabled' => false, 'desc' => '', 'only_password' => true];
|
|
|
|
if (class_exists('\AKSRT\Settings\Settings')) {
|
|
if (method_exists('\AKSRT\Settings\Settings', 'get_payment_recharge_settings')) {
|
|
$recharge = \AKSRT\Settings\Settings::get_payment_recharge_settings();
|
|
$pc = is_array($recharge['pass_charge'] ?? null) ? $recharge['pass_charge'] : [];
|
|
$balance = [
|
|
'enabled' => !empty($pc['enabled']),
|
|
'desc' => (string) ($pc['desc'] ?? ''),
|
|
'only_password' => !isset($pc['only_password']) || !empty($pc['only_password']),
|
|
];
|
|
}
|
|
if (method_exists('\AKSRT\Settings\Settings', 'get_payment_vip_settings')) {
|
|
$vip_s = \AKSRT\Settings\Settings::get_payment_vip_settings();
|
|
$benefit = is_array($vip_s['benefit'] ?? null) ? $vip_s['benefit'] : [];
|
|
$vip = [
|
|
'enabled' => !empty($benefit['pass_charge']),
|
|
'desc' => (string) ($benefit['pass_charge_desc'] ?? ''),
|
|
'only_password' => !isset($benefit['pass_charge_only_password']) || !empty($benefit['pass_charge_only_password']),
|
|
];
|
|
}
|
|
}
|
|
|
|
$credits = ['enabled' => false, 'desc' => '', 'only_password' => true];
|
|
$all = class_exists('\AKSRT\Settings\Settings') && method_exists('\AKSRT\Settings\Settings', 'get_all')
|
|
? \AKSRT\Settings\Settings::get_all()
|
|
: (array) get_option('aksrt_settings', []);
|
|
if (!empty($all['asset_points_card_enabled'])) {
|
|
// Require credits system on when available.
|
|
$credits_on = !class_exists('\AKSRT\Settings\Settings') || !method_exists('\AKSRT\Settings\Settings', 'credits_enabled') || \AKSRT\Settings\Settings::credits_enabled();
|
|
$credits = [
|
|
'enabled' => $credits_on,
|
|
'desc' => sanitize_textarea_field((string) ($all['asset_points_card_description'] ?? '')),
|
|
'only_password' => !isset($all['asset_points_card_password_only']) || !empty($all['asset_points_card_password_only']),
|
|
];
|
|
}
|
|
|
|
return [
|
|
'balance' => $balance,
|
|
'vip' => $vip,
|
|
'credits' => $credits,
|
|
'labels' => $labels,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Create one card.
|
|
*
|
|
* Accepted $data keys:
|
|
* - password (required)
|
|
* - card (optional)
|
|
* - type balance|vip|credits
|
|
* - amount (balance)
|
|
* - level, days (vip; days 0 = permanent)
|
|
* - points (credits)
|
|
* - meta (array override)
|
|
* - expires_at, note
|
|
*/
|
|
public function create(array $data): int {
|
|
global $wpdb;
|
|
|
|
$type = sanitize_key((string) ($data['type'] ?? self::TYPE_BALANCE));
|
|
if (!in_array($type, [self::TYPE_BALANCE, self::TYPE_VIP, self::TYPE_CREDITS], true)) {
|
|
$type = self::TYPE_BALANCE;
|
|
}
|
|
|
|
$password = sanitize_text_field((string) ($data['password'] ?? ''));
|
|
if ($password === '') {
|
|
return 0;
|
|
}
|
|
$card = sanitize_text_field((string) ($data['card'] ?? ''));
|
|
|
|
$meta = $this->normalize_meta($type, $data);
|
|
if ($meta === null) {
|
|
return 0;
|
|
}
|
|
|
|
// Uniqueness: password alone (only_password mode common).
|
|
$exists = $wpdb->get_var($wpdb->prepare(
|
|
"SELECT id FROM {$this->table()} WHERE password = %s LIMIT 1",
|
|
$password
|
|
));
|
|
if ($exists) {
|
|
return 0;
|
|
}
|
|
|
|
$expires_at = sanitize_text_field((string) ($data['expires_at'] ?? ''));
|
|
$note = sanitize_textarea_field((string) ($data['note'] ?? ''));
|
|
$amount_col = (string) ($meta['amount'] ?? $meta['points'] ?? '0');
|
|
|
|
$result = $wpdb->insert($this->table(), [
|
|
'card' => $card,
|
|
'password' => $password,
|
|
'type' => $type,
|
|
'amount' => $amount_col,
|
|
'balance' => $amount_col,
|
|
'status' => self::STATUS_ACTIVE,
|
|
'meta' => wp_json_encode($meta, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
|
'order_number' => '',
|
|
'used_by' => null,
|
|
'used_at' => null,
|
|
'expires_at' => $expires_at !== '' ? $expires_at : null,
|
|
'created_at' => current_time('mysql'),
|
|
'note' => $note,
|
|
]);
|
|
|
|
return $result ? (int) $wpdb->insert_id : 0;
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $meta_fields
|
|
* @return array<int,string> passwords/codes generated
|
|
*/
|
|
public function batch_create(string $type, array $meta_fields, int $count, int $length = 16, string $prefix = '', string $expires_at = '', string $note = ''): array {
|
|
$type = sanitize_key($type);
|
|
if (!in_array($type, [self::TYPE_BALANCE, self::TYPE_VIP, self::TYPE_CREDITS], true)) {
|
|
$type = self::TYPE_BALANCE;
|
|
}
|
|
$count = max(1, min(500, $count));
|
|
$length = max(8, min(32, $length));
|
|
$prefix = sanitize_text_field($prefix);
|
|
$codes = [];
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
$password = $prefix . strtoupper(wp_generate_password($length, false));
|
|
$id = $this->create(array_merge($meta_fields, [
|
|
'type' => $type,
|
|
'password' => $password,
|
|
'expires_at' => $expires_at,
|
|
'note' => $note,
|
|
]));
|
|
if ($id > 0) {
|
|
$codes[] = $password;
|
|
}
|
|
}
|
|
return $codes;
|
|
}
|
|
|
|
/**
|
|
* @return array{success:bool,message:string,type?:string,amount?:string|int,level?:int,days?:int,order_number?:string,balance?:string|int}|\WP_Error
|
|
*/
|
|
public function redeem_for_user(int $user_id, string $password, string $card = '', string $expected_type = '', string $operation_key = '') {
|
|
$user_id = max(0, $user_id);
|
|
$password = sanitize_text_field($password);
|
|
$card = sanitize_text_field($card);
|
|
|
|
if ($user_id <= 0) {
|
|
return new \WP_Error('aksrt_need_login', __('请先登录', 'aksrt'), ['status' => 401]);
|
|
}
|
|
if ($password === '') {
|
|
return new \WP_Error('aksrt_card_empty', __('请输入卡密', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
// Simple per-user throttle.
|
|
$throttle_key = 'aksrt_card_pass_rl_' . $user_id;
|
|
$hits = (int) get_transient($throttle_key);
|
|
if ($hits >= 20) {
|
|
return new \WP_Error('aksrt_card_rate', __('操作过于频繁,请稍后再试', 'aksrt'), ['status' => 429]);
|
|
}
|
|
set_transient($throttle_key, $hits + 1, MINUTE_IN_SECONDS);
|
|
|
|
$row = $this->find_active($password, $card, $expected_type);
|
|
if (!$row) {
|
|
return new \WP_Error('aksrt_card_invalid', __('卡号或卡密错误', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
$type = sanitize_key((string) ($row['type'] ?? self::TYPE_BALANCE));
|
|
if ($expected_type !== '' && $type !== $expected_type) {
|
|
return new \WP_Error('aksrt_card_type', __('当前卡密不能用于该兑换类型', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
$gate = $this->assert_type_enabled($type);
|
|
if (is_wp_error($gate)) {
|
|
return $gate;
|
|
}
|
|
|
|
// only_password: if feature requires card number and empty, fail.
|
|
$opts = $this->get_public_options();
|
|
$only_password = true;
|
|
if ($type === self::TYPE_BALANCE) {
|
|
$only_password = !empty($opts['balance']['only_password']);
|
|
} elseif ($type === self::TYPE_VIP) {
|
|
$only_password = !empty($opts['vip']['only_password']);
|
|
}
|
|
if (!$only_password && $card === '' && (string) ($row['card'] ?? '') !== '') {
|
|
return new \WP_Error('aksrt_card_need_no', __('请输入卡号', 'aksrt'), ['status' => 400]);
|
|
}
|
|
if (!$only_password && $card !== '' && (string) ($row['card'] ?? '') !== '' && (string) $row['card'] !== $card) {
|
|
return new \WP_Error('aksrt_card_invalid', __('卡号或卡密错误', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
if ((string) ($row['status'] ?? '') === self::STATUS_ACTIVE && !empty($row['expires_at']) && strtotime((string) $row['expires_at']) < time()) {
|
|
global $wpdb;
|
|
$wpdb->update($this->table(), ['status' => self::STATUS_EXPIRED], ['id' => (int) $row['id']], ['%s'], ['%d']);
|
|
return new \WP_Error('aksrt_card_expired', __('卡密已过期', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
$meta = $this->decode_meta($row);
|
|
$card_id = (int) $row['id'];
|
|
|
|
if (in_array($type, [self::TYPE_BALANCE, self::TYPE_CREDITS], true)) {
|
|
if ($operation_key === '') {
|
|
return new \WP_Error('aksrt_idempotency_key', __('缺少操作标识', 'aksrt'), ['status' => 400]);
|
|
}
|
|
return $this->redeem_asset_card($user_id, $row, $meta, $card, $operation_key);
|
|
}
|
|
if ($operation_key === '') {
|
|
return new \WP_Error('aksrt_idempotency_key', __('缺少操作标识', 'aksrt'), ['status' => 400]);
|
|
}
|
|
return $this->redeem_vip_card($user_id, $row, $meta, $card, $operation_key);
|
|
}
|
|
|
|
/**
|
|
* @return array<string,mixed>|null
|
|
*/
|
|
public function find_active(string $password, string $card = '', string $type = ''): ?array {
|
|
global $wpdb;
|
|
$password = sanitize_text_field($password);
|
|
if ($password === '') {
|
|
return null;
|
|
}
|
|
|
|
$sql = "SELECT * FROM {$this->table()} WHERE password = %s";
|
|
$params = [$password];
|
|
if ($type !== '') {
|
|
$sql .= ' AND type = %s';
|
|
$params[] = sanitize_key($type);
|
|
}
|
|
$sql .= ' LIMIT 1';
|
|
$row = $wpdb->get_row($wpdb->prepare($sql, $params), ARRAY_A);
|
|
if (!$row) {
|
|
return null;
|
|
}
|
|
return $row;
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $row
|
|
* @param array<string,mixed> $meta
|
|
* @return array<string,mixed>|\WP_Error
|
|
*/
|
|
private function redeem_asset_card(int $user_id, array $row, array $meta, string $submitted_card, string $operation_key) {
|
|
$type = sanitize_key((string) ($row['type'] ?? ''));
|
|
$card_id = (int) ($row['id'] ?? 0);
|
|
$asset = $type === self::TYPE_BALANCE ? \AKSRT\Ledger\AssetType::BALANCE : \AKSRT\Ledger\AssetType::POINTS;
|
|
if ($asset === \AKSRT\Ledger\AssetType::BALANCE) {
|
|
try {
|
|
$amount_minor = \AKSRT\Ledger\AssetAmount::balance(is_string($meta['amount'] ?? null) ? $meta['amount'] : '')->minor();
|
|
} catch (\InvalidArgumentException $e) {
|
|
return new \WP_Error('aksrt_card_amount', __('当前卡密充值金额无效', 'aksrt'), ['status' => 400]);
|
|
}
|
|
$reason = 'card_pass_balance';
|
|
$remark = sprintf(__('卡密充值 %s 元', 'aksrt'), \AKSRT\Ledger\AssetAmount::from_minor($asset, $amount_minor)->decimal());
|
|
} else {
|
|
$amount_minor = (int) ($meta['points'] ?? 0);
|
|
$reason = 'card_pass_credits';
|
|
$remark = sprintf(__('卡密兑换积分 %d', 'aksrt'), $amount_minor);
|
|
}
|
|
if ($card_id <= 0 || $amount_minor <= 0) {
|
|
return new \WP_Error('aksrt_card_amount', __('当前卡密可兑换资产为 0', 'aksrt'), ['status' => 400]);
|
|
}
|
|
$order_number = 'CARD' . $card_id;
|
|
$guard = function () use ($card_id, $user_id, $submitted_card, $type) {
|
|
global $wpdb;
|
|
$current = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table()} WHERE id = %d FOR UPDATE", $card_id), ARRAY_A);
|
|
if (!is_array($current)
|
|
|| (string) ($current['status'] ?? '') !== self::STATUS_ACTIVE
|
|
|| sanitize_key((string) ($current['type'] ?? '')) !== $type
|
|
) {
|
|
return new \WP_Error('aksrt_card_used', __('该卡密已使用或不可用', 'aksrt'), ['status' => 409]);
|
|
}
|
|
if ((string) ($current['card'] ?? '') !== '' && $submitted_card !== '' && !hash_equals((string) $current['card'], $submitted_card)) {
|
|
return new \WP_Error('aksrt_card_invalid', __('卡号或卡密错误', 'aksrt'), ['status' => 400]);
|
|
}
|
|
if (!empty($current['expires_at']) && strtotime((string) $current['expires_at']) < time()) {
|
|
return new \WP_Error('aksrt_card_expired', __('卡密已过期', 'aksrt'), ['status' => 400]);
|
|
}
|
|
if ((int) ($current['used_by'] ?? 0) > 0 && (int) $current['used_by'] !== $user_id) {
|
|
return new \WP_Error('aksrt_card_used', __('该卡密已使用', 'aksrt'), ['status' => 409]);
|
|
}
|
|
return true;
|
|
};
|
|
$mutation = function () use ($user_id, $type, $meta, $card_id, $order_number, $operation_key) {
|
|
global $wpdb;
|
|
$created = $this->create_redeem_order($user_id, $type, $meta, $card_id, '', $operation_key, $order_number, Order::STATUS_PAID);
|
|
if ($created !== $order_number) {
|
|
return new \WP_Error('aksrt_order_create_failed', __('创建订单失败', 'aksrt'), ['status' => 500]);
|
|
}
|
|
$used = $wpdb->query($wpdb->prepare(
|
|
"UPDATE {$this->table()} SET status = %s, used_by = %d, used_at = %s, order_number = %s, balance = 0 WHERE id = %d AND status = %s",
|
|
self::STATUS_USED,
|
|
$user_id,
|
|
current_time('mysql'),
|
|
$order_number,
|
|
$card_id,
|
|
self::STATUS_ACTIVE
|
|
));
|
|
return $used === 1 ? true : new \WP_Error('aksrt_card_used', __('该卡密已使用', 'aksrt'), ['status' => 409]);
|
|
};
|
|
$result = \AKSRT\Ledger\LedgerService::get_instance()->credit(
|
|
$user_id,
|
|
$asset,
|
|
$amount_minor,
|
|
$operation_key,
|
|
$reason,
|
|
['type' => 'charge_card', 'id' => $card_id],
|
|
$remark,
|
|
$user_id,
|
|
['card_type' => $type],
|
|
$mutation,
|
|
$guard
|
|
);
|
|
if (is_wp_error($result)) {
|
|
return $result;
|
|
}
|
|
$balance = $result->balance($user_id, $asset);
|
|
return [
|
|
'success' => true,
|
|
'type' => $type,
|
|
'order_number' => $order_number,
|
|
'transaction_id' => $result->transaction_id,
|
|
'replayed' => $result->replayed,
|
|
'message' => $asset === \AKSRT\Ledger\AssetType::BALANCE
|
|
? sprintf(__('卡密充值成功,充值金额:%s 元', 'aksrt'), \AKSRT\Ledger\AssetAmount::from_minor($asset, $amount_minor)->decimal())
|
|
: sprintf(__('卡密兑换成功,积分:%d', 'aksrt'), $amount_minor),
|
|
'amount' => $asset === \AKSRT\Ledger\AssetType::BALANCE
|
|
? \AKSRT\Ledger\AssetAmount::from_minor($asset, $amount_minor)->decimal()
|
|
: $amount_minor,
|
|
'balance' => $asset === \AKSRT\Ledger\AssetType::BALANCE
|
|
? \AKSRT\Ledger\AssetAmount::from_minor($asset, $balance)->decimal()
|
|
: $balance,
|
|
'reload' => true,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Redeem a VIP card with the card row, order row, and membership metadata in one transaction.
|
|
*
|
|
* @param array<string,mixed> $row
|
|
* @param array<string,mixed> $meta
|
|
* @return array<string,mixed>|\WP_Error
|
|
*/
|
|
private function redeem_vip_card(int $user_id, array $row, array $meta, string $submitted_card, string $operation_key) {
|
|
global $wpdb;
|
|
if (!class_exists('\AKSRT\Content\VipService') || !class_exists(OrderRepository::class)) {
|
|
return new \WP_Error('aksrt_vip_unavailable', __('会员系统不可用', 'aksrt'), ['status' => 500]);
|
|
}
|
|
$card_id = (int) ($row['id'] ?? 0);
|
|
$level = max(1, min(2, (int) ($meta['level'] ?? 1)));
|
|
$days = max(0, (int) ($meta['days'] ?? 0));
|
|
$order_number = 'CARD' . $card_id;
|
|
$order_table = \AKSRT\Core\DBMigrator::table('order');
|
|
if ($card_id <= 0 || sanitize_key((string) ($row['type'] ?? '')) !== self::TYPE_VIP) {
|
|
return new \WP_Error('aksrt_card_type', __('当前卡密不是会员卡', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
for ($attempt = 1; $attempt <= 3; $attempt++) {
|
|
if ($wpdb->query('START TRANSACTION') === false) {
|
|
return new \WP_Error('aksrt_card_db', __('无法开启卡密兑换事务', 'aksrt'), ['status' => 500]);
|
|
}
|
|
try {
|
|
$current = $wpdb->get_row($wpdb->prepare(
|
|
"SELECT * FROM {$this->table()} WHERE id = %d FOR UPDATE",
|
|
$card_id
|
|
), ARRAY_A);
|
|
$existing = $wpdb->get_row($wpdb->prepare(
|
|
"SELECT * FROM {$order_table} WHERE request_key = %s FOR UPDATE",
|
|
$operation_key
|
|
), ARRAY_A);
|
|
if (is_array($existing)) {
|
|
$replay = $this->vip_card_replay_result($existing, $current, $user_id, $card_id, $level, $days, $operation_key);
|
|
if (is_wp_error($replay)) {
|
|
throw new \AKSRT\Ledger\LedgerAbort($replay);
|
|
}
|
|
if ($wpdb->query('COMMIT') === false) {
|
|
throw new \RuntimeException('vip_card_replay_commit_failed');
|
|
}
|
|
return $replay;
|
|
}
|
|
|
|
if (!is_array($current)
|
|
|| (string) ($current['status'] ?? '') !== self::STATUS_ACTIVE
|
|
|| sanitize_key((string) ($current['type'] ?? '')) !== self::TYPE_VIP
|
|
|| !hash_equals((string) ($row['password'] ?? ''), (string) ($current['password'] ?? ''))
|
|
) {
|
|
throw new \AKSRT\Ledger\LedgerAbort(new \WP_Error('aksrt_card_used', __('该卡密已使用或不可用', 'aksrt'), ['status' => 409]));
|
|
}
|
|
if ((string) ($current['card'] ?? '') !== '' && $submitted_card !== '' && !hash_equals((string) $current['card'], $submitted_card)) {
|
|
throw new \AKSRT\Ledger\LedgerAbort(new \WP_Error('aksrt_card_invalid', __('卡号或卡密错误', 'aksrt'), ['status' => 400]));
|
|
}
|
|
if (!empty($current['expires_at']) && strtotime((string) $current['expires_at']) < time()) {
|
|
throw new \AKSRT\Ledger\LedgerAbort(new \WP_Error('aksrt_card_expired', __('卡密已过期', 'aksrt'), ['status' => 400]));
|
|
}
|
|
|
|
$vip_meta_keys = ['aksrt_vip_level', 'aksrt_vip_expires', 'aksrt_vip_base_level', 'aksrt_vip_base_expires', 'aksrt_vip_level_expired'];
|
|
$placeholders = implode(',', array_fill(0, count($vip_meta_keys), '%s'));
|
|
$wpdb->get_results($wpdb->prepare(
|
|
"SELECT umeta_id FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key IN ({$placeholders}) FOR UPDATE",
|
|
array_merge([$user_id], $vip_meta_keys)
|
|
));
|
|
|
|
$current_level = max(0, (int) get_user_meta($user_id, 'aksrt_vip_level', true));
|
|
$current_expiry = max(0, (int) get_user_meta($user_id, 'aksrt_vip_expires', true));
|
|
$activation_base = max($current_expiry, time());
|
|
$created = $this->create_redeem_order(
|
|
$user_id,
|
|
self::TYPE_VIP,
|
|
['level' => $level, 'days' => $days],
|
|
$card_id,
|
|
(string) ($current['password'] ?? ''),
|
|
$operation_key,
|
|
$order_number,
|
|
Order::STATUS_PAID
|
|
);
|
|
if ($created !== $order_number) {
|
|
throw new \RuntimeException('vip_card_order_create_failed');
|
|
}
|
|
|
|
\AKSRT\Content\VipService::get_instance()->activate_vip($user_id, $level, $days);
|
|
$actual_level = max(0, (int) get_user_meta($user_id, 'aksrt_vip_level', true));
|
|
$actual_expiry = max(0, (int) get_user_meta($user_id, 'aksrt_vip_expires', true));
|
|
$permanent_preserved = $days > 0 && $current_level > 0 && $level <= $current_level && $current_expiry === 0;
|
|
$expiry_valid = $days <= 0
|
|
? $actual_expiry === 0
|
|
: ($permanent_preserved ? $actual_expiry === 0 : $actual_expiry >= $activation_base + ($days * DAY_IN_SECONDS));
|
|
if ($actual_level < max($current_level, $level) || !$expiry_valid) {
|
|
throw new \RuntimeException('vip_card_fulfillment_failed');
|
|
}
|
|
|
|
$used = $wpdb->query($wpdb->prepare(
|
|
"UPDATE {$this->table()} SET status = %s, used_by = %d, used_at = %s, order_number = %s, balance = 0 WHERE id = %d AND status = %s",
|
|
self::STATUS_USED,
|
|
$user_id,
|
|
current_time('mysql'),
|
|
$order_number,
|
|
$card_id,
|
|
self::STATUS_ACTIVE
|
|
));
|
|
if ($used !== 1) {
|
|
throw new \AKSRT\Ledger\LedgerAbort(new \WP_Error('aksrt_card_used', __('该卡密已使用', 'aksrt'), ['status' => 409]));
|
|
}
|
|
if ($wpdb->query('COMMIT') === false) {
|
|
throw new \RuntimeException('vip_card_commit_failed');
|
|
}
|
|
|
|
$message = $this->vip_card_message($level, $days);
|
|
$paid_order = OrderRepository::get_instance()->get($order_number);
|
|
if (is_array($paid_order)) {
|
|
try {
|
|
do_action('aksrt_order_status_changed', $order_number, Order::STATUS_PAID, $paid_order);
|
|
do_action('aksrt_order_paid', $order_number, $paid_order);
|
|
} catch (\Throwable $event_error) {
|
|
if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
error_log('[AKSRT VIP Card Event] ' . $event_error->getMessage());
|
|
}
|
|
}
|
|
}
|
|
return [
|
|
'success' => true,
|
|
'type' => self::TYPE_VIP,
|
|
'order_number' => $order_number,
|
|
'transaction_id' => 0,
|
|
'replayed' => false,
|
|
'message' => $message,
|
|
'level' => $level,
|
|
'days' => $days,
|
|
'reload' => true,
|
|
];
|
|
} catch (\AKSRT\Ledger\LedgerAbort $e) {
|
|
$wpdb->query('ROLLBACK');
|
|
clean_user_cache($user_id);
|
|
return $e->error;
|
|
} catch (\Throwable $e) {
|
|
$errno = $this->last_db_errno();
|
|
$wpdb->query('ROLLBACK');
|
|
clean_user_cache($user_id);
|
|
$replay_order = OrderRepository::get_instance()->get_by_request_key($operation_key);
|
|
if (is_array($replay_order)) {
|
|
$replay_card = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table()} WHERE id = %d", $card_id), ARRAY_A);
|
|
return $this->vip_card_replay_result($replay_order, $replay_card, $user_id, $card_id, $level, $days, $operation_key);
|
|
}
|
|
if (in_array($errno, [1205, 1213], true) && $attempt < 3) {
|
|
usleep(random_int(10000, 50000));
|
|
continue;
|
|
}
|
|
if (defined('WP_DEBUG') && WP_DEBUG) {
|
|
error_log('[AKSRT VIP Card] ' . $e->getMessage());
|
|
}
|
|
return new \WP_Error('aksrt_card_db', __('卡密兑换失败,请稍后重试', 'aksrt'), ['status' => 500]);
|
|
}
|
|
}
|
|
return new \WP_Error('aksrt_card_db', __('卡密兑换失败,请稍后重试', 'aksrt'), ['status' => 500]);
|
|
}
|
|
|
|
/** @return array<string,mixed>|\WP_Error */
|
|
private function vip_card_replay_result($order, $card, int $user_id, int $card_id, int $level, int $days, string $operation_key) {
|
|
$order = is_array($order) ? $order : [];
|
|
$card = is_array($card) ? $card : [];
|
|
$note = json_decode((string) ($order['note'] ?? ''), true);
|
|
$note = is_array($note) ? $note : [];
|
|
$stored_meta = is_array($note['meta'] ?? null) ? $note['meta'] : [];
|
|
$same = (string) ($order['request_key'] ?? '') === $operation_key
|
|
&& (int) ($order['user_id'] ?? 0) === $user_id
|
|
&& sanitize_key((string) ($order['product_type'] ?? '')) === \AKSRT\Core\DomainModel::ORDER_PRODUCT_VIP
|
|
&& (int) ($order['product_id'] ?? 0) === $card_id
|
|
&& sanitize_key((string) ($order['payment_method'] ?? '')) === 'card_pass'
|
|
&& (string) ($order['status'] ?? '') === Order::STATUS_PAID
|
|
&& (int) ($note['card_id'] ?? 0) === $card_id
|
|
&& sanitize_key((string) ($note['card_type'] ?? '')) === self::TYPE_VIP
|
|
&& (int) ($stored_meta['level'] ?? 0) === $level
|
|
&& (int) ($stored_meta['days'] ?? -1) === $days
|
|
&& (string) ($card['status'] ?? '') === self::STATUS_USED
|
|
&& (int) ($card['used_by'] ?? 0) === $user_id
|
|
&& (string) ($card['order_number'] ?? '') === (string) ($order['order_number'] ?? '');
|
|
if (!$same) {
|
|
return new \WP_Error('aksrt_ledger_idempotency_conflict', __('相同操作标识已用于其他卡密兑换请求', 'aksrt'), ['status' => 409]);
|
|
}
|
|
return [
|
|
'success' => true,
|
|
'type' => self::TYPE_VIP,
|
|
'order_number' => (string) $order['order_number'],
|
|
'transaction_id' => 0,
|
|
'replayed' => true,
|
|
'message' => $this->vip_card_message($level, $days),
|
|
'level' => $level,
|
|
'days' => $days,
|
|
'reload' => true,
|
|
];
|
|
}
|
|
|
|
private function vip_card_message(int $level, int $days): string {
|
|
return $days > 0
|
|
? sprintf(__('卡密兑换成功:VIP%s %d 天', 'aksrt'), (string) $level, $days)
|
|
: sprintf(__('卡密兑换成功:VIP%s 永久', 'aksrt'), (string) $level);
|
|
}
|
|
|
|
private function last_db_errno(): int {
|
|
global $wpdb;
|
|
if (isset($wpdb->dbh) && $wpdb->dbh instanceof \mysqli) {
|
|
return mysqli_errno($wpdb->dbh);
|
|
}
|
|
if (isset($wpdb->last_errno)) {
|
|
return (int) $wpdb->last_errno;
|
|
}
|
|
return preg_match('/\\b(1205|1213)\\b/', (string) ($wpdb->last_error ?? ''), $matches) ? (int) $matches[1] : 0;
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $meta
|
|
*/
|
|
private function create_redeem_order(int $user_id, string $type, array $meta, int $card_id, string $password, string $request_key = '', string $order_number = '', string $status = ''): string {
|
|
if (!class_exists('AKSRT\\Payment\\OrderRepository') || !class_exists('\AKSRT\Core\DomainModel')) {
|
|
return '';
|
|
}
|
|
|
|
$product_type = \AKSRT\Core\DomainModel::ORDER_PRODUCT_RECHARGE;
|
|
$product_id = $card_id;
|
|
$amount = '0.00';
|
|
$currency = 'CNY';
|
|
|
|
if ($type === self::TYPE_VIP) {
|
|
$product_type = \AKSRT\Core\DomainModel::ORDER_PRODUCT_VIP;
|
|
$amount = '0.00';
|
|
} elseif ($type === self::TYPE_BALANCE) {
|
|
$parsed_amount = \AKSRT\Ledger\AssetAmount::balance(is_string($meta['amount'] ?? null) ? $meta['amount'] : '');
|
|
$amount = $parsed_amount->decimal();
|
|
$currency = 'CNY';
|
|
} elseif ($type === self::TYPE_CREDITS) {
|
|
$product_type = \AKSRT\Core\DomainModel::ORDER_PRODUCT_POINTS;
|
|
$amount = (int) ($meta['points'] ?? 0);
|
|
$currency = 'points';
|
|
}
|
|
|
|
$note = wp_json_encode([
|
|
'card_pass' => 1,
|
|
'card_id' => $card_id,
|
|
'card_type' => $type,
|
|
'meta' => $meta,
|
|
'password' => substr(md5($password), 0, 8),
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
|
|
$order_status = $status !== '' ? $status : Order::STATUS_PENDING;
|
|
$order_data = [
|
|
'order_number' => $order_number !== '' ? $order_number : null,
|
|
'user_id' => $user_id,
|
|
'product_type' => $product_type,
|
|
'product_id' => $product_id,
|
|
'amount' => $amount,
|
|
'currency' => $currency,
|
|
'status' => $order_status,
|
|
'payment_method' => 'card_pass',
|
|
'payment_transaction_id' => $order_status === Order::STATUS_PAID ? 'card_pass_' . $card_id : '',
|
|
'paid_at' => $order_status === Order::STATUS_PAID ? current_time('mysql') : null,
|
|
'note' => is_string($note) ? $note : '',
|
|
'request_key' => $request_key,
|
|
];
|
|
if ($order_status === Order::STATUS_PENDING) {
|
|
$order_data['expires_at'] = \AKSRT\Settings\PaymentRuntime::get_order_expiry_at();
|
|
}
|
|
return OrderRepository::get_instance()->create($order_data);
|
|
}
|
|
|
|
/**
|
|
* @return true|\WP_Error
|
|
*/
|
|
private function assert_type_enabled(string $type) {
|
|
$opts = $this->get_public_options();
|
|
if ($type === self::TYPE_BALANCE && empty($opts['balance']['enabled'])) {
|
|
return new \WP_Error('aksrt_card_disabled', __('余额卡密兑换未开启', 'aksrt'), ['status' => 403]);
|
|
}
|
|
if ($type === self::TYPE_VIP && empty($opts['vip']['enabled'])) {
|
|
return new \WP_Error('aksrt_card_disabled', __('会员卡密兑换未开启', 'aksrt'), ['status' => 403]);
|
|
}
|
|
if ($type === self::TYPE_CREDITS && empty($opts['credits']['enabled'])) {
|
|
return new \WP_Error('aksrt_card_disabled', __('积分卡密兑换未开启', 'aksrt'), ['status' => 403]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $data
|
|
* @return array<string,mixed>|null
|
|
*/
|
|
private function normalize_meta(string $type, array $data): ?array {
|
|
if (is_array($data['meta'] ?? null)) {
|
|
$raw = $data['meta'];
|
|
} else {
|
|
$raw = $data;
|
|
}
|
|
|
|
if ($type === self::TYPE_BALANCE) {
|
|
$value = $raw['amount'] ?? $data['amount'] ?? '';
|
|
if (!is_string($value)) {
|
|
return null;
|
|
}
|
|
try {
|
|
$amount = \AKSRT\Ledger\AssetAmount::balance($value);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return null;
|
|
}
|
|
if ($amount->minor() <= 0) {
|
|
return null;
|
|
}
|
|
return ['amount' => $amount->decimal()];
|
|
}
|
|
if ($type === self::TYPE_VIP) {
|
|
$level = max(1, min(2, (int) ($raw['level'] ?? $data['level'] ?? 1)));
|
|
$days = max(0, (int) ($raw['days'] ?? $data['days'] ?? 0));
|
|
return ['level' => $level, 'days' => $days];
|
|
}
|
|
if ($type === self::TYPE_CREDITS) {
|
|
$points = max(0, (int) ($raw['points'] ?? $data['points'] ?? 0));
|
|
if ($points <= 0) {
|
|
return null;
|
|
}
|
|
return ['points' => $points];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $row
|
|
* @return array<string,mixed>
|
|
*/
|
|
private function decode_meta(array $row): array {
|
|
$meta = [];
|
|
if (!empty($row['meta'])) {
|
|
if (is_array($row['meta'])) {
|
|
$meta = $row['meta'];
|
|
} else {
|
|
$decoded = json_decode((string) $row['meta'], true);
|
|
if (is_array($decoded)) {
|
|
$meta = $decoded;
|
|
}
|
|
}
|
|
}
|
|
return $meta;
|
|
}
|
|
|
|
/**
|
|
* Admin list helper.
|
|
*
|
|
* @return array{items:array,total:int,page:int,max_pages:int}
|
|
*/
|
|
public function get_list(array $args = []): array {
|
|
global $wpdb;
|
|
|
|
$limit = max(1, min(100, (int) ($args['limit'] ?? 20)));
|
|
$offset = max(0, (int) ($args['offset'] ?? 0));
|
|
$status = sanitize_key((string) ($args['status'] ?? ''));
|
|
$type = sanitize_key((string) ($args['type'] ?? ''));
|
|
$search = sanitize_text_field((string) ($args['search'] ?? $args['s'] ?? ''));
|
|
|
|
$where = [];
|
|
$params = [];
|
|
if ($status !== '') {
|
|
$where[] = 'status = %s';
|
|
$params[] = $status;
|
|
}
|
|
if ($type !== '') {
|
|
$where[] = 'type = %s';
|
|
$params[] = $type;
|
|
}
|
|
if ($search !== '') {
|
|
$like = '%' . $wpdb->esc_like($search) . '%';
|
|
$where[] = '(password LIKE %s OR card LIKE %s OR note LIKE %s OR CAST(id AS CHAR) = %s)';
|
|
$params[] = $like;
|
|
$params[] = $like;
|
|
$params[] = $like;
|
|
$params[] = $search;
|
|
}
|
|
$where_sql = $where ? 'WHERE ' . implode(' AND ', $where) : '';
|
|
|
|
$count_sql = "SELECT COUNT(*) FROM {$this->table()} {$where_sql}";
|
|
$total = $params
|
|
? (int) $wpdb->get_var($wpdb->prepare($count_sql, $params))
|
|
: (int) $wpdb->get_var($count_sql);
|
|
|
|
$sql = "SELECT * FROM {$this->table()} {$where_sql} ORDER BY created_at DESC LIMIT %d OFFSET %d";
|
|
$params[] = $limit;
|
|
$params[] = $offset;
|
|
$items = $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);
|
|
|
|
return [
|
|
'items' => $items ?: [],
|
|
'total' => $total,
|
|
'page' => (int) floor($offset / $limit) + 1,
|
|
'max_pages' => (int) ceil(max(1, $total) / $limit),
|
|
];
|
|
}
|
|
|
|
public function disable(int $id): bool {
|
|
global $wpdb;
|
|
return (bool) $wpdb->update($this->table(), [
|
|
'status' => self::STATUS_DISABLED,
|
|
], ['id' => $id], ['%s'], ['%d']);
|
|
}
|
|
|
|
/**
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function get_stats(): array {
|
|
global $wpdb;
|
|
$table = $this->table();
|
|
|
|
$row = $wpdb->get_row(
|
|
"SELECT
|
|
COUNT(*) as total,
|
|
SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) as active,
|
|
SUM(CASE WHEN status = 'used' THEN 1 ELSE 0 END) as used,
|
|
SUM(amount) as total_amount,
|
|
SUM(CASE WHEN status = 'used' THEN amount ELSE 0 END) as used_amount
|
|
FROM {$table}",
|
|
ARRAY_A
|
|
);
|
|
|
|
return [
|
|
'total' => (int) ($row['total'] ?? 0),
|
|
'active' => (int) ($row['active'] ?? 0),
|
|
'used' => (int) ($row['used'] ?? 0),
|
|
'total_amount' => (string) ($row['total_amount'] ?? '0.00'),
|
|
'used_amount' => (string) ($row['used_amount'] ?? '0.00'),
|
|
];
|
|
}
|
|
}
|