public function sendpaymentAction()
 {
     $this->_helper->layout->setLayout('json');
     $callback = $this->getRequest()->getParam('jsoncallback');
     if ($callback != "") {
         // strip all non alphanumeric elements from callback
         $callback = preg_replace('/[^a-zA-Z0-9_]/', '', $callback);
     }
     $this->view->callback = $callback;
     $data = $this->_getParam("data");
     $payment_json = $data['data'][0];
     $charges = $payment_json['charges_ids'];
     $charges = explode(',', $charges);
     try {
         foreach ($charges as $charge_id) {
             $charge = new PAP_Model_Charge();
             $charge->loadById($charge_id);
             $payment = new PAP_Model_Payment();
             $payment->setAmount($charge->getAmount())->setChargeId($charge->getId())->setControl($payment_json['nro_tx'])->setMethodId($payment_json['operacion'])->setPaymentDate($payment_json['fecha']);
             if ($payment_json['operacion'] == "T") {
                 $payment->setEntity($payment_json['banco_origen']);
             } else {
                 $payment->setEntity($payment_json['banco_destino']);
             }
             $payment->save();
             $charge->setStatus('S');
             $charge->save();
         }
         $data = array();
         $data['result_code'] = '0';
         $data['result_message'] = 'Información del pago informada guardada con éxito.';
         $response = $this->getFrontController()->getResponse();
         $response->appendBody($callback . '(' . json_encode($data) . ')');
         $this->getFrontController()->setResponse($response);
     } catch (Exception $ex) {
         $data = array();
         $data['result_code'] = $ex->getCode();
         $data['result_message'] = 'Hubo un error guardando la información del pago. Por favor envíe un email a soporte@promosalpaso.com con dicha información.';
         $response = $this->getFrontController()->getResponse();
         $response->appendBody($callback . '(' . json_encode($data) . ')');
         $this->getFrontController()->setResponse($response);
     }
 }
 public function mpnotificationAction()
 {
     try {
         $this->_helper->layout->disableLayout();
         $this->_helper->viewRenderer->setNoRender(TRUE);
         $user_id = 0;
         if (isset($_GET['id']) && $_GET['topic'] == 'payment') {
             $notification_id = $_GET['id'];
         } else {
             throw new Exception("Se ha llamado al método mpnotification pero no se encontró el parámetro id", 10001);
         }
         //$pendigPayments = PAP_Model_Payment::getPendigPaymentsMP($user_id);
         $config = new PAP_Helper_Config();
         $MPConfig = $config->getMPConfig();
         $MPObject = new PAP_MP($MPConfig['mp_client_id'], $MPConfig['mp_client_secret']);
         $payment_info = $MPObject->get_payment_info($notification_id);
         if ($payment_info["status"] == 200) {
             $jsonPayment = json_decode($payment_info["response"]);
             $payment = new PAP_Model_Payment();
             $payment->loadByControl($jsonPayment->{'id'}, 'MP');
             $payment->setStatus($jsonPayment->{'status'});
             $charge = new PAP_Model_Charge();
             $charge->loadById($payment->getChargeId());
             $charge->setStatus($payment->getStatus());
             $payment->save();
             $charge->save();
         } else {
             throw new Exception("MP Error (" . $payment_info["status"] . ")" . $payment_info["error"] . ": " . $payment_info["message"] . " CAUSE:" . $payment_info["cause"]);
         }
     } catch (Exception $ex) {
         //TODO 4: Logear error en llamada mpnotification.
     }
 }