Files
WooNooW/includes/Api/PaymentsController.php
dwindown ac8870c104 fix: WordPress forms.css override and cache invalidation
🔴 Issue 1: WordPress forms.css Breaking Input Styling (FIXED)
Problem: /wp-admin/css/forms.css overriding our input styles
- box-shadow: 0 0 0 transparent
- border-radius: 4px
- background-color: #fff
- color: #2c3338

Solution:
- Added !important overrides to Input component
- !bg-transparent !border-input !rounded-md !shadow-sm
- Forces our shadcn styles over WordPress admin CSS

Result:  Inputs now look consistent regardless of WP admin CSS

🔴 Issue 2: Toggle Not Saving + Toast Lying (FIXED)
Problem:
- Toggle appears to work but doesn't persist
- Response shows enabled: false but toast says 'enabled'
- WooCommerce gateway cache not clearing

Root Cause:
- WC()->payment_gateways()->payment_gateways() returns cached data
- wp_cache_delete not enough
- Need to force WooCommerce to reload gateways

Solution:
Backend (PaymentGatewaysProvider.php):
- wp_cache_flush() after save
- WC()->payment_gateways()->init() to reload
- Clear cache before fetching updated gateway

Frontend (Payments.tsx):
- await queryClient.invalidateQueries()
- Show toast AFTER refetch completes
- No more lying toast

Result:  Toggle saves correctly + honest toast timing

📋 Technical Details:

WooCommerce Cache Layers:
1. wp_cache (object cache)
2. WC()->payment_gateways() internal cache
3. Gateway instance settings cache

Our Fix:
1. Save to DB
2. wp_cache_flush()
3. WC()->payment_gateways()->init()
4. Fetch fresh data
5. Return to frontend

Files Modified:
- input.tsx: !important overrides for WP admin CSS
- PaymentGatewaysProvider.php: Force WC reload
- PaymentsController.php: Clear cache before fetch
- Payments.tsx: Await invalidation before toast

🎯 Result:
 Inputs look perfect (no WP CSS interference)
 Toggle saves and persists correctly
 Toast shows after real state confirmed
2025-11-05 23:20:54 +07:00

294 lines
9.1 KiB
PHP

<?php
/**
* Payments REST API Controller
*
* Provides REST endpoints for payment gateway management.
*
* @package WooNooW
*/
namespace WooNooW\API;
use WooNooW\Compat\PaymentGatewaysProvider;
use WP_REST_Controller;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
class PaymentsController extends WP_REST_Controller {
/**
* Namespace
*/
protected $namespace = 'woonoow/v1';
/**
* Rest base
*/
protected $rest_base = 'payments';
/**
* Register routes
*/
public function register_routes() {
// GET /woonoow/v1/payments/gateways
register_rest_route($this->namespace, '/' . $this->rest_base . '/gateways', [
[
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_gateways'],
'permission_callback' => [$this, 'check_permission'],
],
'schema' => [$this, 'get_gateways_schema'],
]);
// GET /woonoow/v1/payments/gateways/{id}
register_rest_route($this->namespace, '/' . $this->rest_base . '/gateways/(?P<id>[a-zA-Z0-9_-]+)', [
[
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_gateway'],
'permission_callback' => [$this, 'check_permission'],
'args' => [
'id' => [
'description' => 'Gateway ID',
'type' => 'string',
'required' => true,
],
],
],
]);
// POST /woonoow/v1/payments/gateways/{id}
register_rest_route($this->namespace, '/' . $this->rest_base . '/gateways/(?P<id>[a-zA-Z0-9_-]+)', [
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [$this, 'save_gateway'],
'permission_callback' => [$this, 'check_permission'],
'args' => [
'id' => [
'description' => 'Gateway ID',
'type' => 'string',
'required' => true,
],
],
],
]);
// POST /woonoow/v1/payments/gateways/{id}/toggle
register_rest_route($this->namespace, '/' . $this->rest_base . '/gateways/(?P<id>[a-zA-Z0-9_-]+)/toggle', [
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [$this, 'toggle_gateway'],
'permission_callback' => [$this, 'check_permission'],
'args' => [
'id' => [
'description' => 'Gateway ID',
'type' => 'string',
'required' => true,
],
'enabled' => [
'description' => 'Enable or disable gateway',
'type' => 'boolean',
'required' => true,
],
],
],
]);
}
/**
* Get all payment gateways
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error Response object or error
*/
public function get_gateways(WP_REST_Request $request) {
try {
$gateways = PaymentGatewaysProvider::get_gateways();
$response = rest_ensure_response($gateways);
// Cache for 5 minutes
$response->header('Cache-Control', 'max-age=300');
return $response;
} catch (\Exception $e) {
return new WP_Error(
'get_gateways_failed',
$e->getMessage(),
['status' => 500]
);
}
}
/**
* Get single payment gateway
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error Response object or error
*/
public function get_gateway(WP_REST_Request $request) {
$gateway_id = $request->get_param('id');
try {
$gateway = PaymentGatewaysProvider::get_gateway($gateway_id);
if ($gateway === null) {
return new WP_Error(
'gateway_not_found',
sprintf('Gateway "%s" not found', $gateway_id),
['status' => 404]
);
}
$response = rest_ensure_response($gateway);
// Cache for 5 minutes
$response->header('Cache-Control', 'max-age=300');
return $response;
} catch (\Exception $e) {
return new WP_Error(
'get_gateway_failed',
$e->getMessage(),
['status' => 500]
);
}
}
/**
* Save gateway settings
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error Response object or error
*/
public function save_gateway(WP_REST_Request $request) {
$gateway_id = $request->get_param('id');
$settings = $request->get_json_params();
if (empty($settings)) {
return new WP_Error(
'missing_settings',
'No settings provided',
['status' => 400]
);
}
try {
$result = PaymentGatewaysProvider::save_gateway_settings($gateway_id, $settings);
if (is_wp_error($result)) {
return $result;
}
// Return updated gateway data
$gateway = PaymentGatewaysProvider::get_gateway($gateway_id);
return rest_ensure_response([
'success' => true,
'message' => 'Gateway settings saved successfully',
'gateway' => $gateway,
]);
} catch (\Exception $e) {
return new WP_Error(
'save_gateway_failed',
$e->getMessage(),
['status' => 500]
);
}
}
/**
* Toggle gateway enabled status
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response|WP_Error Response object or error
*/
public function toggle_gateway(WP_REST_Request $request) {
$gateway_id = $request->get_param('id');
$enabled = $request->get_param('enabled');
if (!is_bool($enabled)) {
return new WP_Error(
'invalid_enabled_value',
'The "enabled" parameter must be a boolean',
['status' => 400]
);
}
try {
$result = PaymentGatewaysProvider::toggle_gateway($gateway_id, $enabled);
if (is_wp_error($result)) {
return $result;
}
// Clear cache before fetching updated gateway
wp_cache_flush();
// Return updated gateway data (fresh from DB)
$gateway = PaymentGatewaysProvider::get_gateway($gateway_id);
return rest_ensure_response([
'success' => true,
'message' => $enabled ? 'Gateway enabled' : 'Gateway disabled',
'gateway' => $gateway,
]);
} catch (\Exception $e) {
return new WP_Error(
'toggle_gateway_failed',
$e->getMessage(),
['status' => 500]
);
}
}
/**
* Check permission
*
* @return bool True if user has permission
*/
public function check_permission() {
return current_user_can('manage_woocommerce');
}
/**
* Get gateways collection schema
*
* @return array Schema
*/
public function get_gateways_schema() {
return [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'payment-gateways',
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'id' => [
'description' => 'Gateway ID',
'type' => 'string',
],
'title' => [
'description' => 'Gateway title',
'type' => 'string',
],
'description' => [
'description' => 'Gateway description',
'type' => 'string',
],
'enabled' => [
'description' => 'Whether gateway is enabled',
'type' => 'boolean',
],
'type' => [
'description' => 'Gateway type',
'type' => 'string',
'enum' => ['manual', 'provider', 'other'],
],
],
],
];
}
}