<?php 



require 'libs/mercadoPago/autoload.php';



class MercadoPagoController {    

    

    # procesar pago por mercado pago

    public function showMercadoPagoButton($array) {

        extract($array);

        date_default_timezone_set('America/Bogota');

        $hoy = date("Y-m-d h:i:s A");


        // MercadoPago\SDK::setAccessToken("TEST-7516306202170829-013011-d2cd05c28e5dae8b8935edd6bfcbe3e9-45940957");
        MercadoPago\SDK::setAccessToken("APP_USR-7516306202170829-013011-85cdc5e4f9b616d382ed74fb403fe146-45940957");

        $notificationUrl = "https://secure.boletosqubit.co/".getUrl("shoppingCart", "ShoppingCart", "consultaEstadoPagoMP", array("token" => $orcotoke)); 

        $regreso   = "https://secure.boletosqubit.co/".getUrl("shoppingCart", "ShoppingCart", "getMpagoApproved", array("ext_ref" => $orcotoke));  #, array("ext_ref" => $orcotoke)

        $paymentStatus = "https://secure.boletosqubit.co/".getUrl("shoppingCart", "ShoppingCart", "getMpagoStatus");



        # Create a preference object

        $preference = new MercadoPago\Preference();



        $item = new MercadoPago\Item();

        $item->title = $evennomb;

        $item->currency_id = $moneda;

        $item->description = $locanomb;

        $item->quantity = $orcocabo;

        $item->unit_price = $orcovalo;



        $preference->items = array($item);

        $preference->auto_return = "approved";

        $preference->back_urls = array(

            "success" => $regreso,

            "pending" => $paymentStatus,

            "failure" => $paymentStatus

        );

        $preference->notification_url = $notificationUrl;

        // $preference->external_reference = $orcotoke;

        $preference->expires = true;

        

        $payer = new MercadoPago\Payer();

        $payer->name = $_SESSION["nombre"];

        $payer->email = $_SESSION["email"];

        $payer->date_created = $hoy;

        $payer->phone = array(

            "area_code" => "+57",

            "number" => $_SESSION["celular"]

        );

        $preference->payer = $payer; //<-- Nuevo JFMI 08-01-2022

        
        $preference->save();


        return $preference->id;

    }


    public function consultaMercadoPagoEstadoPago($id,$topic){	

        MercadoPago\SDK::setAccessToken("APP_USR-7516306202170829-013011-85cdc5e4f9b616d382ed74fb403fe146-45940957");

        $merchant_order = null;

        //print_r($id." - ".$topic);

        switch($topic) {

            case "payment":

                $payment = MercadoPago\Payment::find_by_id($id);

                // Get the payment and the corresponding merchant_order reported by the IPN.

                $merchant_order = MercadoPago\MerchantOrder::find_by_id($payment->order->id);

                break;

            case "merchant_order":

                $merchant_order = MercadoPago\MerchantOrder::find_by_id($id);

                //print_r($merchant_order);

                break;
        }

        $paid_amount = 0;

        foreach ($merchant_order->payments as $payment) {  

            if ($payment['status'] == 'approved'){

                $paid_amount += $payment['transaction_amount'];
            }
        }

        // If the payment's transaction amount is equal (or bigger) than the merchant_order's amount you can release your items

        if($paid_amount >= $merchant_order->total_amount){

            if (count($merchant_order->shipments)>0) { // The merchant_order has shipments

                if($merchant_order->shipments[0]->status == "ready_to_ship") {

                    return(1); //"Totally paid. Print the label and release your item.");
                }

            } else { // The merchant_order don't has any shipments

                return(2); //"Totally paid. Release your item.");
            }

        } else {

            return(3); //"Not paid yet. Do not release your item.");
        }		
    }
    

}



?>