✅ 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)
389 lines
13 KiB
PHP
389 lines
13 KiB
PHP
<?php
|
|
/**
|
|
* Payment Gateways Provider
|
|
*
|
|
* Reads WooCommerce payment gateways and transforms them into clean format for SPA.
|
|
* Respects WooCommerce's bone structure - if gateway extends WC_Payment_Gateway, we support it.
|
|
*
|
|
* @package WooNooW
|
|
*/
|
|
|
|
namespace WooNooW\Compat;
|
|
|
|
use WC_Payment_Gateway;
|
|
|
|
class PaymentGatewaysProvider {
|
|
|
|
/**
|
|
* Get all registered payment gateways
|
|
*
|
|
* @return array List of gateways in clean format
|
|
*/
|
|
public static function get_gateways(): array {
|
|
if (!function_exists('WC')) {
|
|
return [];
|
|
}
|
|
|
|
$wc_gateways = WC()->payment_gateways()->payment_gateways();
|
|
$gateways = [];
|
|
|
|
foreach ($wc_gateways as $gateway) {
|
|
// Only support gateways that extend WC_Payment_Gateway (respect the bone)
|
|
if (!$gateway instanceof WC_Payment_Gateway) {
|
|
continue;
|
|
}
|
|
|
|
$gateways[] = self::transform_gateway($gateway);
|
|
}
|
|
|
|
return $gateways;
|
|
}
|
|
|
|
/**
|
|
* Get single gateway by ID
|
|
*
|
|
* @param string $gateway_id Gateway ID
|
|
* @return array|null Gateway data or null if not found
|
|
*/
|
|
public static function get_gateway(string $gateway_id): ?array {
|
|
if (!function_exists('WC')) {
|
|
return null;
|
|
}
|
|
|
|
$wc_gateways = WC()->payment_gateways()->payment_gateways();
|
|
|
|
if (!isset($wc_gateways[$gateway_id])) {
|
|
return null;
|
|
}
|
|
|
|
$gateway = $wc_gateways[$gateway_id];
|
|
|
|
if (!$gateway instanceof WC_Payment_Gateway) {
|
|
return null;
|
|
}
|
|
|
|
return self::transform_gateway($gateway);
|
|
}
|
|
|
|
/**
|
|
* Transform WooCommerce gateway to clean format
|
|
*
|
|
* @param WC_Payment_Gateway $gateway WooCommerce gateway instance
|
|
* @return array Clean gateway data
|
|
*/
|
|
private static function transform_gateway(WC_Payment_Gateway $gateway): array {
|
|
$type = self::categorize_gateway($gateway->id);
|
|
$requirements = self::check_requirements($gateway);
|
|
$settings = self::get_gateway_settings($gateway);
|
|
|
|
return [
|
|
'id' => $gateway->id,
|
|
'title' => $gateway->get_title(),
|
|
'description' => $gateway->get_description(),
|
|
'enabled' => $gateway->enabled === 'yes',
|
|
'type' => $type,
|
|
'icon' => self::get_gateway_icon($gateway->id),
|
|
'method_title' => $gateway->get_method_title(),
|
|
'method_description' => $gateway->get_method_description(),
|
|
'supports' => $gateway->supports,
|
|
'requirements' => $requirements,
|
|
'settings' => $settings,
|
|
'has_fields' => $gateway->has_fields(),
|
|
'countries' => $gateway->countries ?? null,
|
|
'availability' => $gateway->availability ?? 'all',
|
|
'order_button_text' => $gateway->order_button_text ?? null,
|
|
'webhook_url' => self::get_webhook_url($gateway->id),
|
|
'has_custom_ui' => self::has_custom_ui($gateway->id),
|
|
'wc_settings_url' => admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . $gateway->id),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Categorize gateway into type
|
|
*
|
|
* @param string $gateway_id Gateway ID
|
|
* @return string Type: manual, provider, or other
|
|
*/
|
|
private static function categorize_gateway(string $gateway_id): string {
|
|
// Manual payment methods
|
|
$manual = ['bacs', 'cheque', 'cod'];
|
|
if (in_array($gateway_id, $manual, true)) {
|
|
return 'manual';
|
|
}
|
|
|
|
// Recognized payment providers
|
|
$providers = ['stripe', 'paypal', 'stripe_cc', 'ppec_paypal', 'square', 'authorize_net'];
|
|
if (in_array($gateway_id, $providers, true)) {
|
|
return 'provider';
|
|
}
|
|
|
|
// Other WC-compliant gateways
|
|
return 'other';
|
|
}
|
|
|
|
/**
|
|
* Get gateway icon name for UI
|
|
*
|
|
* @param string $gateway_id Gateway ID
|
|
* @return string Icon name (Lucide icon)
|
|
*/
|
|
private static function get_gateway_icon(string $gateway_id): string {
|
|
$icons = [
|
|
'bacs' => 'banknote',
|
|
'cheque' => 'banknote',
|
|
'cod' => 'banknote',
|
|
'stripe' => 'credit-card',
|
|
'stripe_cc' => 'credit-card',
|
|
'paypal' => 'credit-card',
|
|
'ppec_paypal' => 'credit-card',
|
|
'square' => 'credit-card',
|
|
'authorize_net' => 'credit-card',
|
|
];
|
|
|
|
return $icons[$gateway_id] ?? 'credit-card';
|
|
}
|
|
|
|
/**
|
|
* Check gateway requirements
|
|
*
|
|
* @param WC_Payment_Gateway $gateway Gateway instance
|
|
* @return array Requirements status
|
|
*/
|
|
private static function check_requirements(WC_Payment_Gateway $gateway): array {
|
|
$requirements = [
|
|
'met' => true,
|
|
'missing' => [],
|
|
];
|
|
|
|
// Check if gateway has custom requirements method
|
|
if (method_exists($gateway, 'get_requirements')) {
|
|
$reqs = $gateway->get_requirements();
|
|
if (is_array($reqs)) {
|
|
foreach ($reqs as $req => $met) {
|
|
if (!$met) {
|
|
$requirements['met'] = false;
|
|
$requirements['missing'][] = $req;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Common requirements checks
|
|
// Check SSL for gateways that need it
|
|
if (in_array('tokenization', $gateway->supports ?? [], true)) {
|
|
if (!is_ssl() && get_option('woocommerce_force_ssl_checkout') !== 'yes') {
|
|
$requirements['met'] = false;
|
|
$requirements['missing'][] = 'SSL certificate required';
|
|
}
|
|
}
|
|
|
|
return $requirements;
|
|
}
|
|
|
|
/**
|
|
* Get gateway settings fields
|
|
*
|
|
* @param WC_Payment_Gateway $gateway Gateway instance
|
|
* @return array Categorized settings fields
|
|
*/
|
|
private static function get_gateway_settings(WC_Payment_Gateway $gateway): array {
|
|
$form_fields = $gateway->get_form_fields();
|
|
|
|
return [
|
|
'basic' => self::extract_basic_fields($form_fields),
|
|
'api' => self::extract_api_fields($form_fields),
|
|
'advanced' => self::extract_advanced_fields($form_fields),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Extract basic settings fields
|
|
*
|
|
* @param array $form_fields All form fields
|
|
* @return array Basic fields
|
|
*/
|
|
private static function extract_basic_fields(array $form_fields): array {
|
|
$basic_keys = ['enabled', 'title', 'description', 'instructions'];
|
|
return self::filter_fields($form_fields, $basic_keys);
|
|
}
|
|
|
|
/**
|
|
* Extract API-related fields
|
|
*
|
|
* @param array $form_fields All form fields
|
|
* @return array API fields
|
|
*/
|
|
private static function extract_api_fields(array $form_fields): array {
|
|
$api_patterns = ['key', 'secret', 'token', 'api', 'client', 'merchant', 'account'];
|
|
$api_fields = [];
|
|
|
|
foreach ($form_fields as $key => $field) {
|
|
foreach ($api_patterns as $pattern) {
|
|
if (stripos($key, $pattern) !== false) {
|
|
$api_fields[$key] = self::normalize_field($key, $field);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $api_fields;
|
|
}
|
|
|
|
/**
|
|
* Extract advanced settings fields
|
|
*
|
|
* @param array $form_fields All form fields
|
|
* @return array Advanced fields
|
|
*/
|
|
private static function extract_advanced_fields(array $form_fields): array {
|
|
$basic_keys = ['enabled', 'title', 'description', 'instructions'];
|
|
$advanced_fields = [];
|
|
|
|
foreach ($form_fields as $key => $field) {
|
|
// Skip basic fields
|
|
if (in_array($key, $basic_keys, true)) {
|
|
continue;
|
|
}
|
|
|
|
// Skip API fields (already extracted)
|
|
$api_patterns = ['key', 'secret', 'token', 'api', 'client', 'merchant', 'account'];
|
|
$is_api = false;
|
|
foreach ($api_patterns as $pattern) {
|
|
if (stripos($key, $pattern) !== false) {
|
|
$is_api = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$is_api) {
|
|
$advanced_fields[$key] = self::normalize_field($key, $field);
|
|
}
|
|
}
|
|
|
|
return $advanced_fields;
|
|
}
|
|
|
|
/**
|
|
* Filter fields by keys
|
|
*
|
|
* @param array $form_fields All fields
|
|
* @param array $keys Keys to extract
|
|
* @return array Filtered fields
|
|
*/
|
|
private static function filter_fields(array $form_fields, array $keys): array {
|
|
$filtered = [];
|
|
|
|
foreach ($keys as $key) {
|
|
if (isset($form_fields[$key])) {
|
|
$filtered[$key] = self::normalize_field($key, $form_fields[$key]);
|
|
}
|
|
}
|
|
|
|
return $filtered;
|
|
}
|
|
|
|
/**
|
|
* Normalize field to clean format
|
|
*
|
|
* @param string $key Field key
|
|
* @param array $field Field data
|
|
* @return array Normalized field
|
|
*/
|
|
private static function normalize_field(string $key, array $field): array {
|
|
return [
|
|
'id' => $key,
|
|
'type' => $field['type'] ?? 'text',
|
|
'title' => $field['title'] ?? '',
|
|
'description' => $field['description'] ?? '',
|
|
'default' => $field['default'] ?? '',
|
|
'placeholder' => $field['placeholder'] ?? '',
|
|
'required' => !empty($field['required']),
|
|
'options' => $field['options'] ?? null,
|
|
'custom_attributes' => $field['custom_attributes'] ?? [],
|
|
'class' => $field['class'] ?? '',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get webhook URL for gateway
|
|
*
|
|
* @param string $gateway_id Gateway ID
|
|
* @return string|null Webhook URL or null
|
|
*/
|
|
private static function get_webhook_url(string $gateway_id): ?string {
|
|
// Common webhook URL pattern for WooCommerce
|
|
return home_url('/wc-api/' . $gateway_id);
|
|
}
|
|
|
|
/**
|
|
* Check if gateway has custom UI component
|
|
*
|
|
* @param string $gateway_id Gateway ID
|
|
* @return bool True if has custom UI
|
|
*/
|
|
private static function has_custom_ui(string $gateway_id): bool {
|
|
// For now, only Stripe and PayPal will have custom UI (Phase 2)
|
|
$custom_ui_gateways = ['stripe', 'paypal', 'stripe_cc', 'ppec_paypal'];
|
|
return in_array($gateway_id, $custom_ui_gateways, true);
|
|
}
|
|
|
|
/**
|
|
* Save gateway settings
|
|
*
|
|
* @param string $gateway_id Gateway ID
|
|
* @param array $settings Settings to save
|
|
* @return bool|WP_Error True on success, WP_Error on failure
|
|
*/
|
|
public static function save_gateway_settings(string $gateway_id, array $settings) {
|
|
if (!function_exists('WC')) {
|
|
return new \WP_Error('wc_not_available', 'WooCommerce is not available');
|
|
}
|
|
|
|
$wc_gateways = WC()->payment_gateways()->payment_gateways();
|
|
|
|
if (!isset($wc_gateways[$gateway_id])) {
|
|
return new \WP_Error('gateway_not_found', 'Gateway not found');
|
|
}
|
|
|
|
$gateway = $wc_gateways[$gateway_id];
|
|
|
|
if (!$gateway instanceof WC_Payment_Gateway) {
|
|
return new \WP_Error('invalid_gateway', 'Gateway does not extend WC_Payment_Gateway');
|
|
}
|
|
|
|
// Merge with existing settings
|
|
$current_settings = get_option($gateway->get_option_key(), []);
|
|
$new_settings = array_merge($current_settings, $settings);
|
|
|
|
// Use gateway's own validation if available
|
|
if (method_exists($gateway, 'validate_settings_fields')) {
|
|
$gateway->init_settings();
|
|
foreach ($new_settings as $key => $value) {
|
|
$gateway->settings[$key] = $value;
|
|
}
|
|
$gateway->process_admin_options();
|
|
} else {
|
|
// Direct save (faster)
|
|
update_option($gateway->get_option_key(), $new_settings, 'yes');
|
|
}
|
|
|
|
// Clear cache
|
|
wp_cache_delete('woocommerce_payment_gateways', 'options');
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Toggle gateway enabled status
|
|
*
|
|
* @param string $gateway_id Gateway ID
|
|
* @param bool $enabled Enable or disable
|
|
* @return bool|WP_Error True on success, WP_Error on failure
|
|
*/
|
|
public static function toggle_gateway(string $gateway_id, bool $enabled) {
|
|
return self::save_gateway_settings($gateway_id, [
|
|
'enabled' => $enabled ? 'yes' : 'no',
|
|
]);
|
|
}
|
|
}
|