- Trait-split content-pay, vip-service, balance-service, order-repository - Procedural split of user module into user-parts/* - Split scripts enqueue_public into part-1b/1c; settings bulk trait - Bootstrap: explicit notifications, widgets glob-only, home-modules init - ADR: freeze domain placement for pay/revenue/notify boundaries
125 lines
3.8 KiB
PHP
125 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
* Extracted trait: AKSRT_Vip_Service_Rest
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait AKSRT_Vip_Service_Rest {
|
|
|
|
|
|
|
|
public function register_routes(): void {
|
|
register_rest_route('aksrt/v1', '/pay/vip', [
|
|
[
|
|
'methods' => 'GET',
|
|
'callback' => [$this, 'rest_catalog'],
|
|
'permission_callback' => [$this, 'check_rest_nonce'],
|
|
],
|
|
[
|
|
'methods' => 'POST',
|
|
'callback' => [$this, 'rest_purchase'],
|
|
'permission_callback' => [$this, 'check_rest_nonce'],
|
|
],
|
|
]);
|
|
|
|
register_rest_route('aksrt/v1', '/pay/vip/points-exchange', [
|
|
'methods' => 'POST',
|
|
'callback' => [$this, 'rest_points_exchange'],
|
|
'permission_callback' => [$this, 'check_rest_nonce'],
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @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/vip
|
|
*/
|
|
public function rest_catalog(WP_REST_Request $request) {
|
|
$uid = get_current_user_id();
|
|
return new WP_REST_Response($this->get_catalog($uid), 200);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* POST /pay/vip
|
|
* body: { package_id: int, payment_method?: string }
|
|
*/
|
|
public function rest_purchase(WP_REST_Request $request) {
|
|
$uid = get_current_user_id();
|
|
if ($uid <= 0) {
|
|
return new WP_Error('aksrt_need_login', __('请先登录后再开通会员。', 'aksrt'), [
|
|
'status' => 401,
|
|
'login_url' => function_exists('aksrt_get_login_url') ? aksrt_get_login_url() : wp_login_url(),
|
|
]);
|
|
}
|
|
|
|
$params = $request->get_json_params();
|
|
if (!is_array($params)) {
|
|
$params = $request->get_params();
|
|
}
|
|
$package_id = (int) ($params['package_id'] ?? 0);
|
|
$payment_method = sanitize_key((string) ($params['payment_method'] ?? 'balance'));
|
|
if ($payment_method === '' || $payment_method === 'cash') {
|
|
$payment_method = 'balance';
|
|
}
|
|
|
|
$result = $this->purchase($uid, $package_id, $payment_method);
|
|
if (is_wp_error($result)) {
|
|
return $result;
|
|
}
|
|
if (empty($result['success'])) {
|
|
return new WP_Error(
|
|
sanitize_key((string) ($result['code'] ?? 'aksrt_vip_pay_failed')),
|
|
(string) ($result['message'] ?? __('开通失败', 'aksrt')),
|
|
['status' => (int) ($result['status'] ?? 400)]
|
|
);
|
|
}
|
|
return new WP_REST_Response($result, 200);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* POST /pay/vip/points-exchange
|
|
* body: { package_id: int } // points package id = 5000 + index
|
|
*/
|
|
public function rest_points_exchange(WP_REST_Request $request) {
|
|
$uid = get_current_user_id();
|
|
if ($uid <= 0) {
|
|
return new WP_Error('aksrt_need_login', __('请先登录后再兑换。', 'aksrt'), ['status' => 401]);
|
|
}
|
|
$params = $request->get_json_params();
|
|
if (!is_array($params)) {
|
|
$params = $request->get_params();
|
|
}
|
|
$package_id = (int) ($params['package_id'] ?? 0);
|
|
$result = $this->exchange_with_points($uid, $package_id);
|
|
if (is_wp_error($result)) {
|
|
return $result;
|
|
}
|
|
return new WP_REST_Response($result, 200);
|
|
}
|
|
|
|
}
|