Created
December 11, 2018 23:13
-
-
Save fgilio/3d6438b61d63578da39c7879458d29dd to your computer and use it in GitHub Desktop.
MercadoPago check if a payment/merchant order is fully paid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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