Apply the configured order lifetime to all checkout flows and close unpaid orders once their expiry is reached.
242 lines
7.8 KiB
PHP
242 lines
7.8 KiB
PHP
<?php
|
|
/**
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Credits;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait BalanceServicePoints {
|
|
|
|
|
|
|
|
public function points_buy_enabled(): bool {
|
|
if (!class_exists('\AKSRT\Settings\Settings') || !\AKSRT\Settings\Settings::credits_enabled()) {
|
|
return false;
|
|
}
|
|
$all = \AKSRT\Settings\Settings::get_all();
|
|
return !empty($all['points_pay_s']);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function get_points_buy_view(int $user_id): array {
|
|
$all = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
|
|
$rate = max(1, (int) ($all['points_rate'] ?? 100));
|
|
$packages_raw = (string) ($all['pay_points_product'] ?? '');
|
|
$packages = $this->parse_points_packages($packages_raw);
|
|
$custom_s = !isset($all['pay_points_product_custom_s']) || !empty($all['pay_points_product_custom_s']);
|
|
$custom_limit = max(1, (int) ($all['pay_points_product_custom_limit'] ?? 99999));
|
|
$methods = [];
|
|
if (function_exists('aksrt_list_cash_payment_methods')) {
|
|
foreach (aksrt_list_cash_payment_methods(true) as $m) {
|
|
$id = is_array($m) ? (string) ($m['id'] ?? '') : (string) $m;
|
|
if ($id === '') {
|
|
continue;
|
|
}
|
|
if ($id === 'balance' && !(class_exists('AKSRT\\Credits\\Balance') && Balance::enabled())) {
|
|
continue;
|
|
}
|
|
$methods[] = is_array($m) ? $m : ['id' => $id, 'name' => $id];
|
|
}
|
|
}
|
|
if (class_exists('AKSRT\\Credits\\Balance') && Balance::enabled()) {
|
|
$has_bal = false;
|
|
foreach ($methods as $m) {
|
|
if (($m['id'] ?? '') === 'balance') {
|
|
$has_bal = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!$has_bal) {
|
|
array_unshift($methods, ['id' => 'balance', 'name' => __('站内余额', 'aksrt')]);
|
|
}
|
|
}
|
|
return [
|
|
'enabled' => $this->points_buy_enabled(),
|
|
'desc' => (string) ($all['pay_points_desc'] ?? ''),
|
|
'rate' => $rate,
|
|
'points' => class_exists('AKSRT\\Credits\\Credits') ? Credits::balance($user_id) : 0,
|
|
'packages' => $packages,
|
|
'custom_s' => $custom_s,
|
|
'custom_limit' => $custom_limit,
|
|
'payment_methods' => $methods,
|
|
];
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @return array<int,array{key:string,points:int,pay_price:float,title: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;
|
|
}
|
|
$pay = (float) $parts[0];
|
|
$points = (int) $parts[1];
|
|
$title = $parts[2] ?? '';
|
|
if ($pay <= 0 || $points <= 0) {
|
|
continue;
|
|
}
|
|
$out[] = [
|
|
'key' => (string) $i,
|
|
'pay_price' => round($pay, 2),
|
|
'points' => $points,
|
|
'title' => $title !== '' ? $title : sprintf(__('%d 积分', 'aksrt'), $points),
|
|
];
|
|
$i++;
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @return array<string,mixed>|\WP_Error
|
|
*/
|
|
public function create_points_buy_order(int $user_id, string $package_key, int $custom_points, 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_points_buy_view($user_id);
|
|
$rate = max(1, (int) ($view['rate'] ?? 100));
|
|
$points_amount = 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_points = max(0, $custom_points);
|
|
$max = (int) ($view['custom_limit'] ?? 99999);
|
|
if ($custom_points < $rate || $custom_points > $max) {
|
|
return new \WP_Error('aksrt_custom_limit', sprintf(__('购买积分需在 %d ~ %d 之间', 'aksrt'), $rate, $max), ['status' => 400]);
|
|
}
|
|
$points_amount = $custom_points;
|
|
$pay_amount = round($custom_points / $rate, 2);
|
|
if ($pay_amount <= 0) {
|
|
$pay_amount = 0.01;
|
|
}
|
|
$title = sprintf(__('购买 %d 积分', 'aksrt'), $points_amount);
|
|
$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]);
|
|
}
|
|
$points_amount = (int) $found['points'];
|
|
$pay_amount = (float) $found['pay_price'];
|
|
$title = (string) $found['title'];
|
|
}
|
|
if ($points_amount <= 0 || $pay_amount <= 0) {
|
|
return new \WP_Error('aksrt_invalid_price', __('金额无效', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
$payment_method = sanitize_key($payment_method);
|
|
if ($payment_method === '') {
|
|
$methods = $view['payment_methods'] ?? [];
|
|
$payment_method = !empty($methods[0]['id']) ? (string) $methods[0]['id'] : 'alipay';
|
|
}
|
|
|
|
$note = wp_json_encode([
|
|
'points_buy' => 1,
|
|
'package' => $package_key,
|
|
'points_amount' => $points_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_POINTS_BUY,
|
|
'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,
|
|
'points_amount' => $points_amount,
|
|
'message' => $payment_result['message'],
|
|
'reload' => false,
|
|
'poll' => true,
|
|
];
|
|
}
|
|
// PaymentOrchestrator's paid transition already dispatched the fulfillment hook.
|
|
return [
|
|
'redirect' => false,
|
|
'order_number' => $order_number,
|
|
'amount' => $pay_amount,
|
|
'points_amount' => $points_amount,
|
|
'points' => Credits::balance($user_id),
|
|
'message' => __('购买成功', 'aksrt'),
|
|
'reload' => true,
|
|
];
|
|
}
|
|
|
|
}
|