98 lines
2.9 KiB
PHP
98 lines
2.9 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT admin shop coupon and gateway operations.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait AKSRT_Admin_Shop_Controller_Coupon_Gateway_Operations {
|
|
|
|
/**
|
|
* GET /shop/coupons — 优惠券列表
|
|
*/
|
|
public function get_coupons(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_Coupon')) {
|
|
return $this->success(['data' => ['items' => [], 'total' => 0, 'page' => 1, 'total_pages' => 1]]);
|
|
}
|
|
|
|
$page = max(1, (int) $request->get_param('page'));
|
|
$per_page = max(1, min(100, (int) $request->get_param('per_page'))) ?: 20;
|
|
$status = sanitize_key((string) $request->get_param('status'));
|
|
$search = sanitize_text_field((string) $request->get_param('search'));
|
|
|
|
$result = AKSRT_Coupon::get_instance()->get_list([
|
|
'status' => $status,
|
|
'search' => $search,
|
|
'limit' => $per_page,
|
|
'offset' => ($page - 1) * $per_page,
|
|
]);
|
|
|
|
return $this->success(['data' => $result]);
|
|
}
|
|
|
|
/**
|
|
* POST /shop/coupons — 创建优惠券
|
|
*/
|
|
public function create_coupon(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_Coupon')) {
|
|
return $this->error('优惠券系统未启用', 500);
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$code = sanitize_text_field((string) ($params['code'] ?? ''));
|
|
|
|
if ($code === '') {
|
|
return $this->error('优惠码不能为空');
|
|
}
|
|
|
|
$id = AKSRT_Coupon::get_instance()->create($params);
|
|
|
|
if ($id > 0) {
|
|
return $this->success([
|
|
'message' => __('优惠券已创建', 'aksrt'),
|
|
'data' => ['id' => $id],
|
|
], 201);
|
|
}
|
|
|
|
return $this->error(__('创建失败(可能优惠码已存在)', 'aksrt'));
|
|
}
|
|
|
|
/**
|
|
* POST /shop/coupons/{id}/disable — 禁用优惠券
|
|
*/
|
|
public function disable_coupon(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_Coupon')) {
|
|
return $this->error('优惠券系统未启用', 500);
|
|
}
|
|
|
|
$id = (int) $request->get_param('id');
|
|
if ($id <= 0) {
|
|
return $this->error('参数无效');
|
|
}
|
|
|
|
$success = AKSRT_Coupon::get_instance()->disable($id);
|
|
|
|
if ($success) {
|
|
return $this->success(['message' => __('优惠券已禁用', 'aksrt')]);
|
|
}
|
|
return $this->error(__('操作失败', 'aksrt'));
|
|
}
|
|
|
|
/**
|
|
* GET /shop/gateways — 支付网关状态列表
|
|
*/
|
|
public function get_gateways(): WP_REST_Response {
|
|
if (!class_exists('AKSRT_Payment_Gateway')) {
|
|
return $this->success(['data' => ['gateways' => []]]);
|
|
}
|
|
|
|
return $this->success([
|
|
'data' => [
|
|
'gateways' => AKSRT_Payment_Gateway::get_instance()->get_status_summary(),
|
|
],
|
|
]);
|
|
}
|
|
}
|