public function lastStep($order_key, $answer_key)
 {
     require_once Mage::getBaseDir('lib') . '/metododepago2/TodoPago.php';
     $http_header = $this->get_http_header();
     $wsdl = $this->get_wsdls();
     $end_point = $this->get_end_point();
     $todopago_connector = new TodoPago($http_header, $wsdl, $end_point);
     // /a este metodo es al que me va a devolver el gateway en caso que todo salga ok
     Mage::log("Modulo de pago - TodoPago ==> secondStep - orderid: " . $order_id);
     Mage::log("Modulo de pago - TodoPago ==> secondStep - AnswerKey: " . $answer_key);
     $order = new Mage_Sales_Model_Order();
     $order->loadByIncrementId($order_key);
     if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
         $merchant = Mage::getStoreConfig('payment/todopago_modo/idstore_test');
     } else {
         $merchant = Mage::getStoreConfig('payment/todopago_modo/idstore');
     }
     // Security
     if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
         $security = Mage::getStoreConfig('payment/todopago_modo/codigo_seguridad_test');
     } else {
         $security = Mage::getStoreConfig('payment/todopago_modo/codigo_seguridad');
     }
     $requestkey = $order->getTodopagoclave();
     // ahora vuelvo a consumir web service para confirmar la transaccion
     $optionsAnswer = array('Security' => $security, 'Merchant' => $merchant, 'RequestKey' => $requestkey, 'AnswerKey' => $answer_key);
     Mage::log("Modulo de pago - TodoPago ==> secondStep (" . $order_id . ") - AnswerKey: " . json_encode($optionsAnswer));
     try {
         $second_step = $todopago_connector->getAuthorizeAnswer($optionsAnswer);
         Mage::log("Modulo de pago - TodoPago ==> secondStep (" . $order_id . ") - {$second_step}: " . json_encode($second_step));
         $todopagotable = new Todopago_Modulodepago2_Model_Todopagotable();
         $todopagotable->load($order_key, "order_id");
         $todopagotable->setAnswerKey($answer_key);
         $todopagotable->setGetauthorizeanswerStatus(Mage::getModel('core/date')->date('Y-m-d H:i:s') . " - " . $second_step["StatusCode"] . " - " . $second_step['StatusMessage']);
         $todopagotable->save();
         //para saber si es un cupon
         if (strlen($second_step['Payload']['Answer']["BARCODE"]) > 0) {
             if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
                 $order->setStatus(Mage::getStoreConfig('test_todopago_offline'));
                 $order->addStatusHistoryComment("Todo Pago (TEST): " . $second_step['StatusMessage']);
             } else {
                 $order->setStatus(Mage::getStoreConfig('payment/todopago_avanzada/estado_offline'));
                 $order->addStatusHistoryComment("Todo Pago: " . $second_step['StatusMessage']);
             }
             $order->save();
             Mage_Core_Controller_Varien_Action::_redirect('modulodepago2/cupon/index', array('_secure' => true, 'nroop' => $order_id, 'venc' => $second_step['Payload']['Answer']["COUPONEXPDATE"], 'total' => $second_step['Payload']['Request']['AMOUNT'], 'code' => $second_step['Payload']['Answer']["BARCODE"], 'tipocode' => $second_step['Payload']['Answer']["BARCODETYPE"], 'empresa' => $second_step['Payload']['Answer']["PAYMENTMETHODNAME"]));
         } elseif ($second_step['StatusCode'] == -1) {
             if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
                 $order->setStatus('test_todopago_complete');
                 $order->addStatusHistoryComment("Todo Pago (TEST): " . $second_step['StatusMessage']);
             } else {
                 $order->setStatus(Mage::getStoreConfig('payment/todopago_avanzada/order_aprov'));
                 $order->addStatusHistoryComment("Todo Pago: " . $second_step['StatusMessage']);
             }
             $order->save();
             Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure' => true));
         } elseif ($second_step['StatusCode'] != -1) {
             if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
                 $order->setStatus('test_todopago_canceled');
                 $order->addStatusHistoryComment("Todo Pago (TEST): " . $second_step['StatusMessage']);
             } else {
                 $order->setStatus(Mage::getStoreConfig('payment/todopago_avanzada/estado_denegada'));
                 $order->addStatusHistoryComment("Todo Pago: " . $second_step['StatusMessage']);
             }
             $order->save();
             Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failure', array('_secure' => true));
         }
     } catch (Exception $e) {
         if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
             $order->setStatus('test_todopago_canceled');
             $order->addStatusHistoryComment("Todo Pago (TEST)(Exception): " . $e);
         } else {
             $order->setStatus(Mage::getStoreConfig('payment/todopago_avanzada/estado_denegada'));
             $order->addStatusHistoryComment("Todo Pago (Exception): " . $e);
         }
         $order->save();
         Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failure', array('_secure' => true));
     }
 }
Exemplo n.º 2
0
 public function lastStep($order_key, $answer_key)
 {
     /*COMENTO LAS PARTES DONDE SE UTILIZAN LOS ESTADOS CREADOS POR EL MODULO*/
     Mage::log("init: " . __METHOD__);
     $todopago_connector = Mage::helper('modulodepago2/connector')->getConnector();
     // /a este metodo es al que me va a devolver el gateway en caso que todo salga ok
     Mage::log("Modulo de pago - TodoPago ==> secondStep - orderid: " . $order_key);
     Mage::log("Modulo de pago - TodoPago ==> secondStep - AnswerKey: " . $answer_key);
     $order = new Mage_Sales_Model_Order();
     $order->loadByIncrementId($order_key);
     //merchant
     $merchant = Mage::helper('modulodepago2/ambiente')->get_merchant();
     // Security
     $security = Mage::helper('modulodepago2/ambiente')->get_security_code();
     $requestkey = $order->getTodopagoclave();
     // ahora vuelvo a consumir web service para confirmar la transaccion
     $optionsAnswer = array('Security' => $security, 'Merchant' => $merchant, 'RequestKey' => $requestkey, 'AnswerKey' => $answer_key);
     Mage::log("Modulo de pago - TodoPago ==> secondStep (" . $order_key . ") - AnswerKey: " . json_encode($optionsAnswer));
     try {
         Mage::log("try " . __METHOD__);
         $second_step = $todopago_connector->getAuthorizeAnswer($optionsAnswer);
         Mage::log("Modulo de pago - TodoPago ==> secondStep (" . $order_key . ") - second_step: " . json_encode($second_step));
         $todopagotable = new Todopago_Modulodepago2_Model_Todopagotable();
         $todopagotable->load($order_key, "order_id");
         $todopagotable->setAnswerKey($answer_key);
         $todopagotable->setGetauthorizeanswerStatus(Mage::getModel('core/date')->date('Y-m-d H:i:s') . " - " . $second_step["StatusCode"] . " - " . $second_step['StatusMessage']);
         $todopagotable->save();
         //para saber si es un cupon
         if (strlen($second_step['Payload']['Answer']["BARCODE"]) > 0) {
             $status = Mage::getStoreConfig('payment/modulodepago2/estado_offline');
             if (empty($status)) {
                 $status = Mage::getStoreConfig('payment/todopago_avanzada/estado_offline');
             }
             $state = $this->_get_new_order_state($status);
             if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
                 $message = "Todo Pago (TEST): " . $second_step['StatusMessage'];
             } else {
                 $message = "Todo Pago: " . $second_step['StatusMessage'];
             }
             $order->setState($state, $status, $message);
             try {
                 $order->sendNewOrderEmail();
             } catch (Exception $e) {
                 Mage::log("catch : " . __METHOD__);
                 Mage::log("message: " . var_export($e, true));
                 $order->sendOrderUpdateEmail(true, $message);
             }
             $order->save();
             Mage_Core_Controller_Varien_Action::_redirect('modulodepago2/cupon/index', array('_secure' => true, 'nroop' => $order_key, 'venc' => $second_step['Payload']['Answer']["COUPONEXPDATE"], 'total' => $second_step['Payload']['Request']['AMOUNT'], 'code' => $second_step['Payload']['Answer']["BARCODE"], 'tipocode' => $second_step['Payload']['Answer']["BARCODETYPE"], 'empresa' => $second_step['Payload']['Answer']["PAYMENTMETHODNAME"]));
         } elseif ($second_step['StatusCode'] == -1) {
             $status = Mage::getStoreConfig('payment/modulodepago2/order_aprov');
             if (empty($status)) {
                 $status = Mage::getStoreConfig('payment/todopago_avanzada/order_aprov');
             }
             $state = $this->_get_new_order_state($status);
             if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
                 $message = "Todo Pago (TEST): " . $second_step['StatusMessage'];
             } else {
                 $message = "Todo Pago: " . $second_step['StatusMessage'];
             }
             $order->setState($state, $status, $message);
             try {
                 $order->sendNewOrderEmail();
             } catch (Exception $e) {
                 Mage::log("catch : " . __METHOD__);
                 Mage::log("message: " . var_export($e, true));
                 $order->sendOrderUpdateEmail(true, $message);
             }
             $payment = $order->getPayment();
             $payment->setTransactionId($second_step['AuthorizationKey']);
             $payment->setParentTransactionId($payment->getTransactionId());
             $payment->save();
             $order->save();
             $invoice = $order->prepareInvoice()->setTransactionId(1)->addComment("Invoice created.")->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE)->register()->pay();
             Mage::getModel('core/resource_transaction')->addObject($invoice)->addObject($invoice->getOrder())->save();
             Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/success', array('_secure' => true));
         } elseif ($second_step['StatusCode'] != -1) {
             $status = Mage::getStoreConfig('payment/modulodepago2/estado_denegada');
             if (empty($status)) {
                 $status = Mage::getStoreConfig('payment/todopago_avanzada/estado_denegada');
             }
             $state = $this->_get_new_order_state($status);
             if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
                 $message = "Todo Pago (TEST): " . $second_step['StatusMessage'];
             } else {
                 $message = "Todo Pago: " . $second_step['StatusMessage'];
             }
             $order->cancel();
             Mage::log("Orden cancelada");
             $order->setState($state, $status, $message);
             $order->sendOrderUpdateEmail(true, $message);
             $order->save();
             Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failure', array('_secure' => true));
         }
     } catch (Exception $e) {
         Mage::log("catch : " . __METHOD__);
         $status = Mage::getStoreConfig('payment/modulodepago2/estado_denegada');
         if (empty($status)) {
             $status = Mage::getStoreConfig('payment/todopago_avanzada/estado_denegada');
         }
         $state = $this->_get_new_order_state($status);
         if (Mage::getStoreConfig('payment/modulodepago2/modo_test_prod') == "test") {
             $message = "Todo Pago (TEST)(Exception): " . $e;
         } else {
             $message = "Todo Pago (Exception): " . $e;
         }
         $order->cancel();
         Mage::log("Orden cancelada");
         $order->setState($state, $status, $message);
         $order->sendOrderUpdateEmail(true, $message);
         $order->save();
         Mage_Core_Controller_Varien_Action::_redirect('checkout/onepage/failure', array('_secure' => true));
     }
 }