if (!(isset($_SERVER['HTTP_CPT_KEY']) && isset($_SERVER['HTTP_CPT_HMAC']))) {
    _log('received invalid callback');
    die;
}
/* Valid callback so far. */
$auth = $coinapult->authenticate_callback($_SERVER['HTTP_CPT_KEY'], $_SERVER['HTTP_CPT_HMAC'], $_POST);
if (!($auth['auth'] && isset($_POST['transaction_id']))) {
    _log('failed to authenticate the callback: ' . print_r($auth, true));
    die;
}
$tid = $_POST['transaction_id'];
$result = $db->Execute("SELECT `order_id` FROM " . TABLE_COINAPULT_LINK . "\n  WHERE `transaction_id` = '{$tid}'");
if ($result->RecordCount() == 1) {
    $orderid = $result->fields['order_id'];
    _log('found order ' . $orderid . ' for tid ' . $tid);
    $transaction = $coinapult->search(array("transaction_id" => $tid));
    if ($transaction['transaction_id'] == $tid) {
        if ($transaction['state'] == 'complete') {
            /* Invoice got paid. */
            _log('order paid, updating..');
            $db->Execute("UPDATE " . TABLE_ORDERS . " SET orders_status = " . (int) MODULE_PAYMENT_COINAPULT_ORDERPAID_ID . "\n        WHERE orders_id = " . (int) $orderid);
            $comment = "Received " . $transaction['in']['amount'] . "btc\n";
            $sql = "INSERT INTO " . TABLE_ORDERS_STATUS_HISTORY . " (comments, orders_id,\n            orders_status_id, customer_notified, date_added) VALUES (:orderComments,\n            :orderID, :orderStatus, 0, now())";
            $sql = $db->bindVars($sql, ':orderComments', $comment, 'string');
            $sql = $db->bindVars($sql, ':orderID', $orderid, 'integer');
            $sql = $db->bindVars($sql, ':orderStatus', MODULE_PAYMENT_COINAPULT_ORDERPAID_ID, 'integer');
            $db->Execute($sql);
            _log('done!');
        } elseif ($transaction['state'] == 'canceled') {
            _log('payment canceled, updating..');
            $comment = "Insufficient payment. Received " . $transaction['in']['amount'] . "btc. Expected " . $transaction['in']['expected'] . "btc.";