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.

Revisions

  1. fgilio created this gist Dec 11, 2018.
    36 changes: 36 additions & 0 deletions mercadopago-payment-is-paid.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <?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
    }