fix: Route priority issue - /order was matched by /{id}

Problem:
POST /payments/gateways/order → 404 'gateway_not_found'

Root Cause:
WordPress REST API matches routes in registration order.
The /gateways/order route was registered AFTER /gateways/{id}.
So /gateways/order was being matched by /gateways/{id} where id='order'.
Then get_gateway('order') returned 'gateway_not_found'.

Solution:
Register specific routes BEFORE dynamic routes:
1. /gateways (list)
2. /gateways/order (specific - NEW POSITION)
3. /gateways/{id} (dynamic)
4. /gateways/{id}/toggle (dynamic with action)

Route Priority Rules:
 Specific routes first
 Dynamic routes last
 More specific before less specific

Before:
/gateways → OK
/gateways/{id} → Matches everything including 'order'
/gateways/{id}/toggle → OK (more specific than {id})
/gateways/order → Never reached!

After:
/gateways → OK
/gateways/order → Matches 'order' specifically
/gateways/{id} → Matches other IDs
/gateways/{id}/toggle → OK

Result:
 /gateways/order now works correctly
 Sorting saves to database
 No more 'gateway_not_found' error

Files Modified:
- PaymentsController.php: Moved /order route before /{id} routes
This commit is contained in:
dwindown
2025-11-06 14:05:18 +07:00
parent 52f7c1b99d
commit 40fb364035
2 changed files with 42 additions and 29 deletions

View File

@@ -42,6 +42,32 @@ class PaymentsController extends WP_REST_Controller {
'schema' => [$this, 'get_gateways_schema'],
]);
// POST /woonoow/v1/payments/gateways/order
// IMPORTANT: Register this BEFORE the dynamic {id} routes to avoid conflicts
register_rest_route($this->namespace, '/' . $this->rest_base . '/gateways/order', [
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [$this, 'save_gateway_order'],
'permission_callback' => [$this, 'check_permission'],
'args' => [
'category' => [
'description' => 'Gateway category (manual or online)',
'type' => 'string',
'required' => true,
'enum' => ['manual', 'online'],
],
'order' => [
'description' => 'Array of gateway IDs in desired order',
'type' => 'array',
'required' => true,
'items' => [
'type' => 'string',
],
],
],
],
]);
// GET /woonoow/v1/payments/gateways/{id}
register_rest_route($this->namespace, '/' . $this->rest_base . '/gateways/(?P<id>[a-zA-Z0-9_-]+)', [
[
@@ -94,31 +120,6 @@ class PaymentsController extends WP_REST_Controller {
],
],
]);
// POST /woonoow/v1/payments/gateways/order
register_rest_route($this->namespace, '/' . $this->rest_base . '/gateways/order', [
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [$this, 'save_gateway_order'],
'permission_callback' => [$this, 'check_permission'],
'args' => [
'category' => [
'description' => 'Gateway category (manual or online)',
'type' => 'string',
'required' => true,
'enum' => ['manual', 'online'],
],
'order' => [
'description' => 'Array of gateway IDs in desired order',
'type' => 'array',
'required' => true,
'items' => [
'type' => 'string',
],
],
],
],
]);
}
/**