232 lines
7.3 KiB
PHP
232 lines
7.3 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* Plugin Name: Eagle Booking - Redsys Payment Gateway
|
||
|
|
* Plugin URI: https://informatiq.services
|
||
|
|
* Description: Redsys payment gateway integration for Eagle Booking
|
||
|
|
* Version: 1.0.0
|
||
|
|
* Author: Mălin Cenușă
|
||
|
|
* Author URI: https://mălin.ro
|
||
|
|
* Text Domain: eb-redsys-gateway
|
||
|
|
* Domain Path: /languages
|
||
|
|
* Requires at least: 5.0
|
||
|
|
* Requires PHP: 7.2
|
||
|
|
*/
|
||
|
|
|
||
|
|
defined('ABSPATH') || exit;
|
||
|
|
|
||
|
|
class EB_Redsys_Gateway {
|
||
|
|
|
||
|
|
private static $instance = null;
|
||
|
|
|
||
|
|
public static function instance() {
|
||
|
|
if (is_null(self::$instance)) {
|
||
|
|
self::$instance = new self();
|
||
|
|
}
|
||
|
|
return self::$instance;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function __construct() {
|
||
|
|
// Define constants
|
||
|
|
$this->define_constants();
|
||
|
|
|
||
|
|
// Initialize plugin
|
||
|
|
add_action('plugins_loaded', array($this, 'init'));
|
||
|
|
|
||
|
|
// Register activation and deactivation hooks
|
||
|
|
register_activation_hook(__FILE__, array($this, 'activate'));
|
||
|
|
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
|
||
|
|
|
||
|
|
// Add plugin action links
|
||
|
|
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_action_links'));
|
||
|
|
}
|
||
|
|
|
||
|
|
private function define_constants() {
|
||
|
|
define('EB_REDSYS_VERSION', '1.0.0');
|
||
|
|
define('EB_REDSYS_FILE', __FILE__);
|
||
|
|
define('EB_REDSYS_PATH', plugin_dir_path(__FILE__));
|
||
|
|
define('EB_REDSYS_URL', plugin_dir_url(__FILE__));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function init() {
|
||
|
|
// Check if Eagle Booking is active
|
||
|
|
if (!class_exists('Eagle_Booking')) {
|
||
|
|
add_action('admin_notices', array($this, 'eagle_booking_missing_notice'));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Load text domain
|
||
|
|
load_plugin_textdomain('eb-redsys-gateway', false, dirname(plugin_basename(__FILE__)) . '/languages');
|
||
|
|
|
||
|
|
// Include required files
|
||
|
|
$this->includes();
|
||
|
|
|
||
|
|
// Initialize admin
|
||
|
|
if (is_admin()) {
|
||
|
|
$this->init_admin();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Add payment gateway hooks
|
||
|
|
$this->add_gateway_hooks();
|
||
|
|
}
|
||
|
|
|
||
|
|
private function includes() {
|
||
|
|
// API files
|
||
|
|
require_once EB_REDSYS_PATH . 'includes/api/class-redsys-api.php';
|
||
|
|
require_once EB_REDSYS_PATH . 'includes/api/apiRedsysFinal.php';
|
||
|
|
require_once EB_REDSYS_PATH . 'includes/api/RESTConstants.php';
|
||
|
|
require_once EB_REDSYS_PATH . 'includes/api/hash.php';
|
||
|
|
require_once EB_REDSYS_PATH . 'includes/api/hmac.php';
|
||
|
|
require_once EB_REDSYS_PATH . 'includes/api/sha256.php';
|
||
|
|
|
||
|
|
// Gateway files
|
||
|
|
require_once EB_REDSYS_PATH . 'includes/gateway/class-eb-redsys-gateway.php';
|
||
|
|
require_once EB_REDSYS_PATH . 'includes/gateway/payment-functions.php';
|
||
|
|
}
|
||
|
|
|
||
|
|
private function init_admin() {
|
||
|
|
require_once EB_REDSYS_PATH . 'includes/admin/settings.php';
|
||
|
|
new EB_Redsys_Settings();
|
||
|
|
}
|
||
|
|
|
||
|
|
private function add_gateway_hooks() {
|
||
|
|
// Add payment method to Eagle Booking
|
||
|
|
add_filter('eb_payment_methods', array($this, 'add_payment_method'));
|
||
|
|
|
||
|
|
// Add payment gateway tabs and content
|
||
|
|
add_action('eb_payment_gateway_tabs', array($this, 'add_payment_tab'));
|
||
|
|
add_action('eb_payment_gateway_content', array($this, 'add_payment_tab_content'));
|
||
|
|
|
||
|
|
// Process payment
|
||
|
|
add_action('eb_process_payment_redsys', array($this, 'process_payment'));
|
||
|
|
|
||
|
|
// Handle Redsys notifications
|
||
|
|
add_action('init', array($this, 'handle_redsys_notification'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function add_payment_method($methods) {
|
||
|
|
if (get_option('eb_redsys_enabled') === 'yes') {
|
||
|
|
$methods['redsys'] = array(
|
||
|
|
'title' => get_option('eb_redsys_title', __('Credit Card (Redsys)', 'eb-redsys-gateway')),
|
||
|
|
'description' => get_option('eb_redsys_description'),
|
||
|
|
'action_type' => 'redsys'
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return $methods;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function add_payment_tab() {
|
||
|
|
if (get_option('eb_redsys_enabled') === 'yes') {
|
||
|
|
include EB_REDSYS_PATH . 'includes/gateway/tab.php';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function add_payment_tab_content() {
|
||
|
|
if (get_option('eb_redsys_enabled') === 'yes') {
|
||
|
|
include EB_REDSYS_PATH . 'includes/gateway/tab-content.php';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function process_payment($booking_data) {
|
||
|
|
try {
|
||
|
|
$payment = eb_redsys_payment();
|
||
|
|
return $payment->process_payment($booking_data);
|
||
|
|
} catch (Exception $e) {
|
||
|
|
eb_log('Redsys Payment Error: ' . $e->getMessage());
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function handle_redsys_notification() {
|
||
|
|
if (isset($_GET['eb-api']) && $_GET['eb-api'] === 'redsys') {
|
||
|
|
try {
|
||
|
|
$payment = eb_redsys_payment();
|
||
|
|
$result = $payment->process_notification($_POST);
|
||
|
|
|
||
|
|
if ($result['success']) {
|
||
|
|
// Update booking status
|
||
|
|
eb_update_booking_status(
|
||
|
|
$result['booking_id'],
|
||
|
|
'completed',
|
||
|
|
$result['transaction_id']
|
||
|
|
);
|
||
|
|
echo "OK";
|
||
|
|
} else {
|
||
|
|
eb_log('Redsys Notification Error: ' . $result['error']);
|
||
|
|
echo "KO";
|
||
|
|
}
|
||
|
|
} catch (Exception $e) {
|
||
|
|
eb_log('Redsys Notification Exception: ' . $e->getMessage());
|
||
|
|
echo "KO";
|
||
|
|
}
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function activate() {
|
||
|
|
// Check if Eagle Booking is active
|
||
|
|
if (!class_exists('Eagle_Booking')) {
|
||
|
|
deactivate_plugins(plugin_basename(__FILE__));
|
||
|
|
wp_die(
|
||
|
|
__('Please install and activate Eagle Booking before activating this plugin.', 'eb-redsys-gateway'),
|
||
|
|
'Plugin dependency check',
|
||
|
|
array('back_link' => true)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Default settings
|
||
|
|
add_option('eb_redsys_enabled', 'yes');
|
||
|
|
add_option('eb_redsys_test_mode', 'yes');
|
||
|
|
add_option('eb_redsys_title', __('Credit Card (Redsys)', 'eb-redsys-gateway'));
|
||
|
|
add_option('eb_redsys_description', __('Pay securely via credit card using Redsys.', 'eb-redsys-gateway'));
|
||
|
|
|
||
|
|
// Flush rewrite rules
|
||
|
|
flush_rewrite_rules();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function deactivate() {
|
||
|
|
flush_rewrite_rules();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function add_action_links($links) {
|
||
|
|
$settings_link = '<a href="' . admin_url('admin.php?page=eb-redsys-settings') . '">' .
|
||
|
|
__('Settings', 'eb-redsys-gateway') . '</a>';
|
||
|
|
array_unshift($links, $settings_link);
|
||
|
|
return $links;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function eagle_booking_missing_notice() {
|
||
|
|
?>
|
||
|
|
<div class="notice notice-error">
|
||
|
|
<p>
|
||
|
|
<?php _e('Eagle Booking - Redsys Payment Gateway requires Eagle Booking plugin to be installed and activated.', 'eb-redsys-gateway'); ?>
|
||
|
|
</p>
|
||
|
|
</div>
|
||
|
|
<?php
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get plugin version
|
||
|
|
*/
|
||
|
|
public function get_version() {
|
||
|
|
return EB_REDSYS_VERSION;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Log helper
|
||
|
|
*/
|
||
|
|
public static function log($message, $level = 'info') {
|
||
|
|
if (function_exists('eb_log')) {
|
||
|
|
eb_log('[Redsys] ' . $message, $level);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Main instance of plugin
|
||
|
|
*/
|
||
|
|
function EB_Redsys() {
|
||
|
|
return EB_Redsys_Gateway::instance();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Initialize the plugin
|
||
|
|
EB_Redsys();
|