/**
 * 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;
}
/**
 * Checks if specified member was subscribed by specified provider and then
 * initiates new transaction basing on data from transaction, which was created
 * on subscription
 *
 * @param int $localTranID				- ID of transaction which was created on subscription
 * 										  Function initiates new transaction using data from
 * 										  this transaction
 *
 * @return int/bool 					- ID of initiated transaction on success, false otherwise
 *
 *
 */
function initiateSubscriptionTransaction($localTranID)
{
    // arguments validation
    $localTranID = (int) $localTranID;
    // select dummy transaction info
    $tranRes = db_res("SELECT `IDProvider`, `Data` FROM `Transactions`\r\n\t\t\t\t\t\t\tWHERE `ID` = {$localTranID}");
    if (!$tranRes || mysql_num_rows($tranRes) == 0) {
        return false;
    }
    $tranArr = mysql_fetch_assoc($tranRes);
    $tranData = transStringToData($tranArr['Data']);
    // check if member subscribed
    $subsArr = db_arr("SELECT `TransactionID`, `ChargesNumber` FROM `PaymentSubscriptions`\r\n\t\t\t\t\t\t\tWHERE `TransactionID` = {$localTranID}");
    if (!$subsArr) {
        return false;
    }
    $checkoutData['checkout_action'] = $tranData['action'];
    $checkoutData['amount'] = $tranData['amount'];
    $checkoutData['data'] = $tranData['data'];
    $checkoutData['description'] = returnDescByAction($tranData['action'], $tranData['data'], true);
    $res = initiateTransaction($checkoutData, $tranData['memberID'], $tranArr['IDProvider']);
    if (!$res) {
        return false;
    } else {
        return $res;
    }
}
/**
 * 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;
}