function hipayResetOrderStatus($cart = null)
{
    echo '-fnROS';
    if ($_POST['status'] == '117' || $_POST['status'] == '118') {
        $cart = new Cart((int) $_POST['order']->id);
        if ($cart->orderExists()) {
            $orderState = _PS_OS_PAYMENT_;
            if ($_POST['captured_amount'] < $_POST['authorized_amount']) {
                $orderState = Configuration::get('HIPAY_PARTIALLY_CAPTURED') ? Configuration::get('HIPAY_PARTIALLY_CAPTURED') : HipayClass::getConfiguration('HIPAY_PARTIALLY_CAPTURED');
            }
            // FORCE INVOICE CREATION IF OrderState = _PS_OS_PAYMENT_
            if ($orderState == _PS_OS_PAYMENT_) {
                $order_id = retrieveOrderId($cart->id);
                // Retrieve order id
                $order = new Order((int) $order_id);
                // Recreate order
                $newOS = new OrderState((int) $orderState, $order->id_lang);
                // Emulate the order state _PS_OS_PAYMENT_
                // Uf the order state allows invoice and there is no invoice number, then generate the invoice
                if ($newOS->invoice and !$order->invoice_number) {
                    $order->setInvoice();
                }
            }
        }
    }
    // New modification for status challenged
    // Second check for status 112 -> 117 -> 118
    if ($_POST['status'] == '117') {
        if ((bool) $order->getHistory($context->language->id, Configuration::get('HIPAY_CHALLENGED'))) {
            $cart = new Cart((int) $_POST['order']->id);
            if ($cart->orderExists()) {
                $orderState = _PS_OS_PAYMENT_;
            }
            if ($_POST['captured_amount'] < $_POST['authorized_amount']) {
                $orderState = Configuration::get('HIPAY_PARTIALLY_CAPTURED') ? Configuration::get('HIPAY_PARTIALLY_CAPTURED') : HipayClass::getConfiguration('HIPAY_PARTIALLY_CAPTURED');
            }
            // FORCE INVOICE CREATION IF OrderState = _PS_OS_PAYMENT_
            if ($orderState == _PS_OS_PAYMENT_) {
                $order_id = retrieveOrderId($cart->id);
                // Retrieve order id
                $order = new Order((int) $order_id);
                // Recreate order
                $newOS = new OrderState((int) $orderState, $order->id_lang);
                // Emulate the order state _PS_OS_PAYMENT_
                // Uf the order state allows invoice and there is no invoice number, then generate the invoice
                if ($newOS->invoice and !$order->invoice_number) {
                    $order->setInvoice();
                }
            }
        }
    }
    // Update to minimize risk of simultaneous calls for status 116 and 117
    if ($_POST['status'] == '116') {
        usleep(500000);
        // 0.5sec
        echo '/116';
        // If order exists for cart
        $cart = new Cart((int) $_POST['order']->id);
        if ($cart->orderExists()) {
            echo '/C_OK' . $GLOBALS['_HIPAY_CALLBACK_ORDER_ID_'];
            $context = Context::getContext();
            // Retrieve Order ID
            $order_id = retrieveOrderId($cart->id);
            $order = new Order((int) $order_id);
            echo '/' . (int) $order_id;
            // If current state is paiement accepted or capture partielle
            // then skip the process
            if ((bool) $order->getHistory($context->language->id, _PS_OS_PAYMENT_)) {
                echo '/' . (int) _PS_OS_PAYMENT_ . '_U';
                // Update orders
                $sql_update = "UPDATE `" . _DB_PREFIX_ . "orders`\r\n                    SET `current_state` = '" . _PS_OS_PAYMENT_ . "'\r\n                    WHERE `id_order`='" . (int) $order_id . "'";
                Db::getInstance()->execute($sql_update);
                // Insert into order_history
                $sql_insert = "INSERT INTO `" . _DB_PREFIX_ . "order_history` (`id_employee`, `id_order`, `id_order_state`, `date_add`)\r\n                    VALUES ('0', '" . (int) $order_id . "', '" . _PS_OS_PAYMENT_ . "', now());";
                Db::getInstance()->execute($sql_insert);
            }
            if ((bool) $order->getHistory($context->language->id, Configuration::get('HIPAY_PARTIALLY_CAPTURED'))) {
                echo '/' . (int) Configuration::get('HIPAY_PARTIALLY_CAPTURED') . '_U';
                // Update orders
                $sql_update = "UPDATE `" . _DB_PREFIX_ . "orders`\r\n                    SET `current_state` = '" . Configuration::get('HIPAY_PARTIALLY_CAPTURED') . "'\r\n                    WHERE `id_order`='" . (int) $order_id . "'";
                Db::getInstance()->execute($sql_update);
                // Insert into order_history
                $sql_insert = "INSERT INTO `" . _DB_PREFIX_ . "order_history` (`id_employee`, `id_order`, `id_order_state`, `date_add`)\r\n                    VALUES ('0', '" . (int) $order_id . "', '" . Configuration::get('HIPAY_PARTIALLY_CAPTURED') . "', now());";
                Db::getInstance()->execute($sql_insert);
            }
            HipayLogger::addLog($hipay->l('Callback process', 'hipay'), HipayLogger::NOTICE, 'hipayResetOrderStatus status 116 cart already ok - cid : ' . (int) $_POST['order']->id);
            die;
        } else {
            echo '/C_KO' . $GLOBALS['_HIPAY_CALLBACK_ORDER_ID_'];
        }
    }
    HipayLogger::addLog($hipay->l('Callback process', 'hipay'), HipayLogger::NOTICE, 'hipayResetOrderStatus ended - cid : ' . (int) $_POST['order']->id);
    die;
}