Skip to content

Instantly share code, notes, and snippets.

@fgilio
Created December 11, 2018 23:13
Show Gist options
  • Save fgilio/3d6438b61d63578da39c7879458d29dd to your computer and use it in GitHub Desktop.
Save fgilio/3d6438b61d63578da39c7879458d29dd to your computer and use it in GitHub Desktop.
MercadoPago check if a payment/merchant order is fully paid
<?php
/*
* Keep in mind that this is heavily tied to the Laravel Framework
*
* $client_id
* $client_secret
* $externalReference
*/
MercadoPago\SDK::setClientId($client_id);
MercadoPago\SDK::setClientSecret($client_secret);
$paymentService = new MercadoPago\Payment();
$payments = collect($paymentService->search(['external_reference' => $externalReference]));
if ($payments->isEmpty()) {
// No payments attempt made
return;
}
$merchantOrder = MercadoPago\MerchantOrder::find_by_id($payments->first()->order->id);
$paidAmount = collect($merchantOrder->payments)
->filter(function ($payment) {
return $payment->status === 'approved';
})
->sum('transaction_amount');
// If the payment's transaction amount is equal (or bigger) than the merchant_order's amount you can release your items
if ($paidAmount >= $merchantOrder->total_amount) {
// Fully paid!
} else {
// Not paid yet
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment