/* Plugin Name: Payit CRM Description: Handles Payit payments and webhook. Version: 1.2 */ error_log('PAYIT PLUGIN LOADED'); register_activation_hook(__FILE__, 'payit_create_table'); function payit_create_table() { global $wpdb; $table = $wpdb->prefix . 'payit_payments'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, plg_reference VARCHAR(255) NOT NULL, amount DECIMAL(10,2) DEFAULT 0, status VARCHAR(50) DEFAULT 'PENDING', created_at DATETIME DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY plg_reference (plg_reference) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta($sql); } /* |-------------------------------------------------------------------------- | ADMIN MENU |-------------------------------------------------------------------------- */ add_action('rest_api_init', function () { register_rest_route('payit/v1', '/test', [ 'methods' => 'GET', 'callback' => function () { return ['ok' => true, 'time' => time()]; } ]); }); add_action('admin_menu', function () { add_menu_page( 'Payit Payments', 'Payit Payments', 'manage_options', 'payit-payments', 'payit_payments_page', 'dashicons-money', 25 ); }); function payit_payments_page() { global $wpdb; $table = $wpdb->prefix . 'payit_payments'; $payments = $wpdb->get_results("SELECT * FROM $table ORDER BY created_at DESC"); echo '

Payit Payments

'; echo ''; echo ''; if ($payments) { foreach ($payments as $p) { echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } } else { echo ''; } echo '
Reference Amount Status Created
' . esc_html($p->plg_reference) . '£' . esc_html(number_format((float)$p->amount, 2)) . '' . esc_html($p->status) . '' . esc_html($p->created_at) . '
No payments found.
'; } /* |-------------------------------------------------------------------------- | WEBHOOK ENDPOINT |-------------------------------------------------------------------------- | NatWest should call: | https://elcuk.com/wp-json/payit/v1/webhook |-------------------------------------------------------------------------- */ add_action('rest_api_init', function () { // 1. Base webhook (your original endpoint) register_rest_route('payit/v1', '/webhook', [ 'methods' => ['POST'], 'callback' => 'payit_webhook_handler', 'permission_callback' => '__return_true', ]); // 2. Status updates webhook (NatWest appends this automatically) register_rest_route('payit/v1', '/webhook/statusUpdates', [ 'methods' => ['POST'], 'callback' => 'payit_webhook_handler', 'permission_callback' => '__return_true', ]); // test endpoint (keep this) register_rest_route('payit/v1', '/test', [ 'methods' => 'GET', 'callback' => function () { return new WP_REST_Response(['message' => 'Payit plugin is active'], 200); }, 'permission_callback' => '__return_true', ]); }); function payit_write_log($message) { $upload_dir = wp_upload_dir(); $log_file = trailingslashit($upload_dir['basedir']) . 'payit-webhook-log.txt'; file_put_contents($log_file, $message . PHP_EOL, FILE_APPEND); } function payit_get_value_from_paths($data, $paths) { foreach ($paths as $path) { $value = $data; $found = true; foreach ($path as $segment) { if (is_array($value) && array_key_exists($segment, $value)) { $value = $value[$segment]; } else { $found = false; break; } } if ($found && $value !== '' && $value !== null) { return $value; } } return ''; } function payit_webhook_log($stage, $data = [], $transaction_id = null) { $file = wp_upload_dir()['basedir'] . '/payit-full-payment-log.txt'; $log = [ 'time' => date('Y-m-d H:i:s'), 'transaction_id' => $transaction_id, 'stage' => $stage, 'ip' => $_SERVER['REMOTE_ADDR'] ?? null, 'method' => $_SERVER['REQUEST_METHOD'] ?? null, 'data' => $data ]; file_put_contents( $file, json_encode($log, JSON_PRETTY_PRINT) . PHP_EOL . "------------------------\n", FILE_APPEND ); } function payit_webhook_handler($request) { error_log('PAYIT WEBHOOK HANDLER RAN'); global $wpdb; //global tracking hook $transaction_id = $request->get_header('x-transaction-id') ?? ($data['transactionId'] ?? null) ?? ($data['plgReference'] ?? null); $method = $request->get_method(); if ($method === 'GET') { return new WP_REST_Response([ 'message' => 'Payit webhook endpoint is live' ], 200); } $body = $request->get_body(); $data = json_decode($body, true); payit_write_log('STATUS UPDATE HIT: ' . $body); payit_write_log("==== " . date('Y-m-d H:i:s') . " ===="); payit_write_log("RAW BODY: " . $body); if (!is_array($data)) { payit_write_log("ERROR: Invalid JSON"); return new WP_REST_Response(['message' => 'Invalid JSON'], 400); } $reference = payit_get_value_from_paths($data, [ ['plgReference'], ['id'], ['merchantReference'], ['transactionId'], ['paymentRequestId'], ['paymentId'], ['data', 'plgReference'], ['data', 'id'], ['data', 'merchantReference'], ['data', 'transactionId'], ['payment', 'plgReference'], ['payment', 'id'], ['payment', 'merchantReference'], ['payment', 'transactionId'], ]); $status = payit_get_value_from_paths($data, [ ['status'], ['paymentStatus'], ['payitStatus'], ['data', 'status'], ['data', 'paymentStatus'], ['payment', 'status'], ['payment', 'paymentStatus'], ]); $amount = payit_get_value_from_paths($data, [ ['amount'], ['paymentAmount'], ['data', 'amount'], ['payment', 'amount'], ]); if ($status === '') { $status = 'UNKNOWN'; } else { $status = strtoupper((string)$status); } $amount = $amount !== '' ? (float)$amount : 0; payit_write_log("REFERENCE: " . $reference); payit_write_log("STATUS: " . $status); payit_write_log("AMOUNT: " . $amount); if ($reference === '') { payit_write_log("ERROR: Reference not found in payload"); return new WP_REST_Response([ 'message' => 'Reference not found in payload', 'payload' => $data ], 400); } $table = $wpdb->prefix . 'payit_payments'; $existing = $wpdb->get_var( $wpdb->prepare("SELECT id FROM $table WHERE plg_reference = %s LIMIT 1", $reference) ); if ($existing) { $result = $wpdb->update( $table, ['status' => $status], ['plg_reference' => $reference], ['%s'], ['%s'] ); payit_write_log("UPDATE RESULT: " . print_r($result, true)); } else { $result = $wpdb->insert( $table, [ 'plg_reference' => $reference, 'amount' => $amount, 'status' => $status, 'created_at' => current_time('mysql'), ], ['%s', '%f', '%s', '%s'] ); payit_write_log("INSERT RESULT: " . print_r($result, true)); } return new WP_REST_Response([ 'message' => 'OK', 'reference' => $reference, 'status' => $status ], 200); } Fruit Shoot Orange 275ml x12 - ELC UK LTD

New here?

We may have free deliveries to your shop, register and we'll check and get in touch with you with the minimum order amount.

Shopping Cart
0
    0
    Your Cart
    Your cart is emptyReturn to Shop