How to implement IPN notifications, Mercado Pago, for partial payments?

2

I am using checkout basic giving customers the option to pay with two cards.

I use the SDK of PHP v 0.5.2 and I am set to listen to the topic payments of notifications IPN .

/ collections / notifications / [ID] ? access_token = [ACCESS_TOKEN]

I received the notification, I checked the status of the payment and it was approved, then I delivered the product, but I have not implemented the logic of verifying if the amount of the payment is the full amount of the sale. I need to know if there is any attribute in the payment that indicates that it is a partial payment to check directly that variable, or what would be the correct way to implement this functionality?

    
asked by Juan Pressacco 16.01.2017 в 22:46
source

1 answer

2

If you have implemented the option to pay with two means of payment, I recommend you listen to notifications of merchant_order which is the object that contains all the information of a sale, and you will not have problems with payments divided.

Here you have documentation , but basically this is the code what you should implement

    // Get the payment and the corresponding merchant_order reported by the IPN.
if($_GET["topic"] == 'payment'){
    $payment_info = $mp->get("/collections/notifications/" . $_GET["id"]);
    $merchant_order_info = $mp->get("/merchant_orders/" . $payment_info["response"]["collection"]["merchant_order_id"]);
// Get the merchant_order reported by the IPN.
} else if($_GET["topic"] == 'merchant_order'){
    $merchant_order_info = $mp->get("/merchant_orders/" . $_GET["id"]);
}

if ($merchant_order_info["status"] == 200) {
    // If the payment's transaction amount is equal (or bigger) than the merchant_order's amount you can release your items 
    $paid_amount = 0;

    foreach ($merchant_order_info["response"]["payments"] as  $payment) {
        if ($payment['status'] == 'approved'){
            $paid_amount += $payment['transaction_amount'];
        }   
    }

    if($paid_amount >= $merchant_order_info["response"]["total_amount"]){
        if(count($merchant_order_info["response"]["shipments"]) > 0) { // The merchant_order has shipments
            if($merchant_order_info["response"]["shipments"][0]["status"] == "ready_to_ship"){
                print_r("Totally paid. Print the label and release your item.");
            }
        } else { // The merchant_order don't has any shipments
            print_r("Totally paid. Release your item.");
        }
    } else {
        print_r("Not paid yet. Do not release your item.");
    }
    
answered by 17.01.2017 в 14:02