<commit_before><?php
/**
 *                    Jojo CMS
 *                ================
 *
 * Copyright 2008 <NAME> <<EMAIL>>
 * Copyright 2008 JojoCMS
 *
 * See the enclosed file license.txt for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  <NAME> <<EMAIL>>
 * @author  <NAME> <<EMAIL>>
 * @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License
 * @link    http://www.jojocms.org JojoCMS
 */

class jojo_plugin_Jojo_cart_process extends JOJO_Plugin
{
    /**
     * Returns the classname of the first active payment plugin.
     */
    private static function activePaymentPlugin()
    {
        foreach (call_user_func(array(Jojo_Cart_Class, 'getPaymentHandlers')) as $handler) {
            if (call_user_func(array($handler, 'isActive'))) {
                return $handler;
            }
        }
        return false;
    }

    function _getContent()
    {
        global $smarty, $_USERGROUPS, $_USERID;
        $token = Jojo::getGet('token');
        $content = array();
        
        $languageurlprefix = $this->page['pageid'] ? Jojo::getPageUrlPrefix($this->page['pageid']) : $_SESSION['languageurlprefix'];

        /* Make sure there's something in the cart */
        $cart = call_user_func(array(Jojo_Cart_Class, 'getCart'), $token);
        if (!count($cart->items)) {
            Jojo::redirect(_SECUREURL . '/' .$languageurlprefix. 'cart/');
        }

        /* Which payment plugin are we using? */
        $activeplugin = self::activePaymentPlugin();
        $smarty->assign('activeplugin',  $activeplugin);
        if (!$activeplugin) {
            echo 'Error: unable to find active payment plugin';
            $log             = new Jojo_Eventlog();
            $log->code       = 'cart';
            $log->importance = 'critical';
            $log->shortdesc  = 'Active payment plugin error';
            $log->desc       = 'Active payment plugin error on ' . _SITEURI . ' - this may indicate a bug in the payment handler script';
            $log->savetodb();
            unset($log);
            exit;
        }
        $cart->handler = $activeplugin;

        /* Error checking */
        $errors = array();

        /* Ensure the transaction has not already been processed */
        $data = Jojo::selectQuery("SELECT * FROM {cart} WHERE token=? AND status='pending'", $token);
        if (!count($data)) {
            $errors[] = 'This transaction has already been processed.';
        }

        /* Attempt to process the payment */
        if (!count($errors)) {
            $result = call_user_func(array($activeplugin, 'process'));<commit_msg>Introduce a short script delay to assist with intermittant double-processing bug.<commit_after><?php
/**
 *                    Jojo CMS
 *                ================
 *
 * Copyright 2008 <NAME> <<EMAIL>>
 * Copyright 2008 JojoCMS
 *
 * See the enclosed file license.txt for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  <NAME> <<EMAIL>>
 * @author  <NAME> <<EMAIL>>
 * @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License
 * @link    http://www.jojocms.org JojoCMS
 */

class jojo_plugin_Jojo_cart_process extends JOJO_Plugin
{
    /**
     * Returns the classname of the first active payment plugin.
     */
    private static function activePaymentPlugin()
    {
        foreach (call_user_func(array(Jojo_Cart_Class, 'getPaymentHandlers')) as $handler) {
            if (call_user_func(array($handler, 'isActive'))) {
                return $handler;
            }
        }
        return false;
    }

    function _getContent()
    {
        global $smarty, $_USERGROUPS, $_USERID;
        $token = Jojo::getGet('token');
        $content = array();
        
        $languageurlprefix = $this->page['pageid'] ? Jojo::getPageUrlPrefix($this->page['pageid']) : $_SESSION['languageurlprefix'];

        /* Make sure there's something in the cart */
        $cart = call_user_func(array(Jojo_Cart_Class, 'getCart'), $token);
        if (!count($cart->items)) {
            Jojo::redirect(_SECUREURL . '/' .$languageurlprefix. 'cart/');
        }

        /* Which payment plugin are we using? */
        $activeplugin = self::activePaymentPlugin();
        $smarty->assign('activeplugin',  $activeplugin);
        if (!$activeplugin) {
            echo 'Error: unable to find active payment plugin';
            $log             = new Jojo_Eventlog();
            $log->code       = 'cart';
            $log->importance = 'critical';
            $log->shortdesc  = 'Active payment plugin error';
            $log->desc       = 'Active payment plugin error on ' . _SITEURI . ' - this may indicate a bug in the payment handler script';
            $log->savetodb();
            unset($log);
            exit;
        }
        $cart->handler = $activeplugin;

        /* Error checking */
        $errors = array();

        /* Ensure the transaction has not already been processed */
        $data = Jojo::selectQuery("SELECT * FROM {cart} WHERE token=? AND status='pending'", $token);
        if (!count($data)) {
            $errors[] = 'This transaction has already been processed.';
        } else {
            /* pause execution and check again after 0.5 - 1.5 seconds - this aloows other scripts to complete processing*/
            usleep(mt_rand(500000,1500000));
            $data = Jojo::selectQuery("SELECT * FROM {cart} WHERE token=? AND status='pending'", $token);
            if (!count($data)) {
                $errors[] = 'This transaction has already been processed.';
            }
        }
        

        /* Attempt to process the payment */
        if (!count($errors)) {
            $result = call_user_func(array($activeplugin, 'process'));
{'fraction_non_alphanumeric': 0.13752556237218813, 'fraction_numerical': 0.005623721881390593, 'mean_word_length': 2.215890410958904, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1962730', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1612, 'n_words': 439}