<commit_before><?php
/**
 * This file is part of the official Aplazame module for PrestaShop.
 *
 * @author    Aplazame <<EMAIL>>
 * @copyright 2015-2016 Aplazame
 * @license   see file: LICENSE
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

include_once _PS_MODULE_DIR_ . 'aplazame/lib/Aplazame/Aplazame/autoload.php';
include_once _PS_MODULE_DIR_ . 'aplazame/lib/Aplazame/Sdk/autoload.php';

/**
 * @property bool bootstrap
 * @property string confirmUninstall
 * @property string url
 */
class Aplazame extends PaymentModule
{
    const LOG_INFO = 1;
    const LOG_WARNING = 2;
    const LOG_ERROR = 3;
    const LOG_CRITICAL = 4;
    const ORDER_STATE_PENDING = 'APLAZAME_OS_PENDING';

    /**
     * @var string
     */
    private $apiBaseUri;

    /**
     * @var Aplazame_Sdk_Api_Client
     */
    private $apiClient;

    /**
     * @var string[]
     */
    private $limited_currencies;

    public function __construct()
    {
        $this->name = 'aplazame';
        $this->tab = 'payments_gateways';
        $this->version = '4.0.4';
        $this->author = 'Aplazame';
        $this->author_uri = 'https://aplazame.com';
        $this->module_key = '64b13ea3527b4df3fe2e3fc1526ce515';

        parent::__construct();

        $this->displayName = $this->l('Aplazame: buy now, pay later');
        $this->description = $this->l('Boost sales by 50% with Aplazame, a risk free payment method that offers instant credit for online purchases.');
        $this->confirmUninstall = $this->l('Are you sure about removing these details?');

        $this->need_instance = 0;
        $this->bootstrap = true;

        $this->url = 'https://aplazame.com';

        $this->apiBaseUri = getenv('APLAZAME_API_BASE_URI') ? getenv('APLAZAME_API_BASE_URI') : 'https://api.aplazame.com';
    }

    public function install()
    {
        if (!parent::install()) {
            return false;
        }

        if (!extension_loaded('curl')) {
            $this->_errors[] = $this->l('You have to enable the cURL extension on your server to install this module');

            return false;
        }

        if (!$this->installOrderState()) {
            return false;
        }

        Configuration::updateValue('APLAZAME_SANDBOX', false);
        Configuration::updateValue('APLAZAME_BUTTON_IMAGE', 'white-148x46');
        Configuration::updateValue('APLAZAME_BUTTON', '#aplazame_payment_button');
        Configuration::updateValue('APLAZAME_WIDGET_PROD', '0');

        return ($this->registerHook('actionOrderSlipAdd')
            && $this->registerHook('actionOrderStatusPostUpdate')
            && $this->registerHook('displayAdminProductsExtra')
            && $this->registerHook('displayHeader')
            && $this->registerHook('displayPayment')
            && $this->registerHook('displayProductButtons')
            && $this->registerHook('displayRightColumn')
            && $this->registerHook('displayRightColumnProduct')
            && $this->registerHook('displayShoppingCart')
            && $this->registerHook('payment')
            && $this->registerHook('paymentOptions')
            && $this->registerHook('paymentReturn')
            && $this->registerController('AdminAplazameApiProxy', 'Aplazame API Proxy')
        );
    }

    public function installOrderState()
    {
        $orderStatePending = Configuration::get(self::ORDER_STATE_PENDING);
        if ($orderStatePending && Validate::isLoadedObject(new OrderState($orderStatePending))) {
            return true;
        }

        $order_state = new OrderState();
        $order_state->name = array();
        foreach (Language::getLanguages() as $language) {
            $order_state->name[$language['id_lang']] = 'Awaiting for Aplazame payment';
        }
        $order_state->send_email = false;
        $order_state->color = '#4169E1';
        $order_state->hidden = false;
        $order_state->delivery = false;
        $order_state->logable = false;
        $order_state->invoice = false;
        if ($order_state->add()) {
            Configuration::updateValue(self::ORDER_STATE_PENDING, (int) $order_state->id);
        }

        return true;
    }

    public function accept(Cart $cart, $fraud)
    {
        if ($fraud) {
            $this->deny($cart, $fraud);

            return false;
        }

        $cartId = $cart->id;
        $orderStateId = Configuration::get('PS_OS_PAYMENT');

        if (!$cart->orderExists()) {
            return !(false === $this->validateOrder(
                $cartId,
                $orderStateId,
                $cart->getOrderTotal(true),
                $this->displayName
            ));
        }

        return $this->setOrderStateToOrderByCartId($cartId, $orderStateId);
    }

    public function deny(Cart $cart, $fraud)
    {
        $cartId = $cart->id;
        $orderStateId = (int) Configuration::get('PS_OS_CANCELED');

        if (!$cart->orderExists()) {
            return $this->validateOrder(
                $cartId,
                $orderStateId,
                $cart->getOrderTotal(true),
                $this->displayName
            );
        }

        return $this->setOrderStateToOrderByCartId($cartId, $orderStateId);
    }

    public function uninstall()
    {
        Configuration::deleteByName('APLAZAME_SANDBOX');

        return parent::uninstall();
    }

    /**
     * Load the configuration form.
     */
    public function getContent()
    {
        $output = '';

        $settings = array();
        $settingsKeys = array(
            'APLAZAME_SANDBOX',
            'APLAZAME_BUTTON',
            'APLAZAME_SECRET_KEY',
            'APLAZAME_BUTTON_IMAGE',
            'APLAZAME_WIDGET_PROD',
        );

        if (Tools::isSubmit('submitAplazameModule')) {
            $hasFoundErrors = false;

            foreach ($settingsKeys as $key) {<commit_msg>Don't create cancelled orders<commit_after><?php
/**
 * This file is part of the official Aplazame module for PrestaShop.
 *
 * @author    Aplazame <<EMAIL>>
 * @copyright 2015-2016 Aplazame
 * @license   see file: LICENSE
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

include_once _PS_MODULE_DIR_ . 'aplazame/lib/Aplazame/Aplazame/autoload.php';
include_once _PS_MODULE_DIR_ . 'aplazame/lib/Aplazame/Sdk/autoload.php';

/**
 * @property bool bootstrap
 * @property string confirmUninstall
 * @property string url
 */
class Aplazame extends PaymentModule
{
    const LOG_INFO = 1;
    const LOG_WARNING = 2;
    const LOG_ERROR = 3;
    const LOG_CRITICAL = 4;
    const ORDER_STATE_PENDING = 'APLAZAME_OS_PENDING';

    /**
     * @var string
     */
    private $apiBaseUri;

    /**
     * @var Aplazame_Sdk_Api_Client
     */
    private $apiClient;

    /**
     * @var string[]
     */
    private $limited_currencies;

    public function __construct()
    {
        $this->name = 'aplazame';
        $this->tab = 'payments_gateways';
        $this->version = '4.0.4';
        $this->author = 'Aplazame';
        $this->author_uri = 'https://aplazame.com';
        $this->module_key = '64b13ea3527b4df3fe2e3fc1526ce515';

        parent::__construct();

        $this->displayName = $this->l('Aplazame: buy now, pay later');
        $this->description = $this->l('Boost sales by 50% with Aplazame, a risk free payment method that offers instant credit for online purchases.');
        $this->confirmUninstall = $this->l('Are you sure about removing these details?');

        $this->need_instance = 0;
        $this->bootstrap = true;

        $this->url = 'https://aplazame.com';

        $this->apiBaseUri = getenv('APLAZAME_API_BASE_URI') ? getenv('APLAZAME_API_BASE_URI') : 'https://api.aplazame.com';
    }

    public function install()
    {
        if (!parent::install()) {
            return false;
        }

        if (!extension_loaded('curl')) {
            $this->_errors[] = $this->l('You have to enable the cURL extension on your server to install this module');

            return false;
        }

        if (!$this->installOrderState()) {
            return false;
        }

        Configuration::updateValue('APLAZAME_SANDBOX', false);
        Configuration::updateValue('APLAZAME_BUTTON_IMAGE', 'white-148x46');
        Configuration::updateValue('APLAZAME_BUTTON', '#aplazame_payment_button');
        Configuration::updateValue('APLAZAME_WIDGET_PROD', '0');

        return ($this->registerHook('actionOrderSlipAdd')
            && $this->registerHook('actionOrderStatusPostUpdate')
            && $this->registerHook('displayAdminProductsExtra')
            && $this->registerHook('displayHeader')
            && $this->registerHook('displayPayment')
            && $this->registerHook('displayProductButtons')
            && $this->registerHook('displayRightColumn')
            && $this->registerHook('displayRightColumnProduct')
            && $this->registerHook('displayShoppingCart')
            && $this->registerHook('payment')
            && $this->registerHook('paymentOptions')
            && $this->registerHook('paymentReturn')
            && $this->registerController('AdminAplazameApiProxy', 'Aplazame API Proxy')
        );
    }

    public function installOrderState()
    {
        $orderStatePending = Configuration::get(self::ORDER_STATE_PENDING);
        if ($orderStatePending && Validate::isLoadedObject(new OrderState($orderStatePending))) {
            return true;
        }

        $order_state = new OrderState();
        $order_state->name = array();
        foreach (Language::getLanguages() as $language) {
            $order_state->name[$language['id_lang']] = 'Awaiting for Aplazame payment';
        }
        $order_state->send_email = false;
        $order_state->color = '#4169E1';
        $order_state->hidden = false;
        $order_state->delivery = false;
        $order_state->logable = false;
        $order_state->invoice = false;
        if ($order_state->add()) {
            Configuration::updateValue(self::ORDER_STATE_PENDING, (int) $order_state->id);
        }

        return true;
    }

    public function accept(Cart $cart, $fraud)
    {
        if ($fraud) {
            $this->deny($cart, $fraud);

            return false;
        }

        $cartId = $cart->id;
        $orderStateId = Configuration::get('PS_OS_PAYMENT');

        if (!$cart->orderExists()) {
            return !(false === $this->validateOrder(
                $cartId,
                $orderStateId,
                $cart->getOrderTotal(true),
                $this->displayName
            ));
        }

        return $this->setOrderStateToOrderByCartId($cartId, $orderStateId);
    }

    public function deny(Cart $cart, $fraud)
    {
        if (!$cart->orderExists()) {
            return true;
        }

        $cartId = $cart->id;
        $orderStateId = (int) Configuration::get('PS_OS_CANCELED');

        return $this->setOrderStateToOrderByCartId($cartId, $orderStateId);
    }

    public function uninstall()
    {
        Configuration::deleteByName('APLAZAME_SANDBOX');

        return parent::uninstall();
    }

    /**
     * Load the configuration form.
     */
    public function getContent()
    {
        $output = '';

        $settings = array();
        $settingsKeys = array(
            'APLAZAME_SANDBOX',
            'APLAZAME_BUTTON',
            'APLAZAME_SECRET_KEY',
            'APLAZAME_BUTTON_IMAGE',
            'APLAZAME_WIDGET_PROD',
        );

        if (Tools::isSubmit('submitAplazameModule')) {
            $hasFoundErrors = false;

            foreach ($settingsKeys as $key) {
{'fraction_non_alphanumeric': 0.13582063711911357, 'fraction_numerical': 0.008310249307479225, 'mean_word_length': 2.387976539589443, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 114, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, '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': '1031609', 'n_tokens_mistral': 3562, 'n_tokens_neox': 3446, 'n_words': 615}