<?php
/**
 * Simple Callback Receiver
 * Only processes incoming data
 */

date_default_timezone_set('Asia/Kolkata');

// 1. Get the incoming data
$receivedData = file_get_contents('php://input');
$data = [];
parse_str($receivedData, $data);

// 2. Validate required fields
$requiredFields = ['user', 'orderid', 'amount', 'txn_status'];
foreach ($requiredFields as $field) {
if (!isset($data[$field])) {
http_response_code(400);
die(json_encode([
'status' => 'error',
'message' => "Missing required field: $field"
]));
}
}

// 3. Process the data (example - modify as needed)
$response = [
'status' => 'success',
'message' => 'Callback received successfully',
'received_data' => [
'user' => $data['user'],
'order_id' => $data['orderid'],
'amount' => $data['amount'],
'status' => $data['txn_status'],
'utr' => $data['utr_number'] ?? null,
'timestamp' => date('Y-m-d H:i:s')
]
];

// 4. Send response
header('Content-Type: application/json');
echo json_encode($response, JSON_PRETTY_PRINT);

// 5. Optional: Log to file
$logData = [
'timestamp' => date('Y-m-d H:i:s'),
'ip' => $_SERVER['REMOTE_ADDR'],
'data' => $data,
'response' => $response
];
file_put_contents('callback_receiver.log', json_encode($logData) . PHP_EOL, FILE_APPEND);