/**
 * start checkout process
 */
function StartCheckout(&$errorMessage)
{
    global $dir;
    global $memberID;
    // defined in checkout.inc.php
    global $collectDataArr;
    global $enable_recurring;
    global $en_credits;
    global $credit2money;
    // these globals for module require call
    global $site;
    global $providerConf;
    global $checkoutFilename;
    global $checkoutURL;
    global $debugFilename;
    // if buy for credits
    if ($_REQUEST['use_credits'] == 'on' && $en_credits) {
        $amount = sprintf('%.2f', (double) $collectDataArr['amount']);
        $creditsAmount = sprintf("%.2f", (double) ($collectDataArr['amount'] * $credit2money));
        $creditBalance = getProfileCredits($memberID);
        $result = 0;
        if ($collectDataArr['checkout_action'] == 'credits') {
            $errorMessage = 'Credits couldn\'t be bought by credits';
            return false;
        }
        if ($creditBalance < $creditsAmount) {
            $result = 1000;
        } else {
            $purchaseRes = performPurchase($memberID, $collectDataArr['checkout_action'], $collectDataArr['data'], $amount, $result);
            if ($purchaseRes) {
                decProfileCredits($memberID, $creditsAmount);
                $result = 1;
            } else {
                $result = -1;
            }
        }
        $returnURL = returnURLByAction($collectDataArr['checkout_action'], $collectDataArr['data']);
        processValidationResult($result, $errorMessage, 0, $returnURL);
    } else {
        $providerID = (int) $_REQUEST['prov_id'];
        $providerRes = db_res("SELECT `Name`, `CheckoutFilename` FROM `PaymentProviders` WHERE `ID` = {$providerID} AND `Active`");
        if (!$providerRes || mysql_num_rows($providerRes) == 0) {
            $errorMessage = 'Wrong payment provider specified';
            return false;
        }
        $providerArr = mysql_fetch_assoc($providerRes);
        if (strlen(trim($providerArr['CheckoutFilename']))) {
            $checkoutFilename = $providerArr['CheckoutFilename'];
        } else {
            $checkoutFilename = $dir['checkout'] . $providerArr['Name'] . '.php';
        }
        if (!file_exists($checkoutFilename)) {
            $errorMessage = 'Checkout file not found';
            return false;
        }
        require_once $checkoutFilename;
        $validateRes = moduleValidateConfiguration($errorMessage);
        if (!$validateRes) {
            return false;
        }
        $localTranID = initiateTransaction($collectDataArr, $memberID, $providerID);
        if ($localTranID === false) {
            $errorMessage = 'Transaction initiating error';
            return false;
        }
        $subscriptionalPayment = $enable_recurring && $collectDataArr['allow_subscribe'] == 'on' && $_REQUEST['prov_recurring'] == 'on';
        if ($subscriptionalPayment) {
            $subsRes = initiateSubscription($localTranID, $collectDataArr['subscribe_days']);
            if (!$subsRes) {
                $errorMessage = 'Subscription initiating error';
                return false;
            }
        }
        $startRes = moduleStartTransaction($localTranID, $subscriptionalPayment, $collectDataArr['subscribe_days']);
        if (!$startRes) {
            $errorMessage = 'Transaction starting error';
            return false;
        }
    }
    return true;
}
/**
 * start checkout process
 */
function StartCheckout(&$errorMessage)
{
    global $dir;
    global $memberID;
    // defined in checkout.inc.php
    global $collectDataArr;
    global $enable_recurring;
    // these globals for module require call
    global $site;
    global $providerConf;
    global $checkoutFilename;
    global $checkoutURL;
    global $debugFilename;
    $providerID = (int) $_REQUEST['prov_id'];
    $providerRes = db_res("SELECT `Name`, `CheckoutFilename` FROM `PaymentProviders` WHERE `ID` = {$providerID} AND `Active`");
    if (!$providerRes || mysql_num_rows($providerRes) == 0) {
        $errorMessage = 'Wrong payment provider specified';
        return false;
    }
    $providerArr = mysql_fetch_assoc($providerRes);
    if (strlen(trim($providerArr['CheckoutFilename']))) {
        $checkoutFilename = $providerArr['CheckoutFilename'];
    } else {
        $checkoutFilename = $dir['checkout'] . $providerArr['Name'] . '.php';
    }
    if (!file_exists($checkoutFilename)) {
        $errorMessage = 'Checkout file not found';
        return false;
    }
    require_once $checkoutFilename;
    $validateRes = moduleValidateConfiguration($errorMessage);
    if (!$validateRes) {
        return false;
    }
    $localTranID = initiateTransaction($collectDataArr, $memberID, $providerID);
    if ($localTranID === false) {
        $errorMessage = 'Transaction initiating error';
        return false;
    }
    $subscriptionalPayment = $enable_recurring && $collectDataArr['allow_subscribe'] == 'on' && $_REQUEST['prov_recurring'] == 'on';
    if ($subscriptionalPayment) {
        $subsRes = initiateSubscription($localTranID, $collectDataArr['subscribe_days']);
        if (!$subsRes) {
            $errorMessage = 'Subscription initiating error';
            return false;
        }
    }
    $startRes = moduleStartTransaction($localTranID, $subscriptionalPayment, $collectDataArr['subscribe_days']);
    if (!$startRes) {
        $errorMessage = 'Transaction starting error';
        return false;
    }
    return true;
}