function hipayValidateOrder($cart = null, $orderState = _PS_OS_ERROR_)
{
    echo '-fnVO';
    $hipay = new HiPay_Tpp();
    $customer = new Customer((int) $cart->id_customer);
    if ($orderState == 'skip') {
        // Simply log the callback
        $msg = new Message();
        $message = $hipay->l('HiPay - Callback initiated');
        $message .= ' - ' . $hipay->l('Transaction_reference : ') . $_POST['transaction_reference'];
        $message .= ' - ' . $hipay->l('State : ') . $_POST['state'];
        $message .= ' - ' . $hipay->l('Status : ') . $_POST['status'];
        $message .= ' - ' . $hipay->l('Message : ') . $_POST['message'];
        $message .= ' - ' . $hipay->l('data : ') . $_POST['cdata1'];
        $message = strip_tags($message, '<br>');
        if (Validate::isCleanHtml($message)) {
            $msg->message = $message;
            $msg->id_order = intval($order->id);
            $msg->private = 1;
            $msg->add();
        }
        HipayLogger::addLog($hipay->l('Callback process', 'hipay'), HipayLogger::ERROR, 'hipayValidateOrder status skip - cid : ' . (int) $_POST['order']->id);
        die;
        return;
    }
    if ($orderState == '124') {
        // NOTE only status 124 present, because logically, status 124 needs to be processed BEFORE status 125 can be send.
        // 124 = If orderstate is refund requested, we don't add a new order but only the log
        // 125 = If orderstate is refund approved, we don't add a new order but only the log
        hipayUpdateOrder($cart, $orderState);
        return;
    }
    HipayLogger::addLog($hipay->l('Callback paiement starting', 'hipay'), HipayLogger::NOTICE, 'Cart id : ' . $cart->id . ' - Order state : ' . $orderState);
    $id_cart = $cart->id;
    $id_order_state = $orderState;
    $amount_paid = $_POST['captured_amount'];
    $message = $hipay->l('Transaction Reference:') . ' ' . $_POST['transaction_reference'] . '
                ' . $hipay->l('State:') . ' ' . $_POST['state'] . '
                ' . $hipay->l('Status:') . ' ' . $_POST['status'] . '
                ' . $hipay->l('Message:') . ' ' . $_POST['message'] . '
                ' . $hipay->l('Data:') . ' ' . $_POST['cdata1'] . '
                ' . $hipay->l('orderState:') . ' ' . $orderState . '
                ' . $hipay->l('Payment mean:') . ' ' . $_POST['payment_product'] . '
                ' . $hipay->l('Payment has began at:') . ' ' . $_POST['date_created'] . '
                ' . $hipay->l('Payment received at:') . ' ' . $_POST['date_authorized'] . '
                ' . $hipay->l('authorization Code:') . ' ' . $_POST['authorization_code'] . '
                ' . $hipay->l('Currency:') . ' ' . $_POST['currency'] . '
                ' . $hipay->l('Customer IP address:') . ' ' . $_POST['ip_address'];
    /**
     * Validate an order in database
     * Function called from a payment module
     *
     * @param integer $id_cart
     *        	Value
     * @param integer $id_order_state
     *        	Value
     * @param float $amount_paid
     *        	Amount really paid by customer (in the default currency)
     * @param string $payment_method
     *        	Payment method (eg. 'Credit card')
     * @param string $message
     *        	Message to attach to order
     */
    // Local Cards update
    $local_card_name = '';
    // Initialize to empty string
    if ($_POST['payment_product'] != '') {
        // Add the card name
        $local_card_name = ' via ' . (string) ucwords($_POST['payment_product']);
        // Retrieve xml list
        if (file_exists(_PS_ROOT_DIR_ . '/modules/' . $hipay->name . '/special_cards.xml')) {
            $local_cards = simplexml_load_file(_PS_ROOT_DIR_ . '/modules/' . $hipay->name . '/special_cards.xml');
            // If cards exists
            if (isset($local_cards)) {
                // If cards count > 0
                if (count($local_cards)) {
                    // Go through each card
                    foreach ($local_cards as $key => $value) {
                        // If card code value = payment_product value
                        if ((string) $value->code == trim($_POST['payment_product'])) {
                            // Add the card name
                            $local_card_name = ' via ' . (string) $value->name;
                        }
                    }
                }
            }
        }
    }
    $secure_key = $customer->secure_key;
    if ($secure_key == null) {
        // If secure key is null force a secure key
        $secure_key = md5(uniqid(rand(), true));
    }
    // If captured amount is zero, capture amount to be paid to prevent errors
    // Then update to actual captured amount when order has been created.
    $update_order_payment = false;
    if ($amount_paid <= 0) {
        $amount_paid = $_POST['authorized_amount'];
        $update_order_payment = true;
    }
    if ($hipay->validateOrder(intval($id_cart), $id_order_state, $amount_paid, $hipay->displayName . $local_card_name, $message, array(), NULL, false, $secure_key)) {
        $GLOBALS['_HIPAY_CALLBACK_ORDER_ID_'] = $hipay->currentOrder;
        // Check if the amount_paid = 0
        // If amount_paid = 0, PS will an error paid status.
        // We need to update that to the id_order_state.
        if ($amount_paid == 0) {
            $new_order = new order($hipay->currentOrder);
            $history = new OrderHistory();
            $history->id_order = (int) $hipay->currentOrder;
            $history->changeIdOrderState((int) $id_order_state, $new_order, true);
            $history->add();
        }
        // If $update_order_payment = true then update order_payment with captured_amount.
        if ($update_order_payment) {
            $new_order = new order($hipay->currentOrder);
            $sql = "UPDATE `" . _DB_PREFIX_ . "order_payment`\r\n                        SET `amount` = '" . $_POST['captured_amount'] . "'\r\n                        WHERE `order_reference`='" . $new_order->reference . "'";
            Db::getInstance()->execute($sql);
        }
        // Add card details to orderpayments
        // $hipay->currentOrder should give the current cart ID
        if (isset($_POST['payment_method']->token)) {
            $new_order = new order($hipay->currentOrder);
            $sql = "UPDATE `" . _DB_PREFIX_ . "order_payment`\r\n                        SET `card_number` = '" . pSQL($_POST['payment_method']->pan) . "',\r\n                        `transaction_id` = '" . pSQL($_POST['transaction_reference']) . "',\r\n                        `card_brand` = '" . pSQL($_POST['payment_method']->brand) . "',\r\n                        `card_expiration` = '" . pSQL($_POST['payment_method']->card_expiry_month) . "/" . pSQL($_POST['payment_method']->card_expiry_year) . "',\r\n                        `card_holder` = '" . pSQL($_POST['payment_method']->card_holder) . "'\r\n                        WHERE `order_reference`='" . pSQL($new_order->reference) . "'";
            Db::getInstance()->execute($sql);
        }
        // Add HIPAY_CAPTURE message to allow use of refund and capture
        $tag = 'HIPAY_CAPTURE ';
        $amount = $_POST['captured_amount'];
        $msgs = Message::getMessagesByOrderId($hipay->currentOrder, true);
        //true for private messages (got example from AdminOrdersController)
        $create_new_msg = true;
        if (count($msgs)) {
            foreach ($msgs as $msg) {
                $line = $msg['message'];
                if (startsWith($line, $tag)) {
                    $create_new_msg = false;
                    $to_update_msg = new Message($msg['id_message']);
                    $to_update_msg->message = $tag . $amount;
                    $to_update_msg->save();
                    break;
                }
            }
        }
        if ($create_new_msg) {
            // Create msg
            $msg = new Message();
            $message = 'HIPAY_CAPTURE ' . $amount;
            $message = strip_tags($message, '<br>');
            if (Validate::isCleanHtml($message)) {
                $msg->message = $message;
                $msg->id_order = intval((int) $hipay->currentOrder);
                $msg->private = 1;
                $msg->add();
            }
        }
        // 'OK ORDER';
        HipayLogger::addLog($hipay->l('Callback paiement successful', 'hipay'), HipayLogger::NOTICE, 'Cart id : ' . $cart->id . ' - Order state : ' . $orderState . ' - Message : ' . $message);
    } else {
        // 'KO ORDER';
        HipayLogger::addLog($hipay->l('Callback paiement failed', 'hipay'), HipayLogger::NOTICE, 'Cart id : ' . $cart->id . ' - Order state : ' . $orderState . ' - Message : ' . $message);
    }
}