PHP code example of leonardcodep / mercadopago-sdk-php

1. Go to this page and download the library: Download leonardcodep/mercadopago-sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leonardcodep / mercadopago-sdk-php example snippets


  
    rcadoPago\SDK;
    use MercadoPago\Entity\Shared\Payment;
    use MercadoPago\Entity\Shared\Payer;
    try {
        SDK::setAccessToken("YOUR_ACCESS_TOKEN"); // Either Production or SandBox AccessToken
        $payment = new Payment();
        $payment->transaction_amount = 259;
        $payment->token = $request->token;
        $payment->description = "Compra de productos";
        $payment->installments = (int) $request->installments;
        $payment->payment_method_id = $request->payment_method_id;
        $payment->issuer_id = (int) $request->issuer_id;

        $payer = new Payer();
        $payerForm = $request->payer;
        $payer->email = $payerForm['email'];
        $payer->identification = array(
            "type" => $payerForm['identification']['type'],
            "number" => $payerForm['identification']['number']
        );
        $payment->payer = $payer;
        $payment->save();

        if($payment->id === null) {
            $error_message = 'Unknown error cause';
            if(isset($payment->error)) {
                $error_message = $payment->error->message;
            }
            throw new Exception($error_message);
        }

        $response = array(
            "request" => $request,
            "payment_id" => $payment->id,
            "status" => $payment->status,
            "status_detail" => $payment->status_detail,
        );
    } catch (Exception $e) {
        throw new Exception(trim(trim($e->getMessage()), '"'));
    }