ombulabs / mercadopago

Gem to communicate with the MercadoPago API
33 stars 22 forks source link

Redirecting to make payment #7

Closed paulomcnally closed 11 years ago

paulomcnally commented 11 years ago

Hi, I'm implementing the API using PHP and modal mode. I want to know if this is the right way to redirect the page to back_url after making the payment.

JavaScript onreturn:

<script type="text/javascript">
    function checkoutReturn(json){
        if( json.back_url != null && json.collection_id != null  ){
            var url = json.back_url + '?id='+json.collection_id
            window.top.location = url;
            console.log('collection_id: '+json.collection_id);
            console.log('preference_id: '+json.preference_id);
        }
        else{
            console.log('Ventana de mercado pago cerrada');
        }
    }
</script>

HTML:

<a href="https://www.mercadopago.com/checkout/pay?pref_id=<?php echo $d['identificador_de_la_preferencia']; ?>" name="MP-Checkout" class="btn btn_pagar_con_opcion" mp-mode="modal" onreturn="checkoutReturn">&nbsp;</a>

PHP

$mp = new MP("XXXXX", "XXXXX");
$productos = array();
$redireccionamientos = new stdClass();
$redireccionamientos->success = "https://www.mysite.com/caja/terminar/MercadoPagoSuccess.php";
$redireccionamientos->failure = "https://www.mysite.com/caja/terminar/MercadoPagoFailure.php";
$redireccionamientos->pending = "https://www.mysite.com/caja/terminar/MercadoPagoPending.php";
foreach( $_SESSION['carrito']['productos'] as $producto ){
    $producto_datos = Modelos_Productos::obtenerProducto(SISTEMA_TIENDA_PAIS, SISTEMA_ID_TIENDA, $producto['id']);
    $fila = new stdClass();
    $fila->id           =   $producto['id'];
    $fila->title        =   $producto['nombre'];
    if( !empty( $producto_datos['descripcion'] ) ){
        $fila->description  =   util_eliminar_html(util_preparar_var($producto_datos['descripcion'],'html_limpio'));
    }
    $fila->quantity     =   $producto['cantidad'];
    $fila->unit_price   =   5; //$producto['precio_venta'];
    $fila->currency_id  =   "MXN";
    //$fila->picture_url  =   "http://imgs.cdn.com/?tp=p&amp;id=".$producto['id']."&amp;t=340x260";
    array_push($productos, $fila);
}

$player = new stdClass();
$player->name = $_SESSION['cliente']['nombre'];
$player->surname = $_SESSION['cliente']['nombre_completo'];
$player->email = $_SESSION['cliente']['email'];
$exclusiones_tipos_pago = array();
$payment_methods = new stdClass();
//array_push( $exclusiones_tipos_pago, array("id"=>"debit_card") );
//array_push( $exclusiones_tipos_pago, array("id"=>"credit_card") );
array_push( $exclusiones_tipos_pago, array("id"=>"ticket") );
array_push( $exclusiones_tipos_pago, array("id"=>"atm") );
$payment_methods->excluded_payment_types = $exclusiones_tipos_pago;
$datos = array (
    "items" => $productos,
    "payer"=> $player,
    "payment_methods" => $payment_methods
);
$resultado = $mp->create_preference($datos);
rafaelivan commented 11 years ago

Hi, @paulomcnally.

There is an official PHP API for payments integration. I think it would be best to check if you really need to implement the API yourself. Anyway, you could ask your question on the PHP API's page, on the following link:

https://github.com/mercadopago/sdk-php

Cheers