0 votes

Problem external_reference mercadopago

I have a problem, I created a button with the MP library and it charges and everything perfect, but I thought I could send an id and then validate by the external reference.

I create the button

$preference = new MercadoPago\Preference();
  $item = new MercadoPago\Item();
  $item->id = "COD1234";                 //Aqui crei que podia enviar el id de pedido
  $item->title = "Arreglo Floral";
  $item->quantity = 1;
  $item->currency_id = "PEN";
  $item->unit_price = $totalx1;
  $payer = new MercadoPago\Payer();
  $payer->email = "recibos@hola.com";
  $preference->items = array($item);
  $preference->payer = $payer;
  $preference->save();

and run my button

<div id="oculto_visa" style="display: none">
                            <button class="order-button hvr-bounce-out"  type="button">
                            <a href="<?php echo $preference->init_point; ?>">
                            COMPRAR AHORA
                            </a>
                            </button>
                            </div>

Charge and all is well, how do I send an order code to that form and then validate in my system that the receipt was paid?

0voto

You should add the following line in your code

$preference->external_reference = "3114"; 

It would look like this

$preference = new MercadoPago\Preference();
  $item = new MercadoPago\Item();
  $item->id = "COD1234";                 //Aqui crei que podia enviar el id de pedido
  $item->title = "Arreglo Floral";
  $item->quantity = 1;
  $item->currency_id = "PEN";
  $item->unit_price = $totalx1;
  $payer = new MercadoPago\Payer();
  $payer->email = "recibos@hola.com";
  $preference->items = array($item);
  $preference->payer = $payer;
  $preference->external_reference = " **ACA VA EL ID** "; 
  $preference->save();

Then you have to configure the url where you will receive the notifications from mercadopago, that is to say, once meradopago makes the payment, where you will be informed that the payment has been made.

This is done in the following link

https://www.mercadopago.com.ar/ipn-notifications

The file that receives the data must have a format similar to this one

  require __DIR__  . '/vendor/autoload.php';

header("Status: 200");

    MercadoPago\SDK::setAccessToken("**********************************");
 $bd=conectar_bd();

    $merchant_order = null;

    switch($_GET["topic"]) {
        case "payment":

            $payment = MercadoPago\Payment::find_by_id($_GET["id"]);
            // Get the payment and the corresponding merchant_order reported by the IPN.

    }

    $paid_amount = 0;
    if ($payment){

              if ($payment->status == 'approved'){

              }

              if ($payment->status == 'in_process'){

              }

              if ($payment->status == 'rejected'){

                 }

             }

This last code can vary according to your needs but I think it is a good base.

Greetings

0 votes

Welcome Federico, could you give more detail in your answer? It will help the OP to understand it better.... Take a look at How to Answer for more information

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X