<?php
// GET ?action=sms_cancel&api_key=KEY&order_id=ORDERID
// Cancel an active number order and get refund

$user_id  = $GLOBALS['api_user_id'];
$order_id = trim($_GET['order_id'] ?? '');

if (empty($order_id)) {
    api_response(['status' => 'error', 'message' => 'Missing order_id.'], 400);
}

// Reuse internal cancel logic
$_GET['token'] = (string)$user_id;

ob_start();
require __DIR__ . '/../../service/cancelNumber.php';
$output = ob_get_clean();

$data = json_decode($output, true);
if (!$data) {
    api_response(['status' => 'error', 'message' => 'Internal error.'], 500);
}

if ($data['status'] === '200') {
    api_response([
        'status'  => 'success',
        'message' => $data['message'] ?? 'Number cancelled and refunded.',
    ]);
} else {
    api_response([
        'status'  => 'error',
        'message' => $data['message'] ?? 'Cancel failed.',
    ], 400);
}
