Page 1 of 1

Pedido generado antes de finalizar compra Mercado Pago

Posted: Fri Jun 27, 2014 10:41 am
by rodricrzz
Hola.
Utilizo el módulo de Mercado Pago (el oficial que proporcionan ellos en https://github.com/mercadopago/cart-opencart) y al llegar al último paso en el proceso de compra, donde está el botón de "pagar" donde finalmente al hacer click ahí se dirige al servicio de pago, se genera el pedido en la base de datos antes de hacer click en "pagar".
Estuve tratando de analizar un poco el controller php de este modulo y ver si reubicando la siguiente linea:

Code: Select all

$this->model_checkout_order->confirm($this->session->data['order_id'], $this->config->get('mercadopago2_order_status_id'));
en la función de callback o de retorno pero no pude lograr nada.
Este es el código completo de catalog/controller/payment/mercadopago2.php:

Code: Select all

<?php


include_once "mercadopago.php";

class ControllerPaymentMercadopago2 extends Controller {

	private $error;
        public  $sucess = true;
	private $order_info;
        private $message;

	protected function index() {
                $this->data['button_confirm'] = $this->language->get('button_confirm');
		$this->data['button_back'] = $this->language->get('button_back');
                    
		if ($this->config->get('mercadopago2_country')) {
		    $this->data['action'] = $this->config->get('mercadopago2_country');
		}

		$this->load->model('checkout/order');

		$this->language->load('payment/mercadopago2');

		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
	
                $this->model_checkout_order->confirm($this->session->data['order_id'], $this->config->get('mercadopago2_order_status_id'));
                
		//Cambio el código ISO-3 de la moneda por el que se les ocurrio poner a los de mercadopago2!!!
		                            
               switch($order_info['currency_code']) {
			case"ARS":
				$currency = 'ARS';
				break;
                        case"ARG":
				$currency = 'ARS';
				break;    
                        case"VEF":
				$currency = 'VEF';
				break;  
                        case"BRA":
			case"BRL":
                        case"REA":
				$currency = 'BRL';
				break;
			case"MXN":
				$currency = 'MEX';
				break;
			case"CLP":
				$currency = 'CHI';
				break;
			default:
				$currency = 'USD';
				break;
		}
                
                 
            
		$currencies = array('ARS','BRL','MEX','CHI','VEF');
		if (!in_array($currency, $currencies)) {
			$currency = '';
			$this->data['error'] = $this->language->get('currency_no_support');
		}

		$products = '';
		
		foreach ($this->cart->getProducts() as $product) {
			$products .= $product['quantity'] . ' x ' . $product['name'] . ', ';
                }
                $allproducts = $this->cart->getProducts();
                $firstproduct = reset($allproducts);
                
                // dados 2.0
                
                $totalprice = $order_info['total'] * $order_info['currency_value'];                   
            
              	$this->id = 'payment';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/mercadopago2.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/payment/mercadopago2.tpl';
		} else {
			$this->template = 'default/template/payment/mercadopago2.tpl';
		}
                
                $this->data['server'] = $_SERVER;
                $this->data['debug'] = $this->config->get('mercadopago2_debug');
                
                // get credentials 
                
                $client_id     = $this->config->get('mercadopago2_client_id');
		$client_secret = $this->config->get('mercadopago2_client_secret');
                $url           = $this->config->get('mercadopago2_url');
                $installments  = (int) $this->config->get('mercadopago2_installments');

               

		$shipments = array(
			"receiver_address" => array(
				"floor" => "-",
				"zip_code" => $order_info['shipping_postcode'],
				"street_name" => $order_info['shipping_address_1'] . " - " . $order_info['shipping_address_2'] . " - " . $order_info['shipping_city'] . " - " . $order_info['shipping_zone'] . " - " . $order_info['shipping_country'],
				"apartment" => "-",
				"street_number" => "-"
			)
		);
		   
		//Force format YYYY-DD-MMTH:i:s
		$cust = $this->db->query("SELECT * FROM `" . DB_PREFIX . "customer` WHERE customer_id = " . $order_info['customer_id'] . " ");
		$date_created = "";
		$date_creation_user = "";
		
		if($cust->num_rows > 0):
			
			foreach($cust->rows as $customer):
				$date_created = $customer['date_added'];
			endforeach;
			
			$date_creation_user = date('Y-m-d', strtotime($date_created)) . "T" . date('H:i:s',strtotime($date_created));
		endif;
		
		
		
		
		$payer = array(
		    "name" => $order_info['payment_firstname'],
		    "surname" => $order_info['payment_lastname'],
		    "email" => $order_info['email'],
		    "date_created" => $date_creation_user,
		    "phone" => array(
			"area_code" => "-",
			"number" => $order_info['telephone']
		    ),
		    "address" => array(
			"zip_code" => $order_info['payment_postcode'],
			"street_name" => $order_info['payment_address_1'] . " - " . $order_info['payment_address_2'] . " - " . $order_info['payment_city'] . " - " . $order_info['payment_zone'] . " - " . $order_info['payment_country'],
			"street_number" => "-"
		    ),
		    "identification" => array(
			"number" => "null",
			"type" => "null"
		    )
		);
		

	       
		$items = array(
		    array (
		    "id" => $order_info['order_id'],
		    "title" => $firstproduct['name'],
		    "description" => $firstproduct['quantity'] . ' x ' . $firstproduct['name'], // string
		    "quantity" => 1,
		    "unit_price" => round($totalprice, 2), //decimal
		    "currency_id" => $currency ,// string Argentina: ARS (peso argentino) � USD (D�lar estadounidense); Brasil: BRL (Real).,
		    "picture_url"=> HTTP_SERVER . 'image/' . $firstproduct['image'],
		    "category_id"=> $this->config->get('mercadopago2_category_id')
		    )
		);
		
		//excludes_payment_methods
		$exclude = $this->config->get('mercadopago2_methods');
		$installments = (int)$installments;
		
		if($exclude != ''):
		
		    //case exist exclude methods
		    $methods_excludes = preg_split("/[\s,]+/", $exclude);
		    $excludemethods = array();
		    foreach ($methods_excludes as $exclude ){
			if($exclude != "")
			   $excludemethods[] = array('id' => $exclude);     
		    }
		
		    $payment_methods = array(
			"installments" => $installments,
			"excluded_payment_methods" => $excludemethods
		    );
		else:
		    //case not exist exclude methods
		    $payment_methods = array(
			"installments" => $installments
		    );
		endif;
		
		
		//set back url
		$back_urls = array(
		    "pending" => $url . '/index.php?route=payment/mercadopago2/callback',
		    "success" => $url . '/index.php?route=payment/mercadopago2/callback'
		);

		//mount array pref
		$pref = array();
		$pref['external_reference'] = $order_info['order_id'];
		$pref['payer'] = $payer;
		$pref['shipments'] = $shipments;
		$pref['items'] = $items;
		$pref['back_urls'] = $back_urls;
		$pref['payment_methods'] = $payment_methods;
		    

		$mp = new MP ($client_id, $client_secret);
		$preferenceResult = $mp->create_preference($pref);
		
		
		$sandbox = $this->config->get('mercadopago2_sandbox') == 1 ? true:false;
		if($preferenceResult['status'] == 201):
			$this->data['type_checkout'] = $this->config->get('mercadopago2_type_checkout');
			
			if ($sandbox):
				$this->data['link'] = $preferenceResult['response']['sandbox_init_point'];
			else:
				$this->data['link'] = $preferenceResult['response']['init_point'];
			endif;
		else:
			$this->data['error'] = "Error: " . $preferenceResult['status'];
		endif;
		
		$this->render();
            
       }
     
   

	public function callback() {
	      $this->redirect(HTTP_SERVER . 'index.php?route=checkout/success');
	}
        
	public function retorno() {
	if (isset($_REQUEST['id'])) {
			$id = $_REQUEST['id'];
			$client_id     = $this->config->get('mercadopago2_client_id');
			$client_secret = $this->config->get('mercadopago2_client_secret');
			$sandbox = $this->config->get('mercadopago2_sandbox') == 1 ? true:null;
			
			//$checkdata = New Shop($client_id,$client_secret);
			$mp = new MP ($client_id, $client_secret);
			$mp->sandbox_mode($sandbox);
			
			//$dados = $checkdata->GetStatus($id);
			$dados = $mp->get_payment_info ($id);
			$dados = $dados['response'];
			
			$order_id = $dados['collection']['external_reference'];
			$order_status = $dados['collection']['status'];
			
			$this->load->model('checkout/order');
			$order = $this->model_checkout_order->getOrder($order_id);
		
			if ($order['order_status_id'] == '0') {
				$this->model_checkout_order->confirm($order_id, $this->config->get('mercadopago_order_status_id'));
			}
			
			switch ($order_status) {
				case 'approved':
					$this->model_checkout_order->update($order_id, $this->config->get('mercadopago2_order_status_id_completed'));   
					break;
				case 'pending':
					$this->model_checkout_order->update($order_id, $this->config->get('mercadopago2_order_status_id_pending'));    
					break;    
				case 'in_process':
					$this->model_checkout_order->update($order_id, $this->config->get('mercadopago2_order_status_id_process'));       
					break;    
				case 'reject':
					$this->model_checkout_order->update($order_id, $this->config->get('mercadopago2_order_status_id_rejected'));      
					break;    
				case 'refunded':
					$this->model_checkout_order->update($order_id, $this->config->get('mercadopago2_order_status_id_refunded'));      
					break;    
				case 'cancelled':
					$this->model_checkout_order->update($order_id, $this->config->get('mercadopago2_order_status_id_cancelled'));      
					break;    
				case 'in_metiation':
					$this->model_checkout_order->update($order_id, $this->config->get('mercadopago2_order_status_id_in_mediation'));    
					break;
				default:
					$this->model_checkout_order->update($order_id, $this->config->get('mercadopago2_order_status_id_pending'));
					break;
			}
			echo "ID: " . $id . " - Status: " . $order_status;
		}
	}     
}   
?>
y este es el catalog/model/payment/mercadopago2.php:

Code: Select all

<?php 
class ModelPaymentMercadopago2 extends Model {
  	public function getMethod($address) {
		$this->load->language('payment/mercadopago2');
		
		if ($this->config->get('mercadopago2_status')) {
      		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('mercadopago2_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')");
			
			if (!$this->config->get('mercadopago2_geo_zone_id')) {
        		$status = TRUE;
      		} elseif ($query->num_rows) {
      		  	$status = TRUE;
      		} else {
     	  		$status = FALSE;
			}	
      	} else {
			$status = FALSE;
		}
		
		$method_data = array();
	
		if ($status) {  
      		$method_data = array( 
        		'code'         => 'mercadopago2',
        		'title'      => $this->language->get('text_title'),
			'sort_order' => $this->config->get('mercadopago2_sort_order')
      		);




    	}
   
    	return $method_data;
  	}
}
?>

Quizas solo es cuestion de reacomodar alguna linea para que el confirm se ejecute luego del retorno o algo por el estilo.

Graciassss!

Re: Pedido generado antes de finalizar compra Mercado Pago

Posted: Mon Jun 30, 2014 1:34 pm
by rodricrzz
Nadie tiene idea de como solucionarlo?

Re: Pedido generado antes de finalizar compra Mercado Pago

Posted: Tue Oct 21, 2014 8:47 pm
by calelloa
Hola, tengo el mismo problema.

La opción de pago esta antes de la opcion de confirmar el pedido por lo que se realiza el pago y no queda registrado el pedido.

Alguien lo solucionó?

Gracias.

Re: Pedido generado antes de finalizar compra Mercado Pago

Posted: Tue Oct 21, 2014 8:53 pm
by calelloa
Alguna solucion?

Re: Pedido generado antes de finalizar compra Mercado Pago

Posted: Mon Oct 27, 2014 9:36 pm
by rodricrzz
calelloa wrote:Alguna solucion?
Aún no.. y en el foro de soporte de mercado pago siguen sin darnos ninguna solucion

Re: Pedido generado antes de finalizar compra Mercado Pago

Posted: Tue Oct 28, 2014 12:08 am
by Nedned
Hola,
hasta lo que yo veo en el código que has puesto tiene pinta de que la función "retorno" la llama siempre, y debería llamarla despues del la página de success , en el caso de que todo sea correcto.

Code: Select all


public function callback() {
$this->redirect(HTTP_SERVER . 'index.php?route=checkout/success');

----AQUI XXXX

}
Prueba a llamarla desde ahí, como sabes que la va a llamar dos veces, la primera que la llama siemrpe y la segunda que la llamas tu , ponle un if con una variable a false para que la primera no entre y la segunda si entre ... algo así

Code: Select all

public function retorno() {

if  ($variable==true) { 

resto de la funcion

no se si me he explicado correctamente, tampoco tengo mercado pago puesto en mi web así que no puedo probarlo.

Pero entiendo que puede funcionar, si alguíen me da un acceso FTP a una web de "pruebas" se lo pongo y vemos si funciona.

Si alguien tiene el plugin entero que lo suba y lo instalo en una web de pruebas y lo probamos.