<commit_before><?php


class Aplazame_Aplazame_Model_Observer extends Mage_Core_Model_Abstract
{

    protected function is_aplazame_payment($order)
    {
        $code = Aplazame_Aplazame_Model_Payment::METHOD_CODE;

        $parentOrder = Mage::getModel('sales/order')->loadByIncrementId(
            (int)$order->getIncrementId());

        return ($code == $parentOrder->getPayment()->getMethod());
    }

    /**
     * Method for updating the order status after completing a purchase
     */
    public function salesOrderPlaceAfter($observer)
    {
        $order = $observer->getOrder();

        if (!isset($order)) {
            return $this;
        }

        $payment = $order->getPayment();

        if (!isset($payment)) {
            return $this;
        }

        if (!$this->is_aplazame_payment($order)) {          
            return $this;
        }

        $client = Mage::getModel('aplazame/api_client');
        $result = $client->setOrder($order)->updateOrder();

        return $this;
    }

    /**
     * Method after load order edition
     */
    public function salesOrderLoadAfter($observer)
    {
        return $this;
    }

    /**
     * Method used for canceling a Aplazame invoice when a Magento order is canceled
     */
    public function salesOrderPaymentCancel($observer)
    {
        $code = Aplazame_Aplazame_Model_Payment::METHOD_CODE;
        $order = $observer->getOrder();

        $orderId = explode("-", $order->getIncrementId());

        $nextOrder = Mage::getModel('sales/order')->loadByIncrementId(
            $orderId[0] . '-' . ((int)$orderId[1] + 1));

        if ($nextOrder->getId()) {
            return $this;
        }

        if (!$this->is_aplazame_payment($order)) {          
            return $this;
        }

        $client = Mage::getModel('aplazame/api_client');
        $result = $client->setOrder($order)->cancelOrder();

        return $this;
    }<commit_msg>Capturando posible exception cuando se cancela una orden, para no detener el script y mostrar admin session error message en su lugar con el exception msg.<commit_after><?php


class Aplazame_Aplazame_Model_Observer extends Mage_Core_Model_Abstract
{

    protected function is_aplazame_payment($order)
    {
        $code = Aplazame_Aplazame_Model_Payment::METHOD_CODE;

        $parentOrder = Mage::getModel('sales/order')->loadByIncrementId(
            (int)$order->getIncrementId());

        return ($code == $parentOrder->getPayment()->getMethod());
    }

    /**
     * Method for updating the order status after completing a purchase
     */
    public function salesOrderPlaceAfter($observer)
    {
        $order = $observer->getOrder();

        if (!isset($order)) {
            return $this;
        }

        $payment = $order->getPayment();

        if (!isset($payment)) {
            return $this;
        }

        if (!$this->is_aplazame_payment($order)) {          
            return $this;
        }

        $client = Mage::getModel('aplazame/api_client');
        $result = $client->setOrder($order)->updateOrder();

        return $this;
    }

    /**
     * Method after load order edition
     */
    public function salesOrderLoadAfter($observer)
    {
        return $this;
    }

    /**
     * Method used for canceling a Aplazame invoice when a Magento order is canceled
     */
    public function salesOrderPaymentCancel($observer)
    {
        $code = Aplazame_Aplazame_Model_Payment::METHOD_CODE;
        $order = $observer->getOrder();

        $orderId = explode("-", $order->getIncrementId());

        $nextOrder = Mage::getModel('sales/order')->loadByIncrementId(
            $orderId[0] . '-' . ((int)$orderId[1] + 1));

        if ($nextOrder->getId()) {
            return $this;
        }

        if (!$this->is_aplazame_payment($order)) {
            return $this;
        }

        $client = Mage::getModel('aplazame/api_client');

        //@oreales: capturamos el error pero no detenemos el script.
        try {
            $result = $client->setOrder($order)->cancelOrder();
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            return $this;
        }


        return $this;
    }
{'fraction_non_alphanumeric': 0.13325499412455935, 'fraction_numerical': 0.001410105757931845, 'mean_word_length': 2.266308518802763, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, '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': '15615', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1216, 'n_words': 247}