defined( 'SABLEPAY_API_KEY' ) ? (string) SABLEPAY_API_KEY : (string) ( $stored['api_key'] ?? '' ), 'merchant_id' => defined( 'SABLEPAY_MERCHANT_ID' ) ? (string) SABLEPAY_MERCHANT_ID : (string) ( $stored['merchant_id'] ?? '' ), 'base_url' => defined( 'SABLEPAY_BASE_URL' ) ? (string) SABLEPAY_BASE_URL : (string) ( $stored['base_url'] ?? '' ), 'enable_logging' => defined( 'SABLEPAY_ENABLE_LOGGING' ) ? (bool) SABLEPAY_ENABLE_LOGGING : ! empty( $stored['enable_logging'] ), ); /** * Filter the effective SablePay settings. * * @param array $settings Settings. */ return apply_filters( 'sablepay_settings', $settings ); } /** Whether a value is locked by a wp-config.php constant. */ private static function isLocked( $key ) { $map = array( 'api_key' => 'SABLEPAY_API_KEY', 'merchant_id' => 'SABLEPAY_MERCHANT_ID', 'base_url' => 'SABLEPAY_BASE_URL', 'enable_logging' => 'SABLEPAY_ENABLE_LOGGING', ); return isset( $map[ $key ] ) && defined( $map[ $key ] ); } /** Whether the API key and merchant ID look valid. */ public static function isConfigured() { $s = self::get(); return ApiKeyManager::isValidApiKey( trim( $s['api_key'] ) ) && ApiKeyManager::isValidUuid( trim( $s['merchant_id'] ) ); } /** * "sandbox" | "production" | "unknown" | null */ public static function environment() { $s = self::get(); return '' === $s['api_key'] ? null : SablePay::environmentForKey( $s['api_key'] ); } /** Human readable configuration error, or null when configured. */ public static function configError() { $s = self::get(); if ( '' === trim( $s['api_key'] ) || '' === trim( $s['merchant_id'] ) ) { return __( 'Missing configuration. Please set your SablePay API key and Merchant ID in Settings → SablePay.', 'sablepay' ); } if ( ! ApiKeyManager::isValidApiKey( trim( $s['api_key'] ) ) ) { return __( 'Invalid API key format. Expected: sable_sk_(live|sand)_[43 alphanumeric chars]', 'sablepay' ); } if ( ! ApiKeyManager::isValidUuid( trim( $s['merchant_id'] ) ) ) { return __( 'Invalid merchant ID format. Expected UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'sablepay' ); } return self::$initError; } /** * Initialize the PHP SDK from settings (idempotent). * * @return bool True when the SDK is ready. */ public static function initializeSdk() { if ( SablePay::isInitialized() ) { return true; } if ( ! self::isConfigured() ) { return false; } $s = self::get(); try { SablePay::initialize( array( 'apiKey' => trim( $s['api_key'] ), 'merchantId' => trim( $s['merchant_id'] ), 'baseUrl' => trim( $s['base_url'] ), 'enableLogging' => $s['enable_logging'], ) ); return true; } catch ( \Throwable $e ) { self::$initError = 'Initialization failed: ' . $e->getMessage(); return false; } } // ═══════════════════════════════════════════════════════════════════════ // Admin UI // ═══════════════════════════════════════════════════════════════════════ public static function addMenu() { add_options_page( __( 'SablePay', 'sablepay' ), __( 'SablePay', 'sablepay' ), 'manage_options', self::PAGE, array( __CLASS__, 'renderPage' ) ); } public static function registerSettings() { register_setting( 'sablepay', self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'default' => array(), 'show_in_rest' => false, ) ); add_settings_section( 'sablepay_credentials', __( 'API credentials', 'sablepay' ), array( __CLASS__, 'renderSectionIntro' ), self::PAGE ); add_settings_field( 'api_key', __( 'API Key', 'sablepay' ), array( __CLASS__, 'renderApiKeyField' ), self::PAGE, 'sablepay_credentials', array( 'label_for' => 'sablepay_api_key' ) ); add_settings_field( 'merchant_id', __( 'Merchant ID', 'sablepay' ), array( __CLASS__, 'renderTextField' ), self::PAGE, 'sablepay_credentials', array( 'key' => 'merchant_id', 'label_for' => 'sablepay_merchant_id', 'placeholder' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ) ); add_settings_field( 'base_url', __( 'API Base URL', 'sablepay' ), array( __CLASS__, 'renderTextField' ), self::PAGE, 'sablepay_credentials', array( 'key' => 'base_url', 'label_for' => 'sablepay_base_url', 'placeholder' => __( 'Auto (from API key)', 'sablepay' ), 'help' => __( 'Optional. Leave empty to use https://sandbox-api.sablepay.io/api/v1/ for sandbox keys and https://api.sablepay.io/api/v1/ for live keys.', 'sablepay' ) ) ); add_settings_field( 'enable_logging', __( 'Debug logging', 'sablepay' ), array( __CLASS__, 'renderLoggingField' ), self::PAGE, 'sablepay_credentials', array( 'label_for' => 'sablepay_enable_logging' ) ); } /** * Sanitize settings. An empty API key field keeps the stored key. */ public static function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $existing = get_option( self::OPTION, array() ); $existing = is_array( $existing ) ? $existing : array(); $apiKey = isset( $input['api_key'] ) ? trim( sanitize_text_field( wp_unslash( $input['api_key'] ) ) ) : ''; if ( '' === $apiKey ) { $apiKey = (string) ( $existing['api_key'] ?? '' ); } elseif ( ! ApiKeyManager::isValidApiKey( $apiKey ) ) { add_settings_error( self::OPTION, 'api_key', __( 'Invalid API key format. Expected: sable_sk_(live|sand)_[43 alphanumeric chars]', 'sablepay' ) ); $apiKey = (string) ( $existing['api_key'] ?? '' ); } $merchantId = isset( $input['merchant_id'] ) ? trim( sanitize_text_field( wp_unslash( $input['merchant_id'] ) ) ) : ''; if ( '' !== $merchantId && ! ApiKeyManager::isValidUuid( $merchantId ) ) { add_settings_error( self::OPTION, 'merchant_id', __( 'Invalid merchant ID format. Expected UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'sablepay' ) ); $merchantId = (string) ( $existing['merchant_id'] ?? '' ); } $baseUrl = isset( $input['base_url'] ) ? trim( esc_url_raw( wp_unslash( $input['base_url'] ), array( 'https', 'http' ) ) ) : ''; if ( '' !== $baseUrl && 0 !== strpos( $baseUrl, 'https://' ) ) { add_settings_error( self::OPTION, 'base_url', __( 'The API base URL must use HTTPS.', 'sablepay' ) ); $baseUrl = (string) ( $existing['base_url'] ?? '' ); } return array( 'api_key' => $apiKey, 'merchant_id' => $merchantId, 'base_url' => $baseUrl, 'enable_logging' => ! empty( $input['enable_logging'] ), ); } public static function renderSectionIntro() { echo '
' . esc_html__( 'Get your credentials from the SablePay dashboard (sandbox: https://sandbox.sablepay.io). The API key is stored on the server only — the browser talks to the WordPress REST proxy.', 'sablepay' ) . '
'; } private static function lockedNote( $key ) { if ( self::isLocked( $key ) ) { echo '' . esc_html__( 'Defined in wp-config.php — this field is read-only.', 'sablepay' ) . '
'; } } public static function renderApiKeyField() { $s = self::get(); $locked = self::isLocked( 'api_key' ); $masked = '' !== $s['api_key'] ? substr( $s['api_key'], 0, 14 ) . str_repeat( '•', 8 ) . substr( $s['api_key'], -4 ) : ''; printf( '', esc_attr( self::OPTION ), esc_attr( $masked ? $masked : 'sable_sk_sand_...' ), disabled( $locked, true, false ) ); echo '' . esc_html__( 'Sandbox: sable_sk_sand_… · Production: sable_sk_live_… Leave empty to keep the saved key.', 'sablepay' ) . '
'; self::lockedNote( 'api_key' ); } public static function renderTextField( $args ) { $s = self::get(); $key = $args['key']; printf( '', esc_attr( $args['label_for'] ), esc_attr( self::OPTION ), esc_attr( $key ), esc_attr( $s[ $key ] ), esc_attr( $args['placeholder'] ?? '' ), disabled( self::isLocked( $key ), true, false ) ); if ( ! empty( $args['help'] ) ) { echo '' . esc_html( $args['help'] ) . '
'; } self::lockedNote( $key ); } public static function renderLoggingField() { $s = self::get(); printf( '', esc_attr( self::OPTION ), checked( $s['enable_logging'], true, false ), disabled( self::isLocked( 'enable_logging' ), true, false ), esc_html__( 'Log SDK activity to the PHP error log and the browser console. Keep disabled in production.', 'sablepay' ) ); } public static function renderPage() { if ( ! current_user_can( 'manage_options' ) ) { return; } $configured = self::isConfigured(); $env = self::environment(); $notice = isset( $_GET['sablepay_test'] ) ? sanitize_key( wp_unslash( $_GET['sablepay_test'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $message = get_transient( 'sablepay_test_message_' . get_current_user_id() ); ?> | |
[sablepay_button amount="10.50" label="Pay with SablePay"]
SablePay.initializeFromWordPress(); SablePay.launchPayment({ amount: 10.5 }, { onSuccess(r) {}, onFailure(e) {} });
sablepay()->getPaymentStatus( $paymentId );