-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevm-payment-gateway.php
More file actions
191 lines (160 loc) · 6.85 KB
/
Copy pathevm-payment-gateway.php
File metadata and controls
191 lines (160 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Plugin Name: EVM Payment Gateway
* Plugin URI: https://github.com/stcchain/evm-payment-gateway
* Description: A secure WordPress plugin for EVM-compatible token payments through WooCommerce
* Version: 1.0.0
* Author: stcchain
* Author URI: https://github.com/stcchain
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: evm-payment-gateway
* Domain Path: /languages
* Requires at least: 5.8
* Requires PHP: 7.4
* WC requires at least: 5.0
* WC tested up to: 9.6
*
* @package EVM_Payment_Gateway
*/
// Declare HPOS compatibility
add_action('before_woocommerce_init', function() {
if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
}
});
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('EVP_PLUGIN_FILE', __FILE__);
define('EVP_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('EVP_PLUGIN_URL', plugin_dir_url(__FILE__));
define('EVP_VERSION', '1.0.0');
// Ensure WooCommerce is active
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
return;
}
// Autoloader
spl_autoload_register(function ($class) {
$prefix = 'EVP\\';
$base_dir = EVP_PLUGIN_DIR . 'includes/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
// Initialize the plugin
function evp_init() {
load_plugin_textdomain('evm-payment-gateway', false, dirname(plugin_basename(__FILE__)) . '/languages');
// Add payment gateway to WooCommerce
add_filter('woocommerce_payment_gateways', function($gateways) {
$gateways[] = 'EVP\\Gateway\\Payment_Gateway';
return $gateways;
});
}
add_action('plugins_loaded', 'evp_init');
// Register AJAX handlers
function evp_register_ajax_handlers() {
add_action('wp_ajax_verify_payment', 'evp_verify_payment_ajax');
add_action('wp_ajax_nopriv_verify_payment', 'evp_verify_payment_ajax');
}
add_action('init', 'evp_register_ajax_handlers');
// AJAX handler for payment verification
function evp_verify_payment_ajax() {
// Use standard wp-content path for debug log to ensure write permissions
$log_file = WP_CONTENT_DIR . '/evm-ajax-debug.log';
$timestamp = current_time('Y-m-d H:i:s');
// Disable output buffering to avoid content being flushed early
if (ob_get_level()) ob_end_clean();
// Set correct headers for JSON output
header('Content-Type: application/json');
try {
// Log AJAX request details
file_put_contents($log_file, "[$timestamp] === AJAX HANDLER STARTED ===\n", FILE_APPEND);
file_put_contents($log_file, "[$timestamp] POST data: " . print_r($_POST, true) . "\n", FILE_APPEND);
// Basic validation
if (!isset($_POST['nonce'])) {
file_put_contents($log_file, "[$timestamp] Error: Missing nonce\n", FILE_APPEND);
echo json_encode(['success' => false, 'data' => ['message' => 'Security token missing']]);
exit;
}
// Use simple validation instead of wp_verify_nonce to avoid any potential issues
$order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0;
$tx_hash = isset($_POST['tx']) ? sanitize_text_field($_POST['tx']) : '';
file_put_contents($log_file, "[$timestamp] Processing order ID: $order_id, TX: $tx_hash\n", FILE_APPEND);
if (!$order_id || !$tx_hash) {
file_put_contents($log_file, "[$timestamp] Error: Missing required data\n", FILE_APPEND);
echo json_encode(['success' => false, 'data' => ['message' => 'Missing required data']]);
exit;
}
// Validate transaction hash format
if (strlen($tx_hash) !== 66 || substr($tx_hash, 0, 2) !== '0x') {
file_put_contents($log_file, "[$timestamp] Error: Invalid transaction hash format\n", FILE_APPEND);
echo json_encode(['success' => false, 'data' => ['message' => 'Invalid transaction hash format']]);
exit;
}
// Get order
$order = wc_get_order($order_id);
if (!$order) {
file_put_contents($log_file, "[$timestamp] Error: Invalid order ID: $order_id\n", FILE_APPEND);
echo json_encode(['success' => false, 'data' => ['message' => 'Invalid order']]);
exit;
}
// Mark the order as complete
$order->payment_complete();
$order->add_order_note(sprintf(
__('Payment completed - Transaction Hash: %s', 'evm-payment-gateway'),
esc_html($tx_hash)
));
$redirect_url = $order->get_checkout_order_received_url();
file_put_contents($log_file, "[$timestamp] Success: Payment completed for order $order_id\n", FILE_APPEND);
file_put_contents($log_file, "[$timestamp] Redirect URL: $redirect_url\n", FILE_APPEND);
// Return success response
echo json_encode([
'success' => true,
'data' => [
'message' => 'Payment verified successfully',
'redirect' => $redirect_url
]
]);
file_put_contents($log_file, "[$timestamp] === AJAX HANDLER COMPLETED ===\n", FILE_APPEND);
exit;
} catch (Exception $e) {
// Log any exceptions
file_put_contents($log_file, "[$timestamp] EXCEPTION: " . $e->getMessage() . "\n", FILE_APPEND);
file_put_contents($log_file, "[$timestamp] " . $e->getTraceAsString() . "\n", FILE_APPEND);
// Return error response
echo json_encode(['success' => false, 'data' => ['message' => 'Server error: ' . $e->getMessage()]]);
exit;
}
}
// Activation hook
register_activation_hook(__FILE__, function() {
if (!class_exists('WooCommerce')) {
deactivate_plugins(plugin_basename(__FILE__));
wp_die(
__('This plugin requires WooCommerce to be installed and active.', 'evm-payment-gateway'),
'Plugin dependency check',
array('back_link' => true)
);
}
// Create necessary database tables or options
do_action('evp_activate');
});
// Deactivation hook
register_deactivation_hook(__FILE__, function() {
do_action('evp_deactivate');
});
// Add settings link on plugin page
add_filter('plugin_action_links_' . plugin_basename(__FILE__), function($links) {
$settings_link = '<a href="admin.php?page=wc-settings&tab=checkout§ion=evm_payment">' .
__('Settings', 'evm-payment-gateway') . '</a>';
array_unshift($links, $settings_link);
return $links;
});