-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathview-webhook-queue.php
More file actions
244 lines (210 loc) · 10.1 KB
/
view-webhook-queue.php
File metadata and controls
244 lines (210 loc) · 10.1 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* View Webhook Queue Table - Direct Database Access
*
* This script allows you to view the webhook queue table directly.
*
* Usage:
* 1. Via browser: Place in WordPress root and access via browser
* 2. Via command line: php view-webhook-queue.php
* 3. Via WP-CLI: wp eval-file view-webhook-queue.php
*/
// Load WordPress to get database connection
require_once __DIR__ . '/wp-load.php';
global $wpdb;
$table_name = $wpdb->prefix . 'cko_pending_webhooks';
// Check if running from command line
$is_cli = php_sapi_name() === 'cli';
// Require admin capability for browser access
if ( ! $is_cli ) {
if ( ! is_user_logged_in() ) {
wp_die( esc_html__( 'You must be logged in to view this page.', 'checkout-com-unified-payments-api' ) );
}
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'You do not have permission to view this page.', 'checkout-com-unified-payments-api' ) );
}
}
if ($is_cli) {
// Command line output
echo "========================================\n";
echo "Webhook Queue Table Viewer\n";
echo "========================================\n\n";
// Check if table exists — use prepare() for the LIKE value; table name is a fixed internal constant.
$safe_table = esc_sql( $table_name );
$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) );
if (!$table_exists) {
echo "Table '$safe_table' does not exist.\n";
exit(1);
}
// Get statistics
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- $safe_table is an internal fixed constant, not user input.
$total = $wpdb->get_var( "SELECT COUNT(*) FROM `{$safe_table}`" );
$pending = $wpdb->get_var( "SELECT COUNT(*) FROM `{$safe_table}` WHERE processed_at IS NULL" );
$processed = $wpdb->get_var( "SELECT COUNT(*) FROM `{$safe_table}` WHERE processed_at IS NOT NULL" );
// phpcs:enable
echo "Statistics:\n";
echo " Total: $total\n";
echo " Pending: $pending\n";
echo " Processed: $processed\n\n";
// Get records
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $safe_table is an internal fixed constant, not user input.
$records = $wpdb->get_results( "SELECT * FROM `{$safe_table}` ORDER BY created_at DESC LIMIT 20" );
if (empty($records)) {
echo "No webhooks found.\n";
exit(0);
}
echo "Recent Webhooks (showing latest 20):\n";
echo str_repeat("=", 120) . "\n";
foreach ($records as $record) {
echo "ID: {$record->id}\n";
echo " Payment ID: {$record->payment_id}\n";
echo " Order ID: " . ($record->order_id ?: 'N/A') . "\n";
echo " Payment Session ID: " . ($record->payment_session_id ?: 'N/A') . "\n";
echo " Type: {$record->webhook_type}\n";
echo " Status: " . ($record->processed_at ? 'Processed' : 'Pending') . "\n";
echo " Created: {$record->created_at}\n";
if ($record->processed_at) {
echo " Processed: {$record->processed_at}\n";
}
// Show webhook data preview
$data = json_decode($record->webhook_data);
echo " Webhook Data: " . substr(json_encode($data, JSON_PRETTY_PRINT), 0, 200) . "...\n";
echo str_repeat("-", 120) . "\n";
}
echo "\nTo view full webhook data, use SQL query:\n";
echo "SELECT webhook_data FROM $table_name WHERE id = [ID];\n";
} else {
// Browser output
?>
<!DOCTYPE html>
<html>
<head>
<title>Webhook Queue - Direct Access</title>
<style>
body { font-family: monospace; margin: 20px; background: #f5f5f5; }
.container { background: white; padding: 20px; border-radius: 8px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th { background: #333; color: white; padding: 10px; text-align: left; }
td { padding: 8px; border-bottom: 1px solid #ddd; }
.pending { color: #f0b849; font-weight: bold; }
.processed { color: #00a32a; font-weight: bold; }
.stats { display: flex; gap: 20px; margin: 20px 0; }
.stat { padding: 15px; background: #f0f0f0; border-radius: 4px; }
pre { background: #f5f5f5; padding: 10px; overflow-x: auto; font-size: 11px; }
</style>
</head>
<body>
<div class="container">
<h1>Webhook Queue Table - Direct Access</h1>
<?php
// Check if table exists — $table_name is a fixed internal constant, not user input.
$safe_table = esc_sql( $table_name );
$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) );
if (!$table_exists) {
echo '<p style="color: red;">Table does not exist.</p>';
exit;
}
// Get statistics
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- $safe_table is an internal fixed constant, not user input.
$total = $wpdb->get_var( "SELECT COUNT(*) FROM `{$safe_table}`" );
$pending = $wpdb->get_var( "SELECT COUNT(*) FROM `{$safe_table}` WHERE processed_at IS NULL" );
$processed = $wpdb->get_var( "SELECT COUNT(*) FROM `{$safe_table}` WHERE processed_at IS NOT NULL" );
// phpcs:enable
?>
<div class="stats">
<div class="stat">
<strong>Total:</strong> <?php echo esc_html($total); ?>
</div>
<div class="stat">
<strong>Pending:</strong> <span class="pending"><?php echo esc_html($pending); ?></span>
</div>
<div class="stat">
<strong>Processed:</strong> <span class="processed"><?php echo esc_html($processed); ?></span>
</div>
</div>
<h2>Recent Webhooks</h2>
<?php
// Get records
$limit = isset($_GET['limit']) ? absint($_GET['limit']) : 50;
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $safe_table is an internal fixed constant, not user input.
$records = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM `{$safe_table}` ORDER BY created_at DESC LIMIT %d",
$limit
) );
if (empty($records)) {
echo "<p>No webhooks found.</p>";
} else {
?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Payment ID</th>
<th>Order ID</th>
<th>Session ID</th>
<th>Type</th>
<th>Status</th>
<th>Created</th>
<th>Processed</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($records as $record): ?>
<tr>
<td><?php echo esc_html($record->id); ?></td>
<td><code><?php echo esc_html($record->payment_id); ?></code></td>
<td><?php echo esc_html($record->order_id ?: '—'); ?></td>
<td><code><?php echo esc_html($record->payment_session_id ?: '—'); ?></code></td>
<td><?php echo esc_html($record->webhook_type); ?></td>
<td>
<?php if ($record->processed_at): ?>
<span class="processed">Processed</span>
<?php else: ?>
<span class="pending">Pending</span>
<?php endif; ?>
</td>
<td><?php echo esc_html($record->created_at); ?></td>
<td><?php echo esc_html($record->processed_at ?: '—'); ?></td>
<td>
<a href="?view=<?php echo esc_attr($record->id); ?>">View JSON</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if (isset($_GET['view'])):
$view_id = absint($_GET['view']);
$webhook = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $view_id));
if ($webhook):
?>
<h3>Full Webhook Data (ID: <?php echo esc_html($view_id); ?>)</h3>
<pre><?php echo esc_html(json_encode(json_decode($webhook->webhook_data), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); ?></pre>
<p><a href="?">← Back to list</a></p>
<?php endif; endif; ?>
<p>
<a href="?limit=100">Show 100</a> |
<a href="?limit=200">Show 200</a> |
<a href="?limit=500">Show 500</a>
</p>
<?php } ?>
<hr>
<h3>SQL Queries</h3>
<p>You can also run these SQL queries directly:</p>
<pre>
-- View all pending webhooks
SELECT * FROM <?php echo esc_html($table_name); ?> WHERE processed_at IS NULL ORDER BY created_at DESC;
-- View all processed webhooks
SELECT * FROM <?php echo esc_html($table_name); ?> WHERE processed_at IS NOT NULL ORDER BY processed_at DESC;
-- View webhook by payment ID
SELECT * FROM <?php echo esc_html($table_name); ?> WHERE payment_id = 'pay_xxx';
-- View webhook by order ID
SELECT * FROM <?php echo esc_html($table_name); ?> WHERE order_id = '12345';
-- Count by type
SELECT webhook_type, COUNT(*) as count FROM <?php echo esc_html($table_name); ?> GROUP BY webhook_type;
</pre>
</div>
</body>
</html>
<?php
}