feat: Implement Payment Gateways backend foundation
✅ Phase 1 Backend Complete: 📦 PaymentGatewaysProvider.php: - Read WC gateways from WC()->payment_gateways() - Transform to clean JSON format - Categorize: manual/provider/other - Extract settings: basic/api/advanced - Check requirements (SSL, extensions) - Generate webhook URLs - Respect WC bone structure (WC_Payment_Gateway) 📡 PaymentsController.php: - GET /woonoow/v1/payments/gateways (list all) - GET /woonoow/v1/payments/gateways/{id} (single) - POST /woonoow/v1/payments/gateways/{id} (save settings) - POST /woonoow/v1/payments/gateways/{id}/toggle (enable/disable) - Permission checks (manage_woocommerce) - Error handling with proper HTTP codes - Response caching (5 min) 🔌 Integration: - Registered in Api/Routes.php - Auto-discovers all WC-compliant gateways - No new hooks - listens to WC structure 📋 Checklist Progress: - [x] PaymentGatewaysProvider.php - [x] PaymentsController.php - [x] REST API registration - [ ] Frontend components (next)
This commit is contained in:
290
includes/Api/PaymentsController.php
Normal file
290
includes/Api/PaymentsController.php
Normal file
@@ -0,0 +1,290 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
// Return updated gateway data
|
||||
$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'],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user