Wire zibll-style one-shot points/cash cashier for post+_aksrt_pay downloads: server amount from aksrt_get_user_pay_price, ORDER_PRODUCT_RESOURCE unlock, frontend need-purchase opens modal, download box purchased/VIP/purchase CTAs.
583 lines
23 KiB
PHP
583 lines
23 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT Content Pay — post/_aksrt_pay cashier + resource orders.
|
|
*
|
|
* Server-side amount always from aksrt_get_user_pay_price().
|
|
* product_type = ORDER_PRODUCT_RESOURCE (post_id).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Content_Pay {
|
|
|
|
private static $instance = null;
|
|
|
|
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_routes']);
|
|
}
|
|
|
|
public function register_routes(): void {
|
|
register_rest_route('aksrt/v1', '/pay/content/(?P<id>\\d+)', [
|
|
[
|
|
'methods' => 'GET',
|
|
'callback' => [$this, 'rest_preview'],
|
|
'permission_callback' => [$this, 'check_rest_nonce'],
|
|
],
|
|
[
|
|
'methods' => 'POST',
|
|
'callback' => [$this, 'rest_purchase'],
|
|
'permission_callback' => [$this, 'check_rest_nonce'],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* REST nonce for logged-in users (wp_rest). Guests get 401 on purchase paths.
|
|
*
|
|
* @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;
|
|
}
|
|
|
|
/**
|
|
* GET /pay/content/{id}
|
|
*/
|
|
public function rest_preview(WP_REST_Request $request) {
|
|
$post_id = (int) $request['id'];
|
|
$uid = get_current_user_id();
|
|
$preview = $this->preview($post_id, $uid);
|
|
if (is_wp_error($preview)) {
|
|
return $preview;
|
|
}
|
|
return new WP_REST_Response($preview, 200);
|
|
}
|
|
|
|
/**
|
|
* POST /pay/content/{id}
|
|
* body: { mode?: points|cash, payment_method?: balance|alipay|... }
|
|
*/
|
|
public function rest_purchase(WP_REST_Request $request) {
|
|
$post_id = (int) $request['id'];
|
|
$uid = get_current_user_id();
|
|
if ($uid <= 0) {
|
|
$post = get_post($post_id);
|
|
return new WP_Error('aksrt_need_login', __('请先登录后再购买。', 'aksrt'), [
|
|
'status' => 401,
|
|
'login_url' => wp_login_url($post ? get_permalink($post) : home_url('/')),
|
|
]);
|
|
}
|
|
|
|
$params = $request->get_json_params();
|
|
if (!is_array($params)) {
|
|
$params = $request->get_params();
|
|
}
|
|
|
|
$gate = $this->assert_purchasable($post_id, $uid);
|
|
if (is_wp_error($gate)) {
|
|
return $gate;
|
|
}
|
|
|
|
$priced = $gate['priced'];
|
|
$pay_modo = (($priced['modo'] ?? '0') === 'points') ? 'points' : '0';
|
|
|
|
$mode = sanitize_key((string) ($params['mode'] ?? ''));
|
|
if ($mode === '') {
|
|
$mode = $pay_modo === 'points' ? 'points' : 'cash';
|
|
}
|
|
if ($mode === 'points' || $pay_modo === 'points') {
|
|
return $this->rest_wrap_result($this->create_and_pay_points($post_id, $uid));
|
|
}
|
|
|
|
$payment_method = sanitize_key((string) ($params['payment_method'] ?? 'balance'));
|
|
if ($payment_method === '' || $payment_method === 'cash') {
|
|
$payment_method = 'balance';
|
|
}
|
|
return $this->rest_wrap_result($this->create_cash_order($post_id, $uid, $payment_method));
|
|
}
|
|
|
|
/**
|
|
* @param array|WP_Error $result
|
|
* @return WP_REST_Response|WP_Error
|
|
*/
|
|
private function rest_wrap_result($result) {
|
|
if (is_wp_error($result)) {
|
|
return $result;
|
|
}
|
|
if (empty($result['success'])) {
|
|
$code = sanitize_key((string) ($result['code'] ?? 'aksrt_pay_failed'));
|
|
$status = (int) ($result['status'] ?? 400);
|
|
return new WP_Error($code !== '' ? $code : 'aksrt_pay_failed', (string) ($result['message'] ?? __('支付失败', 'aksrt')), array_merge(
|
|
['status' => $status > 0 ? $status : 400],
|
|
is_array($result['data'] ?? null) ? $result['data'] : []
|
|
));
|
|
}
|
|
return new WP_REST_Response($result, 200);
|
|
}
|
|
|
|
/**
|
|
* Cashier preview payload.
|
|
*
|
|
* @return array|WP_Error
|
|
*/
|
|
public function preview(int $post_id, int $user_id = 0) {
|
|
$post = get_post($post_id);
|
|
if (!($post instanceof WP_Post) || $post->post_status !== 'publish') {
|
|
return new WP_Error('aksrt_invalid', __('内容不存在', 'aksrt'), ['status' => 404]);
|
|
}
|
|
|
|
$meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : [];
|
|
$pay_type = AKSRT_Domain_Model::normalize_pay_type((string) ($meta['pay_type'] ?? AKSRT_Domain_Model::PAY_TYPE_NO));
|
|
if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_NO) {
|
|
return new WP_Error('aksrt_not_paid', __('该内容无需购买', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
$uid = $user_id > 0 ? $user_id : get_current_user_id();
|
|
$priced = function_exists('aksrt_get_user_pay_price')
|
|
? aksrt_get_user_pay_price($post, $uid)
|
|
: ['amount' => 0.0, 'base' => 0.0, 'modo' => '0', 'currency' => '¥', 'is_free' => true, 'pay_limit' => 0, 'vip_level' => 0, 'original' => 0.0, 'vip1' => 0.0, 'vip2' => 0.0];
|
|
|
|
$pay_modo = (($priced['modo'] ?? '0') === 'points') ? 'points' : '0';
|
|
$pay_limit = (int) ($priced['pay_limit'] ?? 0);
|
|
$vip_level = (int) ($priced['vip_level'] ?? 0);
|
|
$amount = (float) ($priced['amount'] ?? 0);
|
|
$base = (float) ($priced['base'] ?? $amount);
|
|
|
|
$has_paid = false;
|
|
if ($uid > 0 && class_exists('AKSRT_Order')) {
|
|
$has_paid = AKSRT_Order::get_instance()->has_paid_access(
|
|
$uid,
|
|
AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE,
|
|
$post_id
|
|
);
|
|
}
|
|
|
|
$need_vip = $pay_limit > 0 && $vip_level < $pay_limit;
|
|
$currency = (string) ($priced['currency'] ?? ($pay_modo === 'points'
|
|
? (class_exists('AKSRT_Settings') ? AKSRT_Settings::get_currency_name() : __('积分', 'aksrt'))
|
|
: '¥'));
|
|
|
|
$balance = ($uid > 0 && class_exists('AKSRT_Credits')) ? AKSRT_Credits::balance($uid) : 0;
|
|
$methods = $this->available_payment_methods($pay_modo);
|
|
|
|
$amount_label = $this->format_amount_label($amount, $pay_modo, $currency);
|
|
$base_label = $this->format_amount_label($base, $pay_modo, $currency);
|
|
|
|
$vip_page = function_exists('aksrt_get_user_center_url')
|
|
? aksrt_get_user_center_url()
|
|
: home_url('/');
|
|
|
|
return [
|
|
'post_id' => $post_id,
|
|
'title' => get_the_title($post),
|
|
'permalink' => get_permalink($post),
|
|
'pay_type' => $pay_type,
|
|
'pay_type_label' => AKSRT_Domain_Model::pay_type_labels()[$pay_type] ?? $pay_type,
|
|
'pay_modo' => $pay_modo,
|
|
'pay_limit' => $pay_limit,
|
|
'vip_level' => $vip_level,
|
|
'amount' => $pay_modo === 'points' ? (int) ceil($amount) : round($amount, 2),
|
|
'base' => $pay_modo === 'points' ? (int) ceil($base) : round($base, 2),
|
|
'original' => (float) ($priced['original'] ?? 0),
|
|
'vip1' => (float) ($priced['vip1'] ?? 0),
|
|
'vip2' => (float) ($priced['vip2'] ?? 0),
|
|
'currency' => $currency,
|
|
'amount_label' => $amount_label,
|
|
'base_label' => $base_label,
|
|
'is_free' => !empty($priced['is_free']) || $amount <= 0,
|
|
'has_paid_access' => $has_paid,
|
|
'need_login' => $uid <= 0,
|
|
'need_vip' => $need_vip,
|
|
'need_purchase' => !$has_paid && !$need_vip && $amount > 0,
|
|
'logged_in' => $uid > 0,
|
|
'balance' => $balance,
|
|
'balance_enough' => $pay_modo !== 'points' || $balance >= (int) ceil($amount),
|
|
'payment_methods' => $methods,
|
|
'login_url' => wp_login_url(get_permalink($post)),
|
|
'vip_url' => $vip_page,
|
|
'credits_url' => $vip_page,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Points one-shot purchase → permanent resource order.
|
|
*
|
|
* @return array|WP_Error
|
|
*/
|
|
public function create_and_pay_points(int $post_id, int $user_id) {
|
|
$assert = $this->assert_purchasable($post_id, $user_id);
|
|
if (is_wp_error($assert)) {
|
|
return $assert;
|
|
}
|
|
|
|
$priced = $assert['priced'];
|
|
if ((($priced['modo'] ?? '0') !== 'points')) {
|
|
return new WP_Error('aksrt_wrong_modo', __('该内容不支持积分支付。', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
$amount = (int) ceil((float) ($priced['amount'] ?? 0));
|
|
if ($amount <= 0) {
|
|
return [
|
|
'success' => true,
|
|
'data' => [
|
|
'free' => true,
|
|
'message' => __('无需支付,已可访问。', 'aksrt'),
|
|
'reload' => true,
|
|
],
|
|
];
|
|
}
|
|
|
|
if (!class_exists('AKSRT_Order') || !class_exists('AKSRT_Credits')) {
|
|
return new WP_Error('aksrt_pay_unavailable', __('支付系统不可用。', 'aksrt'), ['status' => 500]);
|
|
}
|
|
|
|
$balance = AKSRT_Credits::balance($user_id);
|
|
if ($balance < $amount) {
|
|
return new WP_Error('aksrt_credits_insufficient', __('积分不足,暂时无法支付。', 'aksrt'), [
|
|
'status' => 402,
|
|
'balance' => $balance,
|
|
'cost' => $amount,
|
|
]);
|
|
}
|
|
|
|
$note = wp_json_encode([
|
|
'content_pay' => 1,
|
|
'pay_modo' => 'points',
|
|
'post_id' => $post_id,
|
|
'vip_level' => (int) ($priced['vip_level'] ?? 0),
|
|
'base' => (float) ($priced['base'] ?? $amount),
|
|
'amount' => $amount,
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
|
|
$order_number = AKSRT_Order_Repository::get_instance()->create([
|
|
'user_id' => $user_id,
|
|
'product_type' => AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE,
|
|
'product_id' => $post_id,
|
|
'amount' => (float) $amount,
|
|
'currency' => 'points',
|
|
'status' => AKSRT_Order::STATUS_PENDING,
|
|
'payment_method' => 'points',
|
|
'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]);
|
|
}
|
|
|
|
$deducted = AKSRT_Credits::deduct(
|
|
$user_id,
|
|
$amount,
|
|
'content_points_pay',
|
|
['type' => 'post', 'id' => $post_id, 'order' => $order_number],
|
|
sprintf(__('积分购买《%s》', 'aksrt'), get_the_title($post_id))
|
|
);
|
|
|
|
if (is_wp_error($deducted) || $deducted === false) {
|
|
AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_CANCELLED, [
|
|
'note' => __('积分扣除失败,自动取消', 'aksrt'),
|
|
]);
|
|
$msg = is_wp_error($deducted) ? $deducted->get_error_message() : __('积分扣除失败。', 'aksrt');
|
|
return new WP_Error('aksrt_credits_deduct_failed', $msg ?: __('积分扣除失败。', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
$paid = AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_PAID, [
|
|
'payment_method' => 'points',
|
|
'payment_transaction_id' => 'points_' . $order_number,
|
|
'note' => sprintf(__('积分支付 %d 积分。', 'aksrt'), $amount),
|
|
]);
|
|
|
|
if (!$paid) {
|
|
AKSRT_Credits::add(
|
|
$user_id,
|
|
$amount,
|
|
'content_points_pay_refund',
|
|
['type' => 'post', 'id' => $post_id, 'order' => $order_number],
|
|
sprintf(__('购买失败退款:%s', 'aksrt'), get_the_title($post_id))
|
|
);
|
|
return new WP_Error('aksrt_order_confirm_failed', __('订单确认失败,已尝试退回积分。', 'aksrt'), ['status' => 500]);
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'data' => [
|
|
'order_number' => $order_number,
|
|
'amount' => $amount,
|
|
'pay_modo' => 'points',
|
|
'balance' => AKSRT_Credits::balance($user_id),
|
|
'message' => __('支付成功', 'aksrt'),
|
|
'reload' => true,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Cash order create + balance/gateway pay.
|
|
*
|
|
* @return array|WP_Error
|
|
*/
|
|
public function create_cash_order(int $post_id, int $user_id, string $payment_method = 'balance') {
|
|
$assert = $this->assert_purchasable($post_id, $user_id);
|
|
if (is_wp_error($assert)) {
|
|
return $assert;
|
|
}
|
|
|
|
$priced = $assert['priced'];
|
|
if ((($priced['modo'] ?? '0') === 'points')) {
|
|
return new WP_Error('aksrt_wrong_modo', __('该内容仅支持积分支付。', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
$amount = round((float) ($priced['amount'] ?? 0), 2);
|
|
if ($amount <= 0) {
|
|
return [
|
|
'success' => true,
|
|
'data' => [
|
|
'free' => true,
|
|
'message' => __('无需支付,已可访问。', 'aksrt'),
|
|
'reload' => true,
|
|
],
|
|
];
|
|
}
|
|
|
|
if (!class_exists('AKSRT_Order') || !class_exists('AKSRT_Order_Repository')) {
|
|
return new WP_Error('aksrt_pay_unavailable', __('支付系统不可用。', 'aksrt'), ['status' => 500]);
|
|
}
|
|
|
|
$payment_method = sanitize_key($payment_method);
|
|
if ($payment_method === '') {
|
|
$payment_method = 'balance';
|
|
}
|
|
|
|
$note = wp_json_encode([
|
|
'content_pay' => 1,
|
|
'pay_modo' => '0',
|
|
'post_id' => $post_id,
|
|
'vip_level' => (int) ($priced['vip_level'] ?? 0),
|
|
'base' => (float) ($priced['base'] ?? $amount),
|
|
'amount' => $amount,
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
|
|
$order_number = AKSRT_Order_Repository::get_instance()->create([
|
|
'user_id' => $user_id,
|
|
'product_type' => AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE,
|
|
'product_id' => $post_id,
|
|
'amount' => $amount,
|
|
'currency' => 'CNY',
|
|
'status' => AKSRT_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_Order_Repository::get_instance()->get($order_number);
|
|
if (!$order) {
|
|
return new WP_Error('aksrt_order_create_failed', __('创建订单失败。', 'aksrt'), ['status' => 500]);
|
|
}
|
|
|
|
$order['subject'] = sprintf(
|
|
/* translators: %s: post title */
|
|
__('购买《%s》', 'aksrt'),
|
|
get_the_title($post_id)
|
|
);
|
|
|
|
if (!class_exists('AKSRT_Payment_Gateway')) {
|
|
return new WP_Error('aksrt_pay_unavailable', __('支付网关不可用。', 'aksrt'), ['status' => 500]);
|
|
}
|
|
|
|
$gateway_manager = AKSRT_Payment_Gateway::get_instance();
|
|
$gateway = $gateway_manager->get_gateway($payment_method);
|
|
if (!$gateway) {
|
|
AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_CANCELLED, [
|
|
'note' => __('支付方式不可用,自动取消', 'aksrt'),
|
|
]);
|
|
return new WP_Error('aksrt_gateway_unavailable', __('当前支付方式不可用。', 'aksrt'), ['status' => 400]);
|
|
}
|
|
if (method_exists($gateway, 'is_configured') && !$gateway->is_configured()) {
|
|
AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_CANCELLED, [
|
|
'note' => __('支付方式未配置,自动取消', 'aksrt'),
|
|
]);
|
|
return new WP_Error('aksrt_gateway_unconfigured', __('该支付方式尚未配置,请联系站长。', 'aksrt'), ['status' => 400]);
|
|
}
|
|
|
|
$payment_result = $gateway->create_payment($order);
|
|
if (empty($payment_result['success'])) {
|
|
AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_CANCELLED, [
|
|
'note' => (string) ($payment_result['message'] ?? __('支付失败,自动取消', 'aksrt')),
|
|
]);
|
|
$msg = (string) ($payment_result['message'] ?? __('支付失败。', 'aksrt'));
|
|
$code = ($payment_method === 'balance' && str_contains($msg, '余额'))
|
|
? 'aksrt_balance_insufficient'
|
|
: 'aksrt_pay_failed';
|
|
return new WP_Error($code, $msg, [
|
|
'status' => 402,
|
|
'order_number' => $order_number,
|
|
]);
|
|
}
|
|
|
|
$code_url = (string) ($payment_result['payment_data']['code_url'] ?? '');
|
|
$payment_url = (string) ($payment_result['payment_url'] ?? '');
|
|
if ($payment_url !== '' || $code_url !== '') {
|
|
return [
|
|
'success' => true,
|
|
'data' => [
|
|
'redirect' => true,
|
|
'payment_url' => $payment_url,
|
|
'qr_content' => $code_url,
|
|
'order_number' => $order_number,
|
|
'amount' => $amount,
|
|
'pay_modo' => '0',
|
|
'message' => $payment_result['message'] ?? __('请在新页面完成支付。', 'aksrt'),
|
|
'reload' => false,
|
|
],
|
|
];
|
|
}
|
|
|
|
// Sync gateway (balance): mark paid.
|
|
$paid = AKSRT_Order::get_instance()->update_status($order_number, AKSRT_Order::STATUS_PAID, [
|
|
'payment_method' => $payment_method,
|
|
'payment_transaction_id' => sanitize_text_field((string) ($payment_result['transaction_id'] ?? '')),
|
|
'note' => __('用户完成内容购买支付。', 'aksrt'),
|
|
]);
|
|
if (!$paid) {
|
|
return new WP_Error('aksrt_order_confirm_failed', __('订单确认失败。', 'aksrt'), ['status' => 500]);
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'data' => [
|
|
'order_number' => $order_number,
|
|
'amount' => $amount,
|
|
'pay_modo' => '0',
|
|
'message' => $payment_result['message'] ?? __('支付成功', 'aksrt'),
|
|
'reload' => true,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array{post:WP_Post,meta:array,priced:array}|WP_Error
|
|
*/
|
|
private function assert_purchasable(int $post_id, int $user_id) {
|
|
$post = get_post($post_id);
|
|
if (!($post instanceof WP_Post) || $post->post_status !== 'publish') {
|
|
return new WP_Error('aksrt_invalid', __('内容不存在', 'aksrt'), ['status' => 404]);
|
|
}
|
|
if ($user_id <= 0) {
|
|
return new WP_Error('aksrt_need_login', __('请先登录后再购买。', 'aksrt'), [
|
|
'status' => 401,
|
|
'login_url' => wp_login_url(get_permalink($post)),
|
|
]);
|
|
}
|
|
|
|
$meta = function_exists('aksrt_get_pay_meta') ? aksrt_get_pay_meta($post) : [];
|
|
$pay_type = AKSRT_Domain_Model::normalize_pay_type((string) ($meta['pay_type'] ?? AKSRT_Domain_Model::PAY_TYPE_NO));
|
|
if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_NO) {
|
|
return new WP_Error('aksrt_not_paid', __('该内容无需购买', 'aksrt'), ['status' => 400]);
|
|
}
|
|
if ($pay_type === AKSRT_Domain_Model::PAY_TYPE_VIP) {
|
|
return new WP_Error('aksrt_need_vip', __('该内容为 VIP 专属,请开通会员。', 'aksrt'), ['status' => 403]);
|
|
}
|
|
|
|
$priced = function_exists('aksrt_get_user_pay_price')
|
|
? aksrt_get_user_pay_price($post, $user_id)
|
|
: ['amount' => 0.0, 'modo' => '0', 'pay_limit' => 0, 'vip_level' => 0, 'is_free' => true, 'base' => 0.0];
|
|
|
|
$pay_limit = (int) ($priced['pay_limit'] ?? 0);
|
|
$vip_level = (int) ($priced['vip_level'] ?? 0);
|
|
if ($pay_limit > 0 && $vip_level < $pay_limit) {
|
|
return new WP_Error('aksrt_need_vip', sprintf(
|
|
/* translators: %d: required VIP level */
|
|
__('该资源仅限 VIP%s 及以上会员购买/下载。', 'aksrt'),
|
|
max(1, $pay_limit)
|
|
), [
|
|
'status' => 403,
|
|
'pay_limit' => $pay_limit,
|
|
]);
|
|
}
|
|
|
|
if (class_exists('AKSRT_Order') && AKSRT_Order::get_instance()->has_paid_access(
|
|
$user_id,
|
|
AKSRT_Domain_Model::ORDER_PRODUCT_RESOURCE,
|
|
$post_id
|
|
)) {
|
|
return new WP_Error('aksrt_already_paid', __('您已购买该内容。', 'aksrt'), [
|
|
'status' => 200,
|
|
'has_paid_access' => true,
|
|
'reload' => true,
|
|
]);
|
|
}
|
|
|
|
return [
|
|
'post' => $post,
|
|
'meta' => $meta,
|
|
'priced' => $priced,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array{id:string,name:string}>
|
|
*/
|
|
private function available_payment_methods(string $pay_modo): array {
|
|
if ($pay_modo === 'points') {
|
|
return [
|
|
['id' => 'points', 'name' => __('积分支付', 'aksrt')],
|
|
];
|
|
}
|
|
|
|
$methods = [];
|
|
if (!class_exists('AKSRT_Payment_Gateway')) {
|
|
return [['id' => 'balance', 'name' => __('站内余额', 'aksrt')]];
|
|
}
|
|
$gw = AKSRT_Payment_Gateway::get_instance();
|
|
foreach (['balance', 'alipay', 'wechatpay', 'paypal', 'stripe'] as $id) {
|
|
$g = $gw->get_gateway($id);
|
|
if (!$g) {
|
|
continue;
|
|
}
|
|
if (method_exists($g, 'is_configured') && !$g->is_configured()) {
|
|
continue;
|
|
}
|
|
$methods[] = [
|
|
'id' => $id,
|
|
'name' => method_exists($g, 'get_name') ? (string) $g->get_name() : $id,
|
|
];
|
|
}
|
|
if ($methods === []) {
|
|
$methods[] = ['id' => 'balance', 'name' => __('站内余额', 'aksrt')];
|
|
}
|
|
return $methods;
|
|
}
|
|
|
|
private function format_amount_label(float $amount, string $pay_modo, string $currency): string {
|
|
if ($amount <= 0) {
|
|
return (string) __('免费', 'aksrt');
|
|
}
|
|
$n = function_exists('aksrt_format_price') ? aksrt_format_price($amount) : (string) $amount;
|
|
if ($pay_modo === 'points') {
|
|
return $n . ' ' . $currency;
|
|
}
|
|
return $currency . $n;
|
|
}
|
|
}
|